menu: the first key is withheld only from the focus a pointer-opened menu gave

RowInput::of withheld Enter or Space from any focused row when it was the
first key to drive the menu, so an icon or row an AccessKit Focus had aimed
at, in a menu shown open or opened by the pointer, did not run: the generated
part-activate case of MenuIconRow got no click.

The rule now also asks that the row still holds the focus the menu gave it as
it opened (focus_came_with_open, from the focus origin). A key on a row or
part that a request focused runs it.

Trial: Return on an icon AccessKit focused in a pointer-opened menu picks it.
This commit is contained in:
skylartaylor committed 2026-11-24 17:44:37 +01:00
1 parent 638397eda9
commit 77cbfce411
3 files changed
+40 -3

No files matched your search

+7
View File
@@ -1071,6 +1071,13 @@ pub fn keyboard_in_menu(ctx: &Context) -> bool {
focused_rec(ctx).is_some_and(|rec| menu_scope(ctx, rec.scope).is_some()) && with(ctx, |state| state.visible)
}
/// Whether the focused stop took the focus when its scope opened (a menu's first row as the menu showed) and has kept
/// it since: no key, press or request (an AccessKit `Focus`) has moved it.
#[doc(hidden)]
pub fn focus_came_with_open(ctx: &Context) -> bool {
focused_rec(ctx).is_some_and(|rec| origin(ctx, &rec) == Some(FocusOrigin::Open))
}
/// The innermost menu scope around the component whose body is running: the menu a row being shown sits in.
#[doc(hidden)]
pub fn running_menu(ctx: &Context) -> Option<Id> {
+7 -3
View File
@@ -65,8 +65,9 @@ pub(crate) struct RowInput {
pub hovered: bool,
/// The row holds the focus.
pub focused: bool,
/// The row took Enter or Space as the first key in a menu the pointer opened, while the pointer had not lit it:
/// the row does not run on it and is not made active by it. [`RowInput::of`] answers the key instead.
/// The row took Enter or Space as the first key in a menu the pointer opened, holding the focus the menu gave it
/// as it opened, while the pointer had not lit it: the row does not run on it and is not made active by it.
/// [`RowInput::of`] answers the key instead.
pub withheld: bool,
keyboard: bool,
gained: bool,
@@ -80,6 +81,8 @@ impl RowInput {
/// Enter or Space as the first key in a menu the pointer opened reaches the focused row, which the pointer may never
/// have lit: the row withholds it, and the key runs the row the pointer lit, or with none lit only closes the menu,
/// as a native menu does. A submenu row is no exception: the first key opens no submenu the pointer did not light.
/// Only the focus the menu gave as it opened is withheld from: a row or part that a request focused (an AccessKit
/// `Focus`, the app) was aimed at, and the key runs it.
pub(crate) fn of<C: Component>(cx: &mut Cx<'_, C>, level: &MenuLevel) -> RowInput {
let moving = cx.core.ctx.input(|input| input.pointer.delta() != Vec2::ZERO);
let hovered = cx.response.hovered();
@@ -92,8 +95,9 @@ impl RowInput {
rest.focused = focused;
let started = keyboard && !rest.keyboard;
rest.keyboard = keyboard;
let opened = focused && crate::runtime::focus::focus_came_with_open(&cx.core.ctx);
let lit = level.active_enabled();
let withheld = started && by_key && lit != Some(id);
let withheld = started && by_key && opened && lit != Some(id);
if withheld {
match lit {
Some(row) => level.run(row),
@@ -6,6 +6,7 @@ use craft_ui::buttons::Button;
use craft_ui::contract::{Show as _, ShowContainer as _, Trigger};
use craft_ui::menu::{Menu, MenuIcon, MenuIconRow, MenuItem};
use craft_ui::runtime::focus::{FocusTarget, request};
use craft_ui::testing::focus::AccessActionKind;
use craft_ui::testing::{Fixture, pointer, trial};
use egui::{Id, Key, Modifiers};
use std::cell::{Cell, RefCell};
@@ -155,3 +156,28 @@ fn return_in_a_pointer_opened_menu_whose_first_row_is_an_icon_row_runs_only_the_
assert!(picks.borrow().is_empty(), "and picks no icon of the focused row");
assert!(fixture.by_label("Ungroup").is_none(), "and the run closes the menu");
}
/// Regression: the first key in a menu the pointer opened was withheld from an icon an AccessKit `Focus` had moved to,
/// so Return picked nothing. Only the focus the menu gave as it opened is withheld from; an icon aimed at is picked.
#[trial]
fn return_on_an_icon_accesskit_focused_in_a_pointer_opened_menu_picks_it() {
let picks = Rc::new(RefCell::new(Vec::new()));
let picked = picks.clone();
let mut fixture = Fixture::new(move |ui| {
let icons =
[MenuIcon::new(IconId::new("align_edge_left"), "Align Left Edges"), MenuIcon::new(IconId::new("align_edge_right"), "Align Right Edges")];
let opener = Button::new("Open").show(ui);
Menu::new("Arrange").opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
picked.borrow_mut().extend(MenuIconRow::new("Align", &icons).show(ui).picked);
MenuItem::new("Group").show(ui);
});
});
fixture.settle();
let opener = fixture.by_label("Open").expect("the opener");
fixture.advance(1.0).click(&opener).settle();
let right = fixture.by_label("Align Right Edges").expect("the menu shows its icons");
fixture.access(AccessActionKind::Focus, &right).settle();
fixture.key(Key::Enter, Modifiers::NONE).settle();
assert_eq!(*picks.borrow(), vec![1], "Return picks the icon AccessKit focused");
assert!(fixture.by_label("Group").is_none(), "and the pick closes the menu");
}