diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index bc7535dd9..efcae6e38 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -35,6 +35,8 @@ struct Strip<'a> { /// The registered bar takes only the Alt+letter chords it claims (`FocusedRecord::menu_bar_by_claim`): every other /// Alt+letter is an ordinary press. menu_bar_by_claim: bool, + /// The registered bar holds the focus (F10 or a lone Alt gave it a title) with no menu open. + bar_focused: bool, /// The focused stop holds drafts (`KeyUse::holds_drafts`): a lone Alt alerts instead of focusing the menu bar. holds_drafts: bool, /// A dialog keeps a canvas open and Space belongs to it: the press is the canvas's momentary command chord. @@ -87,6 +89,7 @@ impl Strip<'_> { alt.other_key = true; let pushed = batch.presses.len(); let claims = batch.claimed.len(); + let menu_keys = batch.menu.len(); let shortcut_text = self.shortcut_text(&input); let kept = self.press(batch, key, &input, repeat, at); // The canvas's Space keeps its text from every field, as a claim does. @@ -94,7 +97,8 @@ impl Strip<'_> { batch.last_press = Some(LastPress { input, pressed: (batch.presses.len() > pushed).then_some(pushed), - claimed: batch.claimed.len() > claims || canvas || shortcut_text, + // A mnemonic's text (a plain letter on the focused bar) is no field's typing. + claimed: batch.claimed.len() > claims || canvas || shortcut_text || batch.menu.len() > menu_keys, }); // The release key is kept for withheld presses too: the held-over rule matches repeats by it. if let Some(press) = batch.presses.get_mut(pushed) { @@ -247,9 +251,15 @@ impl Strip<'_> { // the focused stop records, Escape with any modifier held is a chord for the recorder; only plain Escape is a // scope escape. Over an open press any Escape is the press's cancel: a borrowed tool's drag holds Cmd. let escape = key == Key::Escape && (self.press_captured || (!mods.cmd && !mods.ctrl && !(self.records && !mods.is_none()))); - // A shortcut comes before a mnemonic: an Alt+letter the keymap binds to an enabled command runs it. - let mnemonic = - self.menu_bar && !self.menu_bar_by_claim && self.os != Os::Mac && mods == Mods::ALT && key.is_letter() && !self.shortcut_text(input); + // A shortcut comes before a mnemonic: an Alt+letter the keymap binds to an enabled command runs it. On the + // focused bar a letter without Alt, Ctrl or Cmd is a title's access key, and one no title holds goes nowhere + // (the bar's access mode), so it runs no shortcut either. + let on_bar = self.bar_focused && !mods.alt && !mods.ctrl && !mods.cmd; + let mnemonic = self.menu_bar + && !self.menu_bar_by_claim + && self.os != Os::Mac + && key.is_letter() + && ((mods == Mods::ALT && !self.shortcut_text(input)) || on_bar); let edit = self.edit_chords.iter().find(|(command, chord)| (self.edits)(*command) && self.layout.matches(chord, input, self.os)); let canvas_edit = self.canvas.is_some_and(|canvas| { self.edit_chords.iter().any(|(command, chord)| canvas.edits.contains(*command) && self.layout.matches(chord, input, self.os)) @@ -557,6 +567,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec::SCOPE).show(ui, |ui, cx| { + while let Some((command, _)) = cx.take_command::() { + seen.borrow_mut().push(command); + } + stop.set(Some(craft_ui::buttons::Button::new("Target").show(ui).id)); + }); + }); + fixture.os(Os::Linux); + fixture.settle(); + let button = target.get().expect("the button ran"); + for alt in [true, false] { + for (key, text) in [(egui::Key::B, "b"), (egui::Key::Q, "q")] { + craft_ui::runtime::focus::request(fixture.ctx(), craft_ui::runtime::focus::FocusTarget::Id(button)); + fixture.settle(); + if alt { + fixture.hold(egui::Modifiers::ALT).settle(); + fixture.hold(egui::Modifiers::NONE).settle(); + } else { + fixture.key(egui::Key::F10, egui::Modifiers::NONE); + fixture.settle(); + } + let bar = fixture.focused_stop().map(|(id, _)| id); + assert!(bar.is_some() && bar != Some(button), "the bar takes the keyboard (alt {alt})"); + fixture.type_key(key, text); + fixture.settle(); + assert!(ran.borrow().is_empty(), "{text:?} on the bar runs no shortcut (alt {alt}): {:?}", ran.borrow()); + if key == egui::Key::B { + assert!(fixture.by_label("Round").is_some(), "b opens Brushes, whose access key it is (alt {alt})"); + fixture.key(egui::Key::Escape, egui::Modifiers::NONE); + fixture.key(egui::Key::Escape, egui::Modifiers::NONE); + } else { + assert!(fixture.by_label("Round").is_none() && fixture.by_label("Undo").is_none(), "q opens no menu (alt {alt})"); + assert_eq!(fixture.focused_stop().map(|(id, _)| id), bar, "and the bar keeps the keyboard (alt {alt})"); + fixture.key(egui::Key::Escape, egui::Modifiers::NONE); + } + fixture.settle(); + } + } + // Off the bar the letters are the shortcuts they are bound to. + craft_ui::runtime::focus::request(fixture.ctx(), craft_ui::runtime::focus::FocusTarget::Id(button)); + fixture.settle(); + fixture.type_key(egui::Key::Q, "q"); + fixture.settle(); + assert_eq!(*ran.borrow(), [BarAccessCommand::Quick], "q off the bar runs its shortcut"); +} + /// A command chord pressed while a menu is open closes the menu and runs where focus was, as with the menu closed /// (#9065); the open menu's own keys, the letters its rows' mnemonics name among them, still drive it, and a plain /// key no row names reaches the app's shortcuts.