ui-contract: on the focused menu bar a plain letter is a title's access key: it opens the title it marks, and one no title marks runs no shortcut

This commit is contained in:
bflatastic
2026-11-23 15:24:26 +07:00
parent 7089e2de85
commit ecd1565856
3 changed files with 94 additions and 4 deletions
+15 -4
View File
@@ -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<Arriv
window_shortcuts: &window_shortcuts,
menu_bar,
menu_bar_by_claim: record.menu_bar_by_claim,
bar_focused: record.menu_bar.is_some() && record.focused == record.menu_bar,
holds_drafts: keys.is_some_and(|keys| keys.holds_drafts),
canvas_space: hole.as_ref().is_some_and(|hole| hole.space || hole.space_pressed),
foreign: record.foreign,
+2
View File
@@ -10,6 +10,8 @@
//! Keys follow spec 9.7: Alt and a letter open the menu with that mnemonic (also while another is open). F10 or a
//! lone Alt closes an open menu; with none open it moves focus to the first title, and the next F10 or lone Alt, an
//! Escape, a shortcut (Space, a canvas's momentary Hand, among them) or running a row returns focus to where it was.
//! On the focused bar a letter without Alt is a mnemonic too: it opens the title it marks, and one no title marks does
//! nothing, so it runs no shortcut.
//! Down and Enter open the focused title's menu; Space opens none. A `&` in a title's words row marks its mnemonic; without
//! one the title takes the first letter no earlier title holds. The mnemonic is underlined while Alt is held.
//!
@@ -124,6 +124,83 @@ fn a_shortcut_pressed_on_the_focused_bar_runs_once_where_focus_was_and_gives_foc
assert_eq!(fixture.focused_stop().map(|(id, _)| id), target.get(), "and the focus is back on that stop");
}
/// The commands of the access-key trial: one on a letter a title marks, one on a letter none marks.
#[derive(Clone, Copy, Debug, PartialEq, Eq, craft_ui::contract::CommandSet)]
#[command(prefix = "barAccess", scope = "trial.barAccess")]
#[non_exhaustive]
pub enum BarAccessCommand {
#[command(label = "Brush", default = "B")]
Brush,
#[command(label = "Quick", default = "Q")]
Quick,
}
/// On the bar that a lone Alt or F10 focused, a plain letter is an access key, as in production's menu access mode:
/// the letter a title marks opens that title, not the shortcut on the same letter, and a letter no title marks is
/// swallowed, its shortcut unrun and the bar still focused.
#[trial]
fn a_plain_letter_on_the_focused_bar_opens_its_title_and_runs_no_shortcut() {
use craft_ui::contract::ShowContainer as _;
use craft_ui::runtime::commands::Commands as _;
let ran = Rc::new(RefCell::new(Vec::new()));
let target = Rc::new(Cell::new(None));
let (seen, stop) = (ran.clone(), target.clone());
let mut fixture = Fixture::new(move |ui| {
let titles = vec![
MenuTitle::new("&Edit", |ui| {
MenuItem::command(EditCommands::Undo).show(ui);
}),
MenuTitle::new("&Brushes", |ui| {
MenuItem::new("&Round").show(ui);
}),
];
MenuBar::new("Main menu", titles).show(ui);
craft_ui::layout_components::Stack::new().command_scope(<BarAccessCommand as craft_ui::contract::CommandSet>::SCOPE).show(ui, |ui, cx| {
while let Some((command, _)) = cx.take_command::<BarAccessCommand>() {
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.