Skip to content

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

feat(domain): create `RegisterKeybindingsCommand` and its handler; partially implement parsing command names

Lars Berger175ba047b3f0
diff --git a/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
new file mode 100644
index 00000000..2731129a
--- /dev/null
+++ b/LarsWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
@@ -0,0 +1,63 @@
+using System.Text.RegularExpressions;
+using LarsWM.Domain.Containers;
+using LarsWM.Domain.UserConfigs.Commands;
+using LarsWM.Domain.Workspaces.Commands;
+using LarsWM.Infrastructure.Bussing;
+using LarsWM.Infrastructure.WindowsApi;
+
+namespace LarsWM.Domain.UserConfigs.CommandHandlers
+{
+ class RegisterKeybindingsHandler : ICommandHandler<RegisterKeybindingsCommand>
+ {
+ private Bus _bus;
+ private ContainerService _containerService;
+ private KeybindingService _keybindingService;
+
+ public RegisterKeybindingsHandler(Bus bus, ContainerService containerService, KeybindingService keybindingService)
+ {
+ _bus = bus;
+ _containerService = containerService;
+ _keybindingService = keybindingService;
+ }
+
+ public dynamic Handle(RegisterKeybindingsCommand command)
+ {
+ foreach (var keybinding in command.Keybindings)
+ {
+ var parsedCommand = ParseKeybindingCommand(keybinding.Command);
+
+ switch (parsedCommand)
+ {
+ case FocusWorkspaceKeybindingCommand focusWorkspaceCommand:
+ _keybindingService.AddGlobalKeybinding(
+ keybinding.Bindings[0],
+ () => _bus.Invoke(new FocusWorkspaceCommand(focusWorkspaceCommand.WorkspaceName))
+ );
+ break;
+ }
+ }
+
+ return CommandResponse.Ok;
+ }
+
+ // TODO: Consider changing to return a `Command` and simply invoking directly via bus above.
+ private object ParseKeybindingCommand(string commandName)
+ {
+ var commandString = commandName.Trim().ToLowerInvariant();
+ commandString = Regex.Replace(commandString, @"\s+", " ");
+
+ var commandParts = commandString.Split(" ");
+ switch (commandString)
+ {
+ case var a when Regex.IsMatch(commandString, @"focus workspace"):
+ var match = Regex.Match(a, @"focus workspace (?<workspaceName>.*?)$");
+ // TODO: Check whether a workspace with the name exists (either here or in `FocusWorkspaceHandler`)
+ return new FocusWorkspaceKeybindingCommand(match.Groups["workspaceName"].Value);
+
+ // Throw error with message box to user if no command matches.
+ // default: throw new ArgumentException();
+ default: return 1;
+ }
+ }
+ }
+}
diff --git a/LarsWM.Domain/UserConfigs/Commands/RegisterKeybindingsCommand.cs b/LarsWM.Domain/UserConfigs/Commands/RegisterKeybindingsCommand.cs
new file mode 100644
index 00000000..d608b5b5
--- /dev/null
+++ b/LarsWM.Domain/UserConfigs/Commands/RegisterKeybindingsCommand.cs
@@ -0,0 +1,17 @@
+using System.Collections.Generic;
+using LarsWM.Infrastructure.Bussing;
+
+namespace LarsWM.Domain.UserConfigs.Commands
+{
+ // TODO: Consider renaming to `RegisterUserKeybindingsCommand`.
+ // TODO: Perhaps call command for each keybinding config.
+ public class RegisterKeybindingsCommand : Command
+ {
+ public List<KeybindingConfig> Keybindings { get; }
+
+ public RegisterKeybindingsCommand(List<KeybindingConfig> keybindings)
+ {
+ Keybindings = keybindings;
+ }
+ }
+}
diff --git a/LarsWM.Domain/UserConfigs/KeybindingCommands.cs b/LarsWM.Domain/UserConfigs/KeybindingCommands.cs
new file mode 100644
index 00000000..5d077463
--- /dev/null
+++ b/LarsWM.Domain/UserConfigs/KeybindingCommands.cs
@@ -0,0 +1,12 @@
+namespace LarsWM.Domain.UserConfigs
+{
+ public class FocusWorkspaceKeybindingCommand
+ {
+ public string WorkspaceName { get; }
+
+ public FocusWorkspaceKeybindingCommand(string workspaceName)
+ {
+ WorkspaceName = workspaceName;
+ }
+ }
+}