|
1 | | -using CommunityToolkit.Mvvm.Messaging; |
| 1 | +using CommunityToolkit.Mvvm.Input; |
| 2 | +using CommunityToolkit.Mvvm.Messaging; |
2 | 3 | using Fischless.Configuration; |
3 | 4 | using Fischless.Relauncher.Core.Relaunchs; |
4 | 5 | using Fischless.Relauncher.Extensions; |
|
9 | 10 | using System.Reflection; |
10 | 11 | using System.Windows; |
11 | 12 | using Vanara.PInvoke; |
| 13 | +using Wpf.Ui.Violeta.Win32; |
12 | 14 |
|
13 | 15 | namespace Fischless.Relauncher; |
14 | 16 |
|
15 | | -internal class TrayIconManager |
| 17 | +internal class TrayIconManager : IDisposable |
16 | 18 | { |
17 | 19 | private static TrayIconManager _instance = null!; |
18 | 20 |
|
19 | | - private readonly NotifyIcon _icon = null!; |
| 21 | + private readonly TrayIconHost _icon = null!; |
20 | 22 |
|
21 | | - private readonly ToolStripMenuItem? _itemAutoRun = null; |
22 | | - private readonly ToolStripMenuItem? _itemAutoMute = null; |
| 23 | + private readonly TrayMenuItem _itemAutoRun = null!; |
| 24 | + private readonly TrayMenuItem _itemAutoMute = null!; |
23 | 25 |
|
24 | 26 | private TrayIconManager() |
25 | 27 | { |
26 | | - _icon = new NotifyIcon() |
| 28 | + _icon = new TrayIconHost() |
27 | 29 | { |
28 | | - Text = "Fischless.Relauncher", |
29 | | - Icon = Icon.ExtractAssociatedIcon(Process.GetCurrentProcess().MainModule?.FileName!)!, |
30 | | - Visible = true |
31 | | - }; |
32 | | - _icon.AddMenu($"v{Assembly.GetExecutingAssembly().GetName().Version!.ToString(3)}").Enabled = false; |
33 | | - _icon.AddMenu("-"); |
34 | | - _icon.AddMenu("启动游戏 (&L)", async (_, _) => |
35 | | - { |
36 | | - if (await GenshinLauncher.TryGetProcessAsync()) |
37 | | - { |
38 | | - if (await MessageBox.QuestionAsync("检测到游戏已启动,是否重新启动游戏?") != MessageBoxResult.Yes) |
| 30 | + ToolTipText = "Fischless.Relauncher", |
| 31 | + Icon = Icon.ExtractAssociatedIcon(Process.GetCurrentProcess().MainModule?.FileName!)!.Handle, |
| 32 | + Menu = |
| 33 | + [ |
| 34 | + new TrayMenuItem() |
39 | 35 | { |
40 | | - return; |
41 | | - } |
42 | | - } |
| 36 | + Header = $"v{Assembly.GetExecutingAssembly().GetName().Version!.ToString(3)}", |
| 37 | + IsEnabled = false, |
| 38 | + }, |
| 39 | + new TraySeparator(), |
| 40 | + new TrayMenuItem() |
| 41 | + { |
| 42 | + Header = "启动游戏 (&L)", |
| 43 | + Command = new RelayCommand(async () => |
| 44 | + { |
| 45 | + if (await GenshinLauncher.TryGetProcessAsync()) |
| 46 | + { |
| 47 | + if (await MessageBox.QuestionAsync("检测到游戏已启动,是否重新启动游戏?") != MessageBoxResult.Yes) |
| 48 | + { |
| 49 | + return; |
| 50 | + } |
| 51 | + } |
43 | 52 |
|
44 | | - await GenshinLauncher.LaunchAsync(delayMs: 1000, relaunchMethod: GenshinRelaunchMethod.Kill, option: GenshinLauncherOptionProvider.GetOption()); |
45 | | - }); |
46 | | - _icon.AddMenu("退出游戏 (&K)", async (_, _) => |
47 | | - { |
48 | | - _ = await GenshinLauncher.TryGetProcessAsync(async p => |
49 | | - { |
50 | | - await Task.CompletedTask; |
51 | | - p?.Kill(); |
52 | | - }); |
53 | | - }); |
54 | | - _icon.AddMenu("-"); |
55 | | - _icon.AddMenu("打开设置 (&O)", (_, _) => |
56 | | - { |
57 | | - Application.Current.Windows.OfType<GenshinSettingsWindow>().ToList().ForEach(x => x.Close()); |
58 | | - new GenshinSettingsWindow() |
59 | | - { |
60 | | - Topmost = false, |
61 | | - ShowInTaskbar = true, |
62 | | - WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen, |
63 | | - }.Show(); |
64 | | - }); |
65 | | - (_itemAutoMute = _icon.AddMenu("自动静音 (&M)", |
66 | | - (_, _) => |
67 | | - { |
68 | | - var config = Configurations.Genshin.Get(); |
69 | | - GenshinMuter.IsEnabled = config.IsUseAutoMute = _itemAutoMute!.Checked; |
70 | | - Configurations.Genshin.Set(config); |
71 | | - ConfigurationManager.Save(); |
72 | | - WeakReferenceMessenger.Default.Send(new GenshinAutoMuteChangedMessage()); |
73 | | - }) as ToolStripMenuItem)!.CheckOnClick = true; |
74 | | - _icon.AddMenu("-"); |
75 | | - _itemAutoRun = _icon.AddMenu("启动时自动运行 (&S)", |
76 | | - (_, _) => |
77 | | - { |
78 | | - if (AutoStartupHelper.IsAutorun()) |
79 | | - AutoStartupHelper.RemoveAutorunShortcut(); |
80 | | - else |
81 | | - AutoStartupHelper.CreateAutorunShortcut(); |
82 | | - }) as ToolStripMenuItem; |
83 | | - _icon.AddMenu("重启 (&R)", (_, _) => RuntimeHelper.Restart(forced: true)); |
84 | | - _icon.AddMenu("退出 (&E)", (_, _) => Application.Current.Shutdown()); |
| 53 | + await GenshinLauncher.LaunchAsync(delayMs: 1000, relaunchMethod: GenshinRelaunchMethod.Kill, option: GenshinLauncherOptionProvider.GetOption()); |
| 54 | + }), |
| 55 | + }, |
| 56 | + new TrayMenuItem() |
| 57 | + { |
| 58 | + Header = "退出游戏 (&K)", |
| 59 | + Command = new RelayCommand(async () => |
| 60 | + { |
| 61 | + _ = await GenshinLauncher.TryGetProcessAsync(async p => |
| 62 | + { |
| 63 | + await Task.CompletedTask; |
| 64 | + p?.Kill(); |
| 65 | + }); |
| 66 | + }), |
| 67 | + }, |
| 68 | + new TraySeparator(), |
| 69 | + new TrayMenuItem() |
| 70 | + { |
| 71 | + Header = "打开设置 (&O)", |
| 72 | + Command = new RelayCommand(() => |
| 73 | + { |
| 74 | + Application.Current.Windows.OfType<GenshinSettingsWindow>().ToList().ForEach(x => x.Close()); |
| 75 | + new GenshinSettingsWindow() |
| 76 | + { |
| 77 | + Topmost = false, |
| 78 | + ShowInTaskbar = true, |
| 79 | + WindowStartupLocation = WindowStartupLocation.CenterScreen, |
| 80 | + }.Show(); |
| 81 | + }), |
| 82 | + }, |
| 83 | + _itemAutoMute = new TrayMenuItem() |
| 84 | + { |
| 85 | + Header = "自动静音 (&M)", |
| 86 | + Command = new RelayCommand(() => |
| 87 | + { |
| 88 | + var config = Configurations.Genshin.Get(); |
| 89 | + GenshinMuter.IsEnabled = config.IsUseAutoMute = !_itemAutoMute.IsChecked; |
| 90 | + Configurations.Genshin.Set(config); |
| 91 | + ConfigurationManager.Save(); |
| 92 | + WeakReferenceMessenger.Default.Send(new GenshinAutoMuteChangedMessage()); |
| 93 | + }), |
| 94 | + }, |
| 95 | + new TraySeparator(), |
| 96 | + _itemAutoRun = new TrayMenuItem() |
| 97 | + { |
| 98 | + Header = "启动时自动运行 (&S)", |
| 99 | + Command = new RelayCommand(() => |
| 100 | + { |
| 101 | + if (AutoStartupHelper.IsAutorun()) |
| 102 | + AutoStartupHelper.RemoveAutorunShortcut(); |
| 103 | + else |
| 104 | + AutoStartupHelper.CreateAutorunShortcut(); |
| 105 | + }), |
| 106 | + }, |
| 107 | + new TrayMenuItem() |
| 108 | + { |
| 109 | + Header = "重启 (&R)", |
| 110 | + Command = new RelayCommand(() => RuntimeHelper.Restart(forced: true)), |
| 111 | + }, |
| 112 | + new TrayMenuItem() |
| 113 | + { |
| 114 | + Header = "退出 (&E)", |
| 115 | + Command = new RelayCommand(Application.Current.Shutdown), |
| 116 | + }, |
| 117 | + ], |
| 118 | + }; |
85 | 119 |
|
86 | | - _icon.ContextMenuStrip.Opened += (_, _) => |
| 120 | + _icon.RightDown += (_, _) => |
87 | 121 | { |
88 | | - _itemAutoRun!.Checked = AutoStartupHelper.IsAutorun(); |
89 | | - _itemAutoMute!.Checked = Configurations.Genshin.Get().IsUseAutoMute; |
| 122 | + _itemAutoRun.IsChecked = AutoStartupHelper.IsAutorun(); |
| 123 | + _itemAutoMute.IsChecked = Configurations.Genshin.Get().IsUseAutoMute; |
90 | 124 | }; |
91 | 125 |
|
92 | | - _icon.MouseDoubleClick += async (_, _) => |
| 126 | + _icon.LeftDoubleClick += async (_, _) => |
93 | 127 | { |
94 | 128 | _ = await GenshinLauncher.TryGetProcessAsync(async p => |
95 | 129 | { |
@@ -125,6 +159,11 @@ private TrayIconManager() |
125 | 159 | } |
126 | 160 | } |
127 | 161 |
|
| 162 | + public void Dispose() |
| 163 | + { |
| 164 | + _icon.IsVisible = false; |
| 165 | + } |
| 166 | + |
128 | 167 | public static TrayIconManager GetInstance() |
129 | 168 | { |
130 | 169 | return _instance ??= new TrayIconManager(); |
|
0 commit comments