From 8c66b02cf18b7cf77e5120db1ebfec597fd4590f Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 11:03:06 +0000 Subject: [PATCH] Pointer-over readers and component text widgets in the one pointer resolution The roots that contain the pointer are found whatever egui's click target is. A widget a component made for its own text (a field's text, a selection part) is recorded with its component: a press egui gives it stays the widget's, and the hover goes to that component's topmost shape. Collection rows report pointer_over beside hovered, and the document tab strip names the tab under the pointer by it, so a drop target reads it while another root holds the press. DIAG (CRAFT_DIAG_PRESS) prints each resolution's stack and answer. --- .../src/contract/collection/props.rs | 5 +- .../ui-contract/src/contract/items/hooks.rs | 5 +- crates/ui-contract/src/primitives/sensing.rs | 4 +- .../src/primitives/text_adapter.rs | 2 + crates/ui-contract/src/runtime/pointer.rs | 102 ++++++++++++++---- crates/ui-widgets/src/tabs/document.rs | 2 +- 6 files changed, 92 insertions(+), 28 deletions(-) diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index bcbedcddf..081ae575b 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -616,8 +616,11 @@ pub struct RowState { pub striped: bool, /// A tree row whose children show. pub open: bool, - /// The pointer is over the row. + /// The pointer is over the row and a press there would reach it. pub hovered: bool, + /// The pointer is over the row, whether or not a press there would reach it: a drop target reads it while another + /// component holds the press. + pub pointer_over: bool, /// The collection holds a press on the row, which the item shows on the part it acts on. pub pressed: Option, /// The collection asked for this row's menu and a menu hung from the collection is open: the row keeps its menu diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index fbdeaeba8..d2d16758c 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -501,7 +501,9 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { } // The pointer counts as over a row only while the sensing pass puts it on this collection, so a popup or another // widget above it takes the hover as it does for the rows' parts. - let hover_at = ctx.pointer_latest_pos().filter(|_| crate::primitives::sensing::read_outcome(&ctx, ArenaId::new(cx.core.id)).hover.any()); + let outcome = crate::primitives::sensing::read_outcome(&ctx, ArenaId::new(cx.core.id)); + let hover_at = ctx.pointer_latest_pos().filter(|_| outcome.hover.any()); + let over_at = ctx.pointer_latest_pos().filter(|_| outcome.contains); let menu_open = self.drags.menu.is_some() && crate::contract::overlay::container_open::opened_from(&ctx, cx.core.id); // A double click on a column's edge fits the column to its widest cell among the rows built here, in view. let fit = self.header.fit.as_ref().map(|(column, _)| column.clone()); @@ -539,6 +541,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { current: current.as_ref() == Some(&key), open: self.source.has_children(at) && self.source.open(at), hovered: hover_at.is_some_and(|pos| row.contains(pos)), + pointer_over: over_at.is_some_and(|pos| row.contains(pos)), pressed: self.pressed.as_ref().filter(|(held, _)| *held == at).map(|(_, pressed)| pressed.clone()), menu_open: self.drags.menu.as_ref() == Some(&key) && menu_open, sub_row: self.source.sub_row(at), diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 5e9471488..8a6fe8269 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -202,7 +202,7 @@ impl From> for Hit { } /// The arena's shapes that show hover this pass, from what lies under the pointer (`pointer::Under`): the shape a -/// press at the pointer goes to, and the topmost shape under the pointer when it is the arena's and takes no press. +/// press at the pointer goes to, and the arena's shape that hovers beside it (`Under::hover_top`). #[derive(Clone, Copy, Debug, Default)] pub(crate) struct Hovered { press: Option, @@ -840,7 +840,7 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab let (under, contains, captured) = pointer::with_hover(ctx, read.viewport, |under, captured| { let hovered = under.map_or(Hovered::default(), |under| Hovered { press: under.press.hit_of(arena.0).map(Hit::from), - only: under.hover_only_of(arena.0).map(Hit::from), + only: under.hover_top_of(arena.0).map(Hit::from), }); (hovered, under.is_some_and(|under| under.contains(arena.0)), captured) }); diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index 980eee3ee..3217d7577 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -431,6 +431,7 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut let secondary = crate::runtime::pointer::frame(ctx).press.is_some_and(|press| press.button == craft_behavior::PointerButton::Secondary); let kept = selection(ctx, widget).filter(|(caret, anchor)| secondary && caret != anchor); let shown = text_edit::show(input.ui, edit); + crate::runtime::pointer::record_text_widget(ctx, widget, input.id); if let Some((caret, anchor)) = kept && selection(ctx, widget).is_some_and(|(at, _)| (caret.min(anchor)..=caret.max(anchor)).contains(&at)) { @@ -541,6 +542,7 @@ pub(crate) fn show_selectable(ui: &mut Ui, text: Selectable<'_>) { baseline: None, }, ); + crate::runtime::pointer::record_text_widget(&ctx, widget, owner); if crate::runtime::egui::access::enabled(&ctx, widget) { crate::runtime::egui::access::fold_widget(&ctx, widget, owner); } diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index dc6de4ed8..6dbeee56c 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -132,6 +132,10 @@ pub(crate) struct Shapes { last: LayerRecord, this_bodies: BodyRecord, last_bodies: BodyRecord, + /// The egui widgets components made for their own text (a field's text, a selection part) in the running pass and + /// the last, each with its component: such a widget takes its press itself. + this_texts: IdMap, + last_texts: IdMap, /// The paint order the running pass's next registration takes. next: u32, /// What lies under the pass's hover point, found over `last` at the begin hook with the pass's egui targets: @@ -213,6 +217,7 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) { PerViewport::::with(ctx, viewport, |shapes| { shapes.last = std::mem::take(&mut shapes.this); shapes.last_bodies = std::mem::take(&mut shapes.this_bodies); + shapes.last_texts = std::mem::take(&mut shapes.this_texts); shapes.next = 0; shapes.hover = None; shapes.winner = None; @@ -371,6 +376,15 @@ pub(crate) fn record_shapes_in(ctx: &Context, viewport: ViewportId, layer: Layer }); } +/// Records the egui widget `widget` that the component `owner` made for its own text (a field's text, a selection +/// part): a press egui gives it is the widget's own and goes to no root, and the hover goes to `owner`. +pub(crate) fn record_text_widget(ctx: &Context, widget: Id, owner: Id) { + crate::runtime::replay::journal(ctx, || move |ctx: &Context| record_text_widget(ctx, widget, owner)); + PerViewport::::with(ctx, pass::current_viewport(ctx), |record| { + record.this_texts.insert(widget, owner); + }); +} + /// Records that `layer` shows the transformed body of the root `root`, which is itself shown on `parent`: a press on /// the body layer reaches the root's own shapes beneath the body's. pub(crate) fn record_body_layer(ctx: &Context, layer: Layer, parent: Layer, root: Id) { @@ -592,8 +606,10 @@ impl PressWinners { /// transformed body's layer shows its root's own shapes beneath everything on it. A shape counts only where its clip /// shows it; at a point no clip on the layer holds (one automation names past the window), a shape counts when some of /// it shows inside its clip. The members of an arena whose host withholds them (`withhold_members`) do not count. -/// Where egui's hit test gives the point to a widget no component registered (a raw egui widget on top), nothing is -/// under it. +/// Where egui's hit test gives the point to a widget no component made (a raw egui widget on top), no press or hover +/// goes to a root; where it gives it to a widget a component made for its own text, the press is that widget's and the +/// hover goes to that component's topmost shape under the point. The roots that contain the point are those with a +/// shape under it either way. /// /// Hover shows where a press goes: the press winners' shapes hover, and so does the topmost shape under the point when /// it takes no press (a label with a tooltip over a clickable row). A shape that takes a press and loses it does not. @@ -601,16 +617,17 @@ impl PressWinners { pub(crate) struct Under { /// The roots a press at the point starts on. pub press: PressWinners, - /// The topmost shape under the point, with its root, when it takes no press. - pub hover_only: Option<(Winner, Id)>, + /// The shape that hovers beside the press winners, with its root: the topmost shape under the point when it takes + /// no press, or the topmost one of the component whose text widget takes the press. + pub hover_top: Option<(Winner, Id)>, /// Every root with a shape under the point, on top or not: the roots that contain the pointer. pub over: SmallVec<[Id; 4]>, } impl Under { - /// The topmost shape under the point when it is `root`'s and takes no press. - pub(crate) fn hover_only_of(&self, root: Id) -> Option> { - self.hover_only.filter(|(_, owner)| *owner == root).map(|(winner, _)| winner) + /// The shape of `root` that hovers beside the press winners ([`Under::hover_top`]). + pub(crate) fn hover_top_of(&self, root: Id) -> Option> { + self.hover_top.filter(|(_, owner)| *owner == root).map(|(winner, _)| winner) } /// Whether a shape of `root` lies under the point. @@ -682,12 +699,12 @@ fn find_under(ctx: &Context, viewport: ViewportId, at: Pos2, targets: PressTarge stack.extend(roots.iter().map(|(root, entry)| (1, *root, entry, on_layer))); } let shown = stack.iter().any(|(_, _, entry, point)| entry.placed.iter().any(|placed| placed.clip.contains(*point))); - if targets.click.is_some_and(|widget| !registered_in(&record.last, widget)) { - return Under::default(); - } + let text_of = targets.click.and_then(|widget| record.last_texts.get(&widget).copied()); + let foreign = text_of.is_none() && targets.click.is_some_and(|widget| !registered_in(&record.last, widget)); let mut under = Under::default(); - // The topmost shape under the point, and each root's topmost shape that takes a press, ranked by tier and then - // by the place in paint order of the registration that recorded it. + // The topmost shape under the point (of the text widget's component, when one takes the press), and each root's + // topmost shape that takes a press, ranked by tier and then by the place in paint order of the registration + // that recorded it. let mut top: Option<((u8, u32), Winner, Id)> = None; let mut ranked = Vec::new(); for (tier, root, entry, point) in stack { @@ -705,22 +722,59 @@ fn find_under(ctx: &Context, viewport: ViewportId, at: Pos2, targets: PressTarge if !under.over.contains(&root) { under.over.push(root); } - let shapes: Vec> = kept.iter().map(|&index| entry.shapes[index].clone()).collect(); - let placed = |mut winner: Winner| { - winner.index = kept[winner.index]; - ((tier, entry.placed[winner.index].order), winner) + // The winner among the shapes at `indices` by `kind`, with its rank. + let pick = |indices: &[usize], kind: ResolveFor| { + let shapes: Vec> = indices.iter().map(|&index| entry.shapes[index].clone()).collect(); + resolve(&shapes, point, kind).map(|mut winner| { + winner.index = indices[winner.index]; + ((tier, entry.placed[winner.index].order), winner) + }) }; - if let Some((rank, winner)) = resolve(&shapes, point, ResolveFor::Hover).map(placed) + let owned: Vec; + let hovering: &[usize] = match text_of { + Some(owner) => { + owned = kept.iter().copied().filter(|&index| entry.shapes[index].owner == owner).collect(); + &owned + } + None => &kept, + }; + if let Some((rank, winner)) = pick(hovering, ResolveFor::Hover) && top.as_ref().is_none_or(|(best, ..)| rank > *best) { top = Some((rank, winner, root)); } - if let Some((rank, winner)) = resolve(&shapes, point, ResolveFor::Press).map(placed) { + if let Some((rank, winner)) = pick(&kept, ResolveFor::Press) { ranked.push((rank, winner, root, entry.click)); } } - under.press = winners_of(ranked); - under.hover_only = top.filter(|(_, winner, _)| !winner.sense.takes_press()).map(|(_, winner, root)| (winner, root)); + // DIAG FW-277 round O: every resolution, its stack and its answer, with CRAFT_DIAG_PRESS set. + let diag = std::env::var_os("CRAFT_DIAG_PRESS").is_some(); + if diag { + eprintln!( + "DIAG under at={at:?} layer={layer:?} targets={targets:?} text_of={text_of:?} foreign={foreign} withheld={withheld:?} over={:?}", + under.over + ); + for (rank, winner, root, clicks) in &ranked { + eprintln!( + "DIAG press-candidate root={root:?} rank={rank:?} owner={:?} part={:?} sense={:?} root_clicks={clicks}", + winner.owner, winner.part, winner.sense + ); + } + } + if foreign { + return under; + } + if text_of.is_none() { + under.press = winners_of(ranked); + } + if diag { + eprintln!( + "DIAG press={:?} top={:?}", + under.press, + top.as_ref().map(|(rank, winner, root)| (rank, winner.owner, winner.part, winner.sense, root)) + ); + } + under.hover_top = top.filter(|(_, winner, _)| text_of.is_some() || !winner.sense.takes_press()).map(|(_, winner, root)| (winner, root)); under }) } @@ -792,10 +846,12 @@ fn registered_in(layers: &LayerRecord, widget: Id) -> bool { layers.values().any(|roots| roots.values().any(|root| root.widgets.contains(&widget))) } -/// Whether the egui widget `widget`, one of the latest press's targets, is one no component registered in the pass -/// the press was hit-tested against: a raw egui widget. +/// Whether the egui widget `widget`, one of the latest press's targets, is one no component registered or made for its +/// text in the pass the press was hit-tested against: a raw egui widget. pub(crate) fn foreign_target(ctx: &Context, widget: Id) -> bool { - PerViewport::::with(ctx, pass::current_viewport(ctx), |record| !registered_in(&record.last, widget)) + PerViewport::::with(ctx, pass::current_viewport(ctx), |record| { + !registered_in(&record.last, widget) && !record.last_texts.contains_key(&widget) + }) } /// Every shape of the last pass on the topmost layer at `at` (screen points) whose area contains it, as diff --git a/crates/ui-widgets/src/tabs/document.rs b/crates/ui-widgets/src/tabs/document.rs index e09de027c..340dc95b7 100644 --- a/crates/ui-widgets/src/tabs/document.rs +++ b/crates/ui-widgets/src/tabs/document.rs @@ -242,7 +242,7 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen let hovered = std::cell::RefCell::new(None); let show_strip = |ui: &mut Ui, strip: TabStrip<'_, D>| { strip.show_items(ui, &source, &mut |key, cx| { - if cx.row.hovered { + if cx.row.pointer_over { *hovered.borrow_mut() = Some(key.clone()); } let Some(document) = tabs.iter().find(|tab| &tab.id == key) else {