Skip to content

Commit fb27cb0

Browse files
author
Joyal Jose
committed
feat: introduce close to tray icon
1 parent e35be21 commit fb27cb0

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

Crimson/App.xaml.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Crimson.Utils;
66
using Crimson.ViewModels;
77
using Crimson.Views;
8+
using H.NotifyIcon;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Hosting;
1011
using Microsoft.UI.Xaml;
@@ -26,6 +27,7 @@ public IHost Host
2627
{
2728
get;
2829
}
30+
public static bool HandleClosedEvents { get; set; } = true;
2931

3032
/// <summary>
3133
/// Initializes the singleton application object. This is the first line of authored code
@@ -81,10 +83,13 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
8183
m_window.Closed += OnExit;
8284
}
8385

84-
// Save gamedata to storage on application exit
85-
private static async void OnExit(object sender, object e)
86+
protected void OnExit(object sender, WindowEventArgs args)
8687
{
87-
//await LibraryManager.UpdateJsonFileAsync();
88+
if (HandleClosedEvents)
89+
{
90+
args.Handled = true;
91+
m_window.Hide();
92+
}
8893
}
8994

9095
private Window m_window;

Crimson/Crimson.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<ItemGroup>
2828
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
2929
<PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.0.230907" />
30+
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.0.124" />
3031
<PackageReference Include="Ionic.Zlib.Core" Version="1.0.0" />
3132
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
3233
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />

Crimson/Views/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,6 @@
7070
</Grid>
7171
<local:LoginPage x:Name="LoginPage" Visibility="Collapsed"/>
7272
</Grid>
73+
<crimson:TrayIconView x:Name="TrayIconView" />
7374
</Grid>
7475
</Window>

Crimson/Views/TrayIconView.xaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<UserControl
3+
x:Class="Crimson.Views.TrayIconView"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:tb="using:H.NotifyIcon"
9+
mc:Ignorable="d">
10+
11+
<tb:TaskbarIcon
12+
x:Name="TrayIcon"
13+
x:FieldModifier="public"
14+
ContextMenuMode="SecondWindow"
15+
LeftClickCommand="{x:Bind ShowHideWindowCommand}"
16+
NoLeftClickDelay="True"
17+
ToolTipText="ToolTip"
18+
>
19+
<tb:TaskbarIcon.IconSource>
20+
<tb:GeneratedIconSource
21+
Text="C"
22+
Foreground="Red"
23+
/>
24+
</tb:TaskbarIcon.IconSource>
25+
<tb:TaskbarIcon.ContextFlyout>
26+
<MenuFlyout AreOpenCloseAnimationsEnabled="False">
27+
<MenuFlyoutItem
28+
Command="{x:Bind ShowHideWindowCommand}"
29+
Text="Show/Hide Window"
30+
/>
31+
<MenuFlyoutSeparator />
32+
<MenuFlyoutItem
33+
Command="{x:Bind ExitApplicationCommand}"
34+
Text="Exit"
35+
/>
36+
</MenuFlyout>
37+
</tb:TaskbarIcon.ContextFlyout>
38+
</tb:TaskbarIcon>
39+
</UserControl>

Crimson/Views/TrayIconView.xaml.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
using CommunityToolkit.Mvvm.ComponentModel;
4+
using CommunityToolkit.Mvvm.Input;
5+
using H.NotifyIcon;
6+
7+
// To learn more about WinUI, the WinUI project structure,
8+
// and more about our project templates, see: http://aka.ms/winui-project-info.
9+
10+
namespace Crimson.Views
11+
{
12+
[ObservableObject]
13+
public sealed partial class TrayIconView : UserControl
14+
{
15+
[ObservableProperty]
16+
private bool _isWindowVisible;
17+
18+
public TrayIconView()
19+
{
20+
InitializeComponent();
21+
}
22+
23+
[RelayCommand]
24+
public void ShowHideWindow()
25+
{
26+
var window = ((App)Application.Current).GetWindow();
27+
if (window == null)
28+
{
29+
return;
30+
}
31+
32+
if (window.Visible)
33+
{
34+
window.Hide();
35+
}
36+
else
37+
{
38+
window.Show();
39+
}
40+
IsWindowVisible = window.Visible;
41+
}
42+
43+
[RelayCommand]
44+
public void ExitApplication()
45+
{
46+
App.HandleClosedEvents = false;
47+
TrayIcon.Dispose();
48+
((App)Application.Current).GetWindow()?.Close();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)