diff --git a/crates/engine/src/cmd/edit_cmds.rs b/crates/engine/src/cmd/edit_cmds.rs index d460524c25..002496ef07 100644 --- a/crates/engine/src/cmd/edit_cmds.rs +++ b/crates/engine/src/cmd/edit_cmds.rs @@ -116,12 +116,20 @@ fn copy_linked_sources(source: &Document, copies: &mut Document) -> Result<()> { /// The layer Copy takes whole ([`takes_whole`]): a scratch document in the source's colour /// holding it. fn whole_layer(d: &crate::DocState, id: LayerId) -> Option> { - let l = d.doc.layer(id).filter(|_| takes_whole(d, id))?; + let l = copied_layer(&d.doc, id, &[id]).filter(|_| takes_whole(d, id))?; let mut copies = scratch_doc(&d.doc); - copies.layers.push(l.clone()); + copies.layers.push(l); Some(Box::new(copies)) } +/// Layer `id` as Copy takes it with `ids`. It keeps its clipping mask only when its base is copied +/// too: pasted without it, it would clip to whatever it lands on, so Photoshop releases it (#9736). +fn copied_layer(doc: &Document, id: LayerId, ids: &[LayerId]) -> Option { + let mut l = doc.layer(id)?.clone(); + l.clipped &= crate::clip_group::base_of(doc, id).is_some_and(|base| ids.contains(&base)); + Some(l) +} + /// The layers Copy takes when several are selected and nothing is selected in the pixels /// (Photoshop, #2478): the selected layers that no selected group already carries, bottom-to-top. /// Empty otherwise, and for a single layer ([`takes_whole`] decides that case). @@ -142,7 +150,7 @@ fn multi_copy_ids(d: &crate::DocState) -> Vec { fn whole_layers(d: &crate::DocState, ids: &[LayerId]) -> Option> { let mut copies = scratch_doc(&d.doc); for id in ids { - let mut l = d.doc.layer(*id)?.clone(); + let mut l = copied_layer(&d.doc, *id, ids)?; if d.doc.is_background(*id) { l.name = "Layer 0".into(); l.locks = Default::default(); diff --git a/crates/engine/src/cmd/edit_cmds/tests.rs b/crates/engine/src/cmd/edit_cmds/tests.rs index 089a9873ba..49c7712d90 100644 --- a/crates/engine/src/cmd/edit_cmds/tests.rs +++ b/crates/engine/src/cmd/edit_cmds/tests.rs @@ -864,6 +864,93 @@ fn paste_several_layers_into_another_document() { } } +/// #9736: a clipped layer copied without its base pastes released, as Photoshop's Copy/Paste does, +/// so it can't clip to an unrelated layer it lands on. Copied with its base, it stays clipped to it. +#[test] +fn a_clipped_layer_copied_without_its_base_pastes_released() { + let mut s = session(); + let base = s.active().unwrap().active_layer.unwrap(); + let mid = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); + s.execute("type.create", json!({"text": "H", "size": 30, "x": 20, "y": 60})).unwrap(); + let text = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.setProps", json!({"layer": text.0, "clipped": true})).unwrap(); + s.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + let pasted = |s: &mut Session, ids: &[LayerId]| { + s.set_active(0); + s.execute("layer.select", json!({"layer": ids[0].0})).unwrap(); + for id in &ids[1..] { + s.execute("layer.select", json!({"layer": id.0, "mode": "toggle"})).unwrap(); + } + s.execute("edit.copy", json!({})).unwrap(); + s.set_active(1); + s.execute("edit.paste", json!({})).unwrap(); + let st = s.active().unwrap(); + let top = &st.doc.layers[st.doc.layers.len() - ids.len()..]; + let clipped: Vec = top.iter().map(|l| l.clipped).collect(); + assert!(s.undo()); + clipped + }; + assert_eq!(pasted(&mut s, &[text]), [false], "alone: released, not clipped to the destination layer"); + assert_eq!(pasted(&mut s, &[mid, text]), [false, true], "with its base: still clipped to it"); + assert_eq!(pasted(&mut s, &[base, text]), [false, false], "its base left behind: not clipped to another copied layer"); + s.set_active(0); + assert!(s.active().unwrap().doc.layer(text).unwrap().clipped, "the source is untouched"); +} + +/// #9736: a clipped layer's base is the nearest unclipped layer below it, not the next one, and a +/// clipped group is released like a layer. +#[test] +fn a_clipped_layer_keeps_its_clip_with_its_base_however_far_below() { + let mut s = session(); + let base = s.active().unwrap().active_layer.unwrap(); + let add = |s: &mut Session| { + let id = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); + s.edit("paint", |doc, _| { + doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(Rect::new(20, 20, 40, 40), &[0.0, 0.0, 1.0, 1.0]); + Ok(()) + }) + .unwrap(); + s.execute("layer.setProps", json!({"layer": id.0, "clipped": true})).unwrap(); + id + }; + let (c1, c2, inner) = (add(&mut s), add(&mut s), add(&mut s)); + s.execute("layer.groupLayers", json!({})).unwrap(); + let group = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.setProps", json!({"layer": group.0, "clipped": true})).unwrap(); + assert_eq!(crate::clip_group::base_of(&s.active().unwrap().doc, group), Some(base)); + let inner_clipped = s.active().unwrap().doc.layer(inner).unwrap().clipped; + s.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + let pasted = |s: &mut Session, ids: &[LayerId]| { + s.set_active(0); + s.execute("layer.select", json!({"layer": ids[0].0})).unwrap(); + for id in &ids[1..] { + s.execute("layer.select", json!({"layer": id.0, "mode": "toggle"})).unwrap(); + } + s.execute("edit.copy", json!({})).unwrap(); + s.set_active(1); + s.execute("edit.paste", json!({})).unwrap(); + let st = s.active().unwrap(); + let top = &st.doc.layers[st.doc.layers.len() - ids.len()..]; + let clipped: Vec = top.iter().map(|l| l.clipped).collect(); + let inner_clipped = top.iter().find_map(|l| match &l.content { + LayerContent::Group(g) => g.children.first().map(|c| c.clipped), + _ => None, + }); + assert!(s.undo()); + (clipped, inner_clipped) + }; + assert_eq!(pasted(&mut s, &[base, c2]).0, [false, true], "c1 between them left behind: c2 still clips to the base"); + assert_eq!(pasted(&mut s, &[c1, c2]).0, [false, false], "two clipped layers without their base"); + assert_eq!(pasted(&mut s, &[group]), (vec![false], Some(inner_clipped)), "the group is released, its contents untouched"); + assert_eq!(pasted(&mut s, &[base, group]).0, [false, true], "the group with its base"); + s.set_active(0); + let doc = &s.active().unwrap().doc; + assert!([c1, c2, group].iter().all(|id| doc.layer(*id).unwrap().clipped), "the source is untouched"); + assert_eq!(doc.layer(inner).unwrap().clipped, inner_clipped); +} + /// #2478: the Background in a multi-layer copy pastes as an ordinary, unlocked layer. #[test] fn copying_the_background_with_another_layer_pastes_an_ordinary_layer() {