From b226fe4f2b8219cdfab5961ebbebd1a3161820ed Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 07:30:56 -0800 Subject: [PATCH] ui-contract: a dialog with no field and no default button opens on its first body stop A dialog whose body holds only a list and buttons, with no text field and no default button, opened with nothing focused: Initial::FirstField fell back to the default stop and then to nothing. Escape then closed it with focus nowhere, since focus returns to the opener only from inside the closing scope. Initial::FirstField now falls back to the first Tab entry of the scope's body: a roving group at its arrival member, past read-only text that only a press focuses and past a collection's row children. The body container marks where its body ends (focus::end_body, after the body and before the end region and the footer), so buttons after the body still take no focus on open, and Enter in a confirmation with no default answers nothing. Dialogs that open on a field or a default button do not change. --- crates/ui-contract/src/contract/body/hooks.rs | 8 +++- crates/ui-contract/src/runtime/focus.rs | 32 ++++++++++--- crates/ui-contract/src/runtime/focus/nav.rs | 30 ++++++++---- .../ui-contract/src/runtime/focus/policy.rs | 3 +- crates/ui-contract/src/testing/focus.rs | 8 +++- crates/ui-panels/src/dialog/trials.rs | 42 ++++++++++++++-- .../ui-panels/tests/it/focus/dialog_open.rs | 48 +++++++++++++++++++ crates/ui-panels/tests/it/focus/mod.rs | 1 + 8 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 crates/ui-panels/tests/it/focus/dialog_open.rs diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index a92e7820ed..b33dd86828 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -640,6 +640,9 @@ impl ShapeHooks for BodyHooks<'_> { self.end_unfit = Some(short); } self.after_body(ui, env, &mut plan); + if plan.real { + crate::runtime::focus::end_body(&plan.ctx, plan.id); + } if plan.real && props.end.is_some() { self.end_step(comp, ui, cx, &mut plan); } @@ -647,6 +650,9 @@ impl ShapeHooks for BodyHooks<'_> { // A collapsed body keeps its last full size in layout memory, so opening it again places by that size // and asks for no discard pass. self.body_size = if props.collapse.is_some() { None } else { Some(Vec2::ZERO) }; + if plan.real { + crate::runtime::focus::end_body(&plan.ctx, plan.id); + } } self.close_label(&mut plan); // B4 footer: under the control column of a label-scoped container, which starts where the label step put @@ -1294,7 +1300,7 @@ impl BodyHooks<'_> { plan.footer_env = plan.region_env.derive_local(|scoped| scoped.content_column = true); } if plan.real { - crate::runtime::focus::begin_body(&plan.ctx); + crate::runtime::focus::begin_body(&plan.ctx, plan.id); } // A stack is a content column where its width comes from its content: a content-sized stack in flow, or a // held fill-width container. A stack given its width (a rect it is shown at, or a column's width it passes diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 50a405e4f0..06f17c29dd 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -160,8 +160,11 @@ pub(crate) struct State { pub(crate) scope_order: Vec, pub(crate) last_scope_order: Vec, pub(super) open: Vec<(Id, usize)>, - /// Where a scope's body begins in `stops`; the header's stops come before it, the footer's after the body's. - pub(super) body_from: FastMap, + /// Where a scope's body begins in `stops`, and the container whose body it is; the header's stops come before it, + /// the footer's after the body's. + pub(super) body_from: FastMap, + /// Where the body of the container that marked a scope's body ends in `stops`: its end region and footer follow. + pub(super) body_end: FastMap, pub(crate) groups: FastMap, pub(crate) last_groups: FastMap, pub(crate) row_sets: Vec, @@ -572,13 +575,28 @@ pub(crate) fn focus_in(ctx: &Context, range: std::ops::Range) -> bool { /// Marks the stops registered from here on in the innermost open scope as its body's: they come first in Tab /// order, then the footer's, then the header's, and initial focus goes to the first of them, not to the title -/// row's close action. The first mark of a pass stands. -pub(crate) fn begin_body(ctx: &Context) { +/// row's close action. The first mark of a pass stands; `container` is the container whose body it is. +pub(crate) fn begin_body(ctx: &Context, container: Id) { take_app_widgets(ctx, None); with(ctx, |state| { if let Some((scope, _)) = state.open.last().copied() { let at = state.stops.len(); - state.body_from.entry(scope).or_insert(at); + state.body_from.entry(scope).or_insert((at, container)); + } + }); +} + +/// Ends the body that `container` marked in the innermost open scope (`begin_body`): the stops registered from here on +/// are its end region's and footer's. A dialog with no field and no default stop starts on its first body stop, never +/// on one of theirs. A container nested in the body ends nothing. +pub(crate) fn end_body(ctx: &Context, container: Id) { + take_app_widgets(ctx, None); + with(ctx, |state| { + if let Some((scope, _)) = state.open.last().copied() + && state.body_from.get(&scope).is_some_and(|(_, owner)| *owner == container) + { + let at = state.stops.len(); + state.body_end.entry(scope).or_insert(at); } }); } @@ -673,8 +691,8 @@ pub(crate) fn pop_scope(ctx: &Context) { take_app_widgets(ctx, None); let frame = crate::runtime::egui::pass::frame_nr(ctx, viewport(ctx)); let acts = with(ctx, |state| { - nav::order_regions(state); - nav::initial_focus(state, frame) + let body = nav::order_regions(state); + nav::initial_focus(state, frame, body) }); apply(ctx, acts); } diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index d7d299d379..b86b1bb664 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -192,6 +192,7 @@ impl State { self.last_docs = std::mem::take(&mut self.docs); self.open.clear(); self.body_from.clear(); + self.body_end.clear(); self.entered = false; self.asked = false; self.candidates_seen = 0; @@ -764,17 +765,22 @@ fn cycle(entries: &[Entry], current: Option<&StopRec>, top: Option, forward: } /// Puts the stops of the innermost open scope in Tab order: the body's, then the footer's, then the header's. -/// Inner scopes closed already and lie within one region, so they move with it. -pub(super) fn order_regions(state: &mut State) { - let Some((scope, first)) = state.open.last().copied() else { return }; - let Some(body) = state.body_from.remove(&scope) else { return }; +/// Inner scopes closed already and lie within one region, so they move with it. Returns how many stops the body holds, +/// now the scope's first ones; `None` when the scope marked no body. +pub(super) fn order_regions(state: &mut State) -> Option { + let (scope, first) = state.open.last().copied()?; + let body_end = state.body_end.remove(&scope); + let (body, _) = state.body_from.remove(&scope)?; let end = state.stops.len(); let first = first.min(end); - state.stops[first..end].rotate_left(body.clamp(first, end) - first); + let body = body.clamp(first, end); + state.stops[first..end].rotate_left(body - first); + Some(body_end.map_or(end, |body_end| body_end.clamp(body, end)) - body) } -/// Closes the innermost open scope; a scope shown for the first time moves focus to its initial stop. -pub(super) fn initial_focus(state: &mut State, frame: u64) -> Vec { +/// Closes the innermost open scope; a scope shown for the first time moves focus to its initial stop. `body` is how +/// many of the scope's first stops are its body's (`order_regions`), `None` for all of them. +pub(super) fn initial_focus(state: &mut State, frame: u64, body: Option) -> Vec { let Some((scope, first)) = state.open.pop() else { return Vec::new() }; let Some((policy, entering)) = state.scopes.get(&scope).map(|rec| (rec.policy, rec.entering)) else { return Vec::new() }; let new = match entering { @@ -799,10 +805,18 @@ pub(super) fn initial_focus(state: &mut State, frame: u64) -> Vec { .and_then(|group| lists.arrival(&group)) .unwrap_or_else(|| rec.clone()) }); + // The body's first Tab entry, at its arrival member. The footer's and the header's buttons with no default take no + // focus, so Enter does not answer one that was never chosen. + let in_body = (policy.initial == Initial::FirstField) + .then(|| { + let body = Lists { stops: &stops[..body.unwrap_or(stops.len()).min(stops.len())], ..lists }; + body.entries(|_| true, true).into_iter().next().map(|entry| entry.arrival().clone()) + }) + .flatten(); let pick = match policy.initial { Initial::First => entry.as_ref(), Initial::Default => stops.iter().find(|rec| rec.stop.default).or(entry.as_ref()), - Initial::FirstField => stops.iter().find(|rec| rec.stop.text).or_else(|| stops.iter().find(|rec| rec.stop.default)), + Initial::FirstField => stops.iter().find(|rec| rec.stop.text).or_else(|| stops.iter().find(|rec| rec.stop.default)).or(in_body.as_ref()), Initial::Key(name) => stops.iter().find(|rec| rec.stop.name == Some(name)), _ => None, }; diff --git a/crates/ui-contract/src/runtime/focus/policy.rs b/crates/ui-contract/src/runtime/focus/policy.rs index 7a8acfed7c..8e4ed2c246 100644 --- a/crates/ui-contract/src/runtime/focus/policy.rs +++ b/crates/ui-contract/src/runtime/focus/policy.rs @@ -12,7 +12,8 @@ pub enum Initial { Keep, /// The scope's first stop. First, - /// The scope's first text or number field, else its default stop, else nothing: a dialog's default. + /// The scope's first text or number field, else its default stop, else the first stop of its body, else nothing: + /// a dialog's default. A footer's or a header's buttons are not taken by the last rule. FirstField, /// The stop registered as the default. Default, diff --git a/crates/ui-contract/src/testing/focus.rs b/crates/ui-contract/src/testing/focus.rs index 6ea67d6f4a..9dd31b9f3b 100644 --- a/crates/ui-contract/src/testing/focus.rs +++ b/crates/ui-contract/src/testing/focus.rs @@ -77,7 +77,13 @@ pub fn push_scope(ctx: &Context, id: Id, policy: FocusPolicy, label: Option Option { + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| { + push_scope(ui.ctx(), id("dialog"), FocusPolicy::DIALOG, None); + begin_body(ui.ctx()); + for name in body { + if name.starts_with("text") { + widget(ui, id(name)); + register_stop(ui.ctx(), Stop::new(id(name), KeyUse::of(Input::Text)).press_only()); + } else if name.starts_with("row") { + widget(ui, id(name)); + register_stop(ui.ctx(), Stop::new(id(name), KeyUse::of(Input::Pointer)).in_group(id("row"))); + register_group(ui.ctx(), Group::new(id("row"), Roving::new(NavShape::Flat)).row(id("rows"))); + } else { + control(ui, name); + } + } + end_body(ui.ctx()); + for name in end { + control(ui, name); + } + pop_scope(ui.ctx()); + }; + frames.idle(3, &mut show); + focused(&frames.ctx) +} + +/// A dialog with no field and no default stop opens on the first body stop that Tab reaches, past read-only text, and +/// leaves the buttons after its body unfocused when the body has none, so Enter answers nothing nobody chose. +#[trial] +fn a_dialog_with_no_field_or_default_opens_on_its_first_reachable_body_stop() { + assert_eq!(opens_on(&["list", "new"], &["close"]), Some(id("list")), "the body's first stop takes the focus"); + assert_eq!(opens_on(&["text", "list"], &["close"]), Some(id("list")), "read-only text that Tab passes over does not"); + assert_eq!(opens_on(&["row child", "list"], &["close"]), Some(id("list")), "nor does a row's child, which Tab passes over"); + assert_eq!(opens_on(&[], &["action", "close"]), None, "with nothing in the body, no button after it takes the focus"); +} diff --git a/crates/ui-panels/tests/it/focus/mod.rs b/crates/ui-panels/tests/it/focus/mod.rs index fd2552ac98..0ad8cde79d 100644 --- a/crates/ui-panels/tests/it/focus/mod.rs +++ b/crates/ui-panels/tests/it/focus/mod.rs @@ -1,5 +1,6 @@ //! The focus service and the key stage, driven through registered stops over real egui widgets. +pub(crate) mod dialog_open; pub(crate) mod dialog_tab; pub(crate) mod keys; pub(crate) mod press;