From 79df298db4047fb8bd9b42c276bfa4800873ec99 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Wed, 18 Nov 2026 21:49:36 +0000 Subject: [PATCH] A menu's companion bar is out of the menu's Tab cycle Every layer now pushes its focus scope, a NONE-policy one included, so its stops belong to the layer rather than to the scope it was opened in. Tab and F6 inside a scope skip the stops of a child layer that takes no focus, traps nothing and restores nothing. The layer still sits in its parent's scope chain, so press family, menu walk-out and focus-left dismissal are unchanged. --- .../ui-contract/src/contract/overlay/host.rs | 8 ++++--- .../ui-contract/src/contract/overlay/props.rs | 2 +- crates/ui-contract/src/runtime/focus/nav.rs | 21 ++++++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/ui-contract/src/contract/overlay/host.rs b/crates/ui-contract/src/contract/overlay/host.rs index 733d53e971..35818a7cc8 100644 --- a/crates/ui-contract/src/contract/overlay/host.rs +++ b/crates/ui-contract/src/contract/overlay/host.rs @@ -202,16 +202,18 @@ pub(crate) fn host(ctx: &Context, layer: &Layer<'_>, body: &mut dyn FnMut(&mut U if real && (layer.exiting || matches!(overlay.rank, Rank::Popup | Rank::Modal { .. })) { access::report_layer(ctx, layer.id, ui.unique_id(), ui.max_rect(), overlay.rank.is_modal(), layer.exiting); } - if let (Some(policy), true) = (scope, real) { + // Every layer is a focus scope, one without a policy (`FocusPolicy::NONE`) included: its stops are the layer's, + // not those of the scope it was opened in, so a trap around it leaves them out (a menu's companion bar). + if real { let entering = !std::mem::replace(&mut memory.scope_entered, true); - focus::push_scope_from(ctx, layer.id, policy, None, opener, layer.exiting, Some(entering)); + focus::push_scope_from(ctx, layer.id, overlay.focus, None, opener, layer.exiting, Some(entering)); } areas::bound(ui, placed.room, min_width.min(placed.room.x)); if overlay.fit == OverlayFit::Resizable { ui.set_min_size(overlay.size_limits.min.min(placed.room)); } access::scope::outside(ctx, || body(ui)); - if scope.is_some() && real { + if real { focus::pop_scope(ctx); } Env::restore(ctx, installed); diff --git a/crates/ui-contract/src/contract/overlay/props.rs b/crates/ui-contract/src/contract/overlay/props.rs index 04f669be19..a6f73cca03 100644 --- a/crates/ui-contract/src/contract/overlay/props.rs +++ b/crates/ui-contract/src/contract/overlay/props.rs @@ -278,7 +278,7 @@ impl Overlay { } /// The chrome of a modal over a part of the window the modal covers, placed at `rect`: above the modal, with no - /// focus scope and no dismissal of its own. Its move band moves the window the layer is shown in. + /// focus policy and no dismissal of its own. Its move band moves the window the layer is shown in. pub fn modal_chrome(rect: Rect) -> Overlay { let mut overlay = Overlay::base(Rank::ModalChrome, Place::Float(FloatPlace::centred()), OverlayFit::Content, OwnerClass::Widget, FocusPolicy::NONE); diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index 06c91be66b..c820619301 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -254,7 +254,26 @@ impl State { } fn entries(&self, scope_root: Option, all_controls: bool) -> Vec { - self.last().entries(|rec| self.in_scope(rec.scope, scope_root), all_controls) + self.last().entries(|rec| self.reaches(rec.scope, scope_root), all_controls) + } + + /// Whether Tab and F6 inside the scope `root` reach a stop of `scope`: it lies inside `root`, and not inside a + /// layer opened within `root` that takes no focus, traps nothing and restores nothing (a menu's companion bar). + fn reaches(&self, scope: Option, root: Option) -> bool { + let Some(root) = root else { return true }; + let mut at = scope; + while let Some(id) = at { + if id == root { + return true; + } + let Some(rec) = self.last_scopes.get(&id) else { return false }; + let unreached = rec.entering.is_some() && !rec.policy.trap && !rec.policy.restore && rec.policy.initial == Initial::Keep; + if unreached { + return false; + } + at = rec.parent; + } + false } /// The stops of the running pass that keyboard focus reaches, among those `keep` takes, in Tab order: every member