diff --git a/crates/engine-core/src/cmd/extra_cmds.rs b/crates/engine-core/src/cmd/extra_cmds.rs index 6aa06a7c009..2f952ffc4b9 100644 --- a/crates/engine-core/src/cmd/extra_cmds.rs +++ b/crates/engine-core/src/cmd/extra_cmds.rs @@ -628,13 +628,13 @@ fn is_empty_layer(l: &Layer) -> bool { } } -/// Layer › Ungroup Layers: replace the selected group with its children in place, the topmost -/// child becoming active. Ungrouping an empty group next to other layers simply removes it; -/// when it is the only root layer the edit is refused, as `layer.delete` refuses to drop the -/// last layer (#11118). +/// Layer › Ungroup Layers: replace the selected group with its children in place, selecting +/// every former direct child with the topmost one active (#17362). Ungrouping an empty group +/// next to other layers simply removes it; when it is the only root layer the edit is refused, +/// as `layer.delete` refuses to drop the last layer (#11118). fn ungroup(s: &mut Session, p: &Value) -> Result { let id = layer_param(s, p)?; - s.edit("Ungroup Layers", |doc, active| { + let former = s.edit("Ungroup Layers", |doc, active| { let path = doc.path_of(id).ok_or(EngineError::NoLayer(id))?; let g = doc.layer(id).ok_or(EngineError::NoLayer(id))?; let LayerContent::Group(group) = &g.content else { @@ -651,12 +651,19 @@ fn ungroup(s: &mut Session, p: &Value) -> Result { let sib = if parent.is_empty() { &mut doc.layers } else { doc.layer_at_mut(parent).and_then(Layer::children_mut).ok_or(EngineError::NoLayer(id))? }; sib.remove(idx); let top = children.last().map(|l| l.id); + let ids: Vec = children.iter().map(|l| l.id).collect(); for (k, l) in children.into_iter().enumerate() { sib.insert(idx + k, l); } *active = top.or_else(|| doc.top_layer()); - Ok(()) + Ok(ids) })?; + // The measured reference row: every former direct child is selected and the first (topmost) + // one is active; any other selection is replaced. An empty group leaves today's target + // (not measured). + if let Some(top) = former.last().copied() { + crate::layer_multi_cmds::reselect(s, former, Some(top)); + } Ok(Value::Null) } diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index eac618e76df..6eaefdc6850 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -194,6 +194,7 @@ mod transform_budget_unclamped; mod type_format_targets; mod type_postscript_font; mod undo_keeps_active_layer; +mod ungroup_selection; mod unicode_colors; mod unsupported_smart_filters; mod vector_in_actions_record; diff --git a/crates/engine/tests/it/ungroup_selection.rs b/crates/engine/tests/it/ungroup_selection.rs new file mode 100644 index 00000000000..925986d5593 --- /dev/null +++ b/crates/engine/tests/it/ungroup_selection.rs @@ -0,0 +1,132 @@ +//! #17362 (epic #16768): Layer › Ungroup Layers selects every former direct child, with the +//! topmost one active. The measured reference rows cover the plain group, ungrouping while +//! another layer is selected (the selection is replaced), a group holding a group (one level +//! removed, the inner group moves up), and Undo restoring the group and the previous selection. +//! The empty-group case is not measured and keeps today's behaviour, pinned below. + +use photocraft_doc::LayerId; +use photocraft_engine::Session; +use serde_json::json; + +fn new_layer(s: &mut Session, name: &str) -> LayerId { + LayerId(s.execute("layer.new.layer", json!({ "name": name })).unwrap()["layer"].as_u64().unwrap()) +} + +fn group_layers(s: &mut Session) -> LayerId { + LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap()) +} + +fn target(s: &Session) -> (Option, Vec) { + let st = s.active().unwrap(); + (st.active_layer, st.selected_layers().to_vec()) +} + +fn children_of(s: &Session, id: LayerId) -> Vec { + s.active().unwrap().doc.layer(id).and_then(|l| l.children().map(|ch| ch.iter().map(|c| c.id).collect())).unwrap_or_default() +} + +/// Set equality, order-insensitive: the ticket row states which layers are selected, not an order. +fn selection_is(sel: &[LayerId], want: &[LayerId]) -> bool { + want.iter().all(|id| sel.contains(id)) && sel.len() == want.len() +} + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({ "width": 16, "height": 16 })).unwrap(); + s +} + +#[test] +fn ungroup_selects_every_former_child_with_topmost_active() { + // "After Ungroup Layers every former direct child is selected; the topmost is active." + let mut s = session(); + let a = new_layer(&mut s, "A"); + let b = new_layer(&mut s, "B"); + let c = new_layer(&mut s, "C"); + s.execute("layer.select", json!({ "layers": [a.0, b.0, c.0] })).unwrap(); + let group = group_layers(&mut s); + s.execute("layer.ungroupLayers", json!({})).unwrap(); + let (active, sel) = target(&s); + assert_eq!(active, Some(c), "the topmost former child is the active layer"); + assert!(selection_is(&sel, &[a, b, c]), "every former direct child is selected, got {sel:?}"); + let st = s.active().unwrap(); + assert!(st.doc.layer(group).is_none(), "the group itself is gone"); + assert!(!sel.contains(&group), "the removed group is not part of the selection"); +} + +#[test] +fn ungroup_replaces_another_selection_with_the_former_children() { + // The measured row selects every former child, so a layer that was selected next to the + // group drops out of the selection. + let mut s = session(); + let r = new_layer(&mut s, "R"); + let u = new_layer(&mut s, "U"); + let v = new_layer(&mut s, "V"); + s.execute("layer.select", json!({ "layers": [u.0, v.0] })).unwrap(); + let group = group_layers(&mut s); + // ⇧-click in the panel: R joins the group in the selection, and the group stays active. + s.execute("layer.select", json!({ "layers": [r.0, group.0] })).unwrap(); + s.execute("layer.ungroupLayers", json!({})).unwrap(); + let (active, sel) = target(&s); + assert_eq!(active, Some(v), "the topmost former child is the active layer"); + assert!(selection_is(&sel, &[u, v]), "the selection is exactly the former children, got {sel:?}"); + assert!(!sel.contains(&r), "the layer outside the group is no longer selected"); +} + +#[test] +fn ungroup_a_group_holding_a_group_selects_former_children_one_level_up() { + // "Ungroup Layers on a group that holds a group removes one level: the nested group moves + // up. The former children are all selected; the first (topmost) one is active." + let mut s = session(); + let n1 = new_layer(&mut s, "N1"); + let n2 = new_layer(&mut s, "N2"); + let n3 = new_layer(&mut s, "N3"); + s.execute("layer.select", json!({ "layers": [n2.0, n3.0] })).unwrap(); + let inner = group_layers(&mut s); + s.execute("layer.select", json!({ "layers": [n1.0, inner.0] })).unwrap(); + let outer = group_layers(&mut s); + s.execute("layer.ungroupLayers", json!({})).unwrap(); + let (active, sel) = target(&s); + assert_eq!(active, Some(inner), "the topmost former child (the inner group) is the active layer"); + assert!(selection_is(&sel, &[n1, inner]), "the former children — the inner group included — are all selected, got {sel:?}"); + let st = s.active().unwrap(); + assert!(st.doc.layer(outer).is_none(), "the outer group is gone: one level removed"); + assert_eq!(children_of(&s, inner), vec![n2, n3], "the inner group keeps its own children: they were not selected"); + // The inner group lands where the outer one was, above N1. + let outer_was_at = 1; // [Background, outer] with outer at root index 1 before ungroup + let doc = &st.doc; + assert_eq!(doc.path_of(inner).and_then(|p| p.first().copied()), Some(outer_was_at + 1), "the inner group sits at the outer group's top slot"); + assert_eq!(doc.path_of(n1).and_then(|p| p.first().copied()), Some(outer_was_at), "N1 sits at the outer group's old bottom slot"); +} + +#[test] +fn undo_after_ungroup_restores_the_group_and_the_previous_selection() { + // "Undo restores the group and the previous selection." + let mut s = session(); + let a = new_layer(&mut s, "A"); + let b = new_layer(&mut s, "B"); + s.execute("layer.select", json!({ "layers": [a.0, b.0] })).unwrap(); + let group = group_layers(&mut s); + s.execute("layer.ungroupLayers", json!({})).unwrap(); + assert!(s.undo(), "Ungroup Layers is one history step"); + let (active, sel) = target(&s); + assert_eq!(active, Some(group), "undo makes the restored group active"); + assert!(selection_is(&sel, &[group]), "undo restores the previous selection, got {sel:?}"); + assert_eq!(children_of(&s, group), vec![a, b], "the group and its children come back"); +} + +#[test] +fn ungrouping_an_empty_group_next_to_other_layers_keeps_todays_target() { + // Not measured in the ticket: an empty group simply disappears. Today the active layer + // falls back to the document's top layer and the selection collapses to it; pinned here. + let mut s = session(); + let l = new_layer(&mut s, "L"); + let empty = LayerId(s.execute("layer.new.group", json!({ "name": "Empty" })).unwrap()["layer"].as_u64().unwrap()); + s.execute("layer.select", json!({ "layers": [empty.0] })).unwrap(); + s.execute("layer.ungroupLayers", json!({})).unwrap(); + let (active, sel) = target(&s); + let st = s.active().unwrap(); + assert!(st.doc.layer(empty).is_none(), "the empty group is gone"); + assert_eq!(active, Some(l), "the top surviving layer stays active, as today"); + assert!(selection_is(&sel, &[l]), "the selection collapses to the active layer, as today, got {sel:?}"); +}