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