diff --git a/crates/doc/src/selection.rs b/crates/doc/src/selection.rs index 90afaad022..02c4391921 100644 --- a/crates/doc/src/selection.rs +++ b/crates/doc/src/selection.rs @@ -199,8 +199,8 @@ impl Selection { /// When `id` is also selected itself, it keeps its place in the selection. /// /// Returns whether the selection changed. It does not change when `id` is not inside a selected - /// object, is inside one that is partly selected (anchors or segments), or is one of a selected - /// Shaper Group's original shapes. + /// object, is inside one that is partly selected (anchors or segments), is one of a selected + /// Shaper Group's original shapes, or is hidden, locked or on a locked artboard itself. pub fn expand_to(&mut self, doc: &Document, id: NodeId) -> bool { let Some(chain) = doc.ancestry(id) else { return false }; let Some(top) = chain.iter().position(|a| self.contains(*a)) else { return false }; @@ -217,7 +217,7 @@ impl Selection { if children.iter().take(originals).any(|c| c.id == pair[1]) { return false; } - members.extend(children.iter().skip(originals).map(|c| c.id).filter(|c| *c == id || (*c != pair[1] && editable(*c)))); + members.extend(children.iter().skip(originals).map(|c| c.id).filter(|c| editable(*c) && (*c == id || *c != pair[1]))); } if !members.contains(&id) { return false; diff --git a/crates/doc/src/selection/tests.rs b/crates/doc/src/selection/tests.rs index d685b73bf7..46f726f1e8 100644 --- a/crates/doc/src/selection/tests.rs +++ b/crates/doc/src/selection/tests.rs @@ -109,6 +109,14 @@ fn expand_to_selects_a_member_in_place_of_its_selected_group() { with_locked.set([k]); assert!(with_locked.expand_to(&d, last)); assert_eq!(with_locked.objects, [shown, last]); + // A locked or hidden member itself does not open the group. + for target in [locked, hidden] { + let mut s = Selection::default(); + s.set([k]); + let before = s.clone(); + assert!(!s.expand_to(&d, target), "{target:?}"); + assert_eq!(s, before); + } } /// A selection with every field set: objects 1 to 6 with 3 listed twice, anchors, segments, a key, diff --git a/crates/engine/src/cmd/select.rs b/crates/engine/src/cmd/select.rs index e733204932..eb504b6074 100644 --- a/crates/engine/src/cmd/select.rs +++ b/crates/engine/src/cmd/select.rs @@ -959,9 +959,14 @@ fn anchors(s: &mut Session, p: &Value) -> Result { // A path selected as a whole has every anchor selected: toggle and add start from them all, // but only when no partial anchor selection is active and only on a single isolated object. // A toggle on a path inside a selected group selects the path itself in the group's place, - // beside the group's other objects, and starts from all its anchors. - let inside = mode == "toggle" && sel.expand_to(d, id); - let whole = inside || (sel.contains(id) && sel.partial(id).is_none() && sel.anchors.is_empty() && sel.objects.len() <= 1); + // beside the group's other objects, and starts from all its anchors. A toggle on a group + // member that is itself selected whole (as an earlier toggle in the group leaves the other + // members) starts from all its anchors whatever else is selected. + let toggle = mode == "toggle"; + let inside = toggle && sel.expand_to(d, id); + let own = sel.contains(id) && sel.partial(id).is_none(); + let in_group = || d.ancestry(id).is_some_and(|c| c.len() >= 2 && d.node(c[c.len() - 2]).is_some_and(|n| !n.is_layer())); + let whole = inside || (own && ((sel.anchors.is_empty() && sel.objects.len() <= 1) || (toggle && in_group()))); sel.add(id); // A segment pick's endpoints aren't selected anchors: start from none of them. if sel.segments.remove(&id).is_some() { diff --git a/crates/engine/tests/it/selection_commands.rs b/crates/engine/tests/it/selection_commands.rs index 6a1f69b96e..7d9c01bf3b 100644 --- a/crates/engine/tests/it/selection_commands.rs +++ b/crates/engine/tests/it/selection_commands.rs @@ -112,6 +112,18 @@ fn remove_and_anchor_toggle_open_a_selected_group() { assert_eq!(sel.objects, [a, b, c]); assert_eq!(sel.partial(a), Some(&[(0, 0), (0, 2), (0, 3)].into()), "the other anchors of a stay selected"); assert_eq!((sel.partial(b), sel.partial(c)), (None, None), "b and c stay selected whole"); + // A second Shift-click, on an anchor of b, deselects only that anchor of b. + s.execute("select.anchors", &json!({"id": b.0, "anchors": [[0, 2]], "mode": "toggle"})).unwrap(); + let sel = &s.active().unwrap().selection; + assert_eq!(sel.objects, [a, b, c]); + assert_eq!(sel.partial(a), Some(&[(0, 0), (0, 2), (0, 3)].into())); + assert_eq!(sel.partial(b), Some(&[(0, 0), (0, 1), (0, 3)].into()), "the other anchors of b stay selected"); + assert_eq!(sel.partial(c), None); + // An anchor of a path that is not selected joins the selection by itself. + let d = NodeId(s.execute("shape.rectangle", &json!({"x": 60, "y": 0, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap()); + s.execute("select.set", &json!({"ids": [c.0]})).unwrap(); + s.execute("select.anchors", &json!({"id": d.0, "anchors": [[0, 1]], "mode": "toggle"})).unwrap(); + assert_eq!(s.active().unwrap().selection.partial(d), Some(&[(0, 1)].into())); } #[test]