GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add commands "focus workspace next/prev" (#159)
Vladimir Kuznetsovaa61a231e030
diff --git a/GlazeWM.Domain/Common/Enums/Sequence.cs b/GlazeWM.Domain/Common/Enums/Sequence.csnew file mode 100644index 00000000..40b573c6--- /dev/null+++ b/GlazeWM.Domain/Common/Enums/Sequence.cs@@ -0,0 +1,9 @@+namespace GlazeWM.Domain.Common.Enums+{+ // Used to select the direction when switching the previous/next workspace+ public enum Sequence+ {+ PREVIOUS,+ NEXT+ }+}diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex 5ce44b50..58becba7 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -72,6 +72,7 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<ActivateWorkspaceCommand>, ActivateWorkspaceHandler>();services.AddSingleton<ICommandHandler<DeactivateWorkspaceCommand>, DeactivateWorkspaceHandler>();services.AddSingleton<ICommandHandler<FocusWorkspaceCommand>, FocusWorkspaceHandler>();+ 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.csindex fa02c2cc..d85fafb0 100644--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs@@ -1,4 +1,4 @@-using System;+using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;@@ -113,11 +113,22 @@ namespace GlazeWM.Domain.UserConfigs"up" => new FocusInDirectionCommand(Direction.UP),"down" => new FocusInDirectionCommand(Direction.DOWN),"workspace" when IsValidWorkspace(commandParts[2]) =>- new FocusWorkspaceCommand(commandParts[2]),+ ParseFocusWorkspaceCommand(commandParts),_ => throw new ArgumentException(null, nameof(commandParts)),};}+ private Command ParseFocusWorkspaceCommand(string[] commandParts)+ {+ return commandParts[2] switch+ {+ "prev" => new FocusWorkspaceSequenceCommand(Sequence.PREVIOUS),+ "next" => new FocusWorkspaceSequenceCommand(Sequence.NEXT),+ // errors already checked at the previous level parsing+ _ => new FocusWorkspaceCommand(commandParts[2]),+ };+ }+private Command ParseMoveCommand(string[] commandParts, Container subjectContainer){return commandParts[1] switch@@ -220,6 +231,12 @@ namespace GlazeWM.Domain.UserConfigs/// </summary>private bool IsValidWorkspace(string workspaceName){+ // Validate command "focus workspace next" or "focus workspace prev"+ if (workspaceName == "next" || workspaceName == "prev")+ {+ return true;+ }+var workspaceConfig = _userConfigService.GetWorkspaceConfigByName(workspaceName);return workspaceConfig is not null;diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceSequenceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceSequenceHandler.csnew file mode 100644index 00000000..dc175b9c--- /dev/null+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceSequenceHandler.cs@@ -0,0 +1,59 @@+using System.Linq;+using GlazeWM.Domain.Common.Enums;+using GlazeWM.Domain.UserConfigs;+using GlazeWM.Domain.Workspaces.Commands;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Workspaces.CommandHandlers+{+ internal class FocusWorkspaceSequenceHandler : ICommandHandler<FocusWorkspaceSequenceCommand>+ {+ private readonly Bus _bus;+ private readonly UserConfigService _userConfigService;+ private readonly WorkspaceService _workspaceService;++ public FocusWorkspaceSequenceHandler(+ Bus bus,+ UserConfigService userConfigService,+ WorkspaceService workspaceService)+ {+ _bus = bus;+ _userConfigService = userConfigService;+ _workspaceService = workspaceService;+ }++ public CommandResponse Handle(FocusWorkspaceSequenceCommand command)+ {+ // Get the currently focused workspace.+ var focusedWorkspace = _workspaceService.GetFocusedWorkspace();++ var workspacesConfigs = _userConfigService.WorkspaceConfigs;++ var focusWorkspaceConfig = workspacesConfigs+ .Where(config => config.Name == focusedWorkspace.Name)+ .First();++ // Get index config current focused workspace.+ var currentIndex = workspacesConfigs.IndexOf(focusWorkspaceConfig);++ var configLength = workspacesConfigs.Count();++ // Calculate index requested workspace.+ var newIndex = command.Direction switch+ {+ Sequence.PREVIOUS => currentIndex == 0 ? configLength - 1 : currentIndex - 1,+ Sequence.NEXT => currentIndex == configLength - 1 ? 0 : currentIndex + 1,+ _ => currentIndex,+ };++ // If workspace changed, focus on new workspace.+ if (newIndex != currentIndex)+ {+ var newWorkspaceName = workspacesConfigs[newIndex].Name;+ _bus.Invoke(new FocusWorkspaceCommand(newWorkspaceName));+ }++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Workspaces/Commands/FocusWorkspaceSequenceCommand.cs b/GlazeWM.Domain/Workspaces/Commands/FocusWorkspaceSequenceCommand.csnew file mode 100644index 00000000..f9a7adac--- /dev/null+++ b/GlazeWM.Domain/Workspaces/Commands/FocusWorkspaceSequenceCommand.cs@@ -0,0 +1,15 @@+using GlazeWM.Domain.Common.Enums;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Workspaces.Commands+{+ public class FocusWorkspaceSequenceCommand : Command+ {+ public Sequence Direction { get; }++ public FocusWorkspaceSequenceCommand(Sequence direction)+ {+ Direction = direction;+ }+ }+}