mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 15:21:17 -05:00
Selection: a press that hits nothing checks only masked selected art for a way to click it
When a press with the Selection, Direct Selection or Group Selection tool hits no object, `selected_hidden_at` looks for a selected object whose art a clipping mask hides under the pointer. It worked out how a click reaches each selected object (`path_options`, which searches every parent's children for the next node) before checking whether any mask hides it, and then discarded the result for every object no mask hides. It now checks the mask first and works out the click path only for masked art. With 3,000 selected squares and no clip, a press beside them made 3,000 `path_options` calls and now makes none. The objects found are unchanged.
This commit is contained in:
+15
-3
@@ -669,9 +669,7 @@ pub fn selected_hidden_at(doc: &Document, p: Point, opt: HitOptions, selected: &
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// How a click reaches it, if one does.
|
||||
let start = opt;
|
||||
let Some(opt) = path_options(&nodes, &chain, opt) else { return false };
|
||||
// Hidden by a mask: one of the containers around it clips, and not to `p`. A container
|
||||
// tests its clipping path in outline from the first layer with Preview off down to it, as
|
||||
// a click going down to it does ([`path_options`]).
|
||||
@@ -680,13 +678,19 @@ pub fn selected_hidden_at(doc: &Document, p: Point, opt: HitOptions, selected: &
|
||||
let outline = start.outline || nodes.get(below..=k).is_some_and(|ns| ns.iter().any(|n| previews_off(n)));
|
||||
n.clips() && n.children().and_then(|c| c.first()).is_some_and(|clip| clip.id != id && !clip_contains(clip, p, outline))
|
||||
});
|
||||
// Art no mask hides doesn't count, so how a click reaches it is not looked up.
|
||||
if !masked {
|
||||
return false;
|
||||
}
|
||||
// How a click reaches it, if one does.
|
||||
let Some(opt) = path_options(&nodes, &chain, start) else { return false };
|
||||
// A click may reach it there ([`may_hit`]: not a guide while guides are locked or
|
||||
// hidden, nor a Shaper group's hidden part), and its own art is what a click would hit
|
||||
// in it: a group's hidden, locked or masked contents don't count.
|
||||
let [.., parent, n] = nodes.as_slice() else { return false };
|
||||
let reachable =
|
||||
parent.children().and_then(|c| c.iter().position(|c| c.id == id)).is_some_and(|i| may_hit(parent, i, n, p, opt, &symbols, true));
|
||||
masked && reachable && object_hit(n, p, opt, &symbols, true)
|
||||
reachable && object_hit(n, p, opt, &symbols, true)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -695,6 +699,8 @@ pub fn selected_hidden_at(doc: &Document, p: Point, opt: HitOptions, selected: &
|
||||
/// below a layer whose Preview is off. `None` when a Shaper group's part it can't reach lies on
|
||||
/// the way ([`shaper_hides`]).
|
||||
fn path_options(nodes: &[&Node], chain: &[NodeId], opt: HitOptions) -> Option<HitOptions> {
|
||||
#[cfg(test)]
|
||||
PATHS_CHECKED.with(|c| c.set(c.get() + 1));
|
||||
let scope_at = opt.scope.and_then(|s| chain.iter().position(|a| *a == s));
|
||||
let child_index = |parent: &Node, child: &Node| parent.children().and_then(|c| c.iter().position(|c| c.id == child.id));
|
||||
let hidden = nodes.windows(2).skip(scope_at.unwrap_or(0)).any(|w| match w {
|
||||
@@ -710,6 +716,12 @@ fn path_options(nodes: &[&Node], chain: &[NodeId], opt: HitOptions) -> Option<Hi
|
||||
Some(if outline { HitOptions { outline: true, ..opt } } else { opt })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many objects' ancestries [`path_options`] went down on this thread.
|
||||
static PATHS_CHECKED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// The objects under `p` that [`Hit::top_object`] picks in `scope` (isolation mode), topmost
|
||||
/// first: what clicks there select, one below the other (Cmd/Ctrl-click selects behind).
|
||||
pub fn objects_at(doc: &Document, p: Point, opt: HitOptions, scope: Option<NodeId>) -> Vec<NodeId> {
|
||||
|
||||
@@ -1059,6 +1059,63 @@ fn a_selected_objects_art_hidden_by_a_mask_is_found_under_the_pointer() {
|
||||
assert_eq!(selected_hidden_at(&d, hidden, opt, &[art_id]), Some(art_id));
|
||||
}
|
||||
|
||||
/// How a click reaches a selected object, which searches its parents' children, is looked up
|
||||
/// only when a mask around it hides its art under the pointer: a press beside thousands of
|
||||
/// selected objects under no clip looks up none of them. Art that one of several nested clip
|
||||
/// groups hides is still found, also below thousands of selected objects no mask hides.
|
||||
#[test]
|
||||
fn only_selected_objects_a_mask_hides_are_checked_for_a_click_reaching_them() {
|
||||
let count = |d: &Document, p: Point, selected: &[NodeId]| {
|
||||
PATHS_CHECKED.with(|c| c.set(0));
|
||||
let found = selected_hidden_at(d, p, HitOptions::default(), selected);
|
||||
(found, PATHS_CHECKED.with(std::cell::Cell::get))
|
||||
};
|
||||
// `art` (0..200 × 0..100) in five nested clip groups, each clipping to 0..200 but the one at
|
||||
// `narrow` (counted from the inside), which clips to 0..100 and so hides the art at (150, 50).
|
||||
let nested = |narrow: Option<usize>| {
|
||||
let mut d = Document::new(300.0, 200.0);
|
||||
let l = d.layers[0].id;
|
||||
let art = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 100.0)), Appearance::default_art());
|
||||
let art_id = art.id;
|
||||
let mut inner = art;
|
||||
for depth in 0..5 {
|
||||
let right = if narrow == Some(depth) { 100.0 } else { 200.0 };
|
||||
let mask = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, right, 100.0)), Appearance::default());
|
||||
let mut g = Node::group(d.alloc_id(), vec![Arc::new(mask), Arc::new(inner)]);
|
||||
g.set_clips(true);
|
||||
inner = g;
|
||||
}
|
||||
d.insert(Some(l), 0, inner).unwrap();
|
||||
(d, l, art_id)
|
||||
};
|
||||
// 3,000 selected squares (0..10 × 0..10) on the layer, after whatever it holds.
|
||||
let squares = |d: &mut Document, l: NodeId| {
|
||||
let at = d.node(l).and_then(|n| n.children()).map_or(0, Vec::len);
|
||||
(0..3000)
|
||||
.map(|i| {
|
||||
let square = Node::path(d.alloc_id(), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::default_art());
|
||||
d.insert(Some(l), at + i, square).unwrap()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let p = Point::new(150.0, 50.0);
|
||||
let mut d = Document::new(300.0, 200.0);
|
||||
let l = d.layers[0].id;
|
||||
let flat = squares(&mut d, l);
|
||||
assert_eq!(count(&d, p, &flat), (None, 0), "a press beside them");
|
||||
assert_eq!(count(&d, Point::new(5.0, 5.0), &flat), (None, 0), "a press on them");
|
||||
let (d, _, art_id) = nested(None);
|
||||
assert_eq!(count(&d, p, &[art_id]), (None, 0), "no clip hides it");
|
||||
for depth in 0..5 {
|
||||
let (d, _, art_id) = nested(Some(depth));
|
||||
assert_eq!(count(&d, p, &[art_id]), (Some(art_id), 1), "hidden by the clip group {depth} levels out");
|
||||
}
|
||||
let (mut d, l, art_id) = nested(Some(2));
|
||||
let mut selected = vec![art_id];
|
||||
selected.extend(squares(&mut d, l));
|
||||
assert_eq!(count(&d, p, &selected), (Some(art_id), 1), "below the squares");
|
||||
}
|
||||
|
||||
/// A selected group's own art is what a click would hit in it: not a hidden or locked child, nor
|
||||
/// art its own clipping mask hides, all under the pointer where an outer mask hides them.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user