added tray menu

This commit is contained in:
2026-08-12 04:59:22 +05:00
parent 08192d317e
commit a0d3098fe4
21 changed files with 1327 additions and 42 deletions
+41 -18
View File
@@ -11,14 +11,17 @@ public partial class App : Application
{
private ServiceProvider? _services;
private SingleInstanceGate? _instanceGate;
private MainWindowPlacement? _placement;
private MainWindowPresenter? _presenter;
private ITrayIcon? _tray;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
bool automatic = StartupLaunch.IsAutomatic(e.Args);
var gate = new SingleInstanceGate();
if (!gate.TryAcquire())
if (!gate.TryAcquire(showRunningInstance: !automatic))
{
gate.Dispose();
Shutdown();
@@ -34,9 +37,26 @@ public partial class App : Application
_services.GetRequiredService<ThemeService>();
_placement = _services.GetRequiredService<MainWindowPlacement>();
_presenter = _services.GetRequiredService<MainWindowPresenter>();
MainWindow = _services.GetRequiredService<MainWindow>();
MainWindow.Show();
_tray = _services.GetRequiredService<ITrayIcon>();
_tray.OpenRequested += OnOpenRequested;
_tray.ExitRequested += OnExitRequested;
bool hasTray = _tray.Install();
if (!automatic || !hasTray)
{
_presenter.Show();
}
if (!hasTray)
{
_presenter.Detach();
ShutdownMode = ShutdownMode.OnMainWindowClose;
}
_ = _services.GetRequiredService<SettingsViewModel>().InitializeAsync();
_ = _services.GetRequiredService<UpdateViewModel>().StartAsync();
@@ -46,6 +66,12 @@ public partial class App : Application
protected override void OnExit(ExitEventArgs e)
{
if (_tray is not null)
{
_tray.OpenRequested -= OnOpenRequested;
_tray.ExitRequested -= OnExitRequested;
}
_services?.Dispose();
if (_instanceGate is not null)
@@ -57,21 +83,14 @@ public partial class App : Application
base.OnExit(e);
}
private void OnActivationRequested(object? sender, EventArgs e)
private void OnActivationRequested(object? sender, EventArgs e) => _presenter?.Show();
private void OnOpenRequested(object? sender, EventArgs e) => _presenter?.Show();
private void OnExitRequested(object? sender, EventArgs e)
{
if (MainWindow is not { IsVisible: true })
{
return;
}
if (MainWindow.WindowState == WindowState.Minimized)
{
MainWindow.WindowState = WindowState.Normal;
}
_placement?.Apply(MainWindow);
MainWindow.Activate();
_presenter?.AllowClose();
Shutdown();
}
internal static void ConfigureServices(IServiceCollection services)
@@ -86,6 +105,10 @@ public partial class App : Application
services.AddSingleton<IThemeService>(provider => provider.GetRequiredService<ThemeService>());
services.AddSingleton<MainWindowPlacement>();
services.AddSingleton<MainWindowPresenter>();
services.AddSingleton<TrayIcon>();
services.AddSingleton<ITrayIcon>(provider => provider.GetRequiredService<TrayIcon>());
services.AddSingleton<ILocalizationService, LocalizationService>();
services.AddSingleton<IStartupService, StartupService>();