Skip to content

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

feat(infra,domain): remove `WindowHooksHandler`; instead emit events through `WindowEventService`

Lars Berger976b3a118511
diff --git a/LarsWM.Bootstrapper/Startup.cs b/LarsWM.Bootstrapper/Startup.cs
index 3795ab14..4ffea36e 100644
--- a/LarsWM.Bootstrapper/Startup.cs
+++ b/LarsWM.Bootstrapper/Startup.cs
@@ -18,7 +18,6 @@ namespace LarsWM.Bootstrapper
private MonitorService _monitorService;
private KeybindingService _keybindingService;
private WindowEventService _windowEventService;
- private WindowHooksHandler _windowHooksHandler;
private WindowService _windowService;
private BarManagerService _barManagerService;
@@ -27,7 +26,6 @@ namespace LarsWM.Bootstrapper
MonitorService monitorService,
KeybindingService keybindingService,
WindowEventService windowEventService,
- WindowHooksHandler windowHooksHandler,
WindowService windowService,
BarManagerService barManagerService
)
@@ -36,7 +34,6 @@ namespace LarsWM.Bootstrapper
_monitorService = monitorService;
_keybindingService = keybindingService;
_windowEventService = windowEventService;
- _windowHooksHandler = windowHooksHandler;
_windowService = windowService;
_barManagerService = barManagerService;
}
@@ -52,10 +49,7 @@ namespace LarsWM.Bootstrapper
// Populate initial monitors, windows, workspaces and user config.
PopulateInitialState();
- // TODO: Rename `Init` method to `Start`.
- _windowEventService.Init();
- // TODO: Rename `Configure` method to `Start`.
- _windowHooksHandler.Configure();
+ _windowEventService.Start();
}
/// <summary>
diff --git a/LarsWM.Domain/DependencyInjection.cs b/LarsWM.Domain/DependencyInjection.cs
index c285ed47..158fc51e 100644
--- a/LarsWM.Domain/DependencyInjection.cs
+++ b/LarsWM.Domain/DependencyInjection.cs
@@ -1,4 +1,4 @@
-using LarsWM.Domain.Containers;
+using LarsWM.Domain.Containers;
using LarsWM.Domain.Containers.CommandHandlers;
using LarsWM.Domain.Containers.Commands;
using LarsWM.Domain.Monitors;
@@ -29,7 +29,6 @@ namespace LarsWM.Domain
services.AddSingleton<UserConfigService>();
services.AddSingleton<WindowService>();
services.AddSingleton<WorkspaceService>();
- services.AddSingleton<WindowHooksHandler>();
services.AddTransient<ICommandHandler<AttachContainerCommand>, AttachContainerHandler>();
services.AddTransient<ICommandHandler<ChangeContainerLayoutCommand>, ChangeContainerLayoutHandler>();
diff --git a/LarsWM.Domain/Windows/WindowHooksHandler.cs b/LarsWM.Domain/Windows/WindowHooksHandler.cs
deleted file mode 100644
index 5caf0521..00000000
--- a/LarsWM.Domain/Windows/WindowHooksHandler.cs
+++ /dev/null
@@ -1,87 +0,0 @@
-using System;
-using System.Diagnostics;
-using System.Linq;
-using LarsWM.Domain.Containers;
-using LarsWM.Domain.Containers.Commands;
-using LarsWM.Domain.Windows.Commands;
-using LarsWM.Infrastructure.Bussing;
-using LarsWM.Infrastructure.WindowsApi;
-using LarsWM.Infrastructure.WindowsApi.Enums;
-
-namespace LarsWM.Domain.Windows
-{
- // TODO: Move Configure method to WindowService.
- public class WindowHooksHandler
- {
- private Bus _bus;
- private WindowService _windowService;
- private ContainerService _containerService;
-
- private WindowEventService _windowEventService { get; }
-
- public WindowHooksHandler(
- Bus bus,
- WindowService windowService,
- WindowEventService windowEventService,
- ContainerService containerService)
- {
- _bus = bus;
- _windowService = windowService;
- _windowEventService = windowEventService;
- _containerService = containerService;
- }
-
- public void Configure()
- {
- _windowEventService.WindowHookSubject.Subscribe(observer =>
- {
- // TODO: For performance, instead get window instance by using
- // MonitorService.GetMonitorFromUnaddedWindow and searching its displayed
- // workspace.
- var window = _windowService.GetWindows()
- .FirstOrDefault(w => w.Hwnd == observer.AffectedWindowHandle);
-
- switch (observer.EventType)
- {
- case EventConstant.EVENT_OBJECT_SHOW:
- Debug.WriteLine("show");
- // Don't invoke AddWindowCommand if window is already in tree.
- if (window != null)
- return;
-
- _bus.Invoke(new AddWindowCommand(observer.AffectedWindowHandle));
- break;
- case EventConstant.EVENT_OBJECT_DESTROY:
- Debug.WriteLine("destroy");
- // If window is in tree, detach removed window from its parent.
- if (window != null)
- {
- _bus.Invoke(new RemoveWindowCommand(window));
- _bus.Invoke(new RedrawContainersCommand());
- }
-
- break;
- case EventConstant.EVENT_OBJECT_CLOAKED:
- Debug.WriteLine("cloaked");
- break;
- case EventConstant.EVENT_OBJECT_UNCLOAKED:
- Debug.WriteLine("decloaked");
- break;
- case EventConstant.EVENT_SYSTEM_MINIMIZESTART:
- Debug.WriteLine("minimize start");
- break;
- case EventConstant.EVENT_SYSTEM_MINIMIZEEND:
- Debug.WriteLine("minimize end");
- break;
- case EventConstant.EVENT_SYSTEM_FOREGROUND:
- Debug.WriteLine("foreground");
- if (window == null)
- return;
-
- _bus.Invoke(new FocusWindowCommand(window));
- break;
- }
- });
- }
- }
-}
diff --git a/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs b/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
index e40bca76..7cfbb110 100644
--- a/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
+++ b/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
@@ -1,6 +1,7 @@
-using LarsWM.Infrastructure.WindowsApi.Enums;
+using LarsWM.Infrastructure.Bussing;
+using LarsWM.Infrastructure.WindowsApi.Enums;
+using LarsWM.Infrastructure.WindowsApi.Events;
using System;
-using System.Reactive.Subjects;
using System.Threading;
using System.Windows.Forms;
using static LarsWM.Infrastructure.WindowsApi.WindowsApiService;
@@ -9,23 +10,15 @@ namespace LarsWM.Infrastructure.WindowsApi
{
public class WindowEventService
{
- public Subject<WindowHookEvent> WindowHookSubject = new Subject<WindowHookEvent>();
+ private Bus _bus;
+ private static readonly int CHILDID_SELF = 0;
- public static int CHILDID_SELF = 0;
-
- public struct WindowHookEvent
+ public WindowEventService(Bus bus)
{
- public EventConstant EventType { get; }
- public IntPtr AffectedWindowHandle { get; }
-
- public WindowHookEvent(EventConstant eventType, IntPtr hwnd)
- {
- EventType = eventType;
- AffectedWindowHandle = hwnd;
- }
+ _bus = bus;
}
- public void Init()
+ public void Start()
{
var thread = new Thread(() => CreateWindowEventHook());
thread.Name = "LarsWMWindowHooks";
@@ -50,8 +43,24 @@ namespace LarsWM.Infrastructure.WindowsApi
if (!isWindowEvent)
return;
- var windowHookEvent = new WindowHookEvent(eventType, hwnd);
- WindowHookSubject.OnNext(windowHookEvent);
+ switch (eventType)
+ {
+ case EventConstant.EVENT_OBJECT_SHOW:
+ _bus.RaiseEvent(new WindowShownEvent(hwnd));
+ break;
+ case EventConstant.EVENT_OBJECT_DESTROY:
+ _bus.RaiseEvent(new WindowDestroyedEvent(hwnd));
+ break;
+ case EventConstant.EVENT_SYSTEM_MINIMIZESTART:
+ _bus.RaiseEvent(new WindowMinimizedEvent(hwnd));
+ break;
+ case EventConstant.EVENT_SYSTEM_MINIMIZEEND:
+ _bus.RaiseEvent(new WindowMinimizeEndedEvent(hwnd));
+ break;
+ case EventConstant.EVENT_SYSTEM_FOREGROUND:
+ _bus.RaiseEvent(new WindowFocusedEvent(hwnd));
+ break;
+ }
}
}
}