diff --git a/crates/engine-core/src/cmd/vector_cmds/combine_shapes_locked_layers_tests.rs b/crates/engine-core/src/cmd/vector_cmds/combine_shapes_locked_layers_tests.rs new file mode 100644 index 0000000000..18af379e49 --- /dev/null +++ b/crates/engine-core/src/cmd/vector_cmds/combine_shapes_locked_layers_tests.rs @@ -0,0 +1,124 @@ +//! #18452: no lock blocks the four Layer › Combine Shapes operations, as in the reference application. The result is +//! the top layer, with the locks it had. Lock Pixels, Lock Transparent Pixels and a lock inherited from a group were +//! not measured and follow the same rule. Merge Shape Components was not measured and still refuses. + +use photocraft_doc::vector::Path; +use photocraft_doc::{Document, LayerContent, LayerId, Locks}; +use serde_json::json; + +use crate::Session; + +const OPERATIONS: [&str; 4] = [ + "layer.combineShapes.unite", + "layer.combineShapes.subtractFrontShape", + "layer.combineShapes.intersectShapeAreas", + "layer.combineShapes.excludeOverlappingShapes", +]; +const MERGE_COMPONENTS: &str = "layer.combineShapes.mergeShapeComponents"; + +const ALL: Locks = Locks { transparency: false, pixels: false, position: false, artboard: false, all: true }; +const POSITION: Locks = Locks { transparency: false, pixels: false, position: true, artboard: false, all: false }; +const PIXELS: Locks = Locks { transparency: false, pixels: true, position: false, artboard: false, all: false }; +const TRANSPARENCY: Locks = Locks { transparency: true, pixels: false, position: false, artboard: false, all: false }; + +/// Two overlapping rectangle shape layers, both selected. Returns the bottom and the top one. +fn two_selected_shapes() -> (Session, LayerId, LayerId) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); + let bottom = s.execute("shape.create", json!({"rect": [10, 10, 40, 40]})).unwrap()["layer"].as_u64().unwrap(); + let top = s.execute("shape.create", json!({"rect": [30, 30, 40, 40]})).unwrap()["layer"].as_u64().unwrap(); + select(&mut s, LayerId(bottom), LayerId(top)); + (s, LayerId(bottom), LayerId(top)) +} + +fn select(s: &mut Session, bottom: LayerId, top: LayerId) { + s.execute("layer.select", json!({"layer": bottom.0})).unwrap(); + s.execute("layer.select", json!({"layer": top.0, "mode": "add"})).unwrap(); +} + +fn lock(s: &mut Session, id: LayerId, locks: Locks) { + s.edit("lock", |doc, _| { + doc.layer_mut(id).unwrap().locks = locks; + Ok(()) + }) + .unwrap(); +} + +fn doc(s: &Session) -> &Document { + &s.active().unwrap().doc +} + +fn path(s: &Session, id: LayerId) -> Path { + match &doc(s).layer(id).unwrap().content { + LayerContent::Shape(shape) => shape.path.clone(), + _ => panic!("not a shape layer"), + } +} + +/// The path `operation` gives without any lock. +fn unlocked(operation: &str) -> Path { + let (mut s, _, top) = two_selected_shapes(); + s.execute(operation, json!({})).unwrap(); + path(&s, top) +} + +/// Runs `operation` and checks that it merged the bottom layer into the top one as without a lock, left the top layer +/// with `kept` as its own locks, and that one Undo brings back both layers and their locks. +fn assert_merges(s: &mut Session, bottom: LayerId, top: LayerId, operation: &str, kept: Locks, case: &str) { + let before = doc(s).clone(); + s.execute(operation, json!({})).unwrap_or_else(|e| panic!("{operation}, {case}: {e}")); + assert!(doc(s).layer(bottom).is_none(), "{operation}, {case}: the bottom layer is merged away"); + assert!(path(s, top) == unlocked(operation), "{operation}, {case}: the same shape as without the lock"); + assert_eq!(doc(s).layer(top).unwrap().locks, kept, "{operation}, {case}: the top layer's own locks"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(*doc(s) == before, "{operation}, {case}: one Undo restores the layers and their locks"); +} + +#[test] +fn lock_all_on_the_bottom_layer_does_not_block_and_the_result_has_no_lock() { + for operation in OPERATIONS { + let (mut s, bottom, top) = two_selected_shapes(); + lock(&mut s, bottom, ALL); + assert_merges(&mut s, bottom, top, operation, Locks::default(), "Lock All on the bottom layer"); + } +} + +#[test] +fn a_lock_on_the_top_layer_does_not_block_and_stays_on_the_result() { + for operation in OPERATIONS { + for (locks, case) in [(ALL, "Lock All on the top layer"), (POSITION, "Lock Position on the top layer")] { + let (mut s, bottom, top) = two_selected_shapes(); + lock(&mut s, top, locks); + assert_merges(&mut s, bottom, top, operation, locks, case); + } + } +} + +/// Not measured; the item asks for the same rule. +#[test] +fn lock_pixels_lock_transparency_and_a_locked_group_do_not_block_either() { + for operation in OPERATIONS { + for (locks, case) in [(PIXELS, "Lock Pixels"), (TRANSPARENCY, "Lock Transparent Pixels")] { + let (mut s, bottom, top) = two_selected_shapes(); + lock(&mut s, bottom, locks); + lock(&mut s, top, locks); + assert_merges(&mut s, bottom, top, operation, locks, case); + } + let (mut s, bottom, top) = two_selected_shapes(); + let group = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap()); + lock(&mut s, group, ALL); + select(&mut s, bottom, top); + assert!(doc(&s).effective_locks(top).all, "the group's lock reaches the layers"); + assert_merges(&mut s, bottom, top, operation, Locks::default(), "Lock All on the group around both"); + } +} + +/// Not measured, so Merge Shape Components keeps refusing locked layers. +#[test] +fn merge_shape_components_still_refuses_a_locked_layer() { + let (mut s, bottom, _) = two_selected_shapes(); + lock(&mut s, bottom, ALL); + let before = doc(&s).clone(); + assert!(s.execute(MERGE_COMPONENTS, json!({})).is_err()); + assert!(*doc(&s) == before, "nothing changed"); +} diff --git a/crates/engine-core/src/cmd/vector_cmds/shape.rs b/crates/engine-core/src/cmd/vector_cmds/shape.rs index 419e883a67..15403fbcc5 100644 --- a/crates/engine-core/src/cmd/vector_cmds/shape.rs +++ b/crates/engine-core/src/cmd/vector_cmds/shape.rs @@ -861,7 +861,9 @@ fn combine_targets(s: &Session, p: &Value, narrow: &[&str]) -> Result, label: &str, bake: Option) -> Result { let (&keep, _) = ids.split_last().ok_or_else(|| EngineError::Other("Combine Shapes needs two or more shape layers".into()))?; - if let Some(st) = s.active() { + // No lock blocks the four Combine Shapes operations; the result is the top layer, with its own locks (#18452). + // Merge Shape Components was not measured and still refuses. + if let Some(st) = s.active().filter(|_| op.is_none()) { for id in ids { let locks = st.doc.effective_locks(*id); if locks.all || locks.pixels { @@ -947,6 +949,10 @@ mod triangle_layer_name_tests; #[path = "triangle_box_geometry_tests.rs"] mod triangle_box_geometry_tests; +#[cfg(test)] +#[path = "combine_shapes_locked_layers_tests.rs"] +mod combine_shapes_locked_layers_tests; + #[cfg(test)] mod arrow_affine_regression_tests { use super::*; diff --git a/crates/engine-core/src/cmd/vector_cmds/tests.rs b/crates/engine-core/src/cmd/vector_cmds/tests.rs index 8df2a127c1..c0b555f7d4 100644 --- a/crates/engine-core/src/cmd/vector_cmds/tests.rs +++ b/crates/engine-core/src/cmd/vector_cmds/tests.rs @@ -1296,12 +1296,11 @@ fn combine_shapes_fails_gracefully() { } } assert_eq!(s.active().unwrap().history.past_len(), history); - // Locked layers refuse the merge without touching the document. + // Locks do not block the merge (#18452). s.execute("layer.lockLayers", json!({"all": true})).unwrap(); assert!(doc(&s).effective_locks(LayerId(a)).all); - let before = doc(&s).clone(); - assert!(s.execute("layer.combineShapes.unite", json!({})).is_err()); - assert_eq!(*doc(&s), before); + s.execute("layer.combineShapes.unite", json!({})).unwrap(); + assert!(doc(&s).layer(LayerId(a)).is_none(), "the locked layers merge"); } #[test]