Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TrayIcon sample app #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ Each sample is tagged with it's difficulty. The degree of difficulty describes h

|===

=== DesktopIntegration-Samples

[cols="25h,25,50"]
|===
| Sample | Difficulty | Buzz-Words

| link:src/Avalonia.Samples/Desktop/TrayIcon[Tray Icon Sample]
| 🐣 Beginner
| TrayIcon, ReactiveUI, Binding

|===

=== ✒️ Drawing-Samples

[cols="25h,25,50"]
Expand Down
6 changes: 6 additions & 0 deletions src/Avalonia.Samples/Avalonia.Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleToDoList", "CompleteA
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RectPainter", "Drawing\RectPainter\RectPainter.csproj", "{2B746401-384F-484A-810E-7A65288165E0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrayIcon", "DesktopIntegration\TrayIcon\TrayIcon.csproj", "{EB8D1498-2528-4FE5-B2B0-515293A8880C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -137,6 +139,10 @@ Global
{2B746401-384F-484A-810E-7A65288165E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B746401-384F-484A-810E-7A65288165E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B746401-384F-484A-810E-7A65288165E0}.Release|Any CPU.Build.0 = Release|Any CPU
{EB8D1498-2528-4FE5-B2B0-515293A8880C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB8D1498-2528-4FE5-B2B0-515293A8880C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB8D1498-2528-4FE5-B2B0-515293A8880C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB8D1498-2528-4FE5-B2B0-515293A8880C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
18 changes: 18 additions & 0 deletions src/Avalonia.Samples/DesktopIntegration/TrayIcon/AboutWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="TrayIcon.AboutWindow"
Title="Avalonia Tray Icon Sample"
Width="800" Height="400" WindowState="Normal" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Label FontSize="24" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
Avalonia Tray Icon Sample
</Label>

<Button Name="cmdClose" Click="cmdClose_Click" Grid.Row="1" Margin="30" HorizontalAlignment="Center">_Close</Button>
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Avalonia.Controls;
using Avalonia.Interactivity;

namespace TrayIcon
{
public partial class AboutWindow : Window
{
public AboutWindow()
{
InitializeComponent();
}

void cmdClose_Click(object? sender, RoutedEventArgs e)
{
this.Close();
}
}
}
25 changes: 25 additions & 0 deletions src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TrayIcon"
x:Class="TrayIcon.App"
x:CompileBindings="true"
x:DataType="local:App"
RequestedThemeVariant="Default">
<Application.Styles>
<FluentTheme />
</Application.Styles>

<TrayIcon.Icons>
<TrayIcons>
<TrayIcon Icon="Icon.ico" ToolTipText="TrayIcon Sample">
<TrayIcon.Menu>
<NativeMenu>
<NativeMenuItem Header="_About" Command="{Binding AboutCommand}" />
<NativeMenuItemSeparator />
<NativeMenuItem Header="E_xit" Command="{Binding ExitCommand}" />
</NativeMenu>
</TrayIcon.Menu>
</TrayIcon>
</TrayIcons>
</TrayIcon.Icons>
</Application>
56 changes: 56 additions & 0 deletions src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Windows.Input;

using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

using ReactiveUI;

namespace TrayIcon
{
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public App()
{
AboutCommand = ReactiveCommand.Create(ShowAboutWindow);
ExitCommand = ReactiveCommand.Create(ExitApplication);

DataContext = this;
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
_lifetime = desktop;

desktop.ShutdownMode = Avalonia.Controls.ShutdownMode.OnExplicitShutdown;
}

base.OnFrameworkInitializationCompleted();
}

IClassicDesktopStyleApplicationLifetime? _lifetime;

public ICommand AboutCommand { get; }
public ICommand ExitCommand { get; }

void ShowAboutWindow()
{
var window = new AboutWindow();

window.Show();
}

void ExitApplication()
{
_lifetime?.Shutdown();
}
}
}

18 changes: 18 additions & 0 deletions src/Avalonia.Samples/DesktopIntegration/TrayIcon/App.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embedded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="DeltaQ.RTB.UserInterface.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
using System.Runtime.CompilerServices;
Binary file not shown.
25 changes: 25 additions & 0 deletions src/Avalonia.Samples/DesktopIntegration/TrayIcon/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

using Avalonia;

namespace TrayIcon
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
var app = BuildAvaloniaApp();

app.StartWithClassicDesktopLifetime(args);
}

public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}
}
}
7 changes: 7 additions & 0 deletions src/Avalonia.Samples/DesktopIntegration/TrayIcon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TrayIcon

Sample application that shows a tray icon.

* The tray icon must have an associated icon. The icon should be included in the project as an `<AvaloniaResource>`.
* This sample sets the `DataContext` for `App` to `this`, which allows the command to be defined in the same class and then bound in the `.axaml` file. Consider using a separate view model in production code.

21 changes: 21 additions & 0 deletions src/Avalonia.Samples/DesktopIntegration/TrayIcon/TrayIcon.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IncludeAvaloniaGenerators>true</IncludeAvaloniaGenerators>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.2.0" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.0" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.2.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.2.0" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.2.0" />
<PackageReference Include="Avalonia.Remote.Protocol" Version="11.2.0" />
</ItemGroup>

<ItemGroup>
<AvaloniaResource Include="Icon.ico" />
</ItemGroup>
</Project>