From 924d305bb1cbc7ea077d394251faa80f4d155ada Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Wed, 25 Nov 2026 03:47:35 +0100 Subject: [PATCH] Menu::tab_exits: Shift+Tab and a Tab after Escape go on from the opener Shift+Tab out of a tab-exits menu landed on its opener, and a Tab pressed while the menu played its exit after an Escape landed in the fading menu, whose close then handed the focus back to the opener. A Tab from inside a menu that traps nothing now goes on from the menu's opener, with the menu's own rows no entries, so Tab reaches the stop after the opener and Shift+Tab the stop before it; and a layer playing its exit is no Tab entry. Trials for both. --- crates/ui-contract/src/runtime/focus/nav.rs | 39 +++++++++++++++++++- crates/ui-widgets/tests/it/menu/tab_exits.rs | 39 ++++++++++++++++++-- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index 58924165b3..abaaf0cdbd 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -405,17 +405,54 @@ impl State { let rec = owner.unwrap_or(rec); self.anchored_layer(rec.scope).and_then(|opener| self.find_stop(opener)).unwrap_or(rec) }); + // Tab out of a menu that lets it leave (`Menu::tab_exits`) goes on from the menu's opener, either way, as if the + // menu were part of it: the menu's own rows are no entries, so Shift+Tab reaches the stop before the opener. + let exits = current.as_ref().and_then(|rec| self.tab_exit_menu(rec.scope)); + let current = exits.and_then(|menu| self.return_stop(menu)).or(current); let trap = self.trap_of(current.as_ref()); let in_dialog = trap.and_then(|id| self.last_scopes.get(&id)).is_some_and(|scope| scope.policy.kind == ScopeKind::Dialog); let fields = in_dialog && dialog_tab == TabReach::Fields; let mut entries = self.entries(trap, all_controls && !fields); - entries.retain(|entry| self.anchored_layer(entry.arrival().scope).is_none() && (!fields || entry.arrival().stop.text)); + // A layer playing its exit takes no focus (`exiting`): an Escape that closed a menu leaves it no Tab entry for the + // fade's frames, so the next Tab does not land in it and have the closed menu hand the focus back to its opener. + entries.retain(|entry| { + let scope = entry.arrival().scope; + self.anchored_layer(scope).is_none() + && !self.exiting(scope) + && exits.is_none_or(|menu| !self.within(scope, menu)) + && (!fields || entry.arrival().stop.text) + }); let top = current.as_ref().and_then(|rec| rec.stop.group).map(|group| self.last().outermost(group)); let Some(next) = cycle(&entries, current.as_ref(), top, forward) else { return Vec::new() }; let arrival = entries[next].arrival().clone(); self.focus_stop(arrival) } + /// The menu around `scope` that Tab leaves (`Menu::tab_exits`): a menu scope that traps nothing. + fn tab_exit_menu(&self, scope: Option) -> Option { + let mut at = scope; + while let Some(id) = at { + let rec = self.last_scopes.get(&id)?; + if rec.policy.kind == ScopeKind::Menu && !rec.policy.trap { + return Some(id); + } + at = rec.parent; + } + None + } + + /// Whether the scope `scope`, or one around it, is a layer playing its exit, by this pass's record or else the last. + fn exiting(&self, scope: Option) -> bool { + let mut at = scope; + while let Some(rec) = at.and_then(|id| self.scopes.get(&id).or_else(|| self.last_scopes.get(&id))) { + if rec.closing { + return true; + } + at = rec.parent; + } + false + } + /// F6: the first stop of the next pane, with its label as the announcement. Inside a trapping scope (a modal /// dialog or a popup, also when nothing in it is focused) F6 moves nothing and announces nothing: a pane move /// would carry the focus out of the trap, which only closing the dialog may do. diff --git a/crates/ui-widgets/tests/it/menu/tab_exits.rs b/crates/ui-widgets/tests/it/menu/tab_exits.rs index 050cb31750..c95de69094 100644 --- a/crates/ui-widgets/tests/it/menu/tab_exits.rs +++ b/crates/ui-widgets/tests/it/menu/tab_exits.rs @@ -14,16 +14,19 @@ use std::rc::Rc; struct Rig { fixture: Fixture, ran: Rc>, + before: Rc>>, + opener: Rc>>, after: Rc>>, } fn rig(exits: bool) -> Rig { let ran = Rc::new(Cell::new(0)); + let before = Rc::new(Cell::new(None)); let after = Rc::new(Cell::new(None)); let opener = Rc::new(Cell::new(None)); - let (shown_ran, shown_after, shown_opener) = (ran.clone(), after.clone(), opener.clone()); + let (shown_ran, shown_before, shown_after, shown_opener) = (ran.clone(), before.clone(), after.clone(), opener.clone()); let mut fixture = Fixture::new(move |ui| { - Button::new("Before").show(ui); + shown_before.set(Some(Button::new("Before").show(ui).id)); let preset = Button::new("Preset").show(ui); shown_opener.set(Some(preset.id)); let menu = Menu::new("Presets").opened_by(&preset, Trigger::Press); @@ -44,7 +47,7 @@ fn rig(exits: bool) -> Rig { fixture.key(Key::Enter, Modifiers::NONE); fixture.settle(); assert!(fixture.by_label("Linear").is_some(), "Enter on the focused opener opens the menu"); - Rig { fixture, ran, after } + Rig { fixture, ran, before, opener, after } } impl Rig { @@ -56,6 +59,10 @@ impl Rig { self.fixture.key(Key::Tab, Modifiers::NONE); self.fixture.settle(); } + + fn focused(&self) -> Option { + self.fixture.focused_stop().map(|(id, _)| id) + } } #[trial] @@ -85,3 +92,29 @@ fn arrows_still_move_through_a_tab_exits_menu() { rig.fixture.settle(); assert_eq!(rig.ran.get(), 1, "Enter runs the row the arrows reached"); } + +#[trial] +fn shift_tab_leaves_a_tab_exits_menu_for_the_stop_before_its_opener() { + let mut rig = rig(true); + rig.fixture.key(Key::ArrowDown, Modifiers::NONE); + rig.fixture.key(Key::Tab, Modifiers::SHIFT); + rig.fixture.settle(); + assert!(!rig.menu_open(), "the menu closes"); + assert_eq!(rig.ran.get(), 0, "no row runs"); + assert_eq!(rig.focused(), rig.before.get(), "the focus moves to the stop before the opener, not back to the opener"); +} + +/// An Escape closes the menu and gives the focus back to its opener; a Tab pressed while the menu still plays its exit +/// goes on to the stop after the opener, and the closing menu does not take the focus back. +#[trial] +fn tab_right_after_escape_goes_on_from_the_opener_of_a_tab_exits_menu() { + let mut rig = rig(true); + rig.fixture.key(Key::ArrowDown, Modifiers::NONE); + rig.fixture.key(Key::Escape, Modifiers::NONE); + assert_eq!(rig.focused(), rig.opener.get(), "Escape gives the focus back to the opener"); + rig.fixture.key(Key::Tab, Modifiers::NONE); + rig.fixture.settle(); + assert!(!rig.menu_open(), "the menu is closed"); + assert_eq!(rig.ran.get(), 0, "no row runs"); + assert_eq!(rig.focused(), rig.after.get(), "the focus goes on to the stop after the opener and stays there"); +}