Skip to content

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

refactor(domain): move out logic for removing a monitor to new `RemoveMonitorCommand`

Lars Berger696c66974dc2
diff --git a/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs b/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs
index 8a7298e6..ccd6a626 100644
--- a/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs
+++ b/GlazeWM.Domain/Monitors/CommandHandlers/RefreshMonitorStateHandler.cs
@@ -3,9 +3,7 @@ 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
@@ -61,7 +59,7 @@ namespace GlazeWM.Domain.Monitors.CommandHandlers
// 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);
+ _bus.Invoke(new RemoveMonitorCommand(monitor));
// Display setting changes can spread windows out sporadically, so mark all windows as needing
// a DPI adjustment (just in case).
@@ -79,42 +77,5 @@ namespace GlazeWM.Domain.Monitors.CommandHandlers
{
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/CommandHandlers/RemoveMonitorHandler.cs b/GlazeWM.Domain/Monitors/CommandHandlers/RemoveMonitorHandler.cs
new file mode 100644
index 00000000..834a9169
--- /dev/null
+++ b/GlazeWM.Domain/Monitors/CommandHandlers/RemoveMonitorHandler.cs
@@ -0,0 +1,61 @@
+using System.Linq;
+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 RemoveMonitorHandler : ICommandHandler<RemoveMonitorCommand>
+ {
+ private Bus _bus;
+ private MonitorService _monitorService;
+
+ public RemoveMonitorHandler(Bus bus, MonitorService monitorService)
+ {
+ _bus = bus;
+ _monitorService = monitorService;
+ }
+
+ public CommandResponse Handle(RemoveMonitorCommand command)
+ {
+ var monitorToRemove = command.MonitorToRemove;
+ var targetMonitor = _monitorService.GetMonitors().First(
+ monitor => monitor != monitorToRemove
+ );
+
+ // Keep reference to the focused monitor prior to moving workspaces around.
+ var focusedMonitor = _monitorService.GetFocusedMonitor();
+
+ // 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));
+
+ return CommandResponse.Ok;
+ }
+ }
+}
diff --git a/GlazeWM.Domain/Monitors/Commands/RemoveMonitorCommand.cs b/GlazeWM.Domain/Monitors/Commands/RemoveMonitorCommand.cs
new file mode 100644
index 00000000..5f80c121
--- /dev/null
+++ b/GlazeWM.Domain/Monitors/Commands/RemoveMonitorCommand.cs
@@ -0,0 +1,14 @@
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Monitors.Commands
+{
+ public class RemoveMonitorCommand : Command
+ {
+ public Monitor MonitorToRemove { get; set; }
+
+ public RemoveMonitorCommand(Monitor monitorToRemove)
+ {
+ MonitorToRemove = monitorToRemove;
+ }
+ }
+}