From a885cd5f384f835b672bd8248508af9cfbc325af Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 27 Nov 2026 12:14:56 +0100 Subject: [PATCH] ui-contract: the pointer-move gate compares each root's hover and press winners Overlapping shapes of one root can hit both points of a move while the nearest centre picks another winner; the move now runs a pass then. Co-Authored-By: Claude Opus 5.5 --- crates/ui-contract/src/runtime/pointer.rs | 34 ++++++++++++------- .../ui-contract/tests/it/runtime/pointer.rs | 19 +++++++++++ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index 5ab0a02679..37f2f59501 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -695,32 +695,40 @@ pub fn move_needs_pass(ctx: &Context, viewport: ViewportId, from: Option, follows || ctx.widgets_at(viewport, from) != ctx.widgets_at(viewport, to) || shapes_under(ctx, viewport, from) != shapes_under(ctx, viewport, to) } -/// The layer under `at` and the component shapes there that hit it, as the last finished pass of `viewport` recorded -/// them: each as its root and its index in the root's record. -fn shapes_under(ctx: &Context, viewport: ViewportId, at: Pos2) -> (Option, Vec<(Id, usize)>) { +/// The layer under `at` and, for each root with component shapes there that hit it as the last finished pass of +/// `viewport` recorded them, the root, the indices of those shapes in the root's record and the ones that win its hover +/// and its press: overlapping shapes can hit both points while the nearest centre picks another winner. +fn shapes_under(ctx: &Context, viewport: ViewportId, at: Pos2) -> (Option, Vec) { let Some(layer) = areas::layer_at(ctx, at) else { return (None, Vec::new()) }; let point = areas::layer_map(ctx, layer).map_or(at, |map| map.to_layer(at)); let hit = PerViewport::::with(ctx, viewport, |record| { record.this.get(&layer).map_or_else(Vec::new, |roots| { - let mut hit: Vec<(Id, usize)> = roots + let mut hit: Vec = roots .iter() - .flat_map(|(root, entry)| { - entry - .shapes - .iter() - .zip(&entry.placed) - .enumerate() - .filter(|(_, (shape, placed))| placed.clip.contains(point) && shape.hits(point)) - .map(move |(index, _)| (*root, index)) + .filter_map(|(root, entry)| { + let indices: Vec = (0..entry.shapes.len().min(entry.placed.len())) + .filter(|&index| entry.placed[index].clip.contains(point) && entry.shapes[index].hits(point)) + .collect(); + if indices.is_empty() { + return None; + } + let shapes: Vec> = indices.iter().map(|&index| entry.shapes[index].clone()).collect(); + let winner = |kind| resolve(&shapes, point, kind).and_then(|winner| indices.get(winner.index).copied()); + let (hover, press) = (winner(ResolveFor::Hover), winner(ResolveFor::Press)); + Some((*root, indices, hover, press)) }) .collect(); - hit.sort_unstable_by_key(|(root, index)| (root.value(), *index)); + hit.sort_unstable_by_key(|(root, ..)| root.value()); hit }) }); (Some(layer), hit) } +/// A root with shapes under a point ([`shapes_under`]): the root, the indices of its shapes that hit the point, and +/// the indices of the ones that win its hover and its press there. +type UnderRoot = (Id, Vec, Option, Option); + /// 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) { diff --git a/crates/ui-contract/tests/it/runtime/pointer.rs b/crates/ui-contract/tests/it/runtime/pointer.rs index 282b8e153c..ac0c06eef2 100644 --- a/crates/ui-contract/tests/it/runtime/pointer.rs +++ b/crates/ui-contract/tests/it/runtime/pointer.rs @@ -889,3 +889,22 @@ fn a_move_that_changes_nothing_under_the_pointer_needs_no_pass() { assert!(needs(Some(follow.center()), follow.center() + Vec2::new(5.0, 0.0)), "a move in a rect that follows the pointer runs a pass"); assert!(needs(None, inside), "the pointer coming in runs a pass"); } + +/// Two overlapping parts of one root that hit both points of a move, where the nearest centre picks another part at +/// the end: the hover changes, so the move runs a pass. +#[trial] +fn a_move_that_changes_the_nearest_part_needs_a_pass() { + use craft_ui::runtime::pointer::move_needs_pass; + use egui::ViewportId; + const LEFT: PartKey = PartKey::new("left"); + const RIGHT: PartKey = PartKey::new("right"); + let root = ProbeRoot::new("parts", rect(0.0, 0.0, 140.0, 100.0), Sense::empty()) + .part(ProbePart::new(LEFT, rect(0.0, 0.0, 100.0, 100.0), Sense::CLICK)) + .part(ProbePart::new(RIGHT, rect(40.0, 0.0, 100.0, 100.0), Sense::CLICK)); + let (mut show, _) = scene(vec![root]); + let mut frames = Frames::new(); + frames.idle(2, &mut show); + let needs = |from: Pos2, to: Pos2| move_needs_pass(&frames.ctx, ViewportId::ROOT, Some(from), to); + assert!(!needs(Pos2::new(60.0, 50.0), Pos2::new(65.0, 50.0)), "both points nearer the left part's centre"); + assert!(needs(Pos2::new(60.0, 50.0), Pos2::new(80.0, 50.0)), "the right part's centre is nearer at the end"); +}