|
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,45 @@ public static void SetTheme(string theme, string themeOverridesFile)
|
167 | 169 | }
|
168 | 170 | }
|
169 | 171 |
|
| 172 | + public static void SetupTrayIcon(bool enable) |
| 173 | + { |
| 174 | + if (enable) |
| 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("Preference")) {Command = OpenPreferenceCommand}, |
| 182 | + new NativeMenuItemSeparator(), |
| 183 | + new NativeMenuItem(Text("Quit")) {Command = QuitCommand}, |
| 184 | + ] |
| 185 | + } |
| 186 | + }; |
| 187 | + icons[0].Clicked += (_, _) => ToggleWindow(); |
| 188 | + TrayIcon.SetIcons(Current, icons); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private static void ToggleWindow() { |
| 193 | + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { |
| 194 | + if (desktop.MainWindow.IsVisible) { |
| 195 | + desktop.MainWindow.Hide(); |
| 196 | + } else { |
| 197 | + ShowWindow(); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private static void ShowWindow() |
| 203 | + { |
| 204 | + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { |
| 205 | + desktop.MainWindow.WindowState = WindowState.Normal; |
| 206 | + desktop.MainWindow.Show(); |
| 207 | + desktop.MainWindow.BringIntoView(); |
| 208 | + desktop.MainWindow.Focus(); |
| 209 | + } |
| 210 | + } |
170 | 211 | public static void SetFonts(string defaultFont, string monospaceFont, bool onlyUseMonospaceFontInEditor)
|
171 | 212 | {
|
172 | 213 | var app = Current as App;
|
@@ -320,6 +361,7 @@ public override void OnFrameworkInitializationCompleted()
|
320 | 361 |
|
321 | 362 | TryLaunchedAsNormal(desktop);
|
322 | 363 | }
|
| 364 | + base.OnFrameworkInitializationCompleted(); |
323 | 365 | }
|
324 | 366 | #endregion
|
325 | 367 |
|
@@ -476,11 +518,17 @@ private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop
|
476 | 518 | if (desktop.Args != null && desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]))
|
477 | 519 | startupRepo = desktop.Args[0];
|
478 | 520 |
|
479 |
| - _launcher = new ViewModels.Launcher(startupRepo); |
| 521 | + var pref = ViewModels.Preference.Instance; |
| 522 | + |
| 523 | + SetupTrayIcon(pref.SystemTrayIcon); |
| 524 | + if (_createdSystemTrayIcon) { |
| 525 | + desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown; |
| 526 | + } |
| 527 | + |
| 528 | + _launcher = new ViewModels.Launcher(startupRepo) { InterceptQuit = _createdSystemTrayIcon }; |
480 | 529 | desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
|
481 | 530 |
|
482 | 531 | #if !DISABLE_UPDATE_DETECTION
|
483 |
| - var pref = ViewModels.Preference.Instance; |
484 | 532 | if (pref.ShouldCheck4UpdateOnStartup())
|
485 | 533 | Check4Update();
|
486 | 534 | #endif
|
|
0 commit comments