GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor(domain): move out logic for updating monitor state to new `RefreshMonitorStateCommand`
Lars Bergerc6502dfad9a0
diff --git a/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs b/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.csnew file mode 100644index 00000000..8a7298e6--- /dev/null+++ b/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs@@ -0,0 +1,120 @@+using System.Linq;+using System.Windows.Forms;+using GlazeWM.Domain.Containers;+using GlazeWM.Domain.Containers.Commands;+using GlazeWM.Domain.Monitors.Commands;+using GlazeWM.Domain.Monitors.Events;+using GlazeWM.Domain.Windows;+using GlazeWM.Domain.Workspaces.Commands;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Monitors.CommandHandlers+{+ class RefreshMonitorStateHandler : ICommandHandler<RefreshMonitorStateCommand>+ {+ private Bus _bus;+ private MonitorService _monitorService;+ private ContainerService _containerService;+ private WindowService _windowService;++ public RefreshMonitorStateHandler(+ Bus bus,+ MonitorService monitorService,+ ContainerService containerService,+ WindowService windowService+ )+ {+ _bus = bus;+ _monitorService = monitorService;+ _containerService = containerService;+ _windowService = windowService;+ }++ public CommandResponse Handle(RefreshMonitorStateCommand command)+ {+ foreach (var screen in Screen.AllScreens)+ {+ var foundMonitor = _monitorService.GetMonitors().FirstOrDefault(+ monitor => monitor.DeviceName == screen.DeviceName+ );++ // Add monitor if it doesn't exist in state.+ if (foundMonitor == null)+ {+ _bus.Invoke(new AddMonitorCommand(screen));+ continue;+ }++ // Update monitor with changes to dimensions and positioning.+ foundMonitor.Width = screen.WorkingArea.Width;+ foundMonitor.Height = screen.WorkingArea.Height;+ foundMonitor.X = screen.WorkingArea.X;+ foundMonitor.Y = screen.WorkingArea.Y;+ foundMonitor.IsPrimary = screen.Primary;+ }++ // Verify that all monitors in state exist in `Screen.AllScreens`.+ var monitorsToRemove = _monitorService.GetMonitors().Where(+ monitor => !IsMonitorActive(monitor)+ );++ // Remove any monitors that no longer exist and move their workspaces to other monitors.+ // TODO: Verify that this works with "Duplicate these displays" or "Show only X" settings.+ foreach (var monitor in monitorsToRemove.ToList())+ RemoveMonitor(monitor);++ // Display setting changes can spread windows out sporadically, so mark all windows as needing+ // a DPI adjustment (just in case).+ foreach (var window in _windowService.GetWindows())+ window.HasPendingDpiAdjustment = true;++ // Redraw full container tree.+ _containerService.ContainersToRedraw.Add(_containerService.ContainerTree);+ _bus.Invoke(new RedrawContainersCommand());++ return CommandResponse.Ok;+ }++ private bool IsMonitorActive(Monitor monitor)+ {+ return Screen.AllScreens.Any(screen => screen.DeviceName == monitor.DeviceName);+ }++ // TODO: Move to own command.+ private void RemoveMonitor(Monitor monitorToRemove)+ {+ // Keep reference to the focused monitor prior to moving workspaces around.+ var focusedMonitor = _monitorService.GetFocusedMonitor();++ var targetMonitor = _monitorService.GetMonitors().First(+ monitor => monitor != monitorToRemove+ );++ // Avoid moving empty workspaces.+ var workspacesToMove = monitorToRemove.Children.Where(workspace => workspace.HasChildren());++ foreach (var workspace in workspacesToMove.ToList())+ {+ // Move workspace to target monitor.+ _bus.Invoke(new MoveContainerWithinTreeCommand(workspace, targetMonitor, false));++ // Get windows of the moved workspace.+ var windows = workspace.Descendants+ .Where(descendant => descendant is Window)+ .Cast<Window>();++ // Adjust floating position of moved windows.+ // TODO: If primary monitor changes, does floating placement of all windows need to be updated?+ foreach (var window in windows)+ window.FloatingPlacement =+ window.FloatingPlacement.TranslateToCenter(workspace.ToRectangle());+ }++ _bus.Invoke(new DetachContainerCommand(monitorToRemove));+ _bus.RaiseEvent(new MonitorRemovedEvent(monitorToRemove.DeviceName));++ if (focusedMonitor == monitorToRemove)+ _bus.Invoke(new FocusWorkspaceCommand(targetMonitor.DisplayedWorkspace.Name));+ }+ }+}diff --git a/GlazeWM.Domain/Monitors/Commands/RefreshMonitorStateCommand.cs b/GlazeWM.Domain/Monitors/Commands/RefreshMonitorStateCommand.csnew file mode 100644index 00000000..730a0b61--- /dev/null+++ b/GlazeWM.Domain/Monitors/Commands/RefreshMonitorStateCommand.cs@@ -0,0 +1,11 @@+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Monitors.Commands+{+ public class RefreshMonitorStateCommand : Command+ {+ public RefreshMonitorStateCommand()+ {+ }+ }+}diff --git a/GlazeWM.Domain/Monitors/EventHandlers/DisplaySettingsChangedHandler.cs b/GlazeWM.Domain/Monitors/EventHandlers/DisplaySettingsChangedHandler.csindex 0091b83a..f20c7d8f 100644--- a/GlazeWM.Domain/Monitors/EventHandlers/DisplaySettingsChangedHandler.cs+++ b/GlazeWM.Domain/Monitors/EventHandlers/DisplaySettingsChangedHandler.cs@@ -1,11 +1,4 @@-using System.Linq;-using System.Windows.Forms;-using GlazeWM.Domain.Containers;-using GlazeWM.Domain.Containers.Commands;-using GlazeWM.Domain.Monitors.Commands;-using GlazeWM.Domain.Monitors.Events;-using GlazeWM.Domain.Windows;-using GlazeWM.Domain.Workspaces.Commands;+using GlazeWM.Domain.Monitors.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi.Events;@@ -14,106 +7,15 @@ namespace GlazeWM.Domain.Monitors.EventHandlersclass DisplaySettingsChangedHandler : IEventHandler<DisplaySettingsChangedEvent>{private Bus _bus;- private MonitorService _monitorService;- private ContainerService _containerService;- private WindowService _windowService;- public DisplaySettingsChangedHandler(- Bus bus,- MonitorService monitorService,- ContainerService containerService,- WindowService windowService- )+ public DisplaySettingsChangedHandler(Bus bus){_bus = bus;- _monitorService = monitorService;- _containerService = containerService;- _windowService = windowService;}public void Handle(DisplaySettingsChangedEvent @event){- foreach (var screen in Screen.AllScreens)- {- var foundMonitor = _monitorService.GetMonitors().FirstOrDefault(- monitor => monitor.DeviceName == screen.DeviceName- );-- // Add monitor if it doesn't exist in state.- if (foundMonitor == null)- {- _bus.Invoke(new AddMonitorCommand(screen));- continue;- }-- // Update monitor with changes to dimensions and positioning.- foundMonitor.Width = screen.WorkingArea.Width;- foundMonitor.Height = screen.WorkingArea.Height;- foundMonitor.X = screen.WorkingArea.X;- foundMonitor.Y = screen.WorkingArea.Y;- foundMonitor.IsPrimary = screen.Primary;- }-- // Verify that all monitors in state exist in `Screen.AllScreens`.- var monitorsToRemove = _monitorService.GetMonitors().Where(- monitor => !IsMonitorActive(monitor)- );-- // Remove any monitors that no longer exist and move their workspaces to other monitors.- // TODO: Verify that this works with "Duplicate these displays" or "Show only X" settings.- foreach (var monitor in monitorsToRemove.ToList())- RemoveMonitor(monitor);-- // Display setting changes can spread windows out sporadically, so mark all windows as needing- // a DPI adjustment (just in case).- foreach (var window in _windowService.GetWindows())- window.HasPendingDpiAdjustment = true;-- // Redraw full container tree.- _containerService.ContainersToRedraw.Add(_containerService.ContainerTree);- _bus.Invoke(new RedrawContainersCommand());- }-- private bool IsMonitorActive(Monitor monitor)- {- return Screen.AllScreens.Any(screen => screen.DeviceName == monitor.DeviceName);- }-- // TODO: Move to own command.- private void RemoveMonitor(Monitor monitorToRemove)- {- // Keep reference to the focused monitor prior to moving workspaces around.- var focusedMonitor = _monitorService.GetFocusedMonitor();-- var targetMonitor = _monitorService.GetMonitors().First(- monitor => monitor != monitorToRemove- );-- // Avoid moving empty workspaces.- var workspacesToMove = monitorToRemove.Children.Where(workspace => workspace.HasChildren());-- foreach (var workspace in workspacesToMove.ToList())- {- // Move workspace to target monitor.- _bus.Invoke(new MoveContainerWithinTreeCommand(workspace, targetMonitor, false));-- // Get windows of the moved workspace.- var windows = workspace.Descendants- .Where(descendant => descendant is Window)- .Cast<Window>();-- // Adjust floating position of moved windows.- // TODO: If primary monitor changes, does floating placement of all windows need to be updated?- foreach (var window in windows)- window.FloatingPlacement =- window.FloatingPlacement.TranslateToCenter(workspace.ToRectangle());- }-- _bus.Invoke(new DetachContainerCommand(monitorToRemove));- _bus.RaiseEvent(new MonitorRemovedEvent(monitorToRemove.DeviceName));-- if (focusedMonitor == monitorToRemove)- _bus.Invoke(new FocusWorkspaceCommand(targetMonitor.DisplayedWorkspace.Name));+ _bus.Invoke(new RefreshMonitorStateCommand());}}}