From 01dee65d914c8ee7aa53c7b2c8f7a91f59c73a5f Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 19:53:26 +0000 Subject: [PATCH] fwstub-1: keymap rule 3 exception (lead ruling): with Shift held, a binding of the Shift-kept chord runs before the Shift-dropped one (German Ctrl+Shift+0 runs Ctrl+Shift+= Superscript, not Ctrl+= Subscript); recording, candidates and matches keep rule 3; layout docs; test Co-Authored-By: Claude Opus 5.5 --- crates/keymap/src/layout.rs | 8 +++++++- crates/keymap/src/matcher.rs | 20 ++++++++++++++------ crates/keymap/tests/keymap.rs | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/crates/keymap/src/layout.rs b/crates/keymap/src/layout.rs index ba53977763..878208a75e 100644 --- a/crates/keymap/src/layout.rs +++ b/crates/keymap/src/layout.rs @@ -13,7 +13,13 @@ //! without Shift). A digit-row press that types punctuation also matches its position's digit, //! after the punctuation: AZERTY's `-` key runs Ctrl+- and, when that has no binding, Ctrl+6. //! 3. **Punctuation:** the character typed, without Shift when the layout needs Shift for it -//! (German `/` is ⇧7, so ⇧⌘7 is ⌘/; US `:` is ⇧;, so ⇧; is `:`). A Shift binding on a US +//! (German `/` is ⇧7, so ⇧⌘7 is ⌘/; US `:` is ⇧;, so ⇧; is `:`). One exception, for the +//! command a press runs (lead ruling, [`Matcher`](crate::Matcher)): when the person held Shift +//! and the keymap binds the chord with Shift kept, that binding wins over the Shift-dropped +//! chord's; on a layout where `=` is ⇧0, Ctrl+Shift+0 runs Ctrl+Shift+= (Word's Superscript), +//! not Ctrl+= (Subscript), and Ctrl+= only when Ctrl+Shift+= is unbound. Recording +//! ([`Layout::resolve`]), [`Layout::candidates`] and [`Layout::matches`] keep rule 3. A Shift +//! binding on a US //! base key also matches the same press (`Shift+;` matches `:`), and `=` and `+` stand for //! each other so zoom keys work with and without Shift. egui has no key for the characters the //! ISO key next to left Shift types (`<` and `>` on Turkish Q) and reports both as that key, diff --git a/crates/keymap/src/matcher.rs b/crates/keymap/src/matcher.rs index 5f18cd91f0..cf5c95f3ae 100644 --- a/crates/keymap/src/matcher.rs +++ b/crates/keymap/src/matcher.rs @@ -119,15 +119,23 @@ struct Match<'a> { accept: &'a dyn Fn(&CommandRef) -> bool, } -/// The chords one press can run, most specific first: the layout's candidates, then Delete and Backspace standing -/// for each other. -struct Chords(Vec); +/// The chords one press can run, most specific first: the chord with the Shift the layout dropped kept (rule 3's +/// exception), the layout's candidates, then Delete and Backspace standing for each other. +struct Chords(Vec, Chord); impl Chords { /// `None` when the press names no key. fn of(layout: &Layout, input: &KeyInput) -> Option { let mut chords = layout.candidates(input); - chords.first()?; + let meant = chords.first().copied()?; + // Rule 3's exception (`layout` docs): where the layout dropped the Shift the person held for a symbol that + // needs it, the chord with Shift kept is tried first, so its binding wins over the dropped chord's. The press + // still means the dropped chord (a double press, `Hit::exact`). + if input.mods.shift && !meant.mods.shift && meant.key.is_symbol() { + let kept = Chord::new(input.mods, meant.key); + chords.retain(|chord| *chord != kept); + chords.insert(0, kept); + } let twins: Vec = chords .iter() .filter_map(|c| match c.key { @@ -137,12 +145,12 @@ impl Chords { }) .collect(); chords.extend(twins); - Some(Chords(chords)) + Some(Chords(chords, meant)) } /// The chord the press means ([`Layout::resolve`]). fn first(&self) -> Chord { - self.0[0] + self.1 } /// The first chord with a binding, pressed twice when `double`. diff --git a/crates/keymap/tests/keymap.rs b/crates/keymap/tests/keymap.rs index 446f2a97e6..00aea31f0c 100644 --- a/crates/keymap/tests/keymap.rs +++ b/crates/keymap/tests/keymap.rs @@ -217,3 +217,21 @@ fn a_refused_command_leaves_its_chord_to_the_next_binding() { let all = Matcher::new().press(&Layout::new(), &km, &press).map(|hit| hit.command); assert_eq!(all, Some(cmd("view.zoomIn")), "a plain press accepts every command"); } + +/// Rule 3's exception (lead ruling): on a layout where `=` is ⇧0 (German), Ctrl+Shift+0 types `=` with Shift held. +/// Rule 3 reads it as Ctrl+=, but when the keymap binds Ctrl+Shift+= that binding runs (Word's Superscript, not +/// Subscript), as the old app did; with Ctrl+Shift+= unbound the press is Ctrl+= as before. The layout's own reading, +/// which recording and claims use, keeps rule 3. +#[test] +fn a_held_shift_runs_the_shift_kept_binding_before_rule_3_drops_it() { + let shift_zero = KeyInput::new(Mods { cmd: true, ctrl: false, alt: false, shift: true }).with_typed(Key::Equals).with_physical(Key::Num0); + let ran = |defs: Vec| { + let km = Keymap::new(defs, Scopes::new(), Platform::Linux); + let press = Press { input: &shift_zero, repeat: false, now: 0.0, active: &Active::NONE, outer: &[] }; + Matcher::new().press(&Layout::new(), &km, &press).map(|hit| hit.command) + }; + let both = vec![app("format.subscript", &["Cmd+="]), app("format.superscript", &["Cmd+Shift+="])]; + assert_eq!(ran(both), Some(cmd("format.superscript")), "the Shift-kept binding wins"); + assert_eq!(ran(vec![app("format.subscript", &["Cmd+="])]), Some(cmd("format.subscript")), "unbound, rule 3 holds"); + assert_eq!(Layout::new().resolve(&shift_zero), Some(chord("Cmd+=")), "the press is still recorded as rule 3 reads it"); +}