Skip to content

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

refactor: remove `SystemEventService`; expose observable of display changes (#234)

lars-berger3b7c9f7ced34
diff --git a/GlazeWM.Bootstrapper/Startup.cs b/GlazeWM.Bootstrapper/Startup.cs
index e604b51a..8deaa959 100644
--- a/GlazeWM.Bootstrapper/Startup.cs
+++ b/GlazeWM.Bootstrapper/Startup.cs
@@ -20,7 +20,6 @@ namespace GlazeWM.Bootstrapper
private readonly BarService _barService;
private readonly Bus _bus;
private readonly KeybindingService _keybindingService;
- private readonly SystemEventService _systemEventService;
private readonly WindowEventService _windowEventService;
private SystemTrayIcon _systemTrayIcon { get; set; }
@@ -29,13 +28,11 @@ namespace GlazeWM.Bootstrapper
BarService barService,
Bus bus,
KeybindingService keybindingService,
- SystemEventService systemEventService,
WindowEventService windowEventService)
{
_barService = barService;
_bus = bus;
_keybindingService = keybindingService;
- _systemEventService = systemEventService;
_windowEventService = windowEventService;
}
@@ -62,8 +59,9 @@ namespace GlazeWM.Bootstrapper
// Listen for window events (eg. close, focus).
_windowEventService.Start();
- // Listen for system-related events (eg. changes to display settings).
- _systemEventService.Start();
+ // Listen for changes to display settings.
+ // TODO: Unsubscribe on application exit.
+ SystemEvents.DisplaySettingsChanged.Subscribe((@event) => _bus.EmitAsync(@event));
var systemTrayIconConfig = new SystemTrayIconConfig
{
diff --git a/GlazeWM.Infrastructure/DependencyInjection.cs b/GlazeWM.Infrastructure/DependencyInjection.cs
index 6f6fe461..df4bd55e 100644
--- a/GlazeWM.Infrastructure/DependencyInjection.cs
+++ b/GlazeWM.Infrastructure/DependencyInjection.cs
@@ -13,7 +13,6 @@ namespace GlazeWM.Infrastructure
{
services.AddSingleton<Bus>();
services.AddSingleton<KeybindingService>();
- services.AddSingleton<SystemEventService>();
services.AddSingleton<WindowEventService>();
services.AddSingleton<JsonService>();
services.AddSingleton<YamlService>();
diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemEventService.cs b/GlazeWM.Infrastructure/WindowsApi/SystemEventService.cs
deleted file mode 100644
index 129dd1a7..00000000
--- a/GlazeWM.Infrastructure/WindowsApi/SystemEventService.cs
+++ /dev/null
@@ -1,31 +0,0 @@
-using System;
-using System.Reactive.Linq;
-using GlazeWM.Infrastructure.Bussing;
-using GlazeWM.Infrastructure.Common.Events;
-using Microsoft.Win32;
-
-namespace GlazeWM.Infrastructure.WindowsApi
-{
- public class SystemEventService
- {
- private readonly Bus _bus;
-
- public SystemEventService(Bus bus)
- {
- _bus = bus;
- }
-
- public void Start()
- {
- // TODO: Unsubscribe on application exit.
- // TODO: Add as static field instead and subcribe directly in `Startup`. Consider renaming
- // class to `MonitorEvents, `SystemEvents`, `Win32MonitorApi`.
- var displaySettingChanges = Observable.FromEventPattern<EventHandler, EventArgs>(
- handler => SystemEvents.DisplaySettingsChanged += handler,
- handler => SystemEvents.DisplaySettingsChanged -= handler
- );
-
- displaySettingChanges.Subscribe((_) => _bus.EmitAsync(new DisplaySettingsChangedEvent()));
- }
- }
-}
diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemEvents.cs b/GlazeWM.Infrastructure/WindowsApi/SystemEvents.cs
new file mode 100644
index 00000000..12325fb8
--- /dev/null
+++ b/GlazeWM.Infrastructure/WindowsApi/SystemEvents.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Reactive.Linq;
+using GlazeWM.Infrastructure.Common.Events;
+
+namespace GlazeWM.Infrastructure.WindowsApi
+{
+ public static class SystemEvents
+ {
+ public static IObservable<DisplaySettingsChangedEvent> DisplaySettingsChanged =>
+ Observable.FromEventPattern<EventHandler, EventArgs>(
+ handler => Microsoft.Win32.SystemEvents.DisplaySettingsChanged += handler,
+ handler => Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= handler
+ ).Select((_) => new DisplaySettingsChangedEvent());
+ }
+}