Skip to content

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

feat: add command to move workspace in direction (#179)

Vladimir Kuznetsov4ddeb83d36fb
diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.cs
index ca2d139c..562c6890 100644
--- a/GlazeWM.Domain/DependencyInjection.cs
+++ b/GlazeWM.Domain/DependencyInjection.cs
@@ -77,6 +77,7 @@ namespace GlazeWM.Domain
services.AddSingleton<ICommandHandler<FocusWorkspaceSequenceCommand>, FocusWorkspaceSequenceHandler>();
services.AddSingleton<ICommandHandler<MoveWindowToWorkspaceCommand>, MoveWindowToWorkspaceHandler>();
services.AddSingleton<ICommandHandler<UpdateWorkspacesFromConfigCommand>, UpdateWorkspacesFromConfigHandler>();
+ services.AddSingleton<ICommandHandler<MoveWorkspaceInDirectionCommand>, MoveWorkspaceInDirectionHandler>();
services.AddSingleton<IEventHandler<DisplaySettingsChangedEvent>, DisplaySettingsChangedHandler>();
services.AddSingleton<IEventHandler<WindowDestroyedEvent>, WindowDestroyedHandler>();
diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
index 3efb62f3..1f7528c0 100644
--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
@@ -150,6 +150,19 @@ namespace GlazeWM.Domain.UserConfigs
"to" when IsValidWorkspace(commandParts[3]) => subjectContainer is Window
? new MoveWindowToWorkspaceCommand(subjectContainer as Window, commandParts[3])
: new NoopCommand(),
+ "workspace" => ParseMoveWorkspaceCommand(commandParts, subjectContainer),
+ _ => throw new ArgumentException(null, nameof(commandParts)),
+ };
+ }
+
+ private Command ParseMoveWorkspaceCommand(string[] commandParts, Container subjectContainer)
+ {
+ return commandParts[2] switch
+ {
+ "left" => new MoveWorkspaceInDirectionCommand(Direction.LEFT),
+ "right" => new MoveWorkspaceInDirectionCommand(Direction.RIGHT),
+ "up" => new MoveWorkspaceInDirectionCommand(Direction.UP),
+ "down" => new MoveWorkspaceInDirectionCommand(Direction.DOWN),
_ => throw new ArgumentException(null, nameof(commandParts)),
};
}
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs
new file mode 100644
index 00000000..a3117188
--- /dev/null
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs
@@ -0,0 +1,53 @@
+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/MoveWorkspaceToDirectionCommand.cs
new file mode 100644
index 00000000..591bc16b
--- /dev/null
+++ b/GlazeWM.Domain/Workspaces/Commands/MoveWorkspaceToDirectionCommand.cs
@@ -0,0 +1,15 @@
+using GlazeWM.Domain.Common.Enums;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Workspaces.Commands
+{
+ internal class MoveWorkspaceInDirectionCommand : Command
+ {
+ public Direction Direction { get; }
+
+ public MoveWorkspaceInDirectionCommand(Direction direction)
+ {
+ Direction = direction;
+ }
+ }
+}