diff --git a/docs/wiki/Configuration:-Key-Bindings.md b/docs/wiki/Configuration:-Key-Bindings.md index f8ee0188..3a51b410 100644 --- a/docs/wiki/Configuration:-Key-Bindings.md +++ b/docs/wiki/Configuration:-Key-Bindings.md @@ -15,7 +15,8 @@ binds { } ``` -The hotkey consists of modifiers separated by `+` signs, followed by an XKB key name in the end. +The hotkey consists of modifiers separated by `+` signs, followed by a trigger key in the end. +The trigger key can be either an XKB key name or Since: next release a modifier. Valid modifiers are: @@ -31,6 +32,24 @@ Valid modifiers are: This way, you can test niri in a window without causing too many conflicts with the host compositor's key bindings. For this reason, most of the default keys use the `Mod` modifier. +Specific left or right modifier keys like `Alt_L` or `Alt_R` can be used as triggers, but not as modifiers: + +```kdl +binds { + // Valid: + Ctrl+Alt_L { spawn "alacritty"; } + + // Invalid: + // Alt_L+Ctrl { spawn "alacritty"; } + + // Also, this is equivalent to just Alt_L + // because Alt_L implies the Alt modifier: + Alt+Alt_L { spawn "the Alt here is unnecessary but allowed"; } + // This is the same: + // Alt_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/docs/wiki/Configuration:-Recent-Windows.md b/docs/wiki/Configuration:-Recent-Windows.md index 162abf2b..d9d29a5a 100644 --- a/docs/wiki/Configuration:-Recent-Windows.md +++ b/docs/wiki/Configuration:-Recent-Windows.md @@ -167,7 +167,23 @@ recent-windows { The recent windows binds have lower precedence than the [normal binds](./Configuration:-Key-Bindings.md), meaning that if you have AltTab bound to something else in the normal binds, the `recent-windows` bind won't work. In this case, you can remove the conflicting normal bind. -All binds in this section must have a modifier key like Alt or Mod because the recent windows switcher remains open only while you hold any modifier key. +All binds in this section must have a modifier key like Alt or Mod (that isn't the same as the trigger key) because the recent windows switcher remains open only while you hold any modifier key. + +```kdl +recent-windows { + binds { + // Allowed: has a modifier, two keys. + Ctrl+Alt { next-window; } + + // Not allowed: only a single key. + // Alt { next-window; } + + // Not allowed: modifier matches the key + // (effectively only a single key). + // Alt+Alt_L { next-window; } + } +} +``` #### Bindings inside the switcher diff --git a/niri-config/src/binds.rs b/niri-config/src/binds.rs index 724a36d4..3525a84f 100644 --- a/niri-config/src/binds.rs +++ b/niri-config/src/binds.rs @@ -10,7 +10,9 @@ use niri_ipc::{ use smithay::input::keyboard::keysyms::KEY_NoSymbol; use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE, KEYSYM_NO_FLAGS}; use smithay::input::keyboard::Keysym; +use tracing::warn; +use crate::input::ModKey; use crate::recent_windows::{MruDirection, MruFilter, MruScope}; use crate::utils::{expect_only_children, MergeWith}; @@ -37,6 +39,8 @@ pub struct Key { #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] pub enum Trigger { Keysym(Keysym), + CompositorMod, + Modifier(ModKey), MouseLeft, MouseRight, MouseMiddle, @@ -55,6 +59,75 @@ pub enum Trigger { TabletStylusButton3, } +/// Returns whether `keysym` is a modifier key like `Shift_L`. +/// +/// This function matches what the niri config considers modifiers. +/// So, it does not correspond exactly to `keysym.is_modifier_key()`. +/// For example, unlike XKB, `Caps_Lock` and `ISO_Next_Group` aren't considered modifiers. +pub fn is_keysym_modifier(keysym: Keysym) -> bool { + ModKey::from_keysym(keysym).is_some() +} + +impl Trigger { + pub fn is_modifier(self) -> bool { + match self { + Self::CompositorMod | Self::Modifier(_) => true, + Self::Keysym(keysym) => is_keysym_modifier(keysym), + _ => false, + } + } + + /// The modifier flags that this trigger sets, if it is a modifier key. + pub fn implied_modifier(self) -> Modifiers { + match self { + Self::CompositorMod => Modifiers::COMPOSITOR, + Self::Modifier(modifier) => modifier.to_modifiers(), + Self::Keysym(keysym) => ModKey::from_keysym(keysym) + .map_or(Modifiers::empty(), |modifier| modifier.to_modifiers()), + _ => Modifiers::empty(), + } + } + + /// 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, + Trigger::Keysym(_) | Trigger::CompositorMod | Trigger::Modifier(_) + ) + } +} + +impl Key { + /// Whether the two keys are triggered by the same key press. + /// + /// Two different keybinds can be triggered by the same key press. For example, a modifier key + /// can be spelled as the modifier itself (`Alt`) or as its keysym (`Alt_L`). + pub fn conflicts_with(&self, other: &Self) -> bool { + // The trigger and the modifiers match exactly. + // + // All non-modifier triggers fall into this category. + if *self == *other { + return true; + } + + // Modifiers include the trigger's implied modifier, so differing modifiers can never be + // triggered by the same key press. + if self.modifiers != other.modifiers { + return false; + } + + self.trigger.overlaps(other.trigger) + } +} + bitflags! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Modifiers : u8 { @@ -786,7 +859,9 @@ where ctx.emit_error(e); } Ok(bind) => { - let conflict = binds.iter().position(|prev| prev.key == bind.key); + let conflict = binds + .iter() + .position(|prev| prev.key.conflicts_with(&bind.key)); if let Some(idx) = conflict { // Even though it's technically incorrect, we use // `DecodeError::Missing` here because it labels the bind with @@ -799,7 +874,11 @@ where ctx.emit_error(DecodeError::unexpected( &child.node_name, "keybind", - "duplicate keybind later defined here", + if binds[idx].key == bind.key { + "duplicate keybind later defined here" + } else { + "conflicting keybind later defined here" + }, )); } else { nodes.push(child); @@ -952,34 +1031,36 @@ impl FromStr for Key { return Err(miette!("invalid key with a null byte: {s:?}")); } - let mut modifiers = Modifiers::empty(); + let mut written_modifiers: Vec<(Modifiers, &str)> = Vec::new(); let mut split = s.split('+'); let key = split.next_back().unwrap(); for part in split { let part = part.trim(); - if part.eq_ignore_ascii_case("mod") { - modifiers |= Modifiers::COMPOSITOR + let modifier = if part.eq_ignore_ascii_case("mod") { + Modifiers::COMPOSITOR } else if part.eq_ignore_ascii_case("ctrl") || part.eq_ignore_ascii_case("control") { - modifiers |= Modifiers::CTRL; + Modifiers::CTRL } else if part.eq_ignore_ascii_case("shift") { - modifiers |= Modifiers::SHIFT; + Modifiers::SHIFT } else if part.eq_ignore_ascii_case("alt") { - modifiers |= Modifiers::ALT; + Modifiers::ALT } else if part.eq_ignore_ascii_case("super") || part.eq_ignore_ascii_case("win") { - modifiers |= Modifiers::SUPER; + Modifiers::SUPER } else if part.eq_ignore_ascii_case("iso_level3_shift") || part.eq_ignore_ascii_case("mod5") { - modifiers |= Modifiers::ISO_LEVEL3_SHIFT; + Modifiers::ISO_LEVEL3_SHIFT } else if part.eq_ignore_ascii_case("iso_level5_shift") || part.eq_ignore_ascii_case("mod3") { - modifiers |= Modifiers::ISO_LEVEL5_SHIFT; + Modifiers::ISO_LEVEL5_SHIFT } else { return Err(miette!("invalid modifier: {part}")); - } + }; + + written_modifiers.push((modifier, part)); } let trigger = if key.eq_ignore_ascii_case("MouseLeft") { @@ -1014,6 +1095,20 @@ impl FromStr for Key { Trigger::TabletStylusButton2 } else if key.eq_ignore_ascii_case("TabletStylusButton3") { Trigger::TabletStylusButton3 + } else if key.eq_ignore_ascii_case("Mod") { + Trigger::CompositorMod + } else if key.eq_ignore_ascii_case("Ctrl") || key.eq_ignore_ascii_case("Control") { + Trigger::Modifier(ModKey::Ctrl) + } else if key.eq_ignore_ascii_case("Shift") { + Trigger::Modifier(ModKey::Shift) + } else if key.eq_ignore_ascii_case("Alt") { + Trigger::Modifier(ModKey::Alt) + } else if key.eq_ignore_ascii_case("Super") || key.eq_ignore_ascii_case("Win") { + Trigger::Modifier(ModKey::Super) + } else if key.eq_ignore_ascii_case("ISO_Level3_Shift") || key.eq_ignore_ascii_case("Mod5") { + Trigger::Modifier(ModKey::IsoLevel3Shift) + } else if key.eq_ignore_ascii_case("ISO_Level5_Shift") || key.eq_ignore_ascii_case("Mod3") { + Trigger::Modifier(ModKey::IsoLevel5Shift) } else { let mut keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); // The keyboard event handling code can receive either @@ -1049,6 +1144,15 @@ impl FromStr for Key { Trigger::Keysym(keysym) }; + let mut modifiers = trigger.implied_modifier(); + for (modifier, spelling) in written_modifiers { + if modifiers.contains(modifier) { + warn!("redundant modifier `{spelling}` in keybind `{s}`"); + } + + modifiers |= modifier; + } + Ok(Key { trigger, modifiers }) } } @@ -1115,6 +1219,165 @@ mod tests { ); } + #[test] + fn parse_bare_modifier_spellings() { + // Each modifier can be spelled with its canonical name or an alias. A bare modifier key + // parses to `Trigger::Modifier`. + for (text, modifier) in [ + ("Ctrl", ModKey::Ctrl), + ("Control", ModKey::Ctrl), + ("Shift", ModKey::Shift), + ("Alt", ModKey::Alt), + ("Super", ModKey::Super), + ("Win", ModKey::Super), + ("Mod5", ModKey::IsoLevel3Shift), + ("ISO_Level3_Shift", ModKey::IsoLevel3Shift), + ("Mod3", ModKey::IsoLevel5Shift), + ("ISO_Level5_Shift", ModKey::IsoLevel5Shift), + ] { + assert_eq!( + text.parse::().unwrap(), + Key { + trigger: Trigger::Modifier(modifier), + modifiers: modifier.to_modifiers(), + }, + "parsing `{text}`", + ); + } + } + + #[test] + fn parse_mod_as_trigger() { + // The mod key keeps its own trigger, which is resolved to a real modifier at runtime. + assert_eq!( + "Mod".parse::().unwrap(), + Key { + trigger: Trigger::CompositorMod, + modifiers: Modifiers::COMPOSITOR, + }, + ); + + // It can also be used together with other held modifiers. + assert_eq!( + "Ctrl+Mod".parse::().unwrap(), + Key { + trigger: Trigger::CompositorMod, + modifiers: Modifiers::CTRL | Modifiers::COMPOSITOR, + }, + ); + + // `Mod` also works as a held modifier of another trigger. + assert_eq!( + "Mod+Control_L".parse::().unwrap(), + Key { + trigger: Trigger::Keysym(Keysym::Control_L), + modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, + }, + ); + } + + #[test] + fn parse_multi_modifier_triggers() { + // A modifier key as the trigger with another held modifier, either spelled as the modifier + // itself or as its keysym. + assert_eq!( + "Ctrl+Alt".parse::().unwrap(), + Key { + trigger: Trigger::Modifier(ModKey::Alt), + modifiers: Modifiers::CTRL | Modifiers::ALT, + }, + ); + assert_eq!( + "Ctrl+Alt_L".parse::().unwrap(), + Key { + trigger: Trigger::Keysym(Keysym::Alt_L), + modifiers: Modifiers::CTRL | Modifiers::ALT, + }, + ); + } + + #[test] + fn parse_normalizes_redundant_modifiers() { + let key = |text: &str| text.parse::().unwrap(); + + // Modifier triggers can be written with or without the redundant modifier. + assert_eq!(key("Alt+Alt_L"), key("Alt_L")); + assert_eq!(key("Alt+Alt_R"), key("Alt_R")); + assert_eq!(key("Alt+Alt"), key("Alt")); + assert_eq!(key("Alt+Alt+Alt_L"), key("Alt_L")); + assert_eq!(key("Ctrl+Shift+Control_L"), key("Shift+Control_L")); + assert_eq!(key("Mod+Mod"), key("Mod")); + + // Duplicate modifiers are fine with non-modifier triggers. + assert_eq!(key("Alt+Alt+Q"), key("Alt+Q")); + assert_eq!(key("Mod+Mod+Q"), key("Mod+Q")); + + // Still works if spelled differently. + assert_eq!(key("Ctrl+Control+Q"), key("Ctrl+Q")); + assert_eq!(key("Super+Win+Q"), key("Super+Q")); + } + + #[test] + fn parse_modifier_trigger_includes_implied_modifier() { + // The modifier that the trigger implies is part of the key. + assert_eq!( + "Alt_L".parse::().unwrap(), + Key { + trigger: Trigger::Keysym(Keysym::Alt_L), + modifiers: Modifiers::ALT, + }, + ); + assert_eq!( + "Alt+Alt".parse::().unwrap(), + Key { + trigger: Trigger::Modifier(ModKey::Alt), + modifiers: Modifiers::ALT, + }, + ); + // `Mod` is only resolved at runtime, so no trigger implies it. + assert_eq!( + "Alt+Mod".parse::().unwrap(), + Key { + trigger: Trigger::CompositorMod, + modifiers: Modifiers::ALT | Modifiers::COMPOSITOR, + }, + ); + } + + #[test] + fn key_conflicts() { + let key = |text: &str| text.parse::().unwrap(); + + // Alt_L and Alt_R conflict with Alt, and vice versa. + let alt = key("Alt"); + let alt_l = key("Alt_L"); + let alt_r = key("Alt_R"); + assert!(alt.conflicts_with(&alt_l)); + assert!(alt.conflicts_with(&alt_r)); + assert!(alt_l.conflicts_with(&alt)); + assert!(alt_r.conflicts_with(&alt)); + + // The left and right keysyms are different keys. + assert!(!alt_l.conflicts_with(&alt_r)); + + // Different modifiers mean different binds, even for the same trigger. + assert!(!alt.conflicts_with(&key("Ctrl+Alt_L"))); + + // Non-overlapping triggers don't conflict, even with the same modifiers. + assert!(!key("Mod+Q").conflicts_with(&key("Mod+L"))); + assert!(!key("Q").conflicts_with(&key("Ctrl"))); + + // Identical keys are duplicates. + assert!(key("Q").conflicts_with(&key("Q"))); + assert!(alt.conflicts_with(&alt)); + + // The compositor mod key is only resolved at runtime, so `Mod` isn't compared against the + // mod key's own spellings. + let compositor_mod = key("Mod"); + assert!(!compositor_mod.conflicts_with(&key("Super_L"))); + assert!(!compositor_mod.conflicts_with(&key("Super"))); + } + #[test] fn null_byte() { // Shouldn't panic. diff --git a/niri-config/src/input.rs b/niri-config/src/input.rs index 78e3ed21..0ac0821f 100644 --- a/niri-config/src/input.rs +++ b/niri-config/src/input.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use miette::miette; -use smithay::input::keyboard::XkbConfig; +use smithay::input::keyboard::{keysyms, Keysym, XkbConfig}; use smithay::reexports::input; use crate::binds::Modifiers; @@ -415,7 +415,7 @@ impl FromStr for WarpMouseToFocusMode { } } -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] pub enum ModKey { Ctrl, Shift, @@ -436,6 +436,23 @@ impl ModKey { ModKey::IsoLevel5Shift => Modifiers::ISO_LEVEL5_SHIFT, } } + + /// Returns the modifier key for a keysym, if the keysym is a modifier key. + pub fn from_keysym(keysym: Keysym) -> Option { + match keysym.raw() { + keysyms::KEY_Control_L | keysyms::KEY_Control_R => Some(Self::Ctrl), + keysyms::KEY_Shift_L | keysyms::KEY_Shift_R => Some(Self::Shift), + keysyms::KEY_Alt_L | keysyms::KEY_Alt_R => Some(Self::Alt), + keysyms::KEY_Super_L | keysyms::KEY_Super_R => Some(Self::Super), + keysyms::KEY_ISO_Level3_Shift => Some(Self::IsoLevel3Shift), + keysyms::KEY_ISO_Level5_Shift => Some(Self::IsoLevel5Shift), + _ => None, + } + } + + pub fn matches_keysym(self, keysym: Keysym) -> bool { + Self::from_keysym(keysym) == Some(self) + } } impl FromStr for ModKey { @@ -526,6 +543,55 @@ mod tests { Input::from_part(&part) } + #[test] + fn mod_key_names_are_consistent() { + // The X11 `ModN` modifier indices are a separate numbering from the ISO level numbers, + // and the two happen to be crossed over: ISO Level 3 Shift is Mod5, and ISO Level 5 Shift + // is Mod3. This test guards against "fixing" this apparent mismatch. + for (name, modifier, keysym, modifiers) in [ + ( + "Mod5", + ModKey::IsoLevel3Shift, + keysyms::KEY_ISO_Level3_Shift, + Modifiers::ISO_LEVEL3_SHIFT, + ), + ( + "Mod3", + ModKey::IsoLevel5Shift, + keysyms::KEY_ISO_Level5_Shift, + Modifiers::ISO_LEVEL5_SHIFT, + ), + ] { + // Name -> variant. + assert_eq!( + name.parse::().unwrap(), + modifier, + "parsing `{name}`" + ); + + // Keysym -> variant. + assert_eq!( + ModKey::from_keysym(Keysym::from(keysym)), + Some(modifier), + "keysym for `{name}`", + ); + assert!(modifier.matches_keysym(Keysym::from(keysym))); + + // Variant -> held modifiers. + assert_eq!(modifier.to_modifiers(), modifiers, "modifiers for `{name}`"); + } + + // ISO_Level3_Shift and Mod5 are the same modifier, and so are ISO_Level5_Shift and Mod3. + assert_eq!( + "ISO_Level3_Shift".parse::().unwrap(), + "Mod5".parse::().unwrap() + ); + assert_eq!( + "ISO_Level5_Shift".parse::().unwrap(), + "Mod3".parse::().unwrap() + ); + } + #[test] fn parse_scroll_factor_combined() { // Test combined scroll-factor syntax diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index b8989e8d..6066ebc7 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -229,7 +229,8 @@ where let mut config = config.borrow_mut(); let binds = &mut config.binds.0; // Remove existing binds matching any new bind. - binds.retain(|bind| !part.0.iter().any(|new| new.key == bind.key)); + binds + .retain(|bind| !part.0.iter().any(|new| new.key.conflicts_with(&bind.key))); // Add all new binds. binds.extend(part.0); } diff --git a/niri-config/src/recent_windows.rs b/niri-config/src/recent_windows.rs index 62480a40..ba0710cc 100644 --- a/niri-config/src/recent_windows.rs +++ b/niri-config/src/recent_windows.rs @@ -58,7 +58,7 @@ impl MergeWith for RecentWindows { if let Some(part) = &part.binds { // Remove existing binds matching any new bind. self.binds - .retain(|bind| !part.0.iter().any(|new| new.key == bind.key)); + .retain(|bind| !part.0.iter().any(|new| new.key.conflicts_with(&bind.key))); // Add all new binds. self.binds.extend(part.0.iter().cloned().map(Bind::from)); } @@ -269,7 +269,9 @@ where ctx.emit_error(e); } Ok(bind) => { - let conflict = binds.iter().position(|prev| prev.key == bind.key); + let conflict = binds + .iter() + .position(|prev| prev.key.conflicts_with(&bind.key)); if let Some(idx) = conflict { // Even though it's technically incorrect, we use // `DecodeError::Missing` here because it labels the bind with @@ -282,7 +284,11 @@ where ctx.emit_error(DecodeError::unexpected( &child.node_name, "keybind", - "duplicate keybind later defined here", + if binds[idx].key == bind.key { + "duplicate keybind later defined here" + } else { + "conflicting keybind later defined here" + }, )); } else { nodes.push(child); @@ -325,8 +331,11 @@ where .parse::() .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?; + let mut modifiers = key.modifiers; + modifiers &= !key.trigger.implied_modifier(); + // A modifier is required because MRU remains on screen as long as any modifier is held. - if key.modifiers.is_empty() { + if modifiers.is_empty() { ctx.emit_error(DecodeError::unexpected( &node.node_name, "keybind", @@ -336,7 +345,7 @@ where // FIXME: To support this, all the mods_with_mouse_binds()/mods_with_*wheel_binds()/etc. // will need to learn about recent-windows bindings. - if !matches!(key.trigger, Trigger::Keysym(_)) { + if !key.trigger.is_keyboard() { ctx.emit_error(DecodeError::unexpected( &node.node_name, "key", diff --git a/src/ui/hotkey_overlay.rs b/src/ui/hotkey_overlay.rs index 34bc3955..f95518d0 100644 --- a/src/ui/hotkey_overlay.rs +++ b/src/ui/hotkey_overlay.rs @@ -496,50 +496,37 @@ fn action_name(action: &Action) -> String { fn key_name(screen_reader: bool, mod_key: ModKey, key: &Key) -> String { let mut name = String::new(); - let has_comp_mod = key.modifiers.contains(Modifiers::COMPOSITOR); + // Do not show modifiers implied by the trigger. + let modifiers = key.modifiers & !key.trigger.implied_modifier(); + + let has_comp_mod = modifiers.contains(Modifiers::COMPOSITOR); + + let mod_key_pretty = modifier_name(mod_key); // Compositor mod goes first. if has_comp_mod { - match mod_key { - ModKey::Super => { - name.push_str("Super + "); - } - ModKey::Alt => { - name.push_str("Alt + "); - } - ModKey::Shift => { - name.push_str("Shift + "); - } - ModKey::Ctrl => { - name.push_str("Ctrl + "); - } - ModKey::IsoLevel3Shift => { - name.push_str("Mod5 + "); - } - ModKey::IsoLevel5Shift => { - name.push_str("Mod3 + "); - } - } + name.push_str(mod_key_pretty); + name.push_str(" + "); } - if key.modifiers.contains(Modifiers::SUPER) && !(has_comp_mod && mod_key == ModKey::Super) { + if modifiers.contains(Modifiers::SUPER) && !(has_comp_mod && mod_key == ModKey::Super) { name.push_str("Super + "); } - if key.modifiers.contains(Modifiers::CTRL) && !(has_comp_mod && mod_key == ModKey::Ctrl) { + if modifiers.contains(Modifiers::CTRL) && !(has_comp_mod && mod_key == ModKey::Ctrl) { name.push_str("Ctrl + "); } - if key.modifiers.contains(Modifiers::SHIFT) && !(has_comp_mod && mod_key == ModKey::Shift) { + if modifiers.contains(Modifiers::SHIFT) && !(has_comp_mod && mod_key == ModKey::Shift) { name.push_str("Shift + "); } - if key.modifiers.contains(Modifiers::ALT) && !(has_comp_mod && mod_key == ModKey::Alt) { + if modifiers.contains(Modifiers::ALT) && !(has_comp_mod && mod_key == ModKey::Alt) { name.push_str("Alt + "); } - if key.modifiers.contains(Modifiers::ISO_LEVEL3_SHIFT) + if modifiers.contains(Modifiers::ISO_LEVEL3_SHIFT) && !(has_comp_mod && mod_key == ModKey::IsoLevel3Shift) { name.push_str("Mod5 + "); } - if key.modifiers.contains(Modifiers::ISO_LEVEL5_SHIFT) + if modifiers.contains(Modifiers::ISO_LEVEL5_SHIFT) && !(has_comp_mod && mod_key == ModKey::IsoLevel5Shift) { name.push_str("Mod3 + "); @@ -547,6 +534,8 @@ fn key_name(screen_reader: bool, mod_key: ModKey, key: &Key) -> String { let pretty = match key.trigger { Trigger::Keysym(keysym) => prettify_keysym_name(screen_reader, &keysym_get_name(keysym)), + Trigger::CompositorMod => mod_key_pretty.into(), + Trigger::Modifier(modifier) => modifier_name(modifier).into(), Trigger::MouseLeft => String::from("Mouse Left"), Trigger::MouseRight => String::from("Mouse Right"), Trigger::MouseMiddle => String::from("Mouse Middle"), @@ -569,6 +558,17 @@ fn key_name(screen_reader: bool, mod_key: ModKey, key: &Key) -> String { name } +fn modifier_name(modifier: ModKey) -> &'static str { + match modifier { + ModKey::Super => "Super", + ModKey::Alt => "Alt", + ModKey::Shift => "Shift", + ModKey::Ctrl => "Ctrl", + ModKey::IsoLevel3Shift => "Mod5", + ModKey::IsoLevel5Shift => "Mod3", + } +} + fn prettify_keysym_name(screen_reader: bool, name: &str) -> String { let name = if screen_reader { name @@ -632,6 +632,87 @@ mod tests { // Not bound. assert_snapshot!(check("", Action::Screenshot(true, None)), @" (not bound) : Take a Screenshot"); + // Bare modifier binds. + assert_snapshot!( + check( + r#"binds { + Alt { close-window; } + }"#, + Action::CloseWindow, + ), + @" Alt : Close Focused Window" + ); + assert_snapshot!( + check( + r#"binds { + Ctrl { close-window; } + Shift { close-window; } + }"#, + Action::CloseWindow, + ), + @" Ctrl : Close Focused Window" + ); + assert_snapshot!( + check( + r#"binds { + Mod5 { close-window; } + Mod3 { close-window; } + }"#, + Action::CloseWindow, + ), + @" Mod5 : Close Focused Window" + ); + assert_snapshot!( + check( + r#"binds { + Super { close-window; } + }"#, + Action::CloseWindow, + ), + @" Super : Close Focused Window" + ); + + // Modifier + modifier binds: a held modifier key with another modifier key as the trigger. + assert_snapshot!( + check( + r#"binds { + Ctrl+Alt_L { close-window; } + }"#, + Action::CloseWindow, + ), + @" Ctrl + Alt_L : Close Focused Window" + ); + assert_snapshot!( + check( + r#"binds { + Alt+Ctrl { close-window; } + }"#, + Action::CloseWindow, + ), + @" Alt + Ctrl : Close Focused Window" + ); + + // A Ctrl keysym names only one side of the keyboard, so it keeps its exact name instead of + // being shortened to `Ctrl`. + assert_snapshot!( + check( + r#"binds { + Control_L { close-window; } + }"#, + Action::CloseWindow, + ), + @" Control_L : Close Focused Window" + ); + assert_snapshot!( + check( + r#"binds { + Mod+Control_R { close-window; } + }"#, + Action::CloseWindow, + ), + @" Super + Control_R : Close Focused Window" + ); + // Bound with a default title. assert_snapshot!( check(