Selection: a click or hover that hits nothing walks the document once

A hit test for the selection tools first walks the document without
looking inside unfilled closed paths, and when that finds nothing it
walked the whole document again to look inside them. With the pointer
resting over empty pasteboard that doubled the hit walks on every
frame.

The first walk now notes whether an unfilled closed path holds the
point, and the second walk runs only when one does. Select Behind
(objects_at) and Direct Selection in a dynamic symbol's art
(symbol_part_at) skip their second pass the same way. Hit tests that
do not look inside unfilled paths measure no interiors.
This commit is contained in:
dotCooCoo committed 2026-11-29 09:45:21 -08:00
1 parent 619a6e5ed1
commit 9aa6f7cdae
2 files changed
+161 -8

No files matched your search

+79 -8
View File
@@ -446,6 +446,66 @@ impl Drop for InMask {
}
}
thread_local! {
/// While an [`InteriorProbe`] lives, whether a leaf tested without
/// [`HitOptions::unfilled_interiors`] would have been hit inside an unfilled closed path with
/// it; `None` outside a probe.
static INTERIOR_SEEN: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
}
/// While a guard lives, the leaves tested without [`HitOptions::unfilled_interiors`] note whether
/// they would be hit inside an unfilled closed path with it ([`unfilled_inside`]). A walk that hits
/// nothing then looks inside the unfilled closed paths with a second walk only when one of them
/// holds the point.
struct InteriorProbe {
was: Option<bool>,
}
impl InteriorProbe {
/// A probe when `wanted`, the walk that follows a miss looking inside unfilled closed paths.
/// Otherwise the guard leaves things as they are, and no leaf measures an interior for it.
fn open(wanted: bool) -> Self {
Self { was: if wanted { INTERIOR_SEEN.replace(Some(false)) } else { INTERIOR_SEEN.get() } }
}
/// Did a leaf tested since [`Self::open`] have an unfilled closed path holding the point?
fn seen(&self) -> bool {
INTERIOR_SEEN.get() == Some(true)
}
}
impl Drop for InteriorProbe {
fn drop(&mut self) {
// A probe opened inside another passes what it saw on to it.
let seen = INTERIOR_SEEN.get() == Some(true);
INTERIOR_SEEN.set(self.was.map(|was| was || seen));
}
}
/// The hit inside path or compound path `n`'s closed subpaths, which have no visible fill, when
/// `n` takes one there ([`takes_interior`]) and `contains` says they hold the point. Without
/// [`HitOptions::unfilled_interiors`], under an [`InteriorProbe`], notes whether `n` would take
/// one with it, and hits nothing.
fn unfilled_inside(n: &Node, opt: HitOptions, contains: impl FnOnce() -> bool) -> Option<HitKind> {
if takes_interior(n, opt) {
return contains().then_some(HitKind::Fill);
}
if INTERIOR_SEEN.get() == Some(false) && takes_interior(n, HitOptions { unfilled_interiors: true, ..opt }) {
#[cfg(test)]
INTERIORS_PROBED.with(|c| c.set(c.get() + 1));
if contains() {
INTERIOR_SEEN.set(Some(true));
}
}
None
}
#[cfg(test)]
thread_local! {
/// How many interiors [`unfilled_inside`] measured for an [`InteriorProbe`] on this thread.
static INTERIORS_PROBED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// `f()`, with `mask` under a [`DrawnOff`] guard: the clipping path of a clip group or of a
/// clipping layer is clicked and touched by its own geometry, whatever its geometry effects do to
/// the region it clips to. It is also under an [`InMask`] guard.
@@ -667,10 +727,15 @@ pub fn objects_at(doc: &Document, p: Point, opt: HitOptions, scope: Option<NodeI
let locks = doc.locked_artboards_kept();
let symbols = doc.kept_symbol_boxes().keeping(&locks.symbols);
// As [`hit_test`] does: the objects hit by what they paint first, then those hit only inside
// an unfilled closed path ([`HitOptions::unfilled_interiors`]).
// an unfilled closed path ([`HitOptions::unfilled_interiors`]), when the first pass passes one
// holding `p` ([`InteriorProbe`]).
let painted = HitOptions { unfilled_interiors: false, ..opt };
let interiors = opt.unfilled_interiors && !opt.path_only && !opt.outline;
let mut probe = Some(InteriorProbe::open(interiors));
'passes: for pass in [Some(painted), interiors.then_some(opt)].into_iter().flatten() {
if pass.unfilled_interiors && !probe.take().is_some_and(|probe| probe.seen()) {
break;
}
for layer in doc.layers.iter().rev() {
if !layer.visible || layer.locked || layer.is_template() || doc.pattern_edit.as_ref().is_some_and(|e| e.layer != layer.id) {
continue;
@@ -1017,12 +1082,15 @@ fn object_hit(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes, editab
/// an isolated group on one ([`crate::LockedArtboards::holds`]). Symbol instances are measured by their art
/// ([`Node::geometric_bounds_with`]), and are also hit outside that box where their art paints.
/// With [`HitOptions::unfilled_interiors`], a second walk looks inside the unfilled closed paths
/// only when the first, without it, hits nothing.
/// only when the first, without it, hits nothing and passes an unfilled closed path holding `p`
/// ([`InteriorProbe`]).
fn hit_test_skipping(doc: &Document, p: Point, opt: HitOptions, skip: &dyn Fn(NodeId) -> bool, editable_only: bool) -> Option<Hit> {
let probe = InteriorProbe::open(opt.unfilled_interiors && !opt.path_only && !opt.outline);
let painted = hit_test_walk(doc, p, HitOptions { unfilled_interiors: false, ..opt }, skip, editable_only);
if painted.is_some() || !opt.unfilled_interiors || opt.path_only || opt.outline {
if painted.is_some() || !opt.unfilled_interiors || opt.path_only || opt.outline || !probe.seen() {
return painted;
}
drop(probe);
hit_test_walk(doc, p, opt, skip, editable_only)
}
@@ -1672,8 +1740,8 @@ fn hit_leaf(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Optio
match &n.kind {
NodeKind::Path { path, rule, .. } => {
let has_fill = n.appearance.has_visible_fill();
let interior = !has_fill && path.subpaths.iter().any(|sp| sp.closed) && takes_interior(n, opt);
let inside = || (interior && fill_contains(&closed_subpaths_at([path], p), *rule, p)).then_some(HitKind::Fill);
let interior = !has_fill && path.subpaths.iter().any(|sp| sp.closed);
let inside = || interior.then(|| unfilled_inside(n, opt, || fill_contains(&closed_subpaths_at([path], p), *rule, p))).flatten();
if opt.outline
&& let Some((hit, needs_full_check)) = near_path_body(path, p, opt.tol, 0.0)
{
@@ -1740,7 +1808,7 @@ fn hit_leaf(n: &Node, p: Point, opt: HitOptions, symbols: &SymbolBoxes) -> Optio
return (!opt.outline && !opt.path_only && fill_contains(&near, *rule, p)).then_some(HitKind::Fill);
}
let closed = || closed_subpaths_at(children.iter().filter_map(|m| m.path_data()), p);
(takes_interior(n, opt) && fill_contains(&closed(), *rule, p)).then_some(HitKind::Fill)
unfilled_inside(n, opt, || fill_contains(&closed(), *rule, p))
}
// The type path lies within the bounds: they rule out the rest before it is measured.
NodeKind::Text(t) if opt.type_path_only => {
@@ -2121,11 +2189,14 @@ pub fn symbol_part_at(doc: &Document, inst: &Node, p: Point, opt: HitOptions) ->
let find = FindPart { art: &art, p, opt, symbols: &symbols, whole_instance: false, drawn: true };
find.find(&art.art, &mut path, None, outline).map(|_| path)
};
// As [`hit_test`] does: inside an unfilled closed path only where nothing else is hit.
// As [`hit_test`] does: inside an unfilled closed path only where nothing else is hit, and only
// when the first walk passes one holding `p` ([`InteriorProbe`]).
let probe = InteriorProbe::open(opt.unfilled_interiors && !opt.path_only && !outline);
pick(HitOptions { unfilled_interiors: false, ..opt }).or_else(|| {
if !opt.unfilled_interiors || opt.path_only || outline {
if !opt.unfilled_interiors || opt.path_only || outline || !probe.seen() {
return None;
}
drop(probe);
walk.restart();
pick(opt)
})
+82
View File
@@ -200,6 +200,88 @@ fn symbol_art_parts_are_picked_inside_unfilled_frames_after_what_paints() {
assert_eq!(at(220.0), None, "inside the clipping path where no art paints");
}
/// An open path with a black stroke and no fill, from (`o`, 0) to (100, `o`) to (100, 100): its box
/// holds (50, 50), and it is drawn nowhere near that point.
fn open_corner(d: &mut Document, o: f64) -> Node {
let pts = [Point::new(o, 0.0), Point::new(100.0, o), Point::new(100.0, 100.0)];
let path = vectorcraft_geom::PathData::single(vectorcraft_geom::SubPath::polyline(&pts, false));
Node::path(d.alloc_id(), path, Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0))
}
/// How many leaves [`hit_leaf`] tested while `f` ran, with its result.
fn leaves_tested<R>(f: impl FnOnce() -> R) -> (R, usize) {
LEAVES_TESTED.with(|c| c.set(0));
let r = f();
(r, LEAVES_TESTED.with(std::cell::Cell::get))
}
/// A selection click that hits nothing, where no unfilled closed path holds the point, walks the
/// document once: looking inside unfilled closed paths tests no more leaves than a click without
/// it. An unfilled closed path that holds the point is still hit inside.
#[test]
fn a_selection_click_that_hits_nothing_walks_the_document_once() {
let mut d = Document::new(200.0, 200.0);
let l = d.layers[0].id;
for i in 0..10 {
let corner = open_corner(&mut d, f64::from(i));
d.insert(Some(l), 99, corner).unwrap();
}
let p = Point::new(50.0, 50.0);
let (hit, without) = leaves_tested(|| hit_test(&d, p, SELECTING));
assert_eq!(hit, None);
assert_eq!(without, 10);
let (hit, with) = leaves_tested(|| hit_test(&d, p, INTERIORS));
assert_eq!(hit, None);
assert_eq!(with, without, "hit_test");
let (tops, with) = leaves_tested(|| objects_at(&d, p, INTERIORS, None));
assert!(tops.is_empty());
assert_eq!(with, without, "objects_at");
// An unfilled frame below them that holds the point: the second walk finds it.
let frame = d.alloc_id();
let look = Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0);
d.insert(Some(l), 0, Node::path(frame, shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), look)).unwrap();
assert_eq!(hit_test(&d, p, INTERIORS).map(|h| h.leaf), Some(frame));
assert_eq!(objects_at(&d, p, INTERIORS, None), vec![frame]);
}
/// A hit test that does not look inside unfilled closed paths measures none of their interiors.
#[test]
fn a_hit_test_without_unfilled_interiors_measures_no_interior() {
let mut d = Document::new(200.0, 200.0);
stroked(&mut d, &FRAME, true);
let p = Point::new(50.0, 50.0);
let probed = |f: &dyn Fn()| {
INTERIORS_PROBED.with(|c| c.set(0));
f();
INTERIORS_PROBED.with(std::cell::Cell::get)
};
assert_eq!(probed(&|| assert_eq!(hit_test(&d, p, SELECTING), None)), 0, "hit_test");
assert_eq!(probed(&|| assert!(objects_at(&d, p, SELECTING, None).is_empty())), 0, "objects_at");
assert_eq!(probed(&|| assert_eq!(hit_test(&d, p, HitOptions { path_only: true, ..INTERIORS }), None)), 0, "by path only");
assert_eq!(probed(&|| assert!(hit_test(&d, p, INTERIORS).is_some())), 1, "looking inside");
}
/// Direct Selection on a dynamic symbol's instance that picks no part of its art, where no
/// unfilled closed path of the art holds the point, walks the art once.
#[test]
fn a_symbol_part_pick_that_hits_nothing_walks_the_art_once() {
let mut d = Document::new(200.0, 200.0);
let corners: Vec<Arc<Node>> = (0..10).map(|i| Arc::new(open_corner(&mut d, f64::from(i)))).collect();
let root = Node::group(d.alloc_id(), corners);
d.symbols.push(crate::Symbol { dynamic: true, name: "Corners".into(), art: Arc::new(root) });
let l = d.layers[0].id;
let inst = d.alloc_id();
d.insert(Some(l), 0, Node::new(inst, NodeKind::SymbolInstance { symbol: "Corners".into(), xf: vectorcraft_geom::Affine::IDENTITY })).unwrap();
let n = d.node(inst).unwrap();
let p = Point::new(50.0, 50.0);
let (part, without) = leaves_tested(|| symbol_part_at(&d, n, p, SELECTING));
assert_eq!(part, None);
assert!(without >= 10, "{without} leaves tested");
let (part, with) = leaves_tested(|| symbol_part_at(&d, n, p, INTERIORS));
assert_eq!(part, None);
assert_eq!(with, without);
}
#[test]
fn long_plain_stroke_hits_its_middle_and_curved_end() {
let mut d = Document::new(200.0, 200.0);