diff --git a/modules/services/window-managers/i3-sway/i3.nix b/modules/services/window-managers/i3-sway/i3.nix index eac5ef42e..173d69b5a 100644 --- a/modules/services/window-managers/i3-sway/i3.nix +++ b/modules/services/window-managers/i3-sway/i3.nix @@ -19,7 +19,14 @@ let terminal defaultWorkspace workspaceOutputAssign; keybindings = mkOption { - type = types.attrsOf (types.nullOr types.str); + type = let + withPriority = types.submodule { + options = { + priority = mkOption { type = types.int; }; + value = mkOption { type = types.str; }; + }; + }; + in types.attrsOf (types.nullOr (types.either types.str withPriority)); default = mapAttrs (n: mkOptionDefault) { "${cfg.config.modifier}+Return" = "exec ${cfg.config.terminal}"; "${cfg.config.modifier}+Shift+q" = "kill"; @@ -51,37 +58,87 @@ let "${cfg.config.modifier}+Shift+minus" = "move scratchpad"; "${cfg.config.modifier}+minus" = "scratchpad show"; - "${cfg.config.modifier}+1" = "workspace number 1"; - "${cfg.config.modifier}+2" = "workspace number 2"; - "${cfg.config.modifier}+3" = "workspace number 3"; - "${cfg.config.modifier}+4" = "workspace number 4"; - "${cfg.config.modifier}+5" = "workspace number 5"; - "${cfg.config.modifier}+6" = "workspace number 6"; - "${cfg.config.modifier}+7" = "workspace number 7"; - "${cfg.config.modifier}+8" = "workspace number 8"; - "${cfg.config.modifier}+9" = "workspace number 9"; - "${cfg.config.modifier}+0" = "workspace number 10"; + "${cfg.config.modifier}+1" = { + priority = 101; + value = "workspace number 1"; + }; + "${cfg.config.modifier}+2" = { + priority = 102; + value = "workspace number 2"; + }; + "${cfg.config.modifier}+3" = { + priority = 103; + value = "workspace number 3"; + }; + "${cfg.config.modifier}+4" = { + priority = 104; + value = "workspace number 4"; + }; + "${cfg.config.modifier}+5" = { + priority = 105; + value = "workspace number 5"; + }; + "${cfg.config.modifier}+6" = { + priority = 106; + value = "workspace number 6"; + }; + "${cfg.config.modifier}+7" = { + priority = 107; + value = "workspace number 7"; + }; + "${cfg.config.modifier}+8" = { + priority = 108; + value = "workspace number 8"; + }; + "${cfg.config.modifier}+9" = { + priority = 109; + value = "workspace number 9"; + }; + "${cfg.config.modifier}+0" = { + priority = 110; + value = "workspace number 10"; + }; - "${cfg.config.modifier}+Shift+1" = - "move container to workspace number 1"; - "${cfg.config.modifier}+Shift+2" = - "move container to workspace number 2"; - "${cfg.config.modifier}+Shift+3" = - "move container to workspace number 3"; - "${cfg.config.modifier}+Shift+4" = - "move container to workspace number 4"; - "${cfg.config.modifier}+Shift+5" = - "move container to workspace number 5"; - "${cfg.config.modifier}+Shift+6" = - "move container to workspace number 6"; - "${cfg.config.modifier}+Shift+7" = - "move container to workspace number 7"; - "${cfg.config.modifier}+Shift+8" = - "move container to workspace number 8"; - "${cfg.config.modifier}+Shift+9" = - "move container to workspace number 9"; - "${cfg.config.modifier}+Shift+0" = - "move container to workspace number 10"; + "${cfg.config.modifier}+Shift+1" = { + priority = 201; + value = "move container to workspace number 1"; + }; + "${cfg.config.modifier}+Shift+2" = { + priority = 202; + value = "move container to workspace number 2"; + }; + "${cfg.config.modifier}+Shift+3" = { + priority = 203; + value = "move container to workspace number 3"; + }; + "${cfg.config.modifier}+Shift+4" = { + priority = 204; + value = "move container to workspace number 4"; + }; + "${cfg.config.modifier}+Shift+5" = { + priority = 205; + value = "move container to workspace number 5"; + }; + "${cfg.config.modifier}+Shift+6" = { + priority = 206; + value = "move container to workspace number 6"; + }; + "${cfg.config.modifier}+Shift+7" = { + priority = 207; + value = "move container to workspace number 7"; + }; + "${cfg.config.modifier}+Shift+8" = { + priority = 208; + value = "move container to workspace number 8"; + }; + "${cfg.config.modifier}+Shift+9" = { + priority = 209; + value = "move container to workspace number 9"; + }; + "${cfg.config.modifier}+Shift+0" = { + priority = 210; + value = "move container to workspace number 10"; + }; "${cfg.config.modifier}+Shift+c" = "reload"; "${cfg.config.modifier}+Shift+r" = "restart"; @@ -140,7 +197,7 @@ let inherit (commonFunctions) keybindingsStr keycodebindingsStr modeStr assignStr barStr gapsStr floatingCriteriaStr windowCommandsStr colorSetStr windowBorderString - fontConfigStr keybindingDefaultWorkspace keybindingsRest workspaceOutputStr; + fontConfigStr workspaceOutputStr; startupEntryStr = { command, always, notification, workspace, ... }: concatStringsSep " " [ @@ -174,8 +231,7 @@ let "client.urgent ${colorSetStr colors.urgent}" "client.placeholder ${colorSetStr colors.placeholder}" "client.background ${colors.background}" - (keybindingsStr { keybindings = keybindingDefaultWorkspace; }) - (keybindingsStr { keybindings = keybindingsRest; }) + keybindingsStr (keycodebindingsStr keycodebindings) ] ++ mapAttrsToList (modeStr false) modes ++ mapAttrsToList assignStr assigns ++ map barStr bars diff --git a/modules/services/window-managers/i3-sway/lib/functions.nix b/modules/services/window-managers/i3-sway/lib/functions.nix index 5f661ca89..b2411af3d 100644 --- a/modules/services/window-managers/i3-sway/lib/functions.nix +++ b/modules/services/window-managers/i3-sway/lib/functions.nix @@ -12,20 +12,45 @@ rec { ''${k}="${v}"''; in "[${concatStringsSep " " (mapAttrsToList toCriteria criteria)}]"; - keybindingDefaultWorkspace = filterAttrs (n: v: - cfg.config.defaultWorkspace != null && v == cfg.config.defaultWorkspace) - cfg.config.keybindings; + # Gets the value for something which may be either + # + # {name = ...; value = {priority = ; value = ; }; } + # + # or just a + # + # {name = ...; value = ; } + getPriorityValue = name-value: + if builtins.typeOf name-value.value == "set" && name-value.value ? priority + && name-value.value ? value then + name-value.value.value + else + name-value.value; - keybindingsRest = filterAttrs (n: v: - cfg.config.defaultWorkspace == null || v != cfg.config.defaultWorkspace) - cfg.config.keybindings; + # Gets the priority if present, or 100 for defaultWorkspace and 1000 for + # anything else) + getPriority = name-value: + if cfg.config.defaultWorkspace != null && getPriorityValue name-value + == cfg.config.defaultWorkspace then + 100 + else if builtins.typeOf name-value.value == "set" && name-value.value + ? priority && name-value.value ? value then + name-value.value.priority + else + 1000; - keybindingsStr = { keybindings, bindsymArgs ? "", indent ? "" }: - concatStringsSep "\n" (mapAttrsToList (keycomb: action: + mapSortedAttrs = f: attrs: + builtins.map (name-value: f name-value.name (getPriorityValue name-value)) + (builtins.sort (lhs: rhs: getPriority lhs < getPriority rhs) + (lib.attrsToList attrs)); + + makeKeybindingsStr = { keybindings, bindsymArgs ? "", indent ? "" }: + concatStringsSep "\n" (mapSortedAttrs (keycomb: action: optionalString (action != null) "${indent}bindsym ${ lib.optionalString (bindsymArgs != "") "${bindsymArgs} " }${keycomb} ${action}") keybindings); + keybindingsStr = makeKeybindingsStr { inherit (cfg.config) keybindings; }; + keycodebindingsStr = keycodebindings: concatStringsSep "\n" (mapAttrsToList (keycomb: action: optionalString (action != null) "bindcode ${keycomb} ${action}") @@ -43,7 +68,7 @@ rec { modeStr = bindkeysToCode: name: keybindings: '' mode "${name}" { - ${keybindingsStr { + ${makeKeybindingsStr { inherit keybindings; bindsymArgs = lib.optionalString bindkeysToCode "--to-code"; indent = " "; diff --git a/modules/services/window-managers/i3-sway/lib/options.nix b/modules/services/window-managers/i3-sway/lib/options.nix index 6a3fcf1c1..5fbe4765c 100644 --- a/modules/services/window-managers/i3-sway/lib/options.nix +++ b/modules/services/window-managers/i3-sway/lib/options.nix @@ -899,6 +899,8 @@ in { if isSway then "sway" else "i3" } is launched. This must to correspond to the value of the keybinding of the default workspace. + Alternatively you can specify a priority for any keybinding, default + priority is 1000, keybinding matching defaultWorkspace priority is 100. ''; example = "workspace number 9"; }; diff --git a/tests/modules/services/window-managers/i3/i3-bar-focused-colors-expected.conf b/tests/modules/services/window-managers/i3/i3-bar-focused-colors-expected.conf index 80a3e7533..e5986e625 100644 --- a/tests/modules/services/window-managers/i3/i3-bar-focused-colors-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-bar-focused-colors-expected.conf @@ -15,8 +15,6 @@ client.unfocused #333333 #222222 #888888 #292d2e #222222 client.urgent #2f343a #900000 #ffffff #900000 #900000 client.placeholder #000000 #0c0c0c #ffffff #000000 #0c0c0c client.background #ffffff - -bindsym Mod1+0 workspace number 10 bindsym Mod1+1 workspace number 1 bindsym Mod1+2 workspace number 2 bindsym Mod1+3 workspace number 3 @@ -26,11 +24,7 @@ bindsym Mod1+6 workspace number 6 bindsym Mod1+7 workspace number 7 bindsym Mod1+8 workspace number 8 bindsym Mod1+9 workspace number 9 -bindsym Mod1+Down focus down -bindsym Mod1+Left focus left -bindsym Mod1+Return exec i3-sensible-terminal -bindsym Mod1+Right focus right -bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+0 workspace number 10 bindsym Mod1+Shift+1 move container to workspace number 1 bindsym Mod1+Shift+2 move container to workspace number 2 bindsym Mod1+Shift+3 move container to workspace number 3 @@ -40,6 +34,11 @@ bindsym Mod1+Shift+6 move container to workspace number 6 bindsym Mod1+Shift+7 move container to workspace number 7 bindsym Mod1+Shift+8 move container to workspace number 8 bindsym Mod1+Shift+9 move container to workspace number 9 +bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+Down focus down +bindsym Mod1+Left focus left +bindsym Mod1+Return exec i3-sensible-terminal +bindsym Mod1+Right focus right bindsym Mod1+Shift+Down move down bindsym Mod1+Shift+Left move left bindsym Mod1+Shift+Right move right diff --git a/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf b/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf index 22231abda..507608426 100644 --- a/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-followmouse-expected.conf @@ -15,8 +15,6 @@ client.unfocused #333333 #222222 #888888 #292d2e #222222 client.urgent #2f343a #900000 #ffffff #900000 #900000 client.placeholder #000000 #0c0c0c #ffffff #000000 #0c0c0c client.background #ffffff - -bindsym Mod1+0 workspace number 10 bindsym Mod1+1 workspace number 1 bindsym Mod1+2 workspace number 2 bindsym Mod1+3 workspace number 3 @@ -26,11 +24,7 @@ bindsym Mod1+6 workspace number 6 bindsym Mod1+7 workspace number 7 bindsym Mod1+8 workspace number 8 bindsym Mod1+9 workspace number 9 -bindsym Mod1+Down focus down -bindsym Mod1+Left focus left -bindsym Mod1+Return exec i3-sensible-terminal -bindsym Mod1+Right focus right -bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+0 workspace number 10 bindsym Mod1+Shift+1 move container to workspace number 1 bindsym Mod1+Shift+2 move container to workspace number 2 bindsym Mod1+Shift+3 move container to workspace number 3 @@ -40,6 +34,11 @@ bindsym Mod1+Shift+6 move container to workspace number 6 bindsym Mod1+Shift+7 move container to workspace number 7 bindsym Mod1+Shift+8 move container to workspace number 8 bindsym Mod1+Shift+9 move container to workspace number 9 +bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+Down focus down +bindsym Mod1+Left focus left +bindsym Mod1+Return exec i3-sensible-terminal +bindsym Mod1+Right focus right bindsym Mod1+Shift+Down move down bindsym Mod1+Shift+Left move left bindsym Mod1+Shift+Right move right diff --git a/tests/modules/services/window-managers/i3/i3-fonts-expected.conf b/tests/modules/services/window-managers/i3/i3-fonts-expected.conf index 9ce876874..ad21aca2f 100644 --- a/tests/modules/services/window-managers/i3/i3-fonts-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-fonts-expected.conf @@ -15,8 +15,6 @@ client.unfocused #333333 #222222 #888888 #292d2e #222222 client.urgent #2f343a #900000 #ffffff #900000 #900000 client.placeholder #000000 #0c0c0c #ffffff #000000 #0c0c0c client.background #ffffff - -bindsym Mod1+0 workspace number 10 bindsym Mod1+1 workspace number 1 bindsym Mod1+2 workspace number 2 bindsym Mod1+3 workspace number 3 @@ -26,11 +24,7 @@ bindsym Mod1+6 workspace number 6 bindsym Mod1+7 workspace number 7 bindsym Mod1+8 workspace number 8 bindsym Mod1+9 workspace number 9 -bindsym Mod1+Down focus down -bindsym Mod1+Left focus left -bindsym Mod1+Return exec i3-sensible-terminal -bindsym Mod1+Right focus right -bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+0 workspace number 10 bindsym Mod1+Shift+1 move container to workspace number 1 bindsym Mod1+Shift+2 move container to workspace number 2 bindsym Mod1+Shift+3 move container to workspace number 3 @@ -40,6 +34,11 @@ bindsym Mod1+Shift+6 move container to workspace number 6 bindsym Mod1+Shift+7 move container to workspace number 7 bindsym Mod1+Shift+8 move container to workspace number 8 bindsym Mod1+Shift+9 move container to workspace number 9 +bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+Down focus down +bindsym Mod1+Left focus left +bindsym Mod1+Return exec i3-sensible-terminal +bindsym Mod1+Right focus right bindsym Mod1+Shift+Down move down bindsym Mod1+Shift+Left move left bindsym Mod1+Shift+Right move right diff --git a/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf b/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf index 34460f120..b7fd5022d 100644 --- a/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-keybindings-expected.conf @@ -15,23 +15,16 @@ client.unfocused #333333 #222222 #888888 #292d2e #222222 client.urgent #2f343a #900000 #ffffff #900000 #900000 client.placeholder #000000 #0c0c0c #ffffff #000000 #0c0c0c client.background #ffffff - -bindsym Mod1+0 workspace number 10 +bindsym Mod1+5 workspace number 5 bindsym Mod1+1 workspace number 1 bindsym Mod1+2 workspace number 2 bindsym Mod1+3 workspace number 3 bindsym Mod1+4 workspace number 4 -bindsym Mod1+5 workspace number 5 bindsym Mod1+6 workspace number 6 bindsym Mod1+7 workspace number 7 bindsym Mod1+8 workspace number 8 bindsym Mod1+9 workspace number 9 -bindsym Mod1+Down focus down -bindsym Mod1+Invented invented-key-command -bindsym Mod1+Left overridden-command -bindsym Mod1+Return exec i3-sensible-terminal - -bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+0 workspace number 10 bindsym Mod1+Shift+1 move container to workspace number 1 bindsym Mod1+Shift+2 move container to workspace number 2 bindsym Mod1+Shift+3 move container to workspace number 3 @@ -41,6 +34,12 @@ bindsym Mod1+Shift+6 move container to workspace number 6 bindsym Mod1+Shift+7 move container to workspace number 7 bindsym Mod1+Shift+8 move container to workspace number 8 bindsym Mod1+Shift+9 move container to workspace number 9 +bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+Down focus down +bindsym Mod1+Invented invented-key-command +bindsym Mod1+Left overridden-command +bindsym Mod1+Return exec i3-sensible-terminal + bindsym Mod1+Shift+Down move down bindsym Mod1+Shift+Left move left bindsym Mod1+Shift+Right move right diff --git a/tests/modules/services/window-managers/i3/i3-keybindings.nix b/tests/modules/services/window-managers/i3/i3-keybindings.nix index 2137c53ed..4691dbe9b 100644 --- a/tests/modules/services/window-managers/i3/i3-keybindings.nix +++ b/tests/modules/services/window-managers/i3/i3-keybindings.nix @@ -6,6 +6,7 @@ xsession.windowManager.i3 = { enable = true; + config.defaultWorkspace = "workspace number 5"; config.keybindings = let modifier = config.xsession.windowManager.i3.config.modifier; in lib.mkOptionDefault { diff --git a/tests/modules/services/window-managers/i3/i3-workspace-default-expected.conf b/tests/modules/services/window-managers/i3/i3-workspace-default-expected.conf index cf4f62df3..85c483f24 100644 --- a/tests/modules/services/window-managers/i3/i3-workspace-default-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-workspace-default-expected.conf @@ -16,7 +16,6 @@ client.urgent #2f343a #900000 #ffffff #900000 #900000 client.placeholder #000000 #0c0c0c #ffffff #000000 #0c0c0c client.background #ffffff bindsym Mod1+1 workspace number 1 -bindsym Mod1+0 workspace number 10 bindsym Mod1+2 workspace number 2 bindsym Mod1+3 workspace number 3 bindsym Mod1+4 workspace number 4 @@ -25,11 +24,7 @@ bindsym Mod1+6 workspace number 6 bindsym Mod1+7 workspace number 7 bindsym Mod1+8 workspace number 8 bindsym Mod1+9 workspace number 9 -bindsym Mod1+Down focus down -bindsym Mod1+Left focus left -bindsym Mod1+Return exec i3-sensible-terminal -bindsym Mod1+Right focus right -bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+0 workspace number 10 bindsym Mod1+Shift+1 move container to workspace number 1 bindsym Mod1+Shift+2 move container to workspace number 2 bindsym Mod1+Shift+3 move container to workspace number 3 @@ -39,6 +34,11 @@ bindsym Mod1+Shift+6 move container to workspace number 6 bindsym Mod1+Shift+7 move container to workspace number 7 bindsym Mod1+Shift+8 move container to workspace number 8 bindsym Mod1+Shift+9 move container to workspace number 9 +bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+Down focus down +bindsym Mod1+Left focus left +bindsym Mod1+Return exec i3-sensible-terminal +bindsym Mod1+Right focus right bindsym Mod1+Shift+Down move down bindsym Mod1+Shift+Left move left bindsym Mod1+Shift+Right move right diff --git a/tests/modules/services/window-managers/i3/i3-workspace-output-expected.conf b/tests/modules/services/window-managers/i3/i3-workspace-output-expected.conf index d765ac175..ee3b016f1 100644 --- a/tests/modules/services/window-managers/i3/i3-workspace-output-expected.conf +++ b/tests/modules/services/window-managers/i3/i3-workspace-output-expected.conf @@ -15,8 +15,6 @@ client.unfocused #333333 #222222 #888888 #292d2e #222222 client.urgent #2f343a #900000 #ffffff #900000 #900000 client.placeholder #000000 #0c0c0c #ffffff #000000 #0c0c0c client.background #ffffff - -bindsym Mod1+0 workspace number 10 bindsym Mod1+1 workspace number 1 bindsym Mod1+2 workspace number 2 bindsym Mod1+3 workspace number 3 @@ -26,11 +24,7 @@ bindsym Mod1+6 workspace number 6 bindsym Mod1+7 workspace number 7 bindsym Mod1+8 workspace number 8 bindsym Mod1+9 workspace number 9 -bindsym Mod1+Down focus down -bindsym Mod1+Left focus left -bindsym Mod1+Return exec i3-sensible-terminal -bindsym Mod1+Right focus right -bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+0 workspace number 10 bindsym Mod1+Shift+1 move container to workspace number 1 bindsym Mod1+Shift+2 move container to workspace number 2 bindsym Mod1+Shift+3 move container to workspace number 3 @@ -40,6 +34,11 @@ bindsym Mod1+Shift+6 move container to workspace number 6 bindsym Mod1+Shift+7 move container to workspace number 7 bindsym Mod1+Shift+8 move container to workspace number 8 bindsym Mod1+Shift+9 move container to workspace number 9 +bindsym Mod1+Shift+0 move container to workspace number 10 +bindsym Mod1+Down focus down +bindsym Mod1+Left focus left +bindsym Mod1+Return exec i3-sensible-terminal +bindsym Mod1+Right focus right bindsym Mod1+Shift+Down move down bindsym Mod1+Shift+Left move left bindsym Mod1+Shift+Right move right