|
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;
|
@@ -304,6 +345,7 @@ public override void Initialize()
|
304 | 345 | SetLocale(pref.Locale);
|
305 | 346 | SetTheme(pref.Theme, pref.ThemeOverrides);
|
306 | 347 | SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily, pref.OnlyUseMonoFontInEditor);
|
| 348 | + SetupTrayIcon(pref.SystemTrayIcon); |
307 | 349 | }
|
308 | 350 |
|
309 | 351 | public override void OnFrameworkInitializationCompleted()
|
@@ -476,11 +518,15 @@ 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 |
|
| 521 | + var pref = ViewModels.Preference.Instance; |
| 522 | + if (pref.SystemTrayIcon) { |
| 523 | + desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown; |
| 524 | + } |
| 525 | + |
479 | 526 | _launcher = new ViewModels.Launcher(startupRepo);
|
480 | 527 | desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
|
481 | 528 |
|
482 | 529 | #if !DISABLE_UPDATE_DETECTION
|
483 |
| - var pref = ViewModels.Preference.Instance; |
484 | 530 | if (pref.ShouldCheck4UpdateOnStartup())
|
485 | 531 | Check4Update();
|
486 | 532 | #endif
|
|
0 commit comments