Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(layers): remap linked-layer groups when copying between documents (#3178)

Nigel Ihza Firdausy64ec252dbe1a
diff --git a/crates/engine/src/layer_copy_cmds.rs b/crates/engine/src/layer_copy_cmds.rs
index fd87cb838..eb2d4d318 100644
--- a/crates/engine/src/layer_copy_cmds.rs
+++ b/crates/engine/src/layer_copy_cmds.rs
@@ -8,7 +8,7 @@
//! document. Pixels are converted to the destination's colour profile (Color Settings' intent and
//! black point compensation) and bit depth, as Photoshop converts dragged layers.
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
use photocraft_color::ColorMode;
use photocraft_doc::{Document, LayerContent, LayerId, SmartContentsId};
@@ -65,6 +65,29 @@ fn center(r: Rect) -> [f64; 2] {
/// including another opening of the same file), and the pixels convert to `dest`'s profile and
/// bit depth.
pub(crate) fn adapt_copies(s: &Session, copies: &mut Document, dest: &Document) -> Result<()> {
+ // Link-group IDs are scoped to a document. Two independently linked pairs can both
+ // use group 1; copying one must not link it to the other's existing layers (#3045).
+ // Remap each imported group once so links within the copied batch stay intact.
+ let mut used: HashSet<u64> = dest.walk().into_iter().filter_map(|(_, _, l)| l.link_group).collect();
+ let mut next_group = dest.unused_link_group().ok_or_else(|| EngineError::Other("no free linked-layer group id".into()))?;
+ let groups: Vec<_> = copies.walk().into_iter().filter_map(|(_, _, l)| l.link_group.map(|group| (l.id, group))).collect();
+ let mut remapped = HashMap::new();
+ for (_, group) in &groups {
+ if remapped.contains_key(group) {
+ continue;
+ }
+ while used.contains(&next_group) {
+ next_group = next_group.checked_add(1).unwrap_or(1);
+ }
+ remapped.insert(*group, next_group);
+ used.insert(next_group);
+ next_group = next_group.checked_add(1).unwrap_or(1);
+ }
+ for (id, group) in groups {
+ if let Some(layer) = copies.layer_mut(id) {
+ layer.link_group = remapped.get(&group).copied();
+ }
+ }
let mut contents = HashMap::new();
let smart_ids: Vec<_> = copies.walk().into_iter().filter_map(|(_, _, l)| matches!(l.content, LayerContent::Smart(_)).then_some(l.id)).collect();
for id in smart_ids {
diff --git a/crates/engine/src/layer_copy_cmds/tests.rs b/crates/engine/src/layer_copy_cmds/tests.rs
index 8ff1ee65d..cf6902ca7 100644
--- a/crates/engine/src/layer_copy_cmds/tests.rs
+++ b/crates/engine/src/layer_copy_cmds/tests.rs
@@ -155,3 +155,65 @@ fn bad_params_and_states_fail_without_changing_anything() {
s.close(1);
assert!(!s.is_enabled(cmd));
}
+
+#[test]
+fn copying_linked_layers_does_not_join_an_unrelated_destination_group() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16})).unwrap();
+ let src_a = LayerId(s.execute("layer.new.layer", json!({"name": "Source A"})).unwrap()["layer"].as_u64().unwrap());
+ let src_b = LayerId(s.execute("layer.new.layer", json!({"name": "Source B"})).unwrap()["layer"].as_u64().unwrap());
+ s.edit("link source", |d, _| {
+ d.layer_mut(src_a).unwrap().link_group = Some(1);
+ d.layer_mut(src_b).unwrap().link_group = Some(1);
+ Ok(())
+ })
+ .unwrap();
+
+ s.execute("file.new", json!({"width": 16, "height": 16})).unwrap();
+ let dst_a = LayerId(s.execute("layer.new.layer", json!({"name": "Destination A"})).unwrap()["layer"].as_u64().unwrap());
+ let dst_b = LayerId(s.execute("layer.new.layer", json!({"name": "Destination B"})).unwrap()["layer"].as_u64().unwrap());
+ s.edit("link destination", |d, _| {
+ d.layer_mut(dst_a).unwrap().link_group = Some(1);
+ d.layer_mut(dst_b).unwrap().link_group = Some(1);
+ Ok(())
+ })
+ .unwrap();
+ s.set_active(0);
+ let result = s.execute("layer.copyToDocument", json!({"document": 1, "layers": [src_a.0, src_b.0]})).unwrap();
+ let ids: Vec<LayerId> = result["layers"].as_array().unwrap().iter().map(|id| LayerId(id.as_u64().unwrap())).collect();
+ assert_eq!(ids.len(), 2);
+ let d = doc(&s, 1);
+ assert_eq!(d.layer(dst_a).unwrap().link_group, Some(1));
+ assert_eq!(d.layer(dst_b).unwrap().link_group, Some(1));
+ let first_group = d.layer(ids[0]).unwrap().link_group;
+ assert_eq!(first_group, d.layer(ids[1]).unwrap().link_group, "the copied pair stays linked");
+ assert!(first_group.is_some());
+ assert_ne!(first_group, Some(1), "copied links must not merge with existing ones");
+
+ assert!(s.undo());
+ assert!(doc(&s, 1).layer(ids[0]).is_none());
+ assert!(s.redo());
+ assert_eq!(doc(&s, 1).layer(ids[0]).unwrap().link_group, first_group);
+}
+
+#[test]
+fn copied_link_groups_are_distinct_from_each_other_and_destination() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16})).unwrap();
+ let a = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap());
+ let b = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap());
+ s.edit("link independent source groups", |d, _| {
+ d.layer_mut(a).unwrap().link_group = Some(1);
+ d.layer_mut(b).unwrap().link_group = Some(2);
+ Ok(())
+ })
+ .unwrap();
+ s.execute("file.new", json!({"width": 16, "height": 16})).unwrap();
+ s.set_active(0);
+ let copied = s.execute("layer.copyToDocument", json!({"document": 1, "layers": [a.0, b.0]})).unwrap();
+ let ids: Vec<LayerId> = copied["layers"].as_array().unwrap().iter().map(|id| LayerId(id.as_u64().unwrap())).collect();
+ let first = doc(&s, 1).layer(ids[0]).unwrap().link_group;
+ let second = doc(&s, 1).layer(ids[1]).unwrap().link_group;
+ assert!(first.is_some() && second.is_some());
+ assert_ne!(first, second);
+}