Skip to content

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

feat(infra): listen for window hidden event in window event hook

Lars Bergerb5dc47f88a3f
diff --git a/LarsWM.Infrastructure/WindowsApi/Enums/EventConstant.cs b/LarsWM.Infrastructure/WindowsApi/Enums/EventConstant.cs
index 75a84e27..3e0f2ae7 100644
--- a/LarsWM.Infrastructure/WindowsApi/Enums/EventConstant.cs
+++ b/LarsWM.Infrastructure/WindowsApi/Enums/EventConstant.cs
@@ -8,8 +8,9 @@
EVENT_SYSTEM_FOREGROUND = 0x0003,
EVENT_SYSTEM_MINIMIZESTART = 0x0016,
EVENT_SYSTEM_MINIMIZEEND = 0x0017,
- EVENT_OBJECT_SHOW = 0x8002,
EVENT_OBJECT_DESTROY = 0x8001,
+ EVENT_OBJECT_SHOW = 0x8002,
+ EVENT_OBJECT_HIDE = 0x8003,
EVENT_OBJECT_CLOAKED = 0x8017,
EVENT_OBJECT_UNCLOAKED = 0x8018,
}
diff --git a/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs b/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
index 213114fb..e40bca76 100644
--- a/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
+++ b/LarsWM.Infrastructure/WindowsApi/WindowEventService.cs
@@ -9,8 +9,6 @@ namespace LarsWM.Infrastructure.WindowsApi
{
public class WindowEventService
{
- // TODO: Change from Subject to Observable.
- // TODO: Create the Observable in constructor instead of `Init` method.
public Subject<WindowHookEvent> WindowHookSubject = new Subject<WindowHookEvent>();
public static int CHILDID_SELF = 0;
@@ -29,29 +27,31 @@ namespace LarsWM.Infrastructure.WindowsApi
public void Init()
{
- var callback = new WindowEventProc((IntPtr hWinEventHook, EventConstant eventType, IntPtr hwnd, ObjectIdentifier idObject, int idChild, uint dwEventThread, uint dwmsEventTime) =>
- {
- var isWindowEvent = idChild == CHILDID_SELF && idObject == ObjectIdentifier.OBJID_WINDOW && hwnd != IntPtr.Zero;
+ var thread = new Thread(() => CreateWindowEventHook());
+ thread.Name = "LarsWMWindowHooks";
+ thread.Start();
+ }
- if (!isWindowEvent)
- return;
+ private void CreateWindowEventHook()
+ {
+ SetWinEventHook(EventConstant.EVENT_OBJECT_DESTROY, EventConstant.EVENT_OBJECT_HIDE, IntPtr.Zero, WindowEventHookProc, 0, 0, 0);
+ SetWinEventHook(EventConstant.EVENT_OBJECT_CLOAKED, EventConstant.EVENT_OBJECT_UNCLOAKED, IntPtr.Zero, WindowEventHookProc, 0, 0, 0);
+ SetWinEventHook(EventConstant.EVENT_SYSTEM_MINIMIZESTART, EventConstant.EVENT_SYSTEM_MINIMIZEEND, IntPtr.Zero, WindowEventHookProc, 0, 0, 0);
+ SetWinEventHook(EventConstant.EVENT_SYSTEM_FOREGROUND, EventConstant.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, WindowEventHookProc, 0, 0, 0);
- var windowHookEvent = new WindowHookEvent(eventType, hwnd);
- WindowHookSubject.OnNext(windowHookEvent);
- });
+ // `SetWindowsHookEx` requires a message loop within the thread that is executing the code.
+ Application.Run();
+ }
- // SetWinEventHook requires a message loop within the thread that is executing the code.
- var thread = new Thread(() =>
- {
- SetWinEventHook(EventConstant.EVENT_OBJECT_DESTROY, EventConstant.EVENT_OBJECT_SHOW, IntPtr.Zero, callback, 0, 0, 0);
- SetWinEventHook(EventConstant.EVENT_OBJECT_CLOAKED, EventConstant.EVENT_OBJECT_UNCLOAKED, IntPtr.Zero, callback, 0, 0, 0);
- SetWinEventHook(EventConstant.EVENT_SYSTEM_MINIMIZESTART, EventConstant.EVENT_SYSTEM_MINIMIZEEND, IntPtr.Zero, callback, 0, 0, 0);
- SetWinEventHook(EventConstant.EVENT_SYSTEM_FOREGROUND, EventConstant.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, callback, 0, 0, 0);
- Application.Run();
- });
- thread.Name = "LarsWMWindowHooks";
- thread.Start();
+ private void WindowEventHookProc(IntPtr hWinEventHook, EventConstant eventType, IntPtr hwnd, ObjectIdentifier idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
+ {
+ var isWindowEvent = idChild == CHILDID_SELF && idObject == ObjectIdentifier.OBJID_WINDOW && hwnd != IntPtr.Zero;
+
+ if (!isWindowEvent)
+ return;
+ var windowHookEvent = new WindowHookEvent(eventType, hwnd);
+ WindowHookSubject.OnNext(windowHookEvent);
}
}
}