GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor(domain): add `MonitorService` method for getting a monitor by device name * Remove `GetMonitorForWorkspace` method
Lars Bergerbc8d7aebfa8e
diff --git a/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs b/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.csindex 35e4ab6e..37b39966 100644--- a/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs+++ b/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs@@ -34,9 +34,7 @@ namespace GlazeWM.Domain.Monitors.CommandHandlers{foreach (var screen in Screen.AllScreens){- var foundMonitor = _monitorService.GetMonitors().FirstOrDefault(- monitor => monitor.DeviceName == screen.DeviceName- );+ var foundMonitor = _monitorService.GetMonitorByDeviceName(screen.DeviceName);// Add monitor if it doesn't exist in state.if (foundMonitor == null)diff --git a/GlazeWM.Domain/Monitors/MonitorService.cs b/GlazeWM.Domain/Monitors/MonitorService.csindex e1725057..30ae6777 100644--- a/GlazeWM.Domain/Monitors/MonitorService.cs+++ b/GlazeWM.Domain/Monitors/MonitorService.cs@@ -4,7 +4,6 @@ using System.Linq;using System.Windows.Forms;using GlazeWM.Domain.Common.Enums;using GlazeWM.Domain.Containers;-using GlazeWM.Domain.UserConfigs;using GlazeWM.Infrastructure.WindowsApi;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;@@ -13,12 +12,10 @@ namespace GlazeWM.Domain.Monitorspublic class MonitorService{private readonly ContainerService _containerService;- private readonly UserConfigService _userConfigService;- public MonitorService(ContainerService containerService, UserConfigService userConfigService)+ public MonitorService(ContainerService containerService){_containerService = containerService;- _userConfigService = userConfigService;}/// <summary>@@ -29,10 +26,9 @@ namespace GlazeWM.Domain.Monitorsreturn _containerService.ContainerTree.Children.Cast<Monitor>();}- public Monitor GetMonitorForWorkspace(string workspace)+ public Monitor GetMonitorByDeviceName(string deviceName){- var monitorName = _userConfigService.UserConfig.Workspaces.FirstOrDefault(ws => ws.Name == workspace)?.BindToMonitor;- return GetMonitors().FirstOrDefault(m => m.DeviceName == monitorName);+ return GetMonitors().FirstOrDefault(monitor => monitor.DeviceName == deviceName);}public static Monitor GetMonitorFromChildContainer(Container container)@@ -47,8 +43,7 @@ namespace GlazeWM.Domain.Monitors{var screen = Screen.FromHandle(windowHandle);- return GetMonitors().FirstOrDefault(monitor => monitor.DeviceName == screen.DeviceName)- ?? GetMonitors().First();+ return GetMonitorByDeviceName(screen.DeviceName) ?? GetMonitors().First();}public Monitor GetFocusedMonitor()diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.csindex fcf47246..60947a34 100644--- a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs@@ -1,5 +1,6 @@using GlazeWM.Infrastructure.Bussing;using GlazeWM.Domain.Monitors;+using GlazeWM.Domain.UserConfigs;using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Windows;@@ -14,21 +15,23 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlersinternal class FocusWorkspaceHandler : ICommandHandler<FocusWorkspaceCommand>{private readonly Bus _bus;- private readonly WorkspaceService _workspaceService;- private readonly MonitorService _monitorService;private readonly ContainerService _containerService;+ private readonly MonitorService _monitorService;+ private readonly UserConfigService _userConfigService;+ private readonly WorkspaceService _workspaceService;public FocusWorkspaceHandler(Bus bus,- WorkspaceService workspaceService,+ ContainerService containerService,MonitorService monitorService,- ContainerService containerService- )+ UserConfigService userConfigService,+ WorkspaceService workspaceService){_bus = bus;- _workspaceService = workspaceService;- _monitorService = monitorService;_containerService = containerService;+ _monitorService = monitorService;+ _userConfigService = userConfigService;+ _workspaceService = workspaceService;}public CommandResponse Handle(FocusWorkspaceCommand command)@@ -76,7 +79,7 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers.FirstOrDefault(workspace =>{return !workspace.KeepAlive- &&!workspace.HasChildren()+ && !workspace.HasChildren()&& !workspace.IsDisplayed&& _containerService.PendingFocusContainer != workspace;});@@ -92,7 +95,12 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers/// </summary>private Workspace ActivateWorkspace(string workspaceName){- var targetMonitor = _monitorService.GetMonitorForWorkspace(workspaceName) ?? _monitorService.GetFocusedMonitor();+ // Get the monitor that the workspace should be bound to (if it exists).+ var workspaceConfig = _userConfigService.GetWorkspaceConfigByName(workspaceName);+ var boundMonitor =+ _monitorService.GetMonitorByDeviceName(workspaceConfig.BindToMonitor);++ var targetMonitor = boundMonitor ?? _monitorService.GetFocusedMonitor();_bus.Invoke(new ActivateWorkspaceCommand(workspaceName, targetMonitor));return _workspaceService.GetActiveWorkspaceByName(workspaceName);diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.csindex 0cf3f575..5d3a86a4 100644--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWindowToWorkspaceHandler.cs@@ -1,10 +1,11 @@using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Domain.Workspaces.Commands;-using GlazeWM.Domain.Containers;-using GlazeWM.Domain.Windows;using GlazeWM.Domain.Monitors;+using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;+using GlazeWM.Domain.UserConfigs;+using GlazeWM.Domain.Windows;using GlazeWM.Domain.Windows.Commands;+using GlazeWM.Domain.Workspaces.Commands;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Domain.Workspaces.CommandHandlers@@ -12,21 +13,23 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlersinternal class MoveWindowToWorkspaceHandler : ICommandHandler<MoveWindowToWorkspaceCommand>{private readonly Bus _bus;- private readonly WorkspaceService _workspaceService;private readonly ContainerService _containerService;private readonly MonitorService _monitorService;+ private readonly UserConfigService _userConfigService;+ private readonly WorkspaceService _workspaceService;public MoveWindowToWorkspaceHandler(Bus bus,- WorkspaceService workspaceService,ContainerService containerService,- MonitorService monitorService- )+ MonitorService monitorService,+ UserConfigService userConfigService,+ WorkspaceService workspaceService){_bus = bus;- _workspaceService = workspaceService;_containerService = containerService;_monitorService = monitorService;+ _userConfigService = userConfigService;+ _workspaceService = workspaceService;}public CommandResponse Handle(MoveWindowToWorkspaceCommand command)@@ -35,11 +38,8 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlersvar workspaceName = command.WorkspaceName;var currentWorkspace = WorkspaceService.GetWorkspaceFromChildContainer(windowToMove);- // Make sure workspace opens on the appropriate monitor- var targetMonitor = _monitorService.GetMonitorForWorkspace(workspaceName) ?? MonitorService.GetMonitorFromChildContainer(currentWorkspace);-var targetWorkspace = _workspaceService.GetActiveWorkspaceByName(workspaceName)- ?? ActivateWorkspace(workspaceName, targetMonitor);+ ?? ActivateWorkspace(workspaceName, windowToMove);// Since target workspace could be on a different monitor, adjustments might need to be made// because of DPI.@@ -66,9 +66,19 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlersreturn CommandResponse.Ok;}- private Workspace ActivateWorkspace(string workspaceName, Monitor targetMonitor)+ private Workspace ActivateWorkspace(string workspaceName, Window windowToMove){+ var currentMonitor = MonitorService.GetMonitorFromChildContainer(windowToMove);++ // Get the monitor that the workspace should be bound to (if it exists).+ var workspaceConfig = _userConfigService.GetWorkspaceConfigByName(workspaceName);+ var boundMonitor =+ _monitorService.GetMonitorByDeviceName(workspaceConfig.BindToMonitor);++ // Activate the workspace on the target monitor.+ var targetMonitor = boundMonitor ?? currentMonitor;_bus.Invoke(new ActivateWorkspaceCommand(workspaceName, targetMonitor));+return _workspaceService.GetActiveWorkspaceByName(workspaceName);}