Skip to content

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

feat: prevent empty monitor on workspace move (#195)

lars-berger5058b2ed45a8
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
index 662ad69e..5ba53fb6 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/MoveContainerWithinTreeHandler.cs
@@ -53,7 +53,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
// Get whether the container is the focused descendant in its original subtree.
var isFocusedDescendant = containerToMove == containerToMoveAncestor
- || containerToMoveAncestor.LastFocusedDescendant == containerToMove;
+ || containerToMoveAncestor.LastFocusedDescendant.SelfAndAncestors.Contains(containerToMove);
var targetParentAncestor = targetParent.SelfAndAncestors
.First(ancestor => ancestor.Parent == lowestCommonAncestor);
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceInDirectionHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceInDirectionHandler.cs
new file mode 100644
index 00000000..9ced1909
--- /dev/null
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceInDirectionHandler.cs
@@ -0,0 +1,85 @@
+using System.Linq;
+using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.Monitors;
+using GlazeWM.Domain.Windows;
+using GlazeWM.Domain.Workspaces.Commands;
+using GlazeWM.Domain.Workspaces.Events;
+using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.Exceptions;
+
+namespace GlazeWM.Domain.Workspaces.CommandHandlers
+{
+ internal class MoveWorkspaceInDirectionHandler :
+ ICommandHandler<MoveWorkspaceInDirectionCommand>
+ {
+ private readonly Bus _bus;
+ private readonly MonitorService _monitorService;
+ private readonly WorkspaceService _workspaceService;
+
+ public MoveWorkspaceInDirectionHandler(
+ Bus bus,
+ MonitorService monitorService,
+ WorkspaceService workspaceService)
+ {
+ _bus = bus;
+ _monitorService = monitorService;
+ _workspaceService = workspaceService;
+ }
+
+ public CommandResponse Handle(MoveWorkspaceInDirectionCommand command)
+ {
+ var direction = command.Direction;
+
+ // Get focused workspace + monitor.
+ var focusedWorkspace = _workspaceService.GetFocusedWorkspace();
+ var focusedMonitor = MonitorService.GetMonitorFromChildContainer(focusedWorkspace);
+
+ // Get monitor in the given direction from the focused workspace.
+ var targetMonitor = _monitorService.GetMonitorInDirection(
+ direction,
+ focusedMonitor
+ );
+
+ if (targetMonitor is null)
+ return CommandResponse.Ok;
+
+ // Move workspace to target monitor.
+ _bus.Invoke(
+ new MoveContainerWithinTreeCommand(focusedWorkspace, targetMonitor, false)
+ );
+
+ // Update floating placement since the windows have to cross monitors.
+ foreach (var window in focusedWorkspace.Descendants.OfType<Window>())
+ {
+ window.FloatingPlacement = window.FloatingPlacement.TranslateToCenter(
+ targetMonitor.DisplayedWorkspace.ToRect()
+ );
+ }
+
+ // Prevent original monitor from having no workspaces.
+ if (focusedMonitor.Children.Count == 0)
+ ActivateWorkspaceOnMonitor(focusedMonitor);
+
+ // Update workspaces displayed in bar window.
+ // TODO: Consider creating separate event `WorkspaceMovedEvent`.
+ _bus.Emit(new WorkspaceActivatedEvent(focusedWorkspace));
+
+ _bus.Invoke(new RedrawContainersCommand());
+
+ return CommandResponse.Ok;
+ }
+
+ private void ActivateWorkspaceOnMonitor(Monitor monitor)
+ {
+ // Get name of first workspace that is not active for that specified monitor or any.
+ var inactiveWorkspaceConfig =
+ _workspaceService.GetWorkspaceConfigToActivate(monitor);
+
+ if (inactiveWorkspaceConfig is null)
+ throw new FatalUserException("At least 1 workspace is required per monitor.");
+
+ // Assign the workspace to the empty monitor.
+ _bus.Invoke(new ActivateWorkspaceCommand(inactiveWorkspaceConfig.Name, monitor));
+ }
+ }
+}
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs
deleted file mode 100644
index e796c53d..00000000
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-using System.Linq;
-using GlazeWM.Domain.Containers.Commands;
-using GlazeWM.Domain.Monitors;
-using GlazeWM.Domain.Windows;
-using GlazeWM.Domain.Workspaces.Commands;
-using GlazeWM.Infrastructure.Bussing;
-
-namespace GlazeWM.Domain.Workspaces.CommandHandlers
-{
- internal class MoveWorkspaceInDirectionHandler : ICommandHandler<MoveWorkspaceInDirectionCommand>
- {
- private readonly Bus _bus;
- private readonly MonitorService _monitorService;
- private readonly WorkspaceService _workspaceService;
-
- public MoveWorkspaceInDirectionHandler(
- Bus bus,
- MonitorService monitorService,
- WorkspaceService workspaceService)
- {
- _bus = bus;
- _monitorService = monitorService;
- _workspaceService = workspaceService;
- }
-
- public CommandResponse Handle(MoveWorkspaceInDirectionCommand command)
- {
- // Get focused workspace
- var workspace = _workspaceService.GetFocusedWorkspace();
-
- // Get monitor to move focused workspace
- var focusedMonitor = _monitorService.GetFocusedMonitor();
- var targetMonitor = _monitorService.GetMonitorInDirection(command.Direction, focusedMonitor);
-
- if (targetMonitor != null && targetMonitor != focusedMonitor)
- {
- // Move workspace to target monitor.
- _bus.Invoke(new MoveContainerWithinTreeCommand(workspace, targetMonitor, shouldAdjustSize: false));
-
- // Update floating placement since the window have to cross monitor
- foreach (var window in workspace.Descendants.OfType<Window>())
- {
- window.FloatingPlacement =
- window.FloatingPlacement.TranslateToCenter(targetMonitor.DisplayedWorkspace.ToRect());
- }
-
- _bus.Invoke(new RedrawContainersCommand());
- }
-
- return CommandResponse.Ok;
- }
- }
-}
diff --git a/GlazeWM.Domain/Workspaces/Commands/MoveWorkspaceToDirectionCommand.cs b/GlazeWM.Domain/Workspaces/Commands/MoveWorkspaceInDirectionCommand.cs
similarity index 78%
rename from GlazeWM.Domain/Workspaces/Commands/MoveWorkspaceToDirectionCommand.cs
rename to GlazeWM.Domain/Workspaces/Commands/MoveWorkspaceInDirectionCommand.cs
index 591bc16b..67831835 100644
--- a/GlazeWM.Domain/Workspaces/Commands/MoveWorkspaceToDirectionCommand.cs
+++ b/GlazeWM.Domain/Workspaces/Commands/MoveWorkspaceInDirectionCommand.cs
@@ -5,6 +5,7 @@ namespace GlazeWM.Domain.Workspaces.Commands
{
internal class MoveWorkspaceInDirectionCommand : Command
{
+ // TODO: Add argument for workspace to move instead of assuming the focused workspace.
public Direction Direction { get; }
public MoveWorkspaceInDirectionCommand(Direction direction)