Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(infra): create `SystemTrayService` for adding app to system tray

Lars Berger376396cb0728
diff --git a/GlazeWM.Infrastructure/DependencyInjection.cs b/GlazeWM.Infrastructure/DependencyInjection.cs
index 160c7261..fd273628 100644
--- a/GlazeWM.Infrastructure/DependencyInjection.cs
+++ b/GlazeWM.Infrastructure/DependencyInjection.cs
@@ -10,8 +10,9 @@ namespace GlazeWM.Infrastructure
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services)
{
services.AddSingleton<Bus>();
- services.AddSingleton<WindowEventService>();
services.AddSingleton<KeybindingService>();
+ services.AddSingleton<SystemTrayService>();
+ services.AddSingleton<WindowEventService>();
services.AddSingleton<YamlDeserializationService>();
// TODO: Change WindowsApiService to be compatible with DI.
diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemTrayService.cs b/GlazeWM.Infrastructure/WindowsApi/SystemTrayService.cs
new file mode 100644
index 00000000..e2beb859
--- /dev/null
+++ b/GlazeWM.Infrastructure/WindowsApi/SystemTrayService.cs
@@ -0,0 +1,52 @@
+using GlazeWM.Infrastructure.Bussing;
+using System;
+using System.Drawing;
+using System.Windows.Forms;
+using System.Threading;
+
+namespace GlazeWM.Infrastructure.WindowsApi
+{
+ public class SystemTrayService
+ {
+ private Bus _bus;
+
+ public SystemTrayService(Bus bus)
+ {
+ _bus = bus;
+ }
+
+ public void AddToSystemTray(string iconPath)
+ {
+ var thread = new Thread(() => PopulateSystemTray(iconPath));
+ thread.Name = "GlazeWMSystemTray";
+ thread.Start();
+ }
+
+ private void PopulateSystemTray(string iconPath)
+ {
+ var contextMenuStrip = new ContextMenuStrip();
+
+ ToolStripItem exitMenuItem = new ToolStripMenuItem("Exit");
+ contextMenuStrip.Items.Add(exitMenuItem);
+
+ var notificationIcon = new NotifyIcon()
+ {
+ Icon = new Icon(iconPath),
+ ContextMenuStrip = contextMenuStrip,
+ Text = "GlazeWM"
+ };
+
+ notificationIcon.Visible = true;
+
+ // System tray requires a message loop within the thread that is executing the code.
+ Application.Run();
+ }
+
+ static void OnApplicationExit(object sender, EventArgs e)
+ {
+ // TODO: Call `Dispose()` on `notificationIcon`.
+ // TODO: Emit bus event that application is exiting.
+ Application.Exit();
+ }
+ }
+}