From 4953aafdf99bbb76adf2dfd7b6744b643c7a26fd Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 04:58:01 +0000 Subject: [PATCH] craft-ui: a root's widgets take this pass's member senses once its body has run Step 8 registers the root's own sense and its parts'. After paint_over, every member of its arena has joined, and the root's widget and its transformed body's forwarding widget take the members' senses (egui ORs the sense of a widget registered again in a pass). A press's untargeted check reads the root widget's sense from the pass egui's targets come from. The union of last pass's members goes. --- crates/ui-contract/src/contract/run.rs | 4 + crates/ui-contract/src/primitives/sensing.rs | 89 ++++++++++++------- .../ui-contract/src/runtime/egui/response.rs | 16 ++++ .../ui-contract/src/runtime/egui/widgets.rs | 6 ++ crates/ui-contract/src/testing/sensing.rs | 1 + 5 files changed, 84 insertions(+), 32 deletions(-) diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 87ac0fbb8..19e9a986f 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -1252,6 +1252,10 @@ fn paint_over_step(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostSte e.paint_over(ui, env); steps::unfade(ui, e.core(), fade); ui.set_clip_rect(h.outer_clip); + // Every member of the root's arena has joined: its widgets take their senses. + if h.member.is_none() { + sensing::widen_root(&h.ctx, h.id, p.facts.role == Role::None); + } } /// Steps 15 (settle) and 16 (report, then the tooltip), and the activation step 17 fires. diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 9dbdd7fda..9c20b5f70 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -252,13 +252,14 @@ struct ArenaState { /// The stack position of the root's own `over` parts: above the members its body showed and below the ones /// `paint_over` shows (`place_over_widgets`). Above everything until the owner has placed them. own_over_at: u32, - /// The senses and bounds of the members that joined this pass and last pass. + /// The senses of the members that joined this pass: the root's widgets take them once its body has run + /// (`widen_root`). union_sense: Sense, - previous_union_sense: Sense, + /// The root takes its members' senses: it was enabled at its step 8 this pass. + widens: bool, layer: Option, - /// The layer and the egui sense the root's widgets had at its last step 8, for `sense_early`. + /// The layer the root's widgets had at its last step 8, for `sense_early`. last_layer: Option, - last_sense: Sense, /// The pass whose outcome `sense_early` stepped before the root's step 8. early: u64, root: Option, @@ -305,7 +306,8 @@ fn rotate(state: &mut ArenaState, pass_nr: u64) { std::mem::swap(&mut state.previous_own, &mut state.own); state.own.clear(); state.own_over_at = u32::MAX; - state.previous_union_sense = std::mem::take(&mut state.union_sense); + state.union_sense = Sense::empty(); + state.widens = false; state.pass = pass_nr; } } @@ -433,28 +435,28 @@ impl crate::runtime::Shift for RootRegistration { } } -/// What a root registers in its arena at step 8, kept for a replay. +/// What a root registers in its arena at step 8, kept for a replay. The egui widgets, with the senses they take, +/// are in egui's pass records. #[derive(Clone, Debug, PartialEq)] struct RootRegistration { info: RootInfo, own: Vec, widgets: Vec, layer: Layer, - sense: Sense, } /// Notes a root's step-8 registration in the open records. -fn journal_root(ctx: &Context, arena: ArenaId, info: RootInfo, own: &[PartDecl], widgets: &[Id], (layer, sense): (Layer, Sense)) { +fn journal_root(ctx: &Context, arena: ArenaId, info: RootInfo, own: &[PartDecl], widgets: &[Id], layer: Layer) { crate::runtime::replay::journal( ctx, - || (arena, RootRegistration { info, own: own.to_vec(), widgets: widgets.to_vec(), layer, sense }), + || (arena, RootRegistration { info, own: own.to_vec(), widgets: widgets.to_vec(), layer }), |ctx, (arena, registration)| register_root_again(ctx, *arena, registration.clone()), ); } -/// A replayed root's step-8 registration: its own parts, rect, widgets, layer and sense, with no press run. +/// A replayed root's step-8 registration: its own parts, rect, widgets and layer, with no press run. fn register_root_again(ctx: &Context, arena: ArenaId, registration: RootRegistration) { - journal_root(ctx, arena, registration.info, ®istration.own, ®istration.widgets, (registration.layer, registration.sense)); + journal_root(ctx, arena, registration.info, ®istration.own, ®istration.widgets, registration.layer); let pass_nr = pass::pass_nr(ctx, pass::current_viewport(ctx)); with_arena(ctx, arena, pass_nr, |state| { rotate(state, pass_nr); @@ -462,7 +464,6 @@ fn register_root_again(ctx: &Context, arena: ArenaId, registration: RootRegistra state.root = Some(registration.info); state.widgets = registration.widgets; state.last_layer = Some(registration.layer); - state.last_sense = registration.sense; }); } @@ -549,7 +550,7 @@ impl PassRead { } /// Steps the press machine of the root `id` before its measuring run, from what last pass registered: the root rect, -/// widgets, layer and sense. A body reads the parts' presses of this pass in both of its runs through +/// widgets and layer. A body reads the parts' presses of this pass in both of its runs through /// [`part_press`]; step 8 finds the memoised outcome. Does nothing on a root's first pass and when the root was not /// drawn last pass. `enabled` is what is known before the root's step 5; step 8 ends a press of a root that turns out /// to be inert. @@ -563,11 +564,10 @@ pub(crate) fn sense_early(ctx: &Context, id: Id, enabled: bool) { rotate(state, read.pass_nr); let root = state.root?; let layer = state.last_layer?; - (root.rect.is_positive() && root.rect.is_finite() && !state.previous_own.is_empty()) - .then_some((layer, state.last_sense | state.previous_union_sense)) + (root.rect.is_positive() && root.rect.is_finite() && !state.previous_own.is_empty()).then_some(layer) }); - let Some((layer, sense)) = last else { return }; - outcome_in(ctx, &read, arena, layer, enabled, sense); + let Some(layer) = last else { return }; + outcome_in(ctx, &read, arena, layer, enabled); with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| state.early = read.pass_nr); } @@ -610,7 +610,8 @@ pub(crate) struct RootResult { pub activation: bool, /// The member a click activated. pub activated_member: Option, - /// The sense the root's egui widget carries. + /// The sense the root's egui widget carries at step 8: its own and its parts'. Its members' senses join it once + /// its body has run (`widen_root`). pub sense: Sense, } @@ -622,13 +623,9 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { let arena = ArenaId::new(spec.id); let read = PassRead::of(ctx); let (clip, layer) = (ui.clip_rect(), ui.layer_id()); - let last_union = with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| { - rotate(state, read.pass_nr); - state.previous_union_sense - }); // A disabled root only hovers, whatever its parts and members sense; it keeps the focus stop its own sense or a part's grants. let sense = if spec.enabled { - spec.own.iter().fold(spec.own_sense | last_union, |sense, part| sense | part.sense) + spec.own.iter().fold(spec.own_sense, |sense, part| sense | part.sense) } else { Sense::HOVER | spec.own.iter().fold(spec.own_sense, |sense, part| sense | part.sense).intersection(Sense::FOCUS) }; @@ -649,15 +646,16 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { let background = background_shape(spec.id, spec.rect, spec.own_sense | Sense::HOVER); pointer::record_widgets_in(ctx, read.viewport, layer, spec.id, &widgets, sense.contains(Sense::CLICK)); let info = RootInfo { id: spec.id, rect: spec.rect }; - journal_root(ctx, arena, info, spec.own, &widgets, (layer, sense)); + journal_root(ctx, arena, info, spec.own, &widgets, layer); // The parts last pass registered, which this pass's results read. let known = with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| { + rotate(state, read.pass_nr); + state.widens = spec.enabled; state.own.clear(); state.own.extend_from_slice(spec.own); state.root = Some(info); state.widgets = widgets; state.last_layer = Some(layer); - state.last_sense = sense; // A root that stepped early while it was not yet known to be inert: an open press ends as the disabled root's // would, by stepping again with the cancel. if !spec.enabled && state.early == read.pass_nr && state.outcome.is_some_and(|outcome| outcome.pass == read.pass_nr) { @@ -674,7 +672,7 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { std::iter::once(background).chain(own_shapes(spec.own, spec.id, read.target())), ); let mut response = response; - let outcome = outcome_in(ctx, &read, arena, layer, spec.enabled, sense); + let outcome = outcome_in(ctx, &read, arena, layer, spec.enabled); // The root is hovered while a shape of its arena shows hover (`pointer::Under`): its background, a part or a member; // it contains the pointer while any shape of the arena lies under it. response::hover_flags(&mut response, outcome.hover.any(), outcome.contains); @@ -742,6 +740,33 @@ pub(crate) fn place_over_widgets(ui: &Ui, id: Id, member: Option, decls } } +/// The root `id`, once its body and `paint_over` have run, takes the senses of the members that joined its arena this +/// pass into its egui widget and its transformed body's forwarding widget (egui adds the sense of a widget registered +/// again in a pass), so egui's hit test in the next pass names the root wherever a member takes a press. A root whose +/// widget has no node (`hidden`) keeps it hidden. Nothing for a disabled root, which only hovers, or a root with no +/// arena this pass. +pub(crate) fn widen_root(ctx: &Context, id: Id, hidden: bool) { + let read = PassRead::of(ctx); + let found = PerViewport::::with(ctx, read.viewport, |arenas| { + arenas.by_root.get(&id).filter(|state| state.pass == read.pass_nr && state.widens).map(|state| (state.union_sense, state.last_layer)) + }); + let Some((union, Some(layer))) = found else { return }; + if union.is_empty() { + return; + } + let sense = response::to_egui(union); + if response::widen(ctx, id, sense) && hidden { + crate::runtime::egui::access::hide(ctx, id); + } + let forward = sub(id, "transform"); + if response::widen(ctx, forward, sense) { + crate::runtime::egui::access::hide(ctx, forward); + } + if union.contains(Sense::CLICK) { + pointer::record_widgets_in(ctx, read.viewport, layer, id, &[], true); + } +} + /// Step 8 for a root with no sense, no parts and no members: nothing can hit it, so it has no arena entry, shape /// record or press lifecycle, only its egui widget and its entry in the widget record. pub(crate) fn sense_none(ui: &Ui, id: Id, rect: Rect) -> RootResult { @@ -841,9 +866,8 @@ fn press_view(outcome: &Outcome, holds: impl Fn(&Hit) -> bool, clicks: impl Fn(& } /// Runs the press lifecycle for this pass, once: later calls in the pass return the same outcome. A repeated -/// pass has no events, so phases hold and a release, click or activation does not repeat. `union_sense` is -/// what the root's egui widgets sense. -fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enabled: bool, union_sense: Sense) -> Outcome { +/// pass has no events, so phases hold and a release, click or activation does not repeat. +fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enabled: bool) -> Outcome { let pass_nr = read.pass_nr; let done = with_arena_in(ctx, read.viewport, arena, pass_nr, |state| state.outcome.filter(|outcome| outcome.pass == pass_nr)); if let Some(outcome) = done { @@ -889,12 +913,13 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab // is on top under it for that kind (`pointer::press_winners`); the root starts it for the kinds it won. A // root under a modal layer starts none; where egui names no target of the root's kind (its click target, // its drag target for a drag-only root; a root whose widgets sense neither is never named), only a root the - // modal's canvas admits does. + // modal's canvas admits does. The root's kind is what its widget sensed in the pass egui's targets come from. let root = state.root.map(|root| root.id); let targets = pass_pointer.press_targets; - let untargeted = if union_sense.contains(Sense::CLICK) { + let sensed = crate::runtime::egui::widgets::sensed_last_pass(ctx, arena.0); + let untargeted = if sensed.senses_click() { targets.click.is_none() - } else if union_sense.contains(Sense::DRAG) { + } else if sensed.senses_drag() { targets.drag.is_none() } else { true diff --git a/crates/ui-contract/src/runtime/egui/response.rs b/crates/ui-contract/src/runtime/egui/response.rs index 342292d53..901a14f2f 100644 --- a/crates/ui-contract/src/runtime/egui/response.rs +++ b/crates/ui-contract/src/runtime/egui/response.rs @@ -49,6 +49,22 @@ pub(crate) fn widget(ui: &Ui, id: Id, rect: Rect, sense: Sense) -> Response { response } +/// Adds `sense` to the egui widget `id` the running pass registered, at its rect: egui ORs the senses of a widget +/// registered again in a pass. Returns whether the widget took a sense it lacked; nothing for a widget the pass has +/// not registered, or one with no area, which egui's hit test never names. +pub(crate) fn widen(ctx: &egui::Context, id: Id, sense: egui::Sense) -> bool { + let registered = ctx.viewport(|viewport| viewport.this_pass.widgets.get(id).map(|widget| (widget.sense, widget.rect))); + let Some((has, rect)) = registered else { return false }; + if has.contains(sense) || !(rect.is_positive() && rect.is_finite()) { + return false; + } + // `read_response` answers from the running pass for a registered widget with a positive rect. + ctx.read_response(id).is_some_and(|response| { + let _ = response.interact(sense); + true + }) +} + /// Whether a pointer button went down on `response`'s widget and is still down. pub(crate) fn is_pressed(response: &Response) -> bool { response.is_pointer_button_down_on() diff --git a/crates/ui-contract/src/runtime/egui/widgets.rs b/crates/ui-contract/src/runtime/egui/widgets.rs index 36596c0da..c9c5a160c 100644 --- a/crates/ui-contract/src/runtime/egui/widgets.rs +++ b/crates/ui-contract/src/runtime/egui/widgets.rs @@ -41,6 +41,12 @@ pub(crate) fn above(ctx: &Context, below: Id, skip: &[Id]) -> Vec { found } +/// The sense of the widget `id` in the last finished pass, whose widget rects egui's hit test reads for the running +/// pass's press targets. Hover only when that pass did not show `id`. +pub(crate) fn sensed_last_pass(ctx: &Context, id: Id) -> egui::Sense { + ctx.widget_rect_last_pass(id).map_or(egui::Sense::hover(), |widget| widget.sense) +} + /// An egui widget the runtime makes for a purpose no component registers (a scrim that absorbs presses). #[cfg(feature = "testing")] pub(crate) fn note_widget(ctx: &Context, id: Id) { diff --git a/crates/ui-contract/src/testing/sensing.rs b/crates/ui-contract/src/testing/sensing.rs index f9d54b99c..e68ec0229 100644 --- a/crates/ui-contract/src/testing/sensing.rs +++ b/crates/ui-contract/src/testing/sensing.rs @@ -365,6 +365,7 @@ fn show_root(ui: &mut Ui, ctx: &Context, root: &ProbeRoot) -> RootReport { }; // The owner places its `over` parts once its body has shown: above its members. sensing::place_over_widgets(ui, id, None, &decls); + sensing::widen_root(ctx, id, false); RootReport { root: SenseReport { id,