mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
0023cf370f
commit
6df61942fb
5 files changed
+177
-5
No files matched your search
@@ -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 ----------
|
||||
|
||||
@@ -325,6 +325,22 @@ pub fn delete_selected(s: &mut Session) -> Result<Value> {
|
||||
/// this path, #2777).
|
||||
pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Result<Value> {
|
||||
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<Value> {
|
||||
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<LayerId>, in_place: bool, label: &str, as_set: bool) -> Result<Value> {
|
||||
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();
|
||||
|
||||
@@ -70,7 +70,7 @@ fn duplication() -> Vec<CommandSpec> {
|
||||
"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<CommandSpec> {
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -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<PhotocraftApp>, Vec<LayerId>) {
|
||||
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<LayerId> {
|
||||
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<LayerId> = 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<String> = 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<String> = 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<LayerId> = stack(&app).into_iter().filter(|id| !before.contains(id)).collect();
|
||||
assert_eq!(copies.iter().map(|c| bounds(&app, *c)).collect::<Vec<_>>(), [moved(r1, 1, 0), moved(r2, 1, 0)]);
|
||||
}
|
||||
@@ -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<usize> {
|
||||
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;
|
||||
Reference in new issue
Block a user