GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: binding modes (#186)
lars-bergere23e6528b667
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/sample-config.yamlindex bf795dbd..99953c63 100644--- a/GlazeWM.Bootstrapper/sample-config.yaml+++ b/GlazeWM.Bootstrapper/sample-config.yaml@@ -45,6 +45,22 @@ window_rules:- command: "resize borders 0px -7px -7px -7px"match_process_name: "steam"+binding_modes:+ - name: "resize"+ keybindings:+ # Resize focused window by a percentage or pixel amount.+ - command: "resize width -2%"+ bindings: ["H", "Left"]+ - command: "resize width +2%"+ bindings: ["L", "Right"]+ - command: "resize height +2%"+ bindings: ["K", "Up"]+ - command: "resize height -2%"+ bindings: ["J", "Down"]+ # Press enter/escape to return to default keybindings.+ - command: "binding mode none"+ bindings: ["Escape", "Enter"]+keybindings:# Shift focus in a given direction.- command: "focus left"@@ -66,15 +82,10 @@ keybindings:- command: "move down"bindings: ["Alt+Shift+J", "Alt+Shift+Down"]- # Resize focused window by a percentage or pixel amount.- - command: "resize width -2%"- binding: "Alt+U"- - command: "resize width +2%"- binding: "Alt+P"- - command: "resize height +2%"- binding: "Alt+O"- - command: "resize height -2%"- binding: "Alt+I"+ # Activate binding mode defined above with the name "resize". This enables resizing the+ # focused window via HJKL or arrow keys.+ - command: "binding mode resize"+ binding: "Alt+R"# Change layout direction to be either horizontal or vertical.- command: "layout horizontal"diff --git a/GlazeWM.Domain/Common/CommandHandlers/SetBindingModeHandler.cs b/GlazeWM.Domain/Common/CommandHandlers/SetBindingModeHandler.csnew file mode 100644index 00000000..2e53c149--- /dev/null+++ b/GlazeWM.Domain/Common/CommandHandlers/SetBindingModeHandler.cs@@ -0,0 +1,38 @@+using GlazeWM.Domain.Common.Commands;+using GlazeWM.Domain.UserConfigs;+using GlazeWM.Domain.UserConfigs.Commands;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Common.CommandHandlers+{+ public class SetBindingModeHandler : ICommandHandler<SetBindingModeCommand>+ {+ private readonly Bus _bus;+ private readonly UserConfigService _userConfigService;++ public SetBindingModeHandler(Bus bus, UserConfigService userConfigService)+ {+ _bus = bus;+ _userConfigService = userConfigService;+ }++ public CommandResponse Handle(SetBindingModeCommand command)+ {+ var bindingModeName = command.BindingModeName;++ // If binding mode is "none", then reset keybindings to default.+ if (bindingModeName == "none")+ {+ var defaultKeybindings = _userConfigService.Keybindings;+ _bus.Invoke(new RegisterKeybindingsCommand(defaultKeybindings));+ return CommandResponse.Ok;+ }++ // Otherwise, set keybindings to those defined by the binding mode.+ var bindingMode = _userConfigService.GetBindingModeByName(bindingModeName);+ _bus.Invoke(new RegisterKeybindingsCommand(bindingMode.Keybindings));++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Common/Commands/SetBindingModeCommand.cs b/GlazeWM.Domain/Common/Commands/SetBindingModeCommand.csnew file mode 100644index 00000000..da1e36e4--- /dev/null+++ b/GlazeWM.Domain/Common/Commands/SetBindingModeCommand.cs@@ -0,0 +1,14 @@+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Common.Commands+{+ public class SetBindingModeCommand : Command+ {+ public string BindingModeName { get; }++ public SetBindingModeCommand(string bindingModeName)+ {+ BindingModeName = bindingModeName;+ }+ }+}diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex 80829eb4..e36448dc 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -36,6 +36,7 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<PopulateInitialStateCommand>, PopulateInitialStateHandler>();services.AddSingleton<ICommandHandler<ExecProcessCommand>, ExecProcessHandler>();+ services.AddSingleton<ICommandHandler<SetBindingModeCommand>, SetBindingModeHandler>();services.AddSingleton<ICommandHandler<AttachAndResizeContainerCommand>, AttachAndResizeContainerHandler>();services.AddSingleton<ICommandHandler<AttachContainerCommand>, AttachContainerHandler>();services.AddSingleton<ICommandHandler<ChangeContainerLayoutCommand>, ChangeContainerLayoutHandler>();diff --git a/GlazeWM.Domain/UserConfigs/BindingMode.cs b/GlazeWM.Domain/UserConfigs/BindingMode.csnew file mode 100644index 00000000..5f37b8e0--- /dev/null+++ b/GlazeWM.Domain/UserConfigs/BindingMode.cs@@ -0,0 +1,11 @@+using System.Collections.Generic;++namespace GlazeWM.Domain.UserConfigs+{+ public class BindingMode+ {+ public string Name { get; set; }++ public List<KeybindingConfig> Keybindings { get; set; } = new();+ }+}diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.csindex 1664dd4d..521bf513 100644--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs@@ -91,6 +91,7 @@ namespace GlazeWM.Domain.UserConfigs"ignore" => subjectContainer is Window? new IgnoreWindowCommand(subjectContainer as Window): new NoopCommand(),+ "binding" => ParseBindingCommand(commandParts),_ => throw new ArgumentException(null, nameof(commandString)),};}@@ -127,7 +128,7 @@ namespace GlazeWM.Domain.UserConfigs"prev" => new FocusWorkspaceSequenceCommand(Sequence.PREVIOUS),"next" => new FocusWorkspaceSequenceCommand(Sequence.NEXT),// errors already checked at the previous level parsing- _ => new FocusWorkspaceCommand(commandParts[2]),+ _ => new FocusWorkspaceCommand(commandParts[2]),};}@@ -235,6 +236,16 @@ namespace GlazeWM.Domain.UserConfigs};}+ private Command ParseBindingCommand(string[] commandParts)+ {+ return commandParts[1] switch+ {+ "mode" when IsValidBindingMode(commandParts[2]) =>+ new SetBindingModeCommand(commandParts[2]),+ _ => throw new ArgumentException(null, nameof(commandParts)),+ };+ }+private static Command ParseReloadCommand(string[] commandParts){return commandParts[1] switch@@ -245,7 +256,7 @@ namespace GlazeWM.Domain.UserConfigs}/// <summary>- /// Whether a workspace exists with the given name+ /// Whether a workspace exists with the given name/// or workspace name is part of focus workspace command./// </summary>private bool IsValidWorkspace(string workspaceName)@@ -260,6 +271,13 @@ namespace GlazeWM.Domain.UserConfigsreturn workspaceConfig is not null;}+ private bool IsValidBindingMode(string bindingModeName)+ {+ var bindingMode = _userConfigService.GetBindingModeByName(bindingModeName);++ return bindingMode is not null || bindingModeName == "none";+ }+public static string ExtractProcessName(string processNameAndArgs){var hasSingleQuotes = processNameAndArgs.StartsWith("'");diff --git a/GlazeWM.Domain/UserConfigs/UserConfig.cs b/GlazeWM.Domain/UserConfigs/UserConfig.csindex 8ec84a0e..dfb624fc 100644--- a/GlazeWM.Domain/UserConfigs/UserConfig.cs+++ b/GlazeWM.Domain/UserConfigs/UserConfig.cs@@ -8,10 +8,12 @@ namespace GlazeWM.Domain.UserConfigspublic BarConfig Bar { get; set; } = new BarConfig();- public List<WorkspaceConfig> Workspaces = new();+ public List<WorkspaceConfig> Workspaces { get; set; } = new();- public List<WindowRuleConfig> WindowRules = new();+ public List<WindowRuleConfig> WindowRules { get; set; } = new();- public List<KeybindingConfig> Keybindings = new();+ public List<KeybindingConfig> Keybindings { get; set; } = new();++ public List<BindingMode> BindingModes { get; set; } = new();}}diff --git a/GlazeWM.Domain/UserConfigs/UserConfigService.cs b/GlazeWM.Domain/UserConfigs/UserConfigService.csindex 10192816..1359686b 100644--- a/GlazeWM.Domain/UserConfigs/UserConfigService.cs+++ b/GlazeWM.Domain/UserConfigs/UserConfigService.cs@@ -19,6 +19,7 @@ namespace GlazeWM.Domain.UserConfigspublic List<WorkspaceConfig> WorkspaceConfigs => UserConfig.Workspaces;public List<WindowRuleConfig> WindowRules => UserConfig.WindowRules;public List<KeybindingConfig> Keybindings => UserConfig.Keybindings;+ public List<BindingMode> BindingModes => UserConfig.BindingModes;/// <summary>/// Path to the user's config file.@@ -85,14 +86,21 @@ namespace GlazeWM.Domain.UserConfigspublic WorkspaceConfig GetWorkspaceConfigByName(string workspaceName){- return UserConfig.Workspaces.Find(+ return WorkspaceConfigs.Find((workspaceConfig) => workspaceConfig.Name == workspaceName);}+ public BindingMode GetBindingModeByName(string bindingModeName)+ {+ return BindingModes.Find(+ (bindingMode) => bindingMode.Name == bindingModeName+ );+ }+public List<WindowRuleConfig> GetMatchingWindowRules(Window window){- return UserConfig.WindowRules.Where(rule =>+ return WindowRules.Where(rule =>{return rule.ProcessNameRegex?.IsMatch(window.ProcessName) != false &&rule.ClassNameRegex?.IsMatch(window.ClassName) != false &&