mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Merge pull request #2931 from davidmann4/fix/menu-bar-space-hand
menu bar: Space on a focused title opens no menu (photocraft#16327)
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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(<HandCommand as craft_ui::contract::CommandSet>::SCOPE).show(ui, |ui, cx| {
|
||||
while cx.take_command::<HandCommand>().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(<HandCommand as craft_ui::contract::CommandSet>::SCOPE).show(ui, |ui, cx| {
|
||||
while cx.take_command::<HandCommand>().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})");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user