Skip to content

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

feat: add "focus workspace recent" command (#160)

Vladimir Kuznetsove10b61ee6d2b
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/sample-config.yaml
index c97351df..0dd93933 100644
--- a/GlazeWM.Bootstrapper/sample-config.yaml
+++ b/GlazeWM.Bootstrapper/sample-config.yaml
@@ -134,6 +134,12 @@ keybindings:
binding: "Alt+8"
- command: "focus workspace 9"
binding: "Alt+9"
+ - command: "focus workspace recent"
+ binding: "Alt+R"
+ - command: "focus workspace prev"
+ binding: "Alt+Shift+T"
+ - command: "focus workspace next"
+ binding: "Alt+T"
# Move focused window to a workspace defined in `workspaces` config.
- commands: ["move to workspace 1", "focus workspace 1"]
diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.cs
index 58becba7..46bb14dd 100644
--- a/GlazeWM.Domain/DependencyInjection.cs
+++ b/GlazeWM.Domain/DependencyInjection.cs
@@ -72,6 +72,7 @@ namespace GlazeWM.Domain
services.AddSingleton<ICommandHandler<ActivateWorkspaceCommand>, ActivateWorkspaceHandler>();
services.AddSingleton<ICommandHandler<DeactivateWorkspaceCommand>, DeactivateWorkspaceHandler>();
services.AddSingleton<ICommandHandler<FocusWorkspaceCommand>, FocusWorkspaceHandler>();
+ services.AddSingleton<ICommandHandler<FocusWorkspaceRecentCommand>, FocusWorkspaceRecentHandler>();
services.AddSingleton<ICommandHandler<FocusWorkspaceSequenceCommand>, FocusWorkspaceSequenceHandler>();
services.AddSingleton<ICommandHandler<MoveWindowToWorkspaceCommand>, MoveWindowToWorkspaceHandler>();
services.AddSingleton<ICommandHandler<UpdateWorkspacesFromConfigCommand>, UpdateWorkspacesFromConfigHandler>();
diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
index d85fafb0..7a38b7e0 100644
--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
@@ -122,6 +122,7 @@ namespace GlazeWM.Domain.UserConfigs
{
return commandParts[2] switch
{
+ "recent" => new FocusWorkspaceRecentCommand(),
"prev" => new FocusWorkspaceSequenceCommand(Sequence.PREVIOUS),
"next" => new FocusWorkspaceSequenceCommand(Sequence.NEXT),
// errors already checked at the previous level parsing
@@ -232,7 +233,7 @@ namespace GlazeWM.Domain.UserConfigs
private bool IsValidWorkspace(string workspaceName)
{
// Validate command "focus workspace next" or "focus workspace prev"
- if (workspaceName == "next" || workspaceName == "prev")
+ if (workspaceName == "next" || workspaceName == "prev" || workspaceName == "recent")
{
return true;
}
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs
index 0451394e..a5ed6ed3 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceHandler.cs
@@ -45,6 +45,9 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
if (focusedWorkspace == workspaceToFocus)
return CommandResponse.Ok;
+ // Save currently focused workspace as recent for command "recent"
+ _workspaceService.PushRecentWorkspace(focusedWorkspace);
+
// Set focus to the last focused window in workspace. If the workspace has no descendant
// windows, then set focus to the workspace itself.
var containerToFocus = workspaceToFocus.HasChildren()
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceRecentHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceRecentHandler.cs
new file mode 100644
index 00000000..c7a90968
--- /dev/null
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceRecentHandler.cs
@@ -0,0 +1,48 @@
+using System.Linq;
+using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Domain.Workspaces.Commands;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Workspaces.CommandHandlers
+{
+ internal class FocusWorkspaceRecentHandler : ICommandHandler<FocusWorkspaceRecentCommand>
+ {
+ private readonly Bus _bus;
+ private readonly UserConfigService _userConfigService;
+ private readonly WorkspaceService _workspaceService;
+
+ public FocusWorkspaceRecentHandler(
+ Bus bus,
+ UserConfigService userConfigService,
+ WorkspaceService workspaceService)
+ {
+ _bus = bus;
+ _userConfigService = userConfigService;
+ _workspaceService = workspaceService;
+ }
+
+ public CommandResponse Handle(FocusWorkspaceRecentCommand command)
+ {
+ var recentWorkspace = _workspaceService.PopRecentWorkspace();
+ var workspaceConfigs = _userConfigService.WorkspaceConfigs;
+
+ while (recentWorkspace != null)
+ {
+ // Validate that workspace are still available
+ if (workspaceConfigs.Any(workspace => workspace.Name == recentWorkspace.Name))
+ {
+ // Focus workspace
+ _bus.Invoke(new FocusWorkspaceCommand(recentWorkspace.Name));
+ // Remove last entry so as not to get stuck
+ _workspaceService.PopRecentWorkspace();
+
+ return CommandResponse.Ok;
+ }
+
+ recentWorkspace = _workspaceService.PopRecentWorkspace();
+ }
+
+ return CommandResponse.Fail;
+ }
+ }
+}
diff --git a/GlazeWM.Domain/Workspaces/Commands/FocusWorkspaceRecentCommand.cs b/GlazeWM.Domain/Workspaces/Commands/FocusWorkspaceRecentCommand.cs
new file mode 100644
index 00000000..43d196da
--- /dev/null
+++ b/GlazeWM.Domain/Workspaces/Commands/FocusWorkspaceRecentCommand.cs
@@ -0,0 +1,6 @@
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Workspaces.Commands
+{
+ public class FocusWorkspaceRecentCommand : Command { }
+}
diff --git a/GlazeWM.Domain/Workspaces/WorkspaceService.cs b/GlazeWM.Domain/Workspaces/WorkspaceService.cs
index bf68f346..49ad6265 100644
--- a/GlazeWM.Domain/Workspaces/WorkspaceService.cs
+++ b/GlazeWM.Domain/Workspaces/WorkspaceService.cs
@@ -11,6 +11,8 @@ namespace GlazeWM.Domain.Workspaces
private readonly ContainerService _containerService;
private readonly UserConfigService _userConfigService;
+ private readonly Stack<Workspace> _recentWorkspaces = new();
+
public WorkspaceService(ContainerService containerService, UserConfigService userConfigService)
{
_containerService = containerService;
@@ -68,5 +70,19 @@ namespace GlazeWM.Domain.Workspaces
{
return GetWorkspaceFromChildContainer(_containerService.FocusedContainer);
}
+
+ public Workspace PopRecentWorkspace()
+ {
+ if (_recentWorkspaces.Count > 0)
+ {
+ return _recentWorkspaces.Pop();
+ }
+ return null;
+ }
+
+ public void PushRecentWorkspace(Workspace workspace)
+ {
+ _recentWorkspaces.Push(workspace);
+ }
}
}
diff --git a/README.md b/README.md
index c889b21e..82dde9ef 100644
--- a/README.md
+++ b/README.md
@@ -244,6 +244,7 @@ window_rules:
- layout \<vertical | horizontal>
- focus \<left | right | up | down>
+- focus workspace \<prev | next | recent>
- focus workspace \<workspace name>
- move \<left | right | up | down>
- move to workspace \<workspace name>