Skip to content

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

feat(domain): only iterate through active workspaces in `FocusWorkspaceSequenceHandler`

Lars Berger9e3d0ecbdea1
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceSequenceHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceSequenceHandler.cs
index dc175b9c..7e682ec5 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceSequenceHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/FocusWorkspaceSequenceHandler.cs
@@ -1,4 +1,5 @@
-using System.Linq;
+using System;
+using System.Linq;
using GlazeWM.Domain.Common.Enums;
using GlazeWM.Domain.UserConfigs;
using GlazeWM.Domain.Workspaces.Commands;
@@ -24,34 +25,35 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
public CommandResponse Handle(FocusWorkspaceSequenceCommand command)
{
- // Get the currently focused workspace.
- var focusedWorkspace = _workspaceService.GetFocusedWorkspace();
-
+ var direction = command.Direction;
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);
+ // Get active workspaces in order of their config index.
+ var activeWorkspaces = _workspaceService.GetActiveWorkspaces();
+ var sortedWorkspaces = activeWorkspaces
+ .OrderBy((workspace) =>
+ workspacesConfigs.FindIndex((config) => config.Name == workspace.Name)
+ )
+ .ToList();
- var configLength = workspacesConfigs.Count();
+ // Get config index of the currently focused workspace.
+ var focusedWorkspace = _workspaceService.GetFocusedWorkspace();
+ var configIndex = sortedWorkspaces.IndexOf(focusedWorkspace);
- // Calculate index requested workspace.
- var newIndex = command.Direction switch
+ // Get index in `sortedWorkspaces` of target workspace to focus. Wrap around to start if
+ // there is no previous/next workspace.
+ var indexToFocus = direction switch
{
- Sequence.PREVIOUS => currentIndex == 0 ? configLength - 1 : currentIndex - 1,
- Sequence.NEXT => currentIndex == configLength - 1 ? 0 : currentIndex + 1,
- _ => currentIndex,
+ Sequence.PREVIOUS => configIndex == 0 ? sortedWorkspaces.Count - 1 : configIndex - 1,
+ Sequence.NEXT => configIndex == sortedWorkspaces.Count - 1 ? 0 : configIndex + 1,
+ _ => throw new ArgumentException(nameof(direction)),
};
- // If workspace changed, focus on new workspace.
- if (newIndex != currentIndex)
- {
- var newWorkspaceName = workspacesConfigs[newIndex].Name;
- _bus.Invoke(new FocusWorkspaceCommand(newWorkspaceName));
- }
+ var workspaceToFocus = sortedWorkspaces.ElementAtOrDefault(indexToFocus);
+
+ // Set focus to the previous/next workspace if found.
+ if (workspaceToFocus is not null && workspaceToFocus != focusedWorkspace)
+ _bus.Invoke(new FocusWorkspaceCommand(workspaceToFocus.Name));
return CommandResponse.Ok;
}