Fixing system tray icon.

This commit is contained in:
Burak Kaan Köse
2025-10-21 15:40:19 +02:00
parent 3b3c878d0e
commit ae7d576967
6 changed files with 51 additions and 275 deletions
+21 -31
View File
@@ -1,4 +1,6 @@
using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
@@ -19,7 +21,9 @@ public sealed partial class ShellWindow : WindowEx, IWinoShellWindow, IRecipient
{
public IStatePersistanceService StatePersistanceService { get; } = WinoApplication.Current.Services.GetService<IStatePersistanceService>() ?? throw new Exception("StatePersistanceService not registered in DI container.");
public IPreferencesService PreferencesService { get; } = WinoApplication.Current.Services.GetService<IPreferencesService>() ?? throw new Exception("PreferencesService not registered in DI container.");
private readonly ISystemTrayService _systemTrayService;
public ICommand ShowWinoCommand { get; set; }
public ICommand ExitWinoCommand { get; set; }
public ShellWindow()
{
@@ -31,22 +35,22 @@ public sealed partial class ShellWindow : WindowEx, IWinoShellWindow, IRecipient
MinHeight = 420;
ConfigureTitleBar();
// Initialize system tray service
_systemTrayService = WinoApplication.Current.Services.GetService<ISystemTrayService>() ?? throw new Exception("SystemTrayService not registered in DI container.");
_systemTrayService.Initialize();
_systemTrayService.TrayIconDoubleClicked += OnTrayIconDoubleClicked;
// Handle window closing event to minimize to tray instead of closing
Closed += OnWindowClosed;
// Use the AppWindow.Closing event to handle the close request
AppWindow.Closing += OnAppWindowClosing;
ShowWinoCommand = new RelayCommand(RestoreFromTray);
ExitWinoCommand = new RelayCommand(ForceClose);
SystemTrayIcon.ForceCreate();
}
private void ConfigureTitleBar()
{
AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
// Apply initial theme colors
var themeService = WinoApplication.Current.Services.GetService<INewThemeService>();
if (themeService != null)
@@ -133,54 +137,40 @@ public sealed partial class ShellWindow : WindowEx, IWinoShellWindow, IRecipient
private void OnAppWindowClosing(object sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs e)
{
// Cancel the close and minimize to tray instead
e.Cancel = true;
MinimizeToTray();
}
private void OnWindowClosed(object sender, WindowEventArgs e)
{
// Clean up tray icon when window is actually closed
_systemTrayService?.Dispose();
SystemTrayIcon?.Dispose();
}
private void MinimizeToTray()
{
// Hide the window and show tray icon
this.Hide();
_systemTrayService.Show();
}
private void OnTrayIconDoubleClicked(object? sender, EventArgs e)
{
// Restore the window from tray
RestoreFromTray();
SystemTrayIcon.ForceCreate();
}
private void RestoreFromTray()
{
if (_systemTrayService.IsMinimizedToTray)
{
// Show the window and hide tray icon
this.Show();
this.Activate();
_systemTrayService.Hide();
}
this.Show();
BringToFront();
}
public void ForceClose()
{
// Unsubscribe from the closing event to avoid infinite loop
AppWindow.Closing -= OnAppWindowClosing;
// Clean up system tray
_systemTrayService?.Dispose();
SystemTrayIcon?.Dispose();
UnregisterRecipients();
// Close the window
this.Close();
Close();
// Exit the application
Application.Current.Exit();
}