From dc89eb1d3adf539f079f2ab7031ddcadd600a199 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sat, 21 Nov 2026 23:31:58 +0100 Subject: [PATCH] menu bar: Space on a focused title opens no menu (photocraft#16327) A lone Alt tap or F10 puts the keyboard on the in-window menu bar. The titles took Space as their activation, so Space opened the lit menu (File), and a held Space went on to run its first row. In a canvas app Space is the momentary Hand, and the reference application never opens a menu with it. - The titles activate on Enter only; Down opens the menu as before. Space goes on to the app's shortcuts, and a shortcut pressed on the bar already gives the keyboard back to the stop the bar took it from. - A bar that takes the keyboard while nothing holds it remembers the window's document canvas (the key target then) as that stop, so the shortcut and Escape give the keyboard back to the canvas instead of leaving the bar lit, where the next tool letter would open a menu. Co-Authored-By: Claude Opus 5.5 --- crates/ui-contract/src/runtime/focus.rs | 9 +- crates/ui-widgets/src/menu/bar.rs | 8 +- crates/ui-widgets/tests/it/menu/bar.rs | 121 ++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 4 deletions(-) diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 1adf515be..cc3afde1f 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -563,12 +563,17 @@ pub fn takes_letters(keys: &KeyUse) -> bool { } /// A registered menu bar takes the keyboard (F10, a lone Alt): focus moves to `target`, a stop of the bar, in this -/// pass, and the stop that held it is remembered for [`give_back_bar`]. Focus already inside the bar leaves the -/// remembered stop as it is. +/// pass, and the stop that held it is remembered for [`give_back_bar`]. With nothing focused, that is the window's +/// document canvas, the key target then: a shortcut pressed on the bar (Space, the canvas's momentary Hand) matches +/// there and gives the keyboard back to it, as Escape does. Focus already inside the bar leaves the remembered stop +/// as it is. #[doc(hidden)] pub fn take_for_bar(ctx: &Context, bar: Id, target: FocusTarget) { let held = focused_stop(ctx).filter(|(id, _)| *id != bar); + let in_bar = held.is_none() && is_focused(ctx, bar); with(ctx, |state| { + let canvas = || state.stops.iter().chain(&state.last_stops).find(|rec| rec.stop.keys.chrome_key).map(|rec| rec.stop.key()); + let held = held.or_else(|| (!in_bar).then(canvas).flatten()); if held.is_some() || state.bar_return.is_none() { state.bar_return = held.map(|held| (bar, held)); } diff --git a/crates/ui-widgets/src/menu/bar.rs b/crates/ui-widgets/src/menu/bar.rs index c74ef97b5..732d1f9fd 100644 --- a/crates/ui-widgets/src/menu/bar.rs +++ b/crates/ui-widgets/src/menu/bar.rs @@ -9,7 +9,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, or running a row returns focus to where it was. A `&` in a title's words row marks its mnemonic; without +//! Escape, a shortcut (Space, a canvas's momentary Hand, among them) or running a row returns focus to where it was. +//! 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. //! //! The Help menu can start with a search field over a list of `HelpEntry` commands: typing lists up to `search::MAX_RESULTS` @@ -428,7 +429,10 @@ impl<'a> MenuBar<'a> { impl Component for MenuBar<'_> { const ROLE: Role = Role::MENU_BAR; - const KEYS: crate::runtime::focus::KeyUse = crate::runtime::focus::KeyUse::of(crate::contract::Input::Pointer).with_moves(); + // Enter and Down open the focused title's menu; Space does not. Space is a canvas's momentary Hand, so it goes on + // to the app's shortcuts, and a shortcut pressed on the bar gives the keyboard back to where it was. + const KEYS: crate::runtime::focus::KeyUse = + crate::runtime::focus::KeyUse::of(crate::contract::Input::Pointer).with_moves().with_activate(crate::runtime::focus::ActivateKeys::Enter); type Style = MenuBarStyle; type Output = MenuBarOutput; diff --git a/crates/ui-widgets/tests/it/menu/bar.rs b/crates/ui-widgets/tests/it/menu/bar.rs index 977de977e..d970b55df 100644 --- a/crates/ui-widgets/tests/it/menu/bar.rs +++ b/crates/ui-widgets/tests/it/menu/bar.rs @@ -375,3 +375,124 @@ fn holding_alt_runs_the_memoized_view_of_a_menu_bar() { fixture.hold(Modifiers::NONE); assert!(frame(&mut fixture) > 0, "Alt coming up runs the view, which drops the underlines"); } + +/// What a window's own Space does: the canvas's momentary Hand, say. +#[derive(Clone, Copy, Debug, PartialEq, Eq, craft_ui::contract::CommandSet)] +#[command(prefix = "trialHand", scope = "trial.hand")] +#[non_exhaustive] +pub enum HandCommand { + #[command(label = "Hand", default = "Space")] + Hand, +} + +/// Space on the menu bar that F10 or a lone Alt focused opens no menu: the press goes on to the window's own Space +/// command (a canvas's Hand), as in the reference application, where Space is always the Hand, and the shortcut gives +/// the keyboard back to the stop the bar took it from. Enter and Down still open the focused title's menu. +#[trial] +fn space_on_the_focused_bar_opens_no_menu_and_reaches_the_window() { + use craft_ui::buttons::Button; + use craft_ui::contract::ShowContainer as _; + use craft_ui::layout_components::Stack; + use craft_ui::runtime::commands::Commands as _; + use craft_ui::runtime::focus::{FocusTarget, request}; + use std::cell::Cell; + use std::rc::Rc; + let (hands, clicks, button) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)), Rc::new(Cell::new(None))); + let (seen_hands, seen_clicks, seen_button) = (Rc::clone(&hands), Rc::clone(&clicks), Rc::clone(&button)); + let mut fixture = Fixture::new(move |ui| { + let titles = vec![MenuTitle::new("&File", |ui| { + MenuItem::new("&New").show(ui); + })]; + MenuBar::new("Main menu", titles).show(ui); + Stack::new().command_scope(::SCOPE).show(ui, |ui, cx| { + while cx.take_command::().is_some() { + seen_hands.set(seen_hands.get() + 1); + } + let shown = Button::new("Layer").show(ui); + if shown.clicked() { + seen_clicks.set(seen_clicks.get() + 1); + } + seen_button.set(Some(shown.response.id)); + }); + }); + fixture.os(Os::Linux); + fixture.settle(); + let layer = button.get().expect("the button ran"); + for (enter, alt) in [(None, false), (None, true), (Some(Key::Enter), false), (Some(Key::ArrowDown), false)] { + request(fixture.ctx(), FocusTarget::Id(layer)); + fixture.settle(); + let before = (hands.get(), clicks.get()); + if alt { + fixture.hold(Modifiers::ALT).settle(); + fixture.hold(Modifiers::NONE).settle(); + } else { + fixture.key(Key::F10, Modifiers::NONE); + fixture.settle(); + } + assert!(fixture.focused_stop().is_some_and(|(id, _)| id != layer), "the bar takes the keyboard (alt {alt})"); + if let Some(key) = enter { + fixture.key(key, Modifiers::NONE); + fixture.settle(); + assert!(fixture.by_label("New").is_some(), "{key:?} opens the focused title's menu"); + assert_eq!((hands.get(), clicks.get()), before, "{key:?} reaches neither the window nor the button"); + fixture.key(Key::Escape, Modifiers::NONE); + fixture.key(Key::Escape, Modifiers::NONE); + fixture.settle(); + continue; + } + fixture.key(Key::Space, Modifiers::NONE); + fixture.settle(); + assert!(fixture.by_label("New").is_none(), "Space opens no menu (alt {alt})"); + assert_eq!(fixture.focused_stop().map(|(id, _)| id), Some(layer), "Space takes the keyboard off the bar, back to the button (alt {alt})"); + assert_eq!(hands.get(), before.0 + 1, "the press reaches the window's Space command (alt {alt})"); + assert_eq!(clicks.get(), before.1, "and activates nothing else (alt {alt})"); + } +} + +/// With nothing focused the window's document canvas is the key target: a bar that F10 or a lone Alt focused then +/// gives the keyboard back to the canvas, so Space, the canvas's Hand, matches there and leaves the bar in one press. +#[trial] +fn space_on_a_bar_focused_from_nothing_goes_to_the_document_canvas() { + use craft_ui::contract::ShowContainer as _; + use craft_ui::layout_components::Stack; + use craft_ui::runtime::commands::Commands as _; + use craft_ui::runtime::focus::{KeyUse, host_canvas}; + use std::cell::Cell; + use std::rc::Rc; + let canvas = egui::Id::unique("document canvas"); + let hands = Rc::new(Cell::new(0)); + let seen_hands = Rc::clone(&hands); + let mut fixture = Fixture::new(move |ui| { + let titles = vec![MenuTitle::new("&File", |ui| { + MenuItem::new("&New").show(ui); + })]; + MenuBar::new("Main menu", titles).show(ui); + Stack::new().command_scope(::SCOPE).show(ui, |ui, cx| { + while cx.take_command::().is_some() { + seen_hands.set(seen_hands.get() + 1); + } + let rect = ui.available_rect_before_wrap(); + ui.interact(rect, canvas, egui::Sense::click()); + host_canvas(ui.ctx(), canvas, KeyUse::CANVAS); + }); + }); + fixture.os(Os::Linux); + fixture.settle(); + for alt in [false, true] { + assert!(fixture.focused_stop().is_none_or(|(id, _)| id == canvas), "nothing but the canvas holds the keyboard (alt {alt})"); + if alt { + fixture.hold(Modifiers::ALT).settle(); + fixture.hold(Modifiers::NONE).settle(); + } else { + fixture.key(Key::F10, Modifiers::NONE); + fixture.settle(); + } + assert!(fixture.focused_stop().is_some_and(|(id, _)| id != canvas), "the bar takes the keyboard (alt {alt})"); + let before = hands.get(); + fixture.key(Key::Space, Modifiers::NONE); + fixture.settle(); + assert!(fixture.by_label("New").is_none(), "Space opens no menu (alt {alt})"); + assert_eq!(hands.get(), before + 1, "Space reaches the canvas's command (alt {alt})"); + assert_eq!(fixture.focused_stop().map(|(id, _)| id), Some(canvas), "and the keyboard goes to the canvas (alt {alt})"); + } +}