From 910a14280fec7bd335b01f61cc7c8652e34c1e37 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 20:45:33 +0000 Subject: [PATCH] craft-ui: a component joins its body's roving group in the pass it says so Stops register after update, which calls Cx::join_roving, so a stop and its focusable parts are in the body's roving group from the first pass that shows them. Membership is a field of the pass (roving_member), read by the stops and the report step; the pass-stamped join slots in egui memory (join_group, member_group, join_part_group, part_group) go. testing::focus::pass_stop_groups reads what the running pass registered. --- crates/ui-contract/src/contract/cx/context.rs | 12 +++---- crates/ui-contract/src/contract/run.rs | 36 +++++++++++-------- crates/ui-contract/src/contract/steps.rs | 2 +- .../ui-contract/src/contract/steps/stops.rs | 27 +++----------- crates/ui-contract/src/runtime/env/view.rs | 5 --- .../src/runtime/focus/registration.rs | 27 -------------- crates/ui-contract/src/testing/focus.rs | 5 +++ crates/ui-widgets/tests/it/bars/bar.rs | 22 ++++++++++++ 8 files changed, 60 insertions(+), 76 deletions(-) diff --git a/crates/ui-contract/src/contract/cx/context.rs b/crates/ui-contract/src/contract/cx/context.rs index 3cc1dc3a8..2006c85b1 100644 --- a/crates/ui-contract/src/contract/cx/context.rs +++ b/crates/ui-contract/src/contract/cx/context.rs @@ -153,6 +153,8 @@ pub struct CxCore<'a> { pub(crate) roving_arrival: Option, /// The member of the container's roving group that moves start from, set by `Cx::roving_lead`. pub(crate) roving_lead: Option, + /// The component joined the roving group of the body it is shown in this pass (`Cx::join_roving`). + pub(crate) roving_member: bool, /// The collection's selection (a `Selection`) and lead as the item step holds them, for `Cx::collection_selection`. pub(crate) collection_selection: Option>, /// The events and visible range the item step left for `collection_response`. @@ -222,13 +224,11 @@ impl Cx<'_, C> { crate::runtime::egui::output::open_url(&self.core.ctx, url, new_tab); } - /// Makes this component a member of the roving group of the body it is shown in: its stop joins the group and - /// Tab visits the group once. A component that never calls it keeps its own Tab stop. + /// Makes this component a member of the roving group of the body it is shown in: its stop and its focusable parts + /// join the group, and Tab visits the group once. Called from `update`: the stops register after it, in the same + /// pass. A component that does not call it in a pass keeps its own Tab stop in that pass. pub fn join_roving(&mut self) { - if let Some(group) = self.core.env.roving() { - crate::contract::steps::join_group(&self.core.ctx, group, self.core.id); - focus::registration::join_part_group(&self.core.ctx, group, self.core.id, crate::runtime::memory::sub(self.core.id, "roving")); - } + self.core.roving_member = true; } /// Runs `command` as this component's own activation would, for a command that is not the component's bound diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 883a18d7a..1e2676958 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -390,6 +390,7 @@ fn blank_core<'a>( reveal: None, member_tip: None, header_stops: Vec::new(), + roving_member: false, press_cancel: None, scroll_moves: Vec::new(), value_parts: Default::default(), @@ -696,7 +697,6 @@ struct StopsStep<'a> { rtl: bool, env: &'a Env, resolved: &'a Resolved<'a>, - inert: bool, holder_widget: Option, member: Option, own: Sense, @@ -705,7 +705,7 @@ struct StopsStep<'a> { /// Registers the focus stops of a component that has any; one that sets `STOPS = false` has none, which a debug build checks. fn stops_step(at: StopsStep<'_>, e: &mut dyn ErasedPass<'_>, facts: &TypeFacts, parts: &PartsStep) { - let StopsStep { ctx, id, rtl, env, resolved, inert, holder_widget, member, own, text_keys } = at; + let StopsStep { ctx, id, rtl, env, resolved, holder_widget, member, own, text_keys } = at; let body_roving = e.roving(); if !facts.stops && body_roving.is_none() { debug_assert!( @@ -730,15 +730,15 @@ fn stops_step(at: StopsStep<'_>, e: &mut dyn ErasedPass<'_>, facts: &TypeFacts, ctx, id, holder: holder_widget.unwrap_or_else(|| member.map_or(id, |arena| arena.0)), - focusable: !inert && steps::takes_focus(resolved) && own.contains(Sense::FOCUS) && env.item != Some(id), - parts_focus: !inert && steps::takes_focus(resolved), + focusable: steps::takes_focus(resolved) && own.contains(Sense::FOCUS) && env.item != Some(id), + parts_focus: steps::takes_focus(resolved), text, press_only: text && resolved.read_only && !facts.holds_cells(), keys, role: role.resettable(resettable).value(value).text(text).moves(role.moves || keys.moves).deletes(role.deletes || keys.deletes || text), rtl, parts, - group: steps::member_group(ctx, env.roving, id), + group: env.roving.filter(|_| e.core_ref().roving_member), body: env.roving, row: env.row, item: env.item == Some(id), @@ -850,7 +850,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h let scope = enter_scope(p, ui); let env = host_env.unwrap_or(p.env); if let Some(mut h) = sense_steps(p, ui, host_at, env) { - update_steps(p, &h); + update_steps(p, env, &h); overlay_step(p, ui, env, &h); state_steps(p, ui, env, &h); paint_step(p, ui, env, &mut h); @@ -880,8 +880,10 @@ struct HostStep { /// The component's own direction: right to left unless its axis keeps its order. rtl: bool, rect: Rect, - /// The parts step 7 declared. - decls: super::cx::Decls, + /// What step 7 declared: the parts, roving groups and text parts. The stops register from it after `update`. + parts: PartsStep, + /// The egui widget of the component's text part or editor, which holds key focus for it. + holder_widget: Option, text_keys: Option, own: Sense, member: Option, @@ -959,7 +961,6 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e let own = if env.item == Some(id) { own.difference(Sense::CLICK | Sense::FOCUS) | Sense::HOVER } else { own }; let arena = e.arena(); let member = if arena || facts.text() { None } else { env.arena }; - stops_step(StopsStep { ctx: &ctx, id, rtl, env, resolved, inert, holder_widget, member, own, text_keys }, e, facts, &parts); // A body's wheel share is its scroll taker (body step, step 7); the gate value does not take the wheel. A body root that // senses the wheel itself (a canvas) registers like any other component. A collection that senses the wheel itself // registers in the item step, with its notch. @@ -1078,7 +1079,8 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e id, rtl, rect, - decls: parts.decls, + parts, + holder_widget, text_keys, own, member, @@ -1096,7 +1098,7 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, e /// Step 9, update, with the body's roving group, which registers after `update`. // Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run. #[inline(never)] -fn update_steps(p: &mut Pass<'_, '_, '_>, h: &HostStep) { +fn update_steps(p: &mut Pass<'_, '_, '_>, env: &Env, h: &HostStep) { let (facts, ctx, id, rtl) = (p.facts, &h.ctx, h.id, h.rtl); let e = &mut *p.p; // 9 update @@ -1122,6 +1124,10 @@ fn update_steps(p: &mut Pass<'_, '_, '_>, h: &HostStep) { } e.updated(); steps::record_context(e.core_ref()); + // The stops register after `update`, which says whether the component joins its body's roving group. + let at = + StopsStep { ctx, id, rtl, env, resolved: p.resolved, holder_widget: h.holder_widget, member: h.member, own: h.own, text_keys: h.text_keys }; + stops_step(at, e, facts, &h.parts); // The body's group registers after `update`, which may change its rules from what the component knows now. if let Some(roving) = e.roving() { let core = e.core_ref(); @@ -1239,7 +1245,7 @@ fn content_step(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) /// `paint_over`, after the `over` parts are placed, so the children it shows stack above them (8.3). fn paint_over_step(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) { ui.set_clip_rect(h.outer_clip); - sensing::place_over_widgets(ui, h.id, h.member, h.decls.parts()); + sensing::place_over_widgets(ui, h.id, h.member, h.parts.decls.parts()); ui.set_clip_rect(h.inside); let e = &mut *p.p; let fade = steps::fade(ui, env, e.core()); @@ -1268,17 +1274,17 @@ fn report_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) facts, &steps::ReportIn { node: h.node, - decls: &h.decls, + decls: &h.parts.decls, sense: h.own, live: e.live(), scrolls: e.scrolls(), expanded: e.expanded(), position: e.position(), - group: steps::member_group(ctx, env.roving, id), + group: env.roving.filter(|_| e.core_ref().roving_member), }, ); let shows_label = e.shows_label(); - tooltip::run(e.core_ref(), tooltip::TipIn { ui: &*ui, env, rich: p.tip.take(), parts: &h.decls, shows_label, message }); + tooltip::run(e.core_ref(), tooltip::TipIn { ui: &*ui, env, rich: p.tip.take(), parts: &h.parts.decls, shows_label, message }); // Activations accumulate across the passes of a viewport mount; step 17 fires the bound command once, with the // injected source when there is one. let natural = e.activated() && resolved.enabled; diff --git a/crates/ui-contract/src/contract/steps.rs b/crates/ui-contract/src/contract/steps.rs index 3d4f3551e..eb75e855c 100644 --- a/crates/ui-contract/src/contract/steps.rs +++ b/crates/ui-contract/src/contract/steps.rs @@ -40,7 +40,7 @@ pub(crate) use scroll_owner::{child_reveal, peek_reveal, queue_child_reveal, rev pub(crate) use sense::{part_state, register_wheel}; pub(crate) use settle::settle; pub(crate) use state::{register_reset, state, value_flags}; -pub(crate) use stops::{StopsIn, body_group, join_group, member_group, register_stops, takes_focus}; +pub(crate) use stops::{StopsIn, body_group, register_stops, takes_focus}; pub(crate) use style::{style, style_cx}; pub(crate) use update::{after_update, edit_intents, gesture_gate, key_activation, record_context, secondary_click, takes_activation, update_inputs}; pub(crate) use value::{Shown, ShownState, peek_value, value}; diff --git a/crates/ui-contract/src/contract/steps/stops.rs b/crates/ui-contract/src/contract/steps/stops.rs index a5d30f20e..2511d07da 100644 --- a/crates/ui-contract/src/contract/steps/stops.rs +++ b/crates/ui-contract/src/contract/steps/stops.rs @@ -1,15 +1,13 @@ -//! Step 7's tail: the focus stops and the roving groups they join. +//! After step 9: the focus stops and the roving groups they join. use super::parts::PartsStep; use crate::contract::Resolved; -use crate::runtime::egui::memory as store; use crate::runtime::focus::{self, Group, Stop}; use crate::runtime::memory::sub; use craft_behavior::{DocKey, KeyRole, PartRef}; use egui::{Context, Id}; -/// The parts of last pass's complete set in which this pass's results are read; first-frame parts read idle. -/// Step 7's tail: the focus stops. The root's interest when the component or any part is focusable, its +/// The focus stops, registered after `update`: the root's interest when the component or any part is focusable, its /// `(id, part)` stops, and its roving groups. pub(crate) struct StopsIn<'a> { pub ctx: &'a Context, @@ -27,7 +25,8 @@ pub(crate) struct StopsIn<'a> { pub role: KeyRole, pub rtl: bool, pub parts: &'a PartsStep, - /// The roving group of the body this component is shown in: its own stop joins it. + /// The roving group of the body this component is shown in, when it joined it in this pass (`Cx::join_roving`): its + /// own stop and its focusable parts join it. pub group: Option, /// The roving group of the innermost body this component is shown in, whether or not it joined it. pub body: Option, @@ -42,22 +41,6 @@ pub(crate) struct StopsIn<'a> { } /// The id of the roving group a container's body forms. -/// Records that `id` is a member of the roving `group` in this pass. -pub(crate) fn join_group(ctx: &Context, group: Id, id: Id) { - let slot = sub(group, ("member", id)); - store::put(ctx, slot, crate::runtime::pointer::pass_of(ctx)); - crate::runtime::memory::adopt::(ctx, id, slot); -} - -/// `group` when `id` joined it in this pass or the one before, else `None`: a component that is no member keeps its -/// own Tab stop. -pub(crate) fn member_group(ctx: &Context, group: Option, id: Id) -> Option { - let group = group?; - let pass = crate::runtime::pointer::pass_of(ctx); - let joined = store::peek_clone::(ctx, sub(group, ("member", id)))?; - (joined.saturating_add(1) >= pass).then_some(group) -} - pub(crate) fn body_group(id: Id) -> Id { sub(id, "body_roving") } @@ -103,7 +86,7 @@ pub(crate) fn register_stops(input: &StopsIn<'_>) -> Vec { let stop = if extras.nudges { stop.role(input.role.nudge(true)) } else { stop }; let stop = if extras.default_stop { stop.default_stop() } else { stop }; // A part group that joined the body's roving group has its parts registered there. - let joined = group.and_then(|parts| focus::registration::part_group(ctx, parts)); + let joined = group.and(input.group); let stop = if let Some(group) = row_parts.or(joined).or(group).or(input.row) { stop.in_group(group) } else { stop }; if extras.after_header { after_header.push(stop); diff --git a/crates/ui-contract/src/runtime/env/view.rs b/crates/ui-contract/src/runtime/env/view.rs index 8e8deace2..42d7cb568 100644 --- a/crates/ui-contract/src/runtime/env/view.rs +++ b/crates/ui-contract/src/runtime/env/view.rs @@ -58,11 +58,6 @@ impl<'a> EnvView<'a> { self.scoped.measure } - /// The roving group of the innermost body with `ContainerProps.roving`. - pub(crate) fn roving(&self) -> Option { - self.scoped.roving - } - /// The inside label the scope carries ([`inside_label`](EnvView::inside_label) reads it back). pub(crate) fn inside_held(&self) -> Option<&'a Held> { self.scoped.inside_label.as_ref() diff --git a/crates/ui-contract/src/runtime/focus/registration.rs b/crates/ui-contract/src/runtime/focus/registration.rs index f2d9fb531..d251d2eda 100644 --- a/crates/ui-contract/src/runtime/focus/registration.rs +++ b/crates/ui-contract/src/runtime/focus/registration.rs @@ -180,33 +180,6 @@ impl Group { } } -/// What a component's part group joined: the enclosing body roving group and the pass that recorded it. -#[derive(Clone, Copy)] -struct PartJoin { - group: Id, - pass: u64, -} - -fn part_join_slot(parts: Id) -> Id { - sub(parts, "body_group") -} - -/// Records that the part group `parts` of component `id` joins the body roving group `group` in this pass: the -/// component's focusable parts become members of `group` in reading order, so Tab visits the group once and -/// arrows cross between the parts and the group's other members. -pub(crate) fn join_part_group(ctx: &Context, group: Id, id: Id, parts: Id) { - let slot = part_join_slot(parts); - store::put(ctx, slot, PartJoin { group, pass: crate::runtime::pointer::pass_of(ctx) }); - crate::runtime::memory::adopt::(ctx, id, slot); -} - -/// The body roving group the part group `parts` joined in this pass or the one before, else `None`: parts of a -/// component that never joined stay in their own part group. -pub(crate) fn part_group(ctx: &Context, parts: Id) -> Option { - let join = store::peek_clone::(ctx, part_join_slot(parts))?; - (join.pass.saturating_add(1) >= crate::runtime::pointer::pass_of(ctx)).then_some(join.group) -} - /// The innermost open `KeepAnchor` layer opened from the stop `opener`, the collection its opener hands list keys /// to (the stop with the collection's key role that is registered inside the layer's focus scope) and the layer's /// policy. `None` while no such layer is open or it has no collection. diff --git a/crates/ui-contract/src/testing/focus.rs b/crates/ui-contract/src/testing/focus.rs index eed6399f6..6ea67d6f4 100644 --- a/crates/ui-contract/src/testing/focus.rs +++ b/crates/ui-contract/src/testing/focus.rs @@ -159,3 +159,8 @@ pub fn register_node(ctx: &Context, node: Id, owner: Id, part: Option, pub fn context_request(ctx: &Context, id: Id) -> Option { keys::context_request(ctx, id) } + +/// The stops the running pass registered so far, in order: each stop's component and the roving group it is in. +pub fn pass_stop_groups(ctx: &Context) -> Vec<(Id, Option)> { + focus::with(ctx, |state| state.stops.iter().map(|rec| (rec.stop.id, rec.stop.group)).collect()) +} diff --git a/crates/ui-widgets/tests/it/bars/bar.rs b/crates/ui-widgets/tests/it/bars/bar.rs index 480a73dc3..1b6c76eaf 100644 --- a/crates/ui-widgets/tests/it/bars/bar.rs +++ b/crates/ui-widgets/tests/it/bars/bar.rs @@ -348,3 +348,25 @@ fn a_labelled_check_box_in_a_row_in_a_bar_lays_out_under_rtl() { } assert!(label.max.x <= alpha.min.x + SLACK, "the check box {label:?} follows Alpha {alpha:?} from the right"); } + +/// A tool bar's buttons are members of its roving group from the first pass that shows them: that pass registers the +/// stops in the groups every later pass registers them in. +#[trial] +fn a_tool_bar_is_one_roving_group_from_its_first_pass() { + let seen: Rc)>>>> = Rc::default(); + let passes = seen.clone(); + let mut fixture = Fixture::new(move |ui| { + Button::new("Before").show(ui); + Bar::new("Tools", BarKind::Tool).show(ui, |ui, _| { + IconButton::new(IconId::new("brush")).label("Brush").show(ui); + IconButton::new(IconId::new("eraser")).label("Eraser").show(ui); + }); + passes.borrow_mut().push(craft_ui::testing::focus::pass_stop_groups(ui.ctx())); + }); + fixture.settle(); + let seen = seen.borrow(); + let (first, settled) = (seen.first().expect("a pass ran"), seen.last().expect("a pass ran")); + let grouped = settled.iter().filter(|(_, group)| group.is_some()).count(); + assert!(grouped >= 2, "the tools are in the bar's roving group: {settled:?}"); + assert_eq!(first, settled, "the first pass registers the stops as a settled pass does"); +}