|
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;
|
@@ -167,6 +169,46 @@ public static void SetTheme(string theme, string themeOverridesFile)
|
167 | 169 | }
|
168 | 170 | }
|
169 | 171 |
|
| 172 | + public void SetupTrayIcon(bool enable) |
| 173 | + { |
| 174 | + if (enable && Native.OS.EnsureSingleInstance()) |
| 175 | + { |
| 176 | + var icons = new TrayIcons { |
| 177 | + new TrayIcon { |
| 178 | + Icon = new WindowIcon(new Bitmap(AssetLoader.Open(new Uri("avares://SourceGit/App.ico")))), |
| 179 | + Menu = [ |
| 180 | + new NativeMenuItem(Text("Open")) {Command = Unminimize}, |
| 181 | + new NativeMenuItem(Text("Preferences")) {Command = OpenPreferencesCommand}, |
| 182 | + new NativeMenuItemSeparator(), |
| 183 | + new NativeMenuItem(Text("Quit")) {Command = QuitCommand}, |
| 184 | + ] |
| 185 | + } |
| 186 | + }; |
| 187 | + icons[0].Clicked += (_, _) => ToggleWindow(); |
| 188 | + TrayIcon.SetIcons(Current, icons); |
| 189 | + _createdSystemTrayIcon = true; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static void ToggleWindow() { |
| 194 | + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { |
| 195 | + if (desktop.MainWindow.IsVisible) { |
| 196 | + desktop.MainWindow.Hide(); |
| 197 | + } else { |
| 198 | + ShowWindow(); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private static void ShowWindow() |
| 204 | + { |
| 205 | + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { |
| 206 | + desktop.MainWindow.WindowState = WindowState.Normal; |
| 207 | + desktop.MainWindow.Show(); |
| 208 | + desktop.MainWindow.BringIntoView(); |
| 209 | + desktop.MainWindow.Focus(); |
| 210 | + } |
| 211 | + } |
170 | 212 | public static void SetFonts(string defaultFont, string monospaceFont, bool onlyUseMonospaceFontInEditor)
|
171 | 213 | {
|
172 | 214 | var app = Current as App;
|
@@ -320,6 +362,7 @@ public override void OnFrameworkInitializationCompleted()
|
320 | 362 |
|
321 | 363 | TryLaunchAsNormal(desktop);
|
322 | 364 | }
|
| 365 | + base.OnFrameworkInitializationCompleted(); |
323 | 366 | }
|
324 | 367 | #endregion
|
325 | 368 |
|
@@ -476,11 +519,17 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
|
476 | 519 | if (desktop.Args != null && desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]))
|
477 | 520 | startupRepo = desktop.Args[0];
|
478 | 521 |
|
479 |
| - _launcher = new ViewModels.Launcher(startupRepo); |
| 522 | + var pref = ViewModels.Preferences.Instance; |
| 523 | + |
| 524 | + SetupTrayIcon(pref.SystemTrayIcon); |
| 525 | + if (_createdSystemTrayIcon) { |
| 526 | + desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown; |
| 527 | + } |
| 528 | + |
| 529 | + _launcher = new ViewModels.Launcher(startupRepo) { InterceptQuit = _createdSystemTrayIcon }; |
480 | 530 | desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
|
481 | 531 |
|
482 | 532 | #if !DISABLE_UPDATE_DETECTION
|
483 |
| - var pref = ViewModels.Preferences.Instance; |
484 | 533 | if (pref.ShouldCheck4UpdateOnStartup())
|
485 | 534 | Check4Update();
|
486 | 535 | #endif
|
@@ -543,5 +592,6 @@ private void ShowSelfUpdateResult(object data)
|
543 | 592 | private ResourceDictionary _activeLocale = null;
|
544 | 593 | private ResourceDictionary _themeOverrides = null;
|
545 | 594 | private ResourceDictionary _fontsOverrides = null;
|
| 595 | + private bool _createdSystemTrayIcon = false; |
546 | 596 | }
|
547 | 597 | }
|
0 commit comments