From 6df61942fba90e5faf3f1ab02b4119e1fabaaf8a Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 16:08:52 +0100 Subject: [PATCH] Move tool: Alt+drag of a linked layer copies its partners and moves only the copies (#18137) The Alt duplicate runs layer.duplicate with the new withLinks flag: every layer linked to a selected one is copied too, and the copies are linked only among themselves, so the move that follows leaves the originals where they were, as in the reference application. Co-Authored-By: Claude Opus 5.5 --- .../engine-core/src/cmd/layer_multi_cmds.rs | 2 +- .../src/cmd/layer_multi_cmds/structure.rs | 26 +++- .../src/cmd/table/model/layer_menu.rs | 5 +- .../src/tools/move_alt_drag_linked_tests.rs | 139 ++++++++++++++++++ crates/ui-egui/src/tools/move_mods.rs | 10 +- 5 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 crates/ui-egui/src/tools/move_alt_drag_linked_tests.rs diff --git a/crates/engine-core/src/cmd/layer_multi_cmds.rs b/crates/engine-core/src/cmd/layer_multi_cmds.rs index cbcf73483d..4eaca1dc5a 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds.rs @@ -33,7 +33,7 @@ pub(crate) use selection::{mask_targets, recorded_step, reselect}; pub use selection::{select, selected, set_selection}; pub use structure::deletion_neighbours; pub(crate) use structure::{check_group_depth, relink_copies}; -pub use structure::{delete_selected, duplicate_selected, group_layers, set_visible_selected}; +pub use structure::{delete_selected, duplicate_selected, duplicate_selected_with_links, group_layers, set_visible_selected}; use structure::{link_layers, lock_layers, merge_layers, rename_layer, reverse}; // ---------- enabled predicates ---------- diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs b/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs index e899a90437..af0e848f3f 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds/structure.rs @@ -325,6 +325,22 @@ pub fn delete_selected(s: &mut Session) -> Result { /// this path, #2777). pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Result { let sel = selected(s); + duplicate_layers(s, sel, in_place, label, false) +} + +/// [`duplicate_selected`] that also copies every layer linked to a selected one, as the Move +/// tool's ⌥-drag does in the reference application (#18137): the copies stay linked among +/// themselves as a set (a copy alone from its link group is unlinked), never with the originals, +/// so moving the copies leaves every original where it was. +pub fn duplicate_selected_with_links(s: &mut Session, in_place: bool, label: &str) -> Result { + let sel = selected(s); + let sel = s.active().map(|d| super::move_targets(&d.doc, &sel)).unwrap_or(sel); + duplicate_layers(s, sel, in_place, label, true) +} + +/// Copies `sel` as one block ([`duplicate_selected`]); `as_set` links the copies only among +/// themselves, also a lone copy ([`relink_copy_set`]). +fn duplicate_layers(s: &mut Session, sel: Vec, in_place: bool, label: &str, as_set: bool) -> Result { let old_active = s.active().and_then(|d| d.active_layer); let add_copy = crate::commands::layer_panel_option(s, "addCopyToCopiedLayersAndGroups", true); let mut shift = (0i32, 0i32); @@ -358,7 +374,8 @@ pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Resul } copies.push(nid); } - relink_copies(doc, &copies)?; + let relink = if as_set { relink_copy_set } else { relink_copies }; + relink(doc, &copies)?; // The block can put a group's copy deeper than the group itself. check_group_depth(doc, label)?; let a = new_active.or(copies.last().copied()); @@ -385,6 +402,13 @@ pub(crate) fn relink_copies(doc: &mut Document, copies: &[LayerId]) -> Result<() { return Ok(()); } + relink_copy_set(doc, copies) +} + +/// [`relink_copies`] for copies taken as one set whatever their number: copies of several layers +/// of one link group are linked to each other under a group of their own, a copy that is the only +/// one from its link group is unlinked. +fn relink_copy_set(doc: &mut Document, copies: &[LayerId]) -> Result<()> { // Every copied layer with the link group it was copied with. An explicit stack rather than // recursion: documents can nest deeply. let mut linked: Vec<(LayerId, u64)> = Vec::new(); diff --git a/crates/engine-core/src/cmd/table/model/layer_menu.rs b/crates/engine-core/src/cmd/table/model/layer_menu.rs index bd9a48769f..e2611c1981 100644 --- a/crates/engine-core/src/cmd/table/model/layer_menu.rs +++ b/crates/engine-core/src/cmd/table/model/layer_menu.rs @@ -70,7 +70,7 @@ fn duplication() -> Vec { "Duplicate Layer…", ["Layer"], None, - r##"{"layer":id?,"inPlace":bool=false (artboards: keep the copy on the original instead of beside it),"name":str? (one layer: the copy's name),"document":index|"new"=same document (Photoshop's Destination: "new" makes a new document the size of this canvas holding just the copies),"documentName":str? (the new document's name)} (no layer: every selected layer)"##, + r##"{"layer":id?,"inPlace":bool=false (artboards: keep the copy on the original instead of beside it),"withLinks":bool=false (also copy every layer linked to a selected one; the copies are linked only among themselves, as the Move tool's Alt-drag copies),"name":str? (one layer: the copy's name),"document":index|"new"=same document (Photoshop's Destination: "new" makes a new document the size of this canvas holding just the copies),"documentName":str? (the new document's name)} (no layer: every selected layer)"##, has_layer, |s, p| { if let Some(d) = p.get("document") { @@ -91,6 +91,9 @@ fn duplication() -> Vec { } let in_place = p.get("inPlace").and_then(Value::as_bool).unwrap_or(false); let name = p.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()).map(str::to_string); + if p.get("layer").is_none() && p.get("withLinks").and_then(Value::as_bool).unwrap_or(false) { + return crate::layer_multi_cmds::duplicate_selected_with_links(s, in_place, "Duplicate Layers"); + } if crate::layer_multi_cmds::multi(s, p) { return crate::layer_multi_cmds::duplicate_selected(s, in_place, "Duplicate Layers"); } diff --git a/crates/ui-egui/src/tools/move_alt_drag_linked_tests.rs b/crates/ui-egui/src/tools/move_alt_drag_linked_tests.rs new file mode 100644 index 0000000000..1f06e1f175 --- /dev/null +++ b/crates/ui-egui/src/tools/move_alt_drag_linked_tests.rs @@ -0,0 +1,139 @@ +//! #18137: an ⌥-drag of a layer with linked partners copies the partners too and moves only the copies, as the +//! reference application does; the originals stay where they were. Unlinked layers keep one block of copies above +//! the topmost selected layer. + +use egui::Modifiers; +use photocraft_doc::LayerId; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +/// A document with a Background and the layers `rects` above it (bottom to top), each painted in its rectangle, +/// the Move tool picked. Returns the layers' ids. +fn app_with(rects: &[Rect]) -> (Box, Vec) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 96, "height": 96})).unwrap(); + app.sync_views(); + let mut ids = Vec::new(); + for &rect in rects { + app.run("layer.new.layer", json!({})).unwrap(); + app.session + .edit("paint", |doc, a| { + doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(rect, &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + ids.push(app.session.active().unwrap().active_layer.unwrap()); + } + app.ui.tool = Tool::Move; + app.ui.tool_options.move_auto_select = false; + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + (app, ids) +} + +fn link(app: &mut PhotocraftApp, ids: &[LayerId]) { + app.session + .edit("link", |doc, _| { + for id in ids { + doc.layer_mut(*id).unwrap().link_group = Some(7); + } + Ok(()) + }) + .unwrap(); +} + +fn select(app: &mut PhotocraftApp, ids: &[LayerId]) { + photocraft_engine::layer_multi_cmds::set_selection(&mut app.session, ids.to_vec(), ids.last().copied(), None).unwrap(); +} + +fn bounds(app: &PhotocraftApp, id: LayerId) -> Rect { + app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().content_bounds() +} + +fn name(app: &PhotocraftApp, id: LayerId) -> String { + app.session.active().unwrap().doc.layer(id).unwrap().name.clone() +} + +/// The top-level layers, bottom to top. +fn stack(app: &PhotocraftApp) -> Vec { + app.session.active().unwrap().doc.layers.iter().map(|l| l.id).collect() +} + +fn alt_drag(app: &mut PhotocraftApp, dx: f64, dy: f64) { + tool_event(app, ToolEvent::Down { x: 16.0, y: 16.0, pressure: 1.0 }, Modifiers::ALT); + tool_event(app, ToolEvent::Move { x: 16.0 + dx / 2.0, y: 16.0 + dy / 2.0, pressure: 1.0 }, Modifiers::ALT); + tool_event(app, ToolEvent::Move { x: 16.0 + dx, y: 16.0 + dy, pressure: 1.0 }, Modifiers::ALT); + tool_event(app, ToolEvent::Up { x: 16.0 + dx, y: 16.0 + dy }, Modifiers::ALT); +} + +fn moved(r: Rect, dx: i32, dy: i32) -> Rect { + Rect::new(r.x0 + dx, r.y0 + dy, r.x1 + dx, r.y1 + dy) +} + +#[test] +fn alt_drag_of_a_linked_layer_copies_its_partners_and_moves_only_the_copies() { + let (r1, r2) = (Rect::new(8, 8, 24, 24), Rect::new(40, 40, 56, 56)); + let (mut app, ids) = app_with(&[r1, r2]); + let (l1, l2) = (ids[0], ids[1]); + link(&mut app, &[l1, l2]); + select(&mut app, &[l1]); + let before = stack(&app); + let h0 = app.session.active().unwrap().history.past_len(); + alt_drag(&mut app, 10.0, 6.0); + + assert_eq!(bounds(&app, l1), r1, "the selected original stays"); + assert_eq!(bounds(&app, l2), r2, "its linked partner stays"); + let copies: Vec = stack(&app).into_iter().filter(|id| !before.contains(id)).collect(); + assert_eq!(copies.len(), 2, "a copy of the layer and of its partner"); + let names: Vec = copies.iter().map(|id| name(&app, *id)).collect(); + assert_eq!(names, [format!("{} copy", name(&app, l1)), format!("{} copy", name(&app, l2))]); + assert_eq!(bounds(&app, copies[0]), moved(r1, 10, 6), "the layer's copy moved by the drag"); + assert_eq!(bounds(&app, copies[1]), moved(r2, 10, 6), "the partner's copy moved with it"); + + let st = app.session.active().unwrap(); + assert_eq!(st.selected_layers(), copies.as_slice(), "the copies become the selection"); + assert_eq!(st.history.past_len(), h0 + 1, "one history state"); + assert_eq!(st.history.undo_label(), Some("Move")); + let groups = |id| st.doc.layer(id).unwrap().link_group; + assert!(copies.iter().all(|c| groups(*c).is_none() || groups(*c) != groups(l1)), "no copy is linked with the originals"); + + assert!(app.session.undo()); + assert_eq!(stack(&app), before, "one undo takes the copies back"); +} + +#[test] +fn alt_drag_of_unlinked_layers_keeps_one_block_of_copies_above_the_topmost() { + let rects = [Rect::new(8, 8, 24, 24), Rect::new(30, 8, 40, 24), Rect::new(8, 30, 24, 40)]; + let (mut app, ids) = app_with(&rects); + select(&mut app, &[ids[0], ids[2]]); + let before = stack(&app); + alt_drag(&mut app, 4.0, 4.0); + + let after = stack(&app); + let top = after.iter().position(|id| *id == ids[2]).unwrap(); + assert_eq!(after.len(), before.len() + 2); + assert_eq!(&after[..=top], before.as_slice(), "the originals keep their order below the copies"); + let copies = &after[top + 1..]; + let names: Vec = copies.iter().map(|id| name(&app, *id)).collect(); + assert_eq!(names, [format!("{} copy", name(&app, ids[0])), format!("{} copy", name(&app, ids[2]))], "in the originals' order"); + assert_eq!(bounds(&app, ids[0]), rects[0]); + assert_eq!(bounds(&app, ids[1]), rects[1], "an unselected layer stays"); + assert_eq!(bounds(&app, copies[0]), moved(rects[0], 4, 4)); +} + +#[test] +fn alt_nudge_of_a_linked_layer_leaves_the_originals_too() { + let (r1, r2) = (Rect::new(8, 8, 24, 24), Rect::new(40, 40, 56, 56)); + let (mut app, ids) = app_with(&[r1, r2]); + link(&mut app, &ids); + select(&mut app, &[ids[0]]); + let before = stack(&app); + super::nudge(&mut app, 1.0, 0.0, true); + assert_eq!((bounds(&app, ids[0]), bounds(&app, ids[1])), (r1, r2), "the originals stay"); + let copies: Vec = stack(&app).into_iter().filter(|id| !before.contains(id)).collect(); + assert_eq!(copies.iter().map(|c| bounds(&app, *c)).collect::>(), [moved(r1, 1, 0), moved(r2, 1, 0)]); +} diff --git a/crates/ui-egui/src/tools/move_mods.rs b/crates/ui-egui/src/tools/move_mods.rs index 38937109d4..868a4c7e6f 100644 --- a/crates/ui-egui/src/tools/move_mods.rs +++ b/crates/ui-egui/src/tools/move_mods.rs @@ -71,8 +71,10 @@ pub fn copying(app: &PhotocraftApp, alt: bool) -> bool { /// length before so [`fold_history`] can merge the copy and the move into one step. fn duplicate(app: &mut PhotocraftApp) -> Option { let before = past_len(app)?; - // In place: the copy follows the pointer from the original, artboards too (#1531). - match app.run("layer.duplicate", json!({"inPlace": true})) { + // In place: the copy follows the pointer from the original, artboards too (#1531). Linked + // partners are copied too and the copies are not linked with the originals, so the move takes + // only the copies along (#18137). + match app.run("layer.duplicate", json!({"inPlace": true, "withLinks": true})) { // The state just before the copy's: in non-linear history the copy's step also moved // the later states in front of it (`History::record`), and those must stay. Ok(_) => Some(past_len(app).and_then(|n| n.checked_sub(1)).unwrap_or(before).max(before)), @@ -246,3 +248,7 @@ fn selection_arrow_keys(app: &mut PhotocraftApp, direction: egui::Vec2, mods: Mo #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "move_alt_drag_linked_tests.rs"] +mod move_alt_drag_linked_tests;