From 75db0a3c700e7587bff847f995d5b0c0fe8c7efb Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Sat, 24 Oct 2026 10:04:21 +0300 Subject: [PATCH] Make generic-modifier-only binds press-order-independent --- docs/wiki/Configuration:-Key-Bindings.md | 14 ++ niri-config/src/binds.rs | 26 ++- src/input/mod.rs | 39 +++- src/tests/binds.rs | 233 ++++++++++++++++++++++- src/ui/mru.rs | 8 + 5 files changed, 300 insertions(+), 20 deletions(-) diff --git a/docs/wiki/Configuration:-Key-Bindings.md b/docs/wiki/Configuration:-Key-Bindings.md index 4a7f1237..149418a3 100644 --- a/docs/wiki/Configuration:-Key-Bindings.md +++ b/docs/wiki/Configuration:-Key-Bindings.md @@ -50,6 +50,20 @@ binds { } ``` +Since: next release Generic modifiers like `Shift` or `Alt` can be used as trigger keys. +Binds consisting only of generic modifiers do not depend on the order that you press them: + +```kdl +binds { + // Will trigger both when pressing Alt then Shift, and Shift then Alt. + Alt+Shift { spawn "alacritty"; } + + // The trigger is not a generic modifier, so + // you need to press it last. Only Ctrl then Left Shift will work. + Ctrl+Shift_L { spawn "alacritty"; } +} +``` + Since: 25.05 You can customize the `Mod` key [in the `input` section of the config](./Configuration:-Input.md#mod-key-mod-key-nested). > [!TIP] diff --git a/niri-config/src/binds.rs b/niri-config/src/binds.rs index bed03739..7448c8f8 100644 --- a/niri-config/src/binds.rs +++ b/niri-config/src/binds.rs @@ -127,6 +127,10 @@ impl Trigger { } } + pub fn is_generic_modifier(self) -> bool { + matches!(self, Self::CompositorMod | Self::Modifier(_)) + } + /// The modifier flags that this trigger sets, if it is a modifier key. pub fn implied_modifier(self) -> Modifiers { match self { @@ -138,15 +142,6 @@ impl Trigger { } } - /// Whether the same key press can trigger both of these triggers. - fn overlaps(self, other: Self) -> bool { - match (self, other) { - (Self::Modifier(modifier), Self::Keysym(keysym)) - | (Self::Keysym(keysym), Self::Modifier(modifier)) => modifier.matches_keysym(keysym), - (a, b) => a == b, - } - } - pub fn is_keyboard(self) -> bool { matches!( self, @@ -174,7 +169,10 @@ impl Key { return false; } - self.trigger.overlaps(other.trigger) + // Generic-modifier-only binds are press-order-independent, so only their modifier set + // (which already includes the trigger implied modifier) has to match. + (self.trigger.is_generic_modifier() && other.trigger.is_modifier()) + || (self.trigger.is_modifier() && other.trigger.is_generic_modifier()) } } @@ -1555,6 +1553,14 @@ mod tests { let compositor_mod = key("Mod"); assert!(!compositor_mod.conflicts_with(&key("Super_L"))); assert!(!compositor_mod.conflicts_with(&key("Super"))); + + // Generic-modifier-only keys are order-independent. + assert!(key("Ctrl+Alt").conflicts_with(&key("Alt+Ctrl"))); + assert!(key("Mod+Shift+Alt").conflicts_with(&key("Shift+Alt+Mod"))); + assert!(key("Ctrl+Alt").conflicts_with(&key("Alt+Control_L"))); + assert!(key("Ctrl+Alt_L").conflicts_with(&key("Alt+Ctrl"))); + + assert!(!key("Ctrl+Alt").conflicts_with(&key("Ctrl+Alt+A"))); } #[test] diff --git a/src/input/mod.rs b/src/input/mod.rs index e2225b69..5d15d934 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -4892,16 +4892,47 @@ fn find_bind<'a>( bindings .into_iter() .find(|bind| { + let bind_trigger = bind.key.trigger; + // Check that the bind modifiers match the held modifiers. + // + // Pass the bind's trigger itself since we'll check the trigger match below. + // (Note that this means mod_key is not properly checked when it matches the trigger.) + if !bind_matches_trigger(bind, bind_trigger, mod_key, modifiers) { + return false; + } + + // Binds that consist only of generic modifiers can be hit in any order (trigger key + // does not have to be the last). This is consistent with how these types of binds work + // elsewhere. + // + // For example: + // - Alt + Shift to switch keyboard layout on Windows + // - Ctrl + Alt to show inlay hints in VSCode + if bind_trigger.is_generic_modifier() { + // Bind trigger matching compositor mod isn't properly checked above. + // Check it here. + if (bind_trigger == Trigger::CompositorMod + || bind_trigger == Trigger::Modifier(mod_key)) + && !modifiers.contains(mod_key.to_modifiers()) + { + return false; + } + + // Check that the key we pressed is one of the bind modifiers. + let mut raw_mods = Trigger::Keysym(raw).implied_modifier(); + if mod_key.matches_keysym(raw) { + raw_mods |= Modifiers::COMPOSITOR; + } + return !raw_mods.is_empty() && bind.key.modifiers.intersects(raw_mods); + } + // Modifier-only binds can be written in several ways: // - `Mod { quit; }` - Trigger::CompositorMod // - `Super { quit; }` - Trigger::Modifier(_) // - `Super_L { quit; }` - Trigger::Keysym(_) // - // These only matter for keyboard keys and not for mouse or others. So, check them here, - // and then pass the bind's trigger itself to the simpler matching function. - let bind_trigger = bind.key.trigger; + // These only matter for keyboard keys and not for mouse or others. So, check them here. trigger_matches_keysym(bind_trigger, raw, mod_key) - && bind_matches_trigger(bind, bind_trigger, mod_key, modifiers) }) .cloned() } diff --git a/src/tests/binds.rs b/src/tests/binds.rs index 4e5afe6b..9e3150ee 100644 --- a/src/tests/binds.rs +++ b/src/tests/binds.rs @@ -982,10 +982,10 @@ fn generic_modifier_press_release() { surface key pressed: 42 surface modifiers: depressed=1, latched=0, locked=0, group=0 +LWIN 133 XK_Super_L - surface key pressed: 125 + niri test-action surface modifiers: depressed=65, latched=0, locked=0, group=0 -LWIN 133 XK_Super_L - surface key released: 125 + niri test-action surface modifiers: depressed=1, latched=0, locked=0, group=0 -LFSH 50 XK_Shift_L surface key released: 42 @@ -998,14 +998,14 @@ fn generic_modifier_press_release() { surface key pressed: 42 surface modifiers: depressed=1, latched=0, locked=0, group=0 +LWIN 133 XK_Super_L - surface key pressed: 125 + niri test-action surface modifiers: depressed=65, latched=0, locked=0, group=0 +RWIN 134 XK_Super_R - surface key pressed: 126 + niri test-action -RWIN 134 XK_Super_R - surface key released: 126 + niri test-action -LWIN 133 XK_Super_L - surface key released: 125 + niri test-action surface modifiers: depressed=1, latched=0, locked=0, group=0 -LFSH 50 XK_Shift_L surface key released: 42 @@ -1033,6 +1033,60 @@ fn generic_modifier_press_release() { surface key released: 125 surface modifiers: depressed=0, latched=0, locked=0, group=0 "); + + // Regressions. + let c = " + binds { + Shift+Mod { close-window; } + Ctrl+Super { close-window; } + } + "; + + assert_snapshot!(run(c, "+LFSH -LFSH"), @" + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=1, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(c, "+LFSH +LWIN -LWIN -LFSH"), @" + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=1, latched=0, locked=0, group=0 + +LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=65, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface modifiers: depressed=1, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(c, "+LCTL -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(c, "+LCTL +LWIN -LWIN -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + +LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=68, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); } #[test] @@ -1679,6 +1733,173 @@ fn mru_close_forwards_key_release_without_bind() { "); } +#[test] +fn mru_ignores_generic_modifier_binds() { + let c = " + binds { + Alt+Ctrl { screenshot; } + } + "; + + // If a press runs the screenshot action, + // we will get surface enter after it. + assert_snapshot!(run(c, "+LALT +TAB -TAB +LFSH -LFSH -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LFSH 50 XK_Shift_L + -LFSH 50 XK_Shift_L + -LALT 64 XK_Alt_L + surface enter: [56] + surface modifiers: depressed=0, latched=0, locked=0, group=0 + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(c, "+LALT +TAB -TAB +LCTL -LCTL -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LCTL 37 XK_Control_L + -LCTL 37 XK_Control_L + -LALT 64 XK_Alt_L + surface enter: [56] + surface modifiers: depressed=0, latched=0, locked=0, group=0 + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + let c = " + binds { + Ctrl+Shift { screenshot; } + } + "; + + assert_snapshot!(run(c, "+LALT +TAB -TAB +LFSH -LFSH -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LFSH 50 XK_Shift_L + -LFSH 50 XK_Shift_L + -LALT 64 XK_Alt_L + surface enter: [56] + surface modifiers: depressed=0, latched=0, locked=0, group=0 + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(c, "+LALT +TAB -TAB +LCTL +LFSH -LFSH -LCTL -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LCTL 37 XK_Control_L + +LFSH 50 XK_Shift_L + -LFSH 50 XK_Shift_L + -LCTL 37 XK_Control_L + -LALT 64 XK_Alt_L + surface enter: [56] + surface modifiers: depressed=0, latched=0, locked=0, group=0 + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Now with a normal trigger. + let c = " + binds { + Alt+Control_L { screenshot; } + } + "; + + assert_snapshot!(run(c, "+LALT +TAB -TAB +LFSH -LFSH -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LFSH 50 XK_Shift_L + -LFSH 50 XK_Shift_L + -LALT 64 XK_Alt_L + surface enter: [56] + surface modifiers: depressed=0, latched=0, locked=0, group=0 + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(c, "+LALT +TAB -TAB +LCTL -LCTL -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LCTL 37 XK_Control_L + surface enter: [56] + surface modifiers: depressed=12, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface modifiers: depressed=8, latched=0, locked=0, group=0 + -LALT 64 XK_Alt_L + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + let c = " + binds { + Ctrl+Shift_L { screenshot; } + } + "; + + assert_snapshot!(run(c, "+LALT +TAB -TAB +LFSH -LFSH -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LFSH 50 XK_Shift_L + surface enter: [56] + surface modifiers: depressed=9, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface modifiers: depressed=8, latched=0, locked=0, group=0 + -LALT 64 XK_Alt_L + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(c, "+LALT +TAB -TAB +LCTL +LFSH -LFSH -LCTL -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -TAB 23 XK_Tab + +LCTL 37 XK_Control_L + +LFSH 50 XK_Shift_L + surface enter: [29, 56] + surface modifiers: depressed=13, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface modifiers: depressed=12, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + -LALT 64 XK_Alt_L + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + #[test] fn vt_switch_runs_release_actions_of_held_binds() { // A VT switch can take the input away without ever delivering the releases of everything held diff --git a/src/ui/mru.rs b/src/ui/mru.rs index c0afaaaf..7c1194d0 100644 --- a/src/ui/mru.rs +++ b/src/ui/mru.rs @@ -1886,6 +1886,14 @@ fn make_dynamic_opened_binds(config: &Config) -> Vec { continue; }; + // The idea with dynamic binds is that their modifiers are ignored + // (set to whatever the user is currently holding). This idea does not + // make sense with order-independent generic-modifier-only binds. + // Effectively, they would trigger on every key press. + if bind.key.trigger.is_generic_modifier() { + continue; + } + let action = match press_action { Action::FocusColumnRight | Action::FocusColumnRightOrFirst