|
14 | 14 | using Avalonia.Markup.Xaml; |
15 | 15 | using Avalonia.Media; |
16 | 16 | using Avalonia.Media.Fonts; |
| 17 | +using Avalonia.Media.Imaging; |
| 18 | +using Avalonia.Platform; |
17 | 19 | using Avalonia.Platform.Storage; |
18 | 20 | using Avalonia.Styling; |
19 | 21 | using Avalonia.Threading; |
@@ -85,6 +87,7 @@ public override void Initialize() |
85 | 87 | SetLocale(pref.Locale); |
86 | 88 | SetTheme(pref.Theme, pref.ThemeOverrides); |
87 | 89 | SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor); |
| 90 | + SetupTrayIcon(pref.SystemTrayIcon); |
88 | 91 | } |
89 | 92 |
|
90 | 93 | public override void OnFrameworkInitializationCompleted() |
@@ -192,6 +195,45 @@ public static void SetTheme(string theme, string themeOverridesFile) |
192 | 195 | } |
193 | 196 | } |
194 | 197 |
|
| 198 | + public static void SetupTrayIcon(bool enable) |
| 199 | + { |
| 200 | + if (enable) |
| 201 | + { |
| 202 | + var icons = new TrayIcons { |
| 203 | + new TrayIcon { |
| 204 | + Icon = new WindowIcon(new Bitmap(AssetLoader.Open(new Uri("avares://SourceGit/App.ico")))), |
| 205 | + Menu = [ |
| 206 | + new NativeMenuItem(Text("Open")) {Command = Unminimize}, |
| 207 | + new NativeMenuItem(Text("Preference")) {Command = OpenPreferenceCommand}, |
| 208 | + new NativeMenuItemSeparator(), |
| 209 | + new NativeMenuItem(Text("Quit")) {Command = QuitCommand}, |
| 210 | + ] |
| 211 | + } |
| 212 | + }; |
| 213 | + icons[0].Clicked += (_, _) => ToggleWindow(); |
| 214 | + TrayIcon.SetIcons(Current, icons); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private static void ToggleWindow() { |
| 219 | + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { |
| 220 | + if (desktop.MainWindow.IsVisible) { |
| 221 | + desktop.MainWindow.Hide(); |
| 222 | + } else { |
| 223 | + ShowWindow(); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + private static void ShowWindow() |
| 229 | + { |
| 230 | + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { |
| 231 | + desktop.MainWindow.WindowState = WindowState.Normal; |
| 232 | + desktop.MainWindow.Show(); |
| 233 | + desktop.MainWindow.BringIntoView(); |
| 234 | + desktop.MainWindow.Focus(); |
| 235 | + } |
| 236 | + } |
195 | 237 | public static void SetFonts(string defaultFont, string monospaceFont, bool onlyUseMonospaceFontInEditor) |
196 | 238 | { |
197 | 239 | var app = Current as App; |
@@ -545,11 +587,15 @@ private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop |
545 | 587 | if (desktop.Args != null && desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0])) |
546 | 588 | startupRepo = desktop.Args[0]; |
547 | 589 |
|
| 590 | + var pref = ViewModels.Preference.Instance; |
| 591 | + if (pref.SystemTrayIcon) { |
| 592 | + desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown; |
| 593 | + } |
| 594 | + |
548 | 595 | _launcher = new ViewModels.Launcher(startupRepo); |
549 | 596 | desktop.MainWindow = new Views.Launcher() { DataContext = _launcher }; |
550 | 597 |
|
551 | 598 | #if !DISABLE_UPDATE_DETECTION |
552 | | - var pref = ViewModels.Preference.Instance; |
553 | 599 | if (pref.ShouldCheck4UpdateOnStartup()) |
554 | 600 | Check4Update(); |
555 | 601 | #endif |
|
0 commit comments