diff --git a/crates/doc/src/hit.rs b/crates/doc/src/hit.rs index d49776b2cb..8d918cba32 100644 --- a/crates/doc/src/hit.rs +++ b/crates/doc/src/hit.rs @@ -323,34 +323,31 @@ fn path_options(nodes: &[&Node], chain: &[NodeId], opt: HitOptions) -> Option) -> Vec { - let walk = ArtWalk::open(); + let _walk = ArtWalk::open(); let opt = HitOptions { scope: scope.or(opt.scope), ..opt }; // Normally one hit test per *object* suffices: layers and sublayers aren't selectable, // while a group (including a clipping group) is selected as one object. Repeatedly // searching the full tree for each hit made a stack of n overlapping paths O(n^2). // Isolation and Edit Contents change where selection groups begin; keep the original // traversal for those cases rather than change their Select Behind semantics. - if opt.scope.is_some() { + // Malformed files may contain non-layer roots; let the general traversal handle them. + if opt.scope.is_some() || doc.layers.iter().any(|l| !l.is_layer()) { return objects_at_repeated(doc, p, opt, scope); } let mut tops = Vec::new(); let mut seen = HashSet::new(); let symbols = doc.symbol_boxes(); for layer in doc.layers.iter().rev() { - // Malformed files may contain non-layer roots; let the general traversal handle them. - if !layer.is_layer() { - walk.restart(); - return objects_at_repeated(doc, p, opt, scope); - } if !layer.visible || layer.locked || layer.is_template() || doc.pattern_edit.as_ref().is_some_and(|e| e.layer != layer.id) { continue; } if !objects_at_layer(layer, p, opt, &symbols, &mut tops, &mut seen) { // An envelope in Edit Contents may expose objects *within* a top-level group. - // The repeated traversal handles that mixed hierarchy precisely, with the whole - // limit of symbol art to walk. - walk.restart(); - return objects_at_repeated(doc, p, opt, scope); + // The repeated traversal handles that mixed hierarchy precisely. The objects found + // so far lie above the envelope and stay first; the repeated traversal finds the + // rest with what is left of the limit of symbol art to walk. + tops = objects_at_repeated_after(doc, p, opt, scope, tops); + break; } } // The objects on a locked artboard can't be selected, as `hit_test_skipping` passes over them. @@ -414,7 +411,11 @@ fn objects_at_layer(layer: &Node, p: Point, opt: HitOptions, symbols: &SymbolBox /// The general selection order also handles isolation and nested Edit Contents. fn objects_at_repeated(doc: &Document, p: Point, opt: HitOptions, scope: Option) -> Vec { - let mut tops: Vec = vec![]; + objects_at_repeated_after(doc, p, opt, scope, vec![]) +} + +/// [`objects_at_repeated`] after `tops`, the topmost objects under `p` already found. +fn objects_at_repeated_after(doc: &Document, p: Point, opt: HitOptions, scope: Option, mut tops: Vec) -> Vec { // Each round leaves out the objects found so far (and everything inside them). while let Some(top) = hit_test_skipping(doc, p, opt, &|id| tops.contains(&id), true).map(|h| h.top_object(scope)) { if tops.contains(&top) { diff --git a/crates/doc/src/hit/tests.rs b/crates/doc/src/hit/tests.rs index 74bd959a7f..4912f28857 100644 --- a/crates/doc/src/hit/tests.rs +++ b/crates/doc/src/hit/tests.rs @@ -1452,6 +1452,76 @@ fn objects_at_edit_contents_uses_general_order() { assert_eq!(objects_at(&d, p, opt, None), vec![b, a]); } +/// Select Behind keeps one limit of symbol art to walk when it falls back to the general +/// traversal, for an envelope in Edit Contents and for a root that is not a layer: the fallback +/// walks no more art than what the first pass left, and keeps what the first pass found. +#[test] +fn select_behind_keeps_one_art_limit_when_it_falls_back() { + // 60 instances of `Clipped` with the rectangle hidden, which a click at (150, 50) walks the art + // of and misses: each walk takes 3 objects of the 100. + let stack = |d: &mut Document, count: usize| { + let l = d.layers[0].id; + for _ in 0..count { + let inst = instance_of(d, "Clipped"); + d.insert(Some(l), usize::MAX, Arc::unwrap_or_clone(inst)).unwrap(); + } + }; + // An envelope in Edit Contents at the bottom of the layer, its contents under (150, 50). + let envelope = |d: &mut Document| { + let inner = |id| Arc::new(Node::path(id, shapes::rectangle(Rect::new(120.0, 20.0, 180.0, 80.0)), Appearance::default_art())); + let (a, b) = (d.alloc_id(), d.alloc_id()); + let e = Node::new( + d.alloc_id(), + NodeKind::Envelope { + content: vec![inner(a), inner(b)], + kind: Box::new(crate::live::EnvelopeKind::Warp { style: "arc".to_string(), bend: 0.0, h: 0.0, v: 0.0, horizontal: true, version: 0 }), + fidelity: 100.0, + editing: true, + options: Default::default(), + frame: Box::new(vectorcraft_geom::Affine::IDENTITY), + }, + ); + let l = d.layers[0].id; + d.insert(Some(l), 0, e).unwrap(); + (a, b) + }; + let p = Point::new(150.0, 50.0); + let mut d = Document::new(600.0, 400.0); + clipped_symbol(&mut d, false); + let (a, b) = envelope(&mut d); + stack(&mut d, 60); + let (behind, _, arts) = with_art_limit(100, || objects_at(&d, p, HitOptions::default(), None)); + assert_eq!(behind, vec![b, a]); + assert!(arts <= 34, "envelope in Edit Contents: {arts} instances' art walked"); + // An instance that the first pass hits by its art, the limit then used up, stays in the list: + // `Shown` is `Clipped` with its rectangle shown, below 32 instances of `Clipped`. + let mut d = Document::new(600.0, 400.0); + clipped_symbol(&mut d, false); + let (a, b) = envelope(&mut d); + let (mut mask, art) = half_clipped(&mut d); + mask.appearance.effects.push(test_move()); + let mut clip = Node::group(d.alloc_id(), vec![Arc::new(mask), Arc::new(art)]); + clip.set_clips(true); + group_symbol(&mut d, "Shown", vec![Arc::new(clip)]); + let hit = instance_of(&mut d, "Shown"); + let hit_id = hit.id; + let l = d.layers[0].id; + d.insert(Some(l), usize::MAX, Arc::unwrap_or_clone(hit)).unwrap(); + stack(&mut d, 32); + let (behind, _, arts) = with_art_limit(100, || objects_at(&d, p, HitOptions::default(), None)); + assert_eq!(behind, vec![hit_id, b, a], "hit before the fallback"); + assert!(arts <= 34, "hit before the fallback: {arts} instances' art walked"); + // A root that is not a layer, below the layer. + let mut d = Document::new(600.0, 400.0); + clipped_symbol(&mut d, false); + stack(&mut d, 60); + let root = Arc::new(Node::group(d.alloc_id(), vec![])); + d.layers.insert(0, root); + let (behind, _, arts) = with_art_limit(100, || objects_at(&d, p, HitOptions::default(), None)); + assert!(behind.is_empty()); + assert!(arts <= 34, "root that is not a layer: {arts} instances' art walked"); +} + #[test] fn locked_and_hidden_skip() { let mut d = Document::new(200.0, 200.0);