mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
de45e61fad
commit
a885cd5f38
2 files changed
+40
-13
No files matched your search
@@ -695,32 +695,40 @@ pub fn move_needs_pass(ctx: &Context, viewport: ViewportId, from: Option<Pos2>,
|
||||
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<Layer>, 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<Layer>, Vec<UnderRoot>) {
|
||||
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::<Shapes>::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<UnderRoot> = 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<usize> = (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<Shape<Id>> = 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<usize>, Option<usize>, Option<usize>);
|
||||
|
||||
/// 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) {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in new issue
Block a user