diff --git a/crates/doc/src/hit.rs b/crates/doc/src/hit.rs index bb4dcbd416..2a92cfb160 100644 --- a/crates/doc/src/hit.rs +++ b/crates/doc/src/hit.rs @@ -2,6 +2,7 @@ use std::cell::RefCell; use std::collections::{HashMap, HashSet}; +use std::ops::ControlFlow; use std::rc::Rc; use std::sync::{Arc, OnceLock}; @@ -403,6 +404,8 @@ thread_local! { thread_local! { /// How many objects' drawn art [`drawn`] evaluated on this thread. static DRAWN_EVALUATED: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// How many times [`has_own_effects`] read an object's effects on this thread. + static OWN_EFFECTS_READ: std::cell::Cell = const { std::cell::Cell::new(0) }; } /// While a guard lives, [`drawn`] gives no art: the art it gave already has every geometry effect @@ -527,7 +530,7 @@ fn plain(mask: bool, f: impl FnOnce() -> T) -> T { /// kept alone. fn drawn(symbols: &SymbolBoxes, n: &Node) -> Option<(Rc>, Option)> { let f = DRAWN_ART.get()?; - if DRAWN_OFF.get() || !evaluates(n) || !(has_own_effects(n) || draws_effects(symbols, n)) { + if DRAWN_OFF.get() || !evaluates(n) || !draws_effects(symbols, n) { return None; } let doc = symbols.document(); @@ -568,7 +571,7 @@ fn evaluates(n: &Node) -> bool { match &n.kind { NodeKind::Path { guide, .. } => !guide, NodeKind::Compound { .. } => true, - NodeKind::Group { .. } | NodeKind::Layer { .. } => has_own_effects(n) || !n.appearance.items.is_empty(), + NodeKind::Group { .. } | NodeKind::Layer { .. } => !n.appearance.items.is_empty() || has_own_effects(n), _ => false, } } @@ -1188,23 +1191,24 @@ pub(crate) fn clip_region_inputs() -> (u64, bool) { /// Does `clip`, or anything it holds, have a visible effect of its own or on a fill or stroke? Only /// then can the registered [`ClipRegion`] find a geometry effect in it. fn has_effects(clip: &Node) -> bool { - let mut found = false; - clip.walk(&mut |n| found |= has_own_effects(n)); - found + clip.try_walk(&mut |n| if has_own_effects(n) { ControlFlow::Break(()) } else { ControlFlow::Continue(()) }).is_break() } /// Does `n`, anything it holds, or the art of a symbol an instance in it shows have a visible -/// effect? Only then can the registered [`DrawnArt`] find a geometry effect in it. +/// effect? Only then can the registered [`DrawnArt`] find a geometry effect in it. The walk starts +/// at `n`, so its own effects are read once, here, and it ends at the first object with an effect. fn draws_effects(symbols: &SymbolBoxes, n: &Node) -> bool { - let mut found = false; - n.walk(&mut |c| { - found = found || has_own_effects(c) || matches!(&c.kind, NodeKind::SymbolInstance { symbol, .. } if symbols.has_effects(symbol)); - }); - found + n.try_walk(&mut |c| { + let has = has_own_effects(c) || matches!(&c.kind, NodeKind::SymbolInstance { symbol, .. } if symbols.has_effects(symbol)); + if has { ControlFlow::Break(()) } else { ControlFlow::Continue(()) } + }) + .is_break() } /// Does `n` itself have a visible effect, of its own or on one of its own fills or strokes? fn has_own_effects(n: &Node) -> bool { + #[cfg(test)] + OWN_EFFECTS_READ.with(|c| c.set(c.get() + 1)); let visible = |e: &crate::Effect| e.visible; n.appearance.effects.iter().any(visible) || n.appearance.items.iter().any(|i| i.effects().iter().any(visible)) } diff --git a/crates/doc/src/hit/tests.rs b/crates/doc/src/hit/tests.rs index 2f51917dde..a678797ff5 100644 --- a/crates/doc/src/hit/tests.rs +++ b/crates/doc/src/hit/tests.rs @@ -3652,6 +3652,48 @@ fn a_shaper_groups_original_shapes_in_symbol_art_are_not_picked() { assert_eq!(symbol_part_at(&d, n, Point::new(50.0, 50.0), opt), Some(vec![0, 1]), "over the shown face"); } +/// A hover over empty canvas past many plain paths reads each path's effects once, and the layer's +/// once. +#[test] +fn hovers_past_plain_paths_read_each_paths_effects_once() { + set_drawn_art(shifted_art); + let mut d = Document::new(1000.0, 1000.0); + let l = d.layers[0].id; + let paths = 100; + for i in 0..paths { + let n = square(&mut d, (i % 10) as f64 * 60.0, (i / 10) as f64 * 60.0); + d.insert(Some(l), 0, n).unwrap(); + } + let reads = || OWN_EFFECTS_READ.with(std::cell::Cell::get); + let before = reads(); + assert!(hit_test(&d, Point::new(900.0, 900.0), HitOptions::default()).is_none()); + let read = reads() - before; + assert_eq!(read, paths + 1, "each path's effects and the layer's are read once"); + assert!(hit_at(&d, 25.0, 25.0, HitOptions::default()).is_some(), "a square is still hit"); +} + +/// Whether a compound clipping path, or a compound path, has a live effect is settled by the first +/// object found with one: the members after it are not read. +#[test] +fn an_effect_on_a_compound_path_is_found_without_reading_its_members() { + set_clip_region(moved_clip_region); + set_drawn_art(shifted_art); + let mut d = Document::new(1000.0, 1000.0); + let members: Vec<_> = (0..100) + .map(|i| Arc::new(Node::path(d.alloc_id(), shapes::rectangle(Rect::new(i as f64, 0.0, i as f64 + 5.0, 5.0)), Default::default()))) + .collect(); + let mut mask = Node::new(d.alloc_id(), NodeKind::Compound { children: members, rule: FillRule::NonZero }); + mask.appearance.effects.push(test_move()); + let reads = || OWN_EFFECTS_READ.with(std::cell::Cell::get); + let before = reads(); + assert!(has_effects(&mask)); + assert_eq!(reads() - before, 1, "the clipping path's own effect ends the search"); + let symbols = d.symbol_boxes(); + let before = reads(); + assert!(draws_effects(&symbols, &mask)); + assert_eq!(reads() - before, 1, "the compound path's own effect ends the search"); +} + /// The pointer resting over empty canvas past many paths reads their anchors for their culling /// boxes on the first hover only: the next hovers use the boxes the paths keep. #[test]