Skip to content

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

fix(image): arbitrary canvas rotation moves position-locked children in groups (#3078) (#3155)

buivanvu94680518b4251b
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs
index 96e6e0cb5..def9440c8 100644
--- a/crates/engine/src/mode_cmds.rs
+++ b/crates/engine/src/mode_cmds.rs
@@ -107,12 +107,8 @@ pub(crate) fn rotate_arbitrary(s: &mut Session, p: &Value) -> Result<Value> {
m.surface = crate::transform_cmds::warp_gray(&m.surface, &h, interp)?;
}
} else {
- // Rotating the whole image moves locked layers too.
- let locks = l.locks;
- l.locks.position = false;
- l.locks.all = false;
- crate::transform_cmds::transform_layer(None, photocraft_doc::Locks::default(), l, &h, Some(a), interp)?;
- l.locks = locks;
+ // Rotating the whole image moves locked layers too, including inside groups.
+ with_released_movement_locks(l, |l| crate::transform_cmds::transform_layer(None, photocraft_doc::Locks::default(), l, &h, Some(a), interp))?;
}
// What Free Transform leaves alone: unlinked vector masks, gradient angles, artboards…
crate::canvas_geom::transform_layer_geometry(l, &a, false, dpi);
@@ -132,6 +128,25 @@ pub(crate) fn rotate_arbitrary(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"width": size.width, "height": size.height}))
}
+/// Runs `f` with movement locks (`position` and `all`) temporarily released on `l` and all its
+/// descendants, so canvas-wide rotation moves every layer, then restores each lock.
+fn with_released_movement_locks<R>(l: &mut Layer, f: impl FnOnce(&mut Layer) -> Result<R>) -> Result<R> {
+ let mut saved = Vec::new();
+ crate::image_cmds::for_each_layer(std::slice::from_mut(l), &mut |x| {
+ saved.push(x.locks);
+ x.locks.position = false;
+ x.locks.all = false;
+ });
+ let r = f(l);
+ let mut saved = saved.into_iter();
+ crate::image_cmds::for_each_layer(std::slice::from_mut(l), &mut |x| {
+ if let Some(locks) = saved.next() {
+ x.locks = locks;
+ }
+ });
+ r
+}
+
// ---------- flatten helpers ----------
/// The flattened image as straight RGBA (over white when `opaque`).
diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rs
index 4aaf396af..1fbed4018 100644
--- a/crates/engine/src/mode_cmds/tests.rs
+++ b/crates/engine/src/mode_cmds/tests.rs
@@ -81,6 +81,48 @@ fn rotate_arbitrary_other_modes() {
}
}
+#[test]
+fn rotate_arbitrary_rotates_grouped_locked_children_and_preserves_lock() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "background": "white"})).unwrap();
+ s.execute("layer.new.layer", json!({"name": "Paint"})).unwrap();
+ s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap();
+ s.execute("layer.setProps", json!({"locks": {"position": true}})).unwrap();
+ s.execute("layer.groupLayers", json!({})).unwrap();
+
+ // 1. 30 degree rotation succeeds and canvas grows to 22x22.
+ let r30 = s.execute("image.rotation.arbitrary", json!({"angle": 30, "interpolation": "nearest"})).unwrap();
+ assert_eq!((r30["width"].as_u64(), r30["height"].as_u64()), (Some(22), Some(22)));
+
+ // 2. Child remains position-locked after canvas rotation.
+ let d = doc(&s);
+ let group = &d.layers[1];
+ let photocraft_doc::LayerContent::Group(g) = &group.content else { panic!("expected group") };
+ assert_eq!(g.children.len(), 1);
+ let child = &g.children[0];
+ assert_eq!(child.name, "Paint");
+ assert!(child.locks.position, "position lock must be preserved on child");
+
+ // 3. Free Transform of the child alone still refuses with lock error.
+ let child_id = child.id.0;
+ s.execute("layer.select", json!({"layer": child_id})).unwrap();
+ let err = s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 0, 0]})).unwrap_err();
+ assert!(err.to_string().contains("locked"), "child Free Transform must remain refused");
+
+ // 4. Undo restores 16x16 size.
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(doc(&s).size, Size::new(16, 16));
+
+ // 5. 90 degree rotation also succeeds and composite matches dedicated 90cw turn.
+ let r90 = s.execute("image.rotation.arbitrary", json!({"angle": 90, "direction": "cw", "interpolation": "nearest"})).unwrap();
+ assert_eq!((r90["width"].as_u64(), r90["height"].as_u64()), (Some(16), Some(16)));
+ let d_arb = doc(&s).clone();
+ s.execute("edit.undo", json!({})).unwrap();
+ s.execute("image.imageRotation.90cw", json!({})).unwrap();
+ let d_cw = doc(&s);
+ assert_eq!(photocraft_compose::flatten(&d_arb).px, photocraft_compose::flatten(d_cw).px);
+}
+
#[test]
fn flattened_mode_pixels_match_the_allocating_conversion_at_every_channel_count() {
let px = [[0.0, 0.25, 0.75, 0.0], [0.9, 0.3, 0.2, 0.2], [0.1, 0.7, 0.4, 0.7], [1.0; 4]];