File tree Expand file tree Collapse file tree 5 files changed +100
-3
lines changed Expand file tree Collapse file tree 5 files changed +100
-3
lines changed Original file line number Diff line number Diff line change 5
5
using Crimson . Utils ;
6
6
using Crimson . ViewModels ;
7
7
using Crimson . Views ;
8
+ using H . NotifyIcon ;
8
9
using Microsoft . Extensions . DependencyInjection ;
9
10
using Microsoft . Extensions . Hosting ;
10
11
using Microsoft . UI . Xaml ;
@@ -26,6 +27,7 @@ public IHost Host
26
27
{
27
28
get ;
28
29
}
30
+ public static bool HandleClosedEvents { get ; set ; } = true ;
29
31
30
32
/// <summary>
31
33
/// 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
81
83
m_window . Closed += OnExit ;
82
84
}
83
85
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 )
86
87
{
87
- //await LibraryManager.UpdateJsonFileAsync();
88
+ if ( HandleClosedEvents )
89
+ {
90
+ args . Handled = true ;
91
+ m_window . Hide ( ) ;
92
+ }
88
93
}
89
94
90
95
private Window m_window ;
Original file line number Diff line number Diff line change 27
27
<ItemGroup >
28
28
<PackageReference Include =" CommunityToolkit.Mvvm" Version =" 8.2.2" />
29
29
<PackageReference Include =" CommunityToolkit.WinUI.Controls.SettingsControls" Version =" 8.0.230907" />
30
+ <PackageReference Include =" H.NotifyIcon.WinUI" Version =" 2.0.124" />
30
31
<PackageReference Include =" Ionic.Zlib.Core" Version =" 1.0.0" />
31
32
<PackageReference Include =" Microsoft.Extensions.Hosting" Version =" 8.0.0" />
32
33
<PackageReference Include =" Microsoft.WindowsAppSDK" Version =" 1.5.240227000" />
Original file line number Diff line number Diff line change 70
70
</Grid >
71
71
<local : LoginPage x : Name =" LoginPage" Visibility =" Collapsed" />
72
72
</Grid >
73
+ <crimson : TrayIconView x : Name =" TrayIconView" />
73
74
</Grid >
74
75
</Window >
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments