GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(domain): change parsing of keybinding commands to use pattern matching
Lars Berger513bb1598783
diff --git a/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.csindex 5a9ca1cc..87a6656d 100644--- a/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs+++ b/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs@@ -4,6 +4,7 @@ using LarsWM.Domain.Common.Enums;using LarsWM.Domain.Containers;using LarsWM.Domain.Containers.Commands;using LarsWM.Domain.UserConfigs.Commands;+using LarsWM.Domain.Windows.Commands;using LarsWM.Domain.Workspaces.Commands;using LarsWM.Infrastructure.Bussing;using LarsWM.Infrastructure.WindowsApi;@@ -15,6 +16,8 @@ namespace LarsWM.Domain.UserConfigs.CommandHandlersprivate Bus _bus;private ContainerService _containerService;private KeybindingService _keybindingService;+ private string _directionRegex = "up|down|left|right";+ private string _moveRegex => $"move {_directionRegex}";public RegisterKeybindingsHandler(Bus bus, ContainerService containerService, KeybindingService keybindingService){@@ -27,7 +30,8 @@ namespace LarsWM.Domain.UserConfigs.CommandHandlers{foreach (var keybinding in command.Keybindings){- var parsedCommand = ParseKeybindingCommand(keybinding.Command);+ var commandName = FormatCommandName(keybinding.Command);+ var parsedCommand = ParseKeybindingCommand(commandName);foreach (var binding in keybinding.Bindings)// Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.@@ -37,42 +41,82 @@ namespace LarsWM.Domain.UserConfigs.CommandHandlersreturn CommandResponse.Ok;}+ private string FormatCommandName(string commandName)+ {+ var formattedCommandString = commandName.Trim().ToLowerInvariant();+ return Regex.Replace(formattedCommandString, @"\s+", " ");+ }+private Command ParseKeybindingCommand(string commandName){- var commandString = commandName.Trim().ToLowerInvariant();- commandString = Regex.Replace(commandString, @"\s+", " ");+ var commandParts = commandName.Split(" ");- var commandParts = commandString.Split(" ");- switch (commandString)+ return commandParts[0] switch{- case var _ when Regex.IsMatch(commandString, @"focus workspace"):- var match = Regex.Match(commandString, @"focus workspace (?<workspaceName>.*?)$");- // TODO: Check whether a workspace with the name exists (either here or in `FocusWorkspaceHandler`)- return new FocusWorkspaceCommand(match.Groups["workspaceName"].Value);+ "layout" => ParseLayoutKeybindingCommand(commandParts),+ // TODO: Change this to close command once implemented.+ "close" => new FocusWorkspaceCommand("1"),+ "focus" => ParseFocusKeybindingCommand(commandParts),+ "move" => ParseMoveKeybindingCommand(commandParts),+ "resize" => ParseResizeKeybindingCommand(commandParts),+ _ => throw new ArgumentException($"Invalid command {commandName}"),+ };+ }- case var _ when Regex.IsMatch(commandString, @"layout vertical"):- return new ChangeContainerLayoutCommand(Layout.VERTICAL);+ private Command ParseLayoutKeybindingCommand(string[] commandParts)+ {+ return commandParts[1] switch+ {+ "vertical" => new ChangeContainerLayoutCommand(Layout.VERTICAL),+ "horizontal" => new ChangeContainerLayoutCommand(Layout.HORIZONTAL),+ _ => throw new ArgumentException(),+ };+ }- case var _ when Regex.IsMatch(commandString, @"layout horizontal"):- return new ChangeContainerLayoutCommand(Layout.HORIZONTAL);+ // TODO: Change this to focus command once implemented.+ private Command ParseFocusKeybindingCommand(string[] commandParts)+ {+ return commandParts[1] switch+ {+ "left" => new FocusWorkspaceCommand("1"),+ "right" => new FocusWorkspaceCommand("1"),+ "up" => new FocusWorkspaceCommand("1"),+ "down" => new FocusWorkspaceCommand("1"),+ _ => throw new ArgumentException(),+ };+ }- // Throw error with message box to user if no command matches.- // default: throw new ArgumentException();- default: return new FocusWorkspaceCommand("1");- }+ private Command ParseMoveKeybindingCommand(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),+ _ => throw new ArgumentException(),+ };}- private string ExtractWorkspaceName(string commandName)+ private Command ParseResizeKeybindingCommand(string[] commandParts){- throw new NotImplementedException();+ return commandParts[1] switch+ {+ "left" => new ResizeFocusedWindowCommand(ResizeDirection.SHRINK_WIDTH),+ "right" => new ResizeFocusedWindowCommand(ResizeDirection.GROW_WIDTH),+ "up" => new ResizeFocusedWindowCommand(ResizeDirection.GROW_HEIGHT),+ "down" => new ResizeFocusedWindowCommand(ResizeDirection.SHRINK_HEIGHT),+ _ => throw new ArgumentException(),+ };}- private string ExtractDirection(string commandName)+ private string ExtractWorkspaceName(string commandName){- throw new NotImplementedException();+ var match = Regex.Match(commandName, @"focus workspace (?<workspaceName>.*?)$");+ return match.Groups["workspaceName"].Value;}- private string ExtractLayout(string commandName)+ private Direction ExtractDirection(string commandName){throw new NotImplementedException();}