Skip to content

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

feat: created MonitorService with method for getting Monitor from a Window handle

Lars80cb7355a969
diff --git a/LarsWM.Core/Monitors/MonitorService.cs b/LarsWM.Core/Monitors/MonitorService.cs
new file mode 100644
index 00000000..23ea639b
--- /dev/null
+++ b/LarsWM.Core/Monitors/MonitorService.cs
@@ -0,0 +1,30 @@
+using LarsWM.Core.Windows;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Forms;
+
+namespace LarsWM.Core.Monitors
+{
+ public class MonitorService
+ {
+ private AppState _appState;
+
+ public MonitorService(AppState appState)
+ {
+ _appState = appState;
+ }
+
+ public Monitor GetMonitorFromWindowHandle(Window window)
+ {
+ var screen = Screen.FromHandle(window.Hwnd);
+
+ var matchedMonitor = _appState.Monitors.FirstOrDefault(m => m.Screen.DeviceName == screen.DeviceName);
+ if (matchedMonitor == null)
+ return _appState.Monitors[0];
+
+ return matchedMonitor;
+ }
+ }
+}
diff --git a/LarsWM.Core/Program.cs b/LarsWM.Core/Program.cs
index 30a7979c..ef7c6968 100644
--- a/LarsWM.Core/Program.cs
+++ b/LarsWM.Core/Program.cs
@@ -1,4 +1,5 @@
using LarsWM.Core.Common.Services;
+using LarsWM.Core.Monitors;
using LarsWM.Core.Monitors.CommandHandlers;
using LarsWM.Core.Monitors.EventHandler;
using Microsoft.Extensions.DependencyInjection;
@@ -39,6 +40,7 @@ namespace LarsWM.Core
serviceCollection.AddSingleton<IBus, Bus>();
serviceCollection.AddSingleton<AppState>();
+ serviceCollection.AddSingleton<MonitorService>();
serviceCollection.AddSingleton<AddMonitorHandler>();
serviceCollection.AddSingleton<MonitorAddedHandler>();
serviceCollection.AddSingleton<Startup>();