From 6f8bf4265a51cc852fc4648f047f1562d00a02ba Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 19:48:07 +0000 Subject: [PATCH] fwstub-1: a bound Alt chord's text is withheld only when a command bound to it is enabled (S-L177; Keymap::holders, commands::runs_now): a disabled one leaves the character typed, as Word's Option+Shift+- em dash; trial Co-Authored-By: Claude Opus 5.5 --- crates/keymap/src/keymap.rs | 13 ++++++ .../ui-contract/src/runtime/commands/board.rs | 7 +++ .../ui-contract/src/runtime/commands/mod.rs | 1 + crates/ui-contract/src/runtime/keys/input.rs | 14 +++--- .../src/testing/host_key_trials.rs | 45 +++++++++++++++++++ 5 files changed, 75 insertions(+), 5 deletions(-) diff --git a/crates/keymap/src/keymap.rs b/crates/keymap/src/keymap.rs index 8acf7060dd..0de471f1c5 100644 --- a/crates/keymap/src/keymap.rs +++ b/crates/keymap/src/keymap.rs @@ -454,6 +454,19 @@ impl Keymap { self.by_trigger.get(&want)?.iter().find(|&&(i, k)| self.at(i, k).is_some()).map(|&(i, _)| self.catalog[i].command.clone()) } + /// Every command a single press of `chord` is bound to, in any scope, in catalog order, each once: a caller that + /// asks whether the press can run anything now reads each one's state. + pub fn holders(&self, chord: &Chord) -> Vec { + let want = Trigger { chord: chord.effective(self.platform), double: false }; + let mut found: Vec = Vec::new(); + for &(i, k) in self.by_trigger.get(&want).into_iter().flatten() { + if self.at(i, k).is_some() && !found.contains(&i) { + found.push(i); + } + } + found.into_iter().map(|i| self.catalog[i].command.clone()).collect() + } + /// Adds a binding to a command. pub fn assign(&mut self, command: &CommandRef, binding: Binding, policy: Policy) -> Result { let i = self.find(command).ok_or(AssignError::UnknownCommand)?; diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index fcd166e14d..efcf3c4bdd 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -693,6 +693,13 @@ pub(crate) fn holder(ctx: &Context, chord: &Chord) -> Option { with(ctx, |board| board.keymap.as_ref().and_then(|cache| cache.keymap.holder(chord))) } +/// Whether a command the window's keymap binds `chord` to is enabled, as the last pass resolved it for a surface with +/// no component of its own (the state a menu row shows): a press of the chord can run something. Notes no reader. +pub(crate) fn runs_now(ctx: &Context, chord: &Chord) -> bool { + let holders = with(ctx, |board| board.keymap.as_ref().map(|cache| cache.keymap.holders(chord)).unwrap_or_default()); + holders.iter().any(|command| resolve_from(ctx, command, None).0.enabled) +} + /// The chords bound to the window's keys, which the input hook takes before any component. pub(crate) fn window_shortcuts(ctx: &Context) -> Arc<[(WindowShortcut, Chord)]> { with(ctx, |board| board.keymap.as_ref().map(|cache| Arc::clone(&cache.window_shortcuts)).unwrap_or_default()) diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index a983faccc6..1e5ca621b3 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -26,6 +26,7 @@ pub use board::keymap_builds; #[doc(hidden)] pub use board::note_page_root; pub(crate) use board::publish as publish_for; +pub(crate) use board::runs_now; pub(crate) use board::take as take_for_instance; pub(crate) use board::{ begin, binds_scoped, edit_chords, end, install, note_focus, platform, register_instance, set_input, take_delivered, take_fired, window_shortcuts, diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index 9000f33d72..a67c03ec92 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -53,15 +53,17 @@ struct Strip<'a> { /// An Alt release in this input ended an Alt+numpad code (`raw_input::take_alt_code_ended`): the code's digits /// never reach this stage, so without this the release would read as a lone Alt. alt_code_ended: bool, - /// Whether the window's keymap binds a chord (`commands::holder`), enabled or not. + /// Whether the window's keymap binds a chord to a command that is enabled now (`commands::runs_now`). bound: &'a dyn Fn(&Chord) -> bool, } impl Strip<'_> { /// Whether the text the platform sends with the press `input` stands for is a shortcut's own and belongs to no - /// field: Alt is held and the keymap binds the press, so it fires a command or is refused as one. A platform sends - /// its key's text with an Alt shortcut (Alt+Q on Linux). A plain key's text is typing, which a focused field or - /// list takes before any shortcut (`route::holds_press`), and Cmd and Ctrl chords type nothing. + /// field: Alt is held and the keymap binds the press to a command that is enabled, so it fires (S-L177). A platform + /// sends its key's text with an Alt shortcut (Alt+Q on Linux). A bound Alt chord whose command is disabled does + /// nothing, so its text is typed, as Word types Option+Shift+-'s em dash where Collapse does not apply. A plain key's + /// text is typing, which a focused field or list takes before any shortcut (`route::holds_press`), and Cmd and Ctrl + /// chords type nothing. fn shortcut_text(&self, input: &KeyInput) -> bool { input.mods.alt && self.layout.candidates(input).iter().any(|chord| (self.bound)(chord)) } @@ -470,7 +472,9 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec (Vec, String) { + let field = named_id("document"); + let typed = RefCell::new(String::new()); + let mut show = |ui: &mut Ui| { + widget(ui, field); + register_stop(ui.ctx(), Stop::new(field, KeyUse::of(Input::Text))); + let mut intents = crate::testing::focus::intents_of(ui.ctx(), Some(field), false); + while let Some(text) = intents.take_text() { + typed.borrow_mut().push_str(&text); + } + }; + let Ok(binding) = Binding::parse("Alt+Shift+-") else { return (vec!["the trial's chord does not parse".to_owned()], String::new()) }; + let Ok(catalog) = AppCatalog::new(vec![CommandDef::new(COLLAPSE, "Collapse", Scope::app()).with_default(binding)], 1) else { + return (vec!["the trial's catalog is refused".to_owned()], String::new()); + }; + let state = crate::runtime::commands::CommandState::new().enabled(enabled); + let mut frames = Frames::new(); + frames.commands(CommandInput::new().catalog(catalog.typing_rule(TypingRule::PlainKeysWait)).state(COLLAPSE, state)); + frames.frame(Vec::new(), &mut show); + focus_stop(&mut frames, &mut show, field); + take_output(&frames.ctx); + typed.borrow_mut().clear(); + let alt_shift = Modifiers { alt: true, shift: true, ..Modifiers::NONE }; + frames.frame(vec![key_press(Key::Minus, alt_shift), Event::Text("\u{2014}".into())], &mut show); + frames.frame(Vec::new(), &mut show); + let fired = take_output(&frames.ctx).fired.into_iter().map(|fired| fired.command.id).collect(); + (fired, typed.take()) +} + +/// fwstub-1 (4): the text of a bound Alt chord is withheld only when its command runs (S-L177, as production's +/// `shortcuts.rs` returned unhandled for a disabled Alt command): with the command disabled the shortcut does nothing +/// and the character is typed, as Word types Option+Shift+-'s em dash outside Outline view; enabled, the command runs +/// and nothing is typed. +#[trial] +fn a_disabled_alt_chord_leaves_its_text_typed() { + assert_eq!(alt_shift_minus(false), (Vec::new(), "\u{2014}".to_owned()), "disabled: the em dash is typed"); + assert_eq!(alt_shift_minus(true), (vec![COLLAPSE.to_owned()], String::new()), "enabled: the command runs and types nothing"); +}