Skip to content

Commit 4db812d

Browse files
committed
refactor: use tray icon from violeta
1 parent bb92983 commit 4db812d

File tree

4 files changed

+113
-77
lines changed

4 files changed

+113
-77
lines changed

src/Fischless.Relauncher/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Fischless.Relauncher;
1313

14-
public partial class App : System.Windows.Application
14+
public partial class App : Application
1515
{
1616
// The.NET Generic Host provides dependency injection, configuration, logging, and other services.
1717
// https://docs.microsoft.com/dotnet/core/extensions/generic-host

src/Fischless.Relauncher/Fischless.Relauncher.csproj

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<Resource Include="Assets\Images\*.png" />
2323
<Resource Include="Assets\Fonts\*.ttf" />
2424
</ItemGroup>
25-
25+
2626
<ItemGroup>
2727
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
2828
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.8" />
@@ -32,16 +32,14 @@
3232
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.8" />
3333
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.8" />
3434
<PackageReference Include="Microsoft-WindowsAPICodePack-Shell" Version="1.1.5" />
35-
<PackageReference Include="Vanara.PInvoke.DwmApi" Version="4.1.6" />
36-
<PackageReference Include="Vanara.PInvoke.Kernel32" Version="4.1.6" />
37-
<PackageReference Include="Vanara.PInvoke.NtDll" Version="4.1.6" />
38-
<PackageReference Include="Vanara.PInvoke.SHCore" Version="4.1.6" />
39-
<PackageReference Include="Vanara.PInvoke.User32" Version="4.1.6" />
40-
<PackageReference Include="Vanara.PInvoke.UxTheme" Version="4.1.6" />
35+
<PackageReference Include="Vanara.PInvoke.DwmApi" Version="4.2.0" />
36+
<PackageReference Include="Vanara.PInvoke.Kernel32" Version="4.2.0" />
37+
<PackageReference Include="Vanara.PInvoke.NtDll" Version="4.2.0" />
38+
<PackageReference Include="Vanara.PInvoke.SHCore" Version="4.2.0" />
39+
<PackageReference Include="Vanara.PInvoke.User32" Version="4.2.0" />
40+
<PackageReference Include="Vanara.PInvoke.UxTheme" Version="4.2.0" />
4141
<PackageReference Include="YamlDotNet" Version="16.3.0" />
42-
<PackageReference Include="NotifyIconEx" Version="1.1.2" />
43-
<PackageReference Include="WPF-UI" Version="4.0.3" />
44-
<PackageReference Include="WPF-UI.Violeta" Version="4.0.3.2" />
42+
<PackageReference Include="WPF-UI.Violeta" Version="4.0.3.6" />
4543
<PackageReference Include="EleCho.WpfSuite" Version="0.10.20" />
4644
<PackageReference Include="PresentMonFps" Version="2.0.5" />
4745
<PackageReference Include="NAudio.Wasapi" Version="2.2.1" />
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
global using Application = System.Windows.Application;
22
global using MessageBox = Wpf.Ui.Violeta.Controls.MessageBox;
3-
global using NotifyIcon = NotifyIconEx.NotifyIcon;

src/Fischless.Relauncher/TrayIconManager.cs

Lines changed: 104 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CommunityToolkit.Mvvm.Messaging;
1+
using CommunityToolkit.Mvvm.Input;
2+
using CommunityToolkit.Mvvm.Messaging;
23
using Fischless.Configuration;
34
using Fischless.Relauncher.Core.Relaunchs;
45
using Fischless.Relauncher.Extensions;
@@ -9,87 +10,120 @@
910
using System.Reflection;
1011
using System.Windows;
1112
using Vanara.PInvoke;
13+
using Wpf.Ui.Violeta.Win32;
1214

1315
namespace Fischless.Relauncher;
1416

15-
internal class TrayIconManager
17+
internal class TrayIconManager : IDisposable
1618
{
1719
private static TrayIconManager _instance = null!;
1820

19-
private readonly NotifyIcon _icon = null!;
21+
private readonly TrayIconHost _icon = null!;
2022

21-
private readonly ToolStripMenuItem? _itemAutoRun = null;
22-
private readonly ToolStripMenuItem? _itemAutoMute = null;
23+
private readonly TrayMenuItem _itemAutoRun = null!;
24+
private readonly TrayMenuItem _itemAutoMute = null!;
2325

2426
private TrayIconManager()
2527
{
26-
_icon = new NotifyIcon()
28+
_icon = new TrayIconHost()
2729
{
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()
3935
{
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+
}
4352

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+
};
85119

86-
_icon.ContextMenuStrip.Opened += (_, _) =>
120+
_icon.RightDown += (_, _) =>
87121
{
88-
_itemAutoRun!.Checked = AutoStartupHelper.IsAutorun();
89-
_itemAutoMute!.Checked = Configurations.Genshin.Get().IsUseAutoMute;
122+
_itemAutoRun.IsChecked = AutoStartupHelper.IsAutorun();
123+
_itemAutoMute.IsChecked = Configurations.Genshin.Get().IsUseAutoMute;
90124
};
91125

92-
_icon.MouseDoubleClick += async (_, _) =>
126+
_icon.LeftDoubleClick += async (_, _) =>
93127
{
94128
_ = await GenshinLauncher.TryGetProcessAsync(async p =>
95129
{
@@ -125,6 +159,11 @@ private TrayIconManager()
125159
}
126160
}
127161

162+
public void Dispose()
163+
{
164+
_icon.IsVisible = false;
165+
}
166+
128167
public static TrayIconManager GetInstance()
129168
{
130169
return _instance ??= new TrayIconManager();

0 commit comments

Comments
 (0)