Skip to content

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

feat(domain): allow keybindings to invoke multiple commands in a sequence

Lars Berger947cd9ca4cb5
diff --git a/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
index 3a632c30..026fd4cd 100644
--- a/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
+++ b/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
@@ -1,11 +1,5 @@
-using System;
-using System.Text.RegularExpressions;
-using LarsWM.Domain.Common.Enums;
-using LarsWM.Domain.Containers.Commands;
+using System.Linq;
using LarsWM.Domain.UserConfigs.Commands;
-using LarsWM.Domain.Windows.Commands;
-using LarsWM.Domain.Workspaces;
-using LarsWM.Domain.Workspaces.Commands;
using LarsWM.Infrastructure.Bussing;
using LarsWM.Infrastructure.WindowsApi;
@@ -15,124 +9,45 @@ namespace LarsWM.Domain.UserConfigs.CommandHandlers
{
private Bus _bus;
private KeybindingService _keybindingService;
- private WorkspaceService _workspaceService;
+ private CommandParsingService _commandParsingService;
- public RegisterKeybindingsHandler(Bus bus, KeybindingService keybindingService, WorkspaceService workspaceService)
+ public RegisterKeybindingsHandler(Bus bus, KeybindingService keybindingService, CommandParsingService commandParsingService)
{
_bus = bus;
_keybindingService = keybindingService;
- _workspaceService = workspaceService;
+ _commandParsingService = commandParsingService;
}
public CommandResponse Handle(RegisterKeybindingsCommand command)
{
- foreach (var keybinding in command.Keybindings)
+ foreach (var keybindingConfig in command.Keybindings)
{
- var commandName = FormatCommandName(keybinding.Command);
- Command parsedCommand = null;
-
- try
- {
- parsedCommand = ParseCommand(commandName);
- }
- catch
+ // Parse command strings defined in keybinding config.
+ var parsedCommands = keybindingConfig.CommandList.Select(commandString =>
{
- throw new FatalUserException($"Invalid command '{commandName}'.");
- }
-
- foreach (var binding in keybinding.Bindings)
- // Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.
- _keybindingService.AddGlobalKeybinding(binding, () => _bus.Invoke((dynamic)parsedCommand));
+ try
+ {
+ commandString = _commandParsingService.FormatCommand(commandString);
+ return _commandParsingService.ParseCommand(commandString);
+ }
+ catch
+ {
+ throw new FatalUserException($"Invalid command '{commandString}'.");
+ }
+ });
+
+ // Register all keybindings for a command sequence.
+ foreach (var binding in keybindingConfig.BindingList)
+ _keybindingService.AddGlobalKeybinding(binding, () =>
+ {
+ // Invoke commands in sequence on keybinding press. Use `dynamic` to resolve the
+ // command type at runtime and allow multiple dispatch.
+ foreach (var parsedCommand in parsedCommands)
+ _bus.Invoke((dynamic)parsedCommand);
+ });
}
return CommandResponse.Ok;
}
-
- private string FormatCommandName(string commandName)
- {
- var formattedCommandString = commandName.Trim().ToLowerInvariant();
- return Regex.Replace(formattedCommandString, @"\s+", " ");
- }
-
- private Command ParseCommand(string commandName)
- {
- var commandParts = commandName.Split(" ");
-
- return commandParts[0] switch
- {
- "layout" => ParseLayoutCommand(commandParts),
- "focus" => ParseFocusCommand(commandParts),
- "move" => ParseMoveCommand(commandParts),
- "resize" => ParseResizeCommand(commandParts),
- "close" => new CloseFocusedWindowCommand(),
- _ => throw new ArgumentException(),
- };
- }
-
- private Command ParseLayoutCommand(string[] commandParts)
- {
- return commandParts[1] switch
- {
- "vertical" => new ChangeFocusedContainerLayoutCommand(Layout.VERTICAL),
- "horizontal" => new ChangeFocusedContainerLayoutCommand(Layout.HORIZONTAL),
- _ => throw new ArgumentException(),
- };
- }
-
- private Command ParseFocusCommand(string[] commandParts)
- {
- return commandParts[1] switch
- {
- "left" => new FocusInDirectionCommand(Direction.LEFT),
- "right" => new FocusInDirectionCommand(Direction.RIGHT),
- "up" => new FocusInDirectionCommand(Direction.UP),
- "down" => new FocusInDirectionCommand(Direction.DOWN),
- "workspace" => new FocusWorkspaceCommand(GetValidWorkspaceName(commandParts[2])),
- _ => throw new ArgumentException(),
- };
- }
-
- private Command ParseMoveCommand(string[] commandParts)
- {
- return commandParts[1] switch
- {
- "left" => new MoveFocusedWindowCommand(Direction.LEFT),
- "right" => new MoveFocusedWindowCommand(Direction.RIGHT),
- "up" => new MoveFocusedWindowCommand(Direction.UP),
- "down" => new MoveFocusedWindowCommand(Direction.DOWN),
- "to" => new MoveFocusedWindowToWorkspaceCommand(GetValidWorkspaceName(commandParts[3])),
- _ => throw new ArgumentException(),
- };
- }
-
- private Command ParseResizeCommand(string[] commandParts)
- {
- return commandParts[1] switch
- {
- "grow" => commandParts[2] switch
- {
- "height" => new ResizeFocusedWindowCommand(ResizeDirection.GROW_HEIGHT),
- "width" => new ResizeFocusedWindowCommand(ResizeDirection.GROW_WIDTH),
- _ => throw new ArgumentException(),
- },
- "shrink" => commandParts[2] switch
- {
- "height" => new ResizeFocusedWindowCommand(ResizeDirection.SHRINK_HEIGHT),
- "width" => new ResizeFocusedWindowCommand(ResizeDirection.SHRINK_WIDTH),
- _ => throw new ArgumentException(),
- },
- _ => throw new ArgumentException(),
- };
- }
-
- private string GetValidWorkspaceName(string workspaceName)
- {
- var workspace = _workspaceService.GetInactiveWorkspaceByName(workspaceName);
-
- if (workspace == null)
- throw new ArgumentException();
-
- return workspaceName;
- }
}
}