GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: improve encapsulation of system tray logic (#131)
lars-berger78b195881d68
diff --git a/GlazeWM.Bootstrapper/Startup.cs b/GlazeWM.Bootstrapper/Startup.csindex 56030775..53d07a74 100644--- a/GlazeWM.Bootstrapper/Startup.cs+++ b/GlazeWM.Bootstrapper/Startup.cs@@ -1,11 +1,13 @@-using GlazeWM.Bar;+using GlazeWM.Bar;using GlazeWM.Domain.Common.Commands;+using GlazeWM.Domain.UserConfigs.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.Exceptions;using GlazeWM.Infrastructure.WindowsApi;using GlazeWM.Infrastructure.WindowsApi.Events;using System;+using System.Collections.Generic;using System.Linq;using System.Reactive.Linq;using System.Windows.Forms;@@ -20,16 +22,16 @@ namespace GlazeWM.Bootstrapperprivate readonly ExceptionHandler _exceptionHandler;private readonly KeybindingService _keybindingService;private readonly SystemEventService _systemEventService;- private readonly SystemTrayService _systemTrayService;private readonly WindowEventService _windowEventService;+ private SystemTrayIcon _systemTrayIcon { get; set; }+public Startup(BarService barService,Bus bus,ExceptionHandler exceptionHandler,KeybindingService keybindingService,SystemEventService systemEventService,- SystemTrayService systemTrayService,WindowEventService windowEventService){_barService = barService;@@ -37,7 +39,6 @@ namespace GlazeWM.Bootstrapper_exceptionHandler = exceptionHandler;_keybindingService = keybindingService;_systemEventService = systemEventService;- _systemTrayService = systemTrayService;_windowEventService = windowEventService;}@@ -67,8 +68,19 @@ namespace GlazeWM.Bootstrapper// Listen for system-related events (eg. changes to display settings)._systemEventService.Start();+ var systemTrayIconConfig = new SystemTrayIconConfig+ {+ HoverText = "GlazeWM",+ IconResourceName = "GlazeWM.Bootstrapper.icon.ico",+ Actions = new Dictionary<string, Action>+ {+ { "Exit", () => _bus.RaiseEvent(new ApplicationExitingEvent()) },+ }+ };+// Add application to system tray.- _systemTrayService.AddToSystemTray();+ _systemTrayIcon = new SystemTrayIcon(systemTrayIconConfig);+ _systemTrayIcon.Show();Application.Run();}@@ -82,7 +94,7 @@ namespace GlazeWM.Bootstrapper{_bus.Invoke(new ShowAllWindowsCommand());_barService.ExitApp();- _systemTrayService.RemoveFromSystemTray();+ _systemTrayIcon.Remove();Application.Exit();}}diff --git a/GlazeWM.Infrastructure/DependencyInjection.cs b/GlazeWM.Infrastructure/DependencyInjection.csindex ee39d97c..74d58faf 100644--- a/GlazeWM.Infrastructure/DependencyInjection.cs+++ b/GlazeWM.Infrastructure/DependencyInjection.cs@@ -16,7 +16,6 @@ namespace GlazeWM.Infrastructureservices.AddSingleton<ExceptionHandler>();services.AddSingleton<KeybindingService>();services.AddSingleton<SystemEventService>();- services.AddSingleton<SystemTrayService>();services.AddSingleton<WindowEventService>();services.AddSingleton<JsonService>();services.AddSingleton<YamlService>();diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemTrayIcon.cs b/GlazeWM.Infrastructure/WindowsApi/SystemTrayIcon.csnew file mode 100644index 00000000..a7cd50c7--- /dev/null+++ b/GlazeWM.Infrastructure/WindowsApi/SystemTrayIcon.cs@@ -0,0 +1,53 @@+using System;+using System.Drawing;+using System.Reflection;+using System.Windows.Forms;++namespace GlazeWM.Infrastructure.WindowsApi+{+ public class SystemTrayIcon+ {+ private readonly SystemTrayIconConfig _config;+ private NotifyIcon _notifyIcon { get; set; }++ public SystemTrayIcon(SystemTrayIconConfig config)+ {+ _config = config;+ }++ public void Show()+ {+ var contextMenuStrip = new ContextMenuStrip();++ foreach (var action in _config.Actions)+ {+ // Convert action to `EventHandler` delegate.+ void actionDelegate(object sender, EventArgs e) => action.Value();++ // Create an entry in the context menu that is shown on click.+ contextMenuStrip.Items.Add(action.Key, null, actionDelegate);+ }++ // Get the embedded icon resource from the entry assembly.+ var assembly = Assembly.GetEntryAssembly();+ using var stream = assembly.GetManifestResourceStream(_config.IconResourceName);++ _notifyIcon = new NotifyIcon+ {+ Icon = new Icon(stream),+ ContextMenuStrip = contextMenuStrip,+ Text = _config.HoverText,+ Visible = true+ };+ }++ public void Remove()+ {+ if (_notifyIcon is null)+ return;++ _notifyIcon.Visible = false;+ _notifyIcon.Dispose();+ }+ }+}diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemTrayIconConfig.cs b/GlazeWM.Infrastructure/WindowsApi/SystemTrayIconConfig.csnew file mode 100644index 00000000..a6434a23--- /dev/null+++ b/GlazeWM.Infrastructure/WindowsApi/SystemTrayIconConfig.cs@@ -0,0 +1,12 @@+using System;+using System.Collections.Generic;++namespace GlazeWM.Infrastructure.WindowsApi+{+ public class SystemTrayIconConfig+ {+ public string HoverText { get; init; }+ public string IconResourceName { get; init; }+ public Dictionary<string, Action> Actions { get; init; }+ }+}diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemTrayService.cs b/GlazeWM.Infrastructure/WindowsApi/SystemTrayService.csdeleted file mode 100644index b25ad4ba..00000000--- a/GlazeWM.Infrastructure/WindowsApi/SystemTrayService.cs+++ /dev/null@@ -1,50 +0,0 @@-using System;-using System.Drawing;-using System.Reflection;-using System.Windows.Forms;-using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Infrastructure.Common.Commands;--namespace GlazeWM.Infrastructure.WindowsApi-{- public class SystemTrayService- {- private readonly Bus _bus;- private NotifyIcon _notifyIcon { get; set; }-- public SystemTrayService(Bus bus)- {- _bus = bus;- }-- public void AddToSystemTray()- {- var contextMenuStrip = new ContextMenuStrip();- contextMenuStrip.Items.Add("Exit", null, ExitApplication);-- var assembly = Assembly.GetEntryAssembly();- const string iconResourceName = "GlazeWM.Bootstrapper.icon.ico";-- // Get the embedded icon resource from the entry assembly.- using var stream = assembly.GetManifestResourceStream(iconResourceName);-- _notifyIcon = new NotifyIcon- {- Icon = new Icon(stream),- ContextMenuStrip = contextMenuStrip,- Text = "GlazeWM",- Visible = true- };- }-- private void ExitApplication(object sender, EventArgs e)- {- _bus.Invoke(new ExitApplicationCommand(false));- }-- public void RemoveFromSystemTray()- {- _notifyIcon?.Dispose();- }- }-}