Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Rotate left/right turns the crop with the image instead of cutting it (#962)

MarekSpodBiedryc822501a2b11
diff --git a/crates/engine/src/cmd/library.rs b/crates/engine/src/cmd/library.rs
index 27e96f4fd..7d85aeaaa 100644
--- a/crates/engine/src/cmd/library.rs
+++ b/crates/engine/src/cmd/library.rs
@@ -208,6 +208,10 @@ fn rotate(s: &mut Session, p: &Value, cw: bool) -> Result<Value> {
if let Some(d) = s.develop_of(id) {
let mut d = (*d).clone();
d.orientation = d.orientation.rotated(cw);
+ // the crop lives in the oriented frame, so it turns with the image (else it keeps the old width and height and cuts the photo)
+ let turn = lightcraft_geom::Orientation::Normal.rotated(cw);
+ d.crop.geometry.rect = turn.map_norm_rect(d.crop.geometry.rect);
+ d.crop.aspect = d.crop.aspect.map(|(w, h)| (h, w));
ops.extend(s.develop_op(id, d, "Rotate"));
}
}
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index cea8475e6..014837eb7 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -1106,3 +1106,24 @@ fn undo_and_redo_show_the_photo_they_change() {
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.selection, before, "a step over several photos keeps the selection");
}
+
+/// Rotate turns the crop with the image: a crop of the left half becomes the top half, and turning back restores it.
+#[test]
+fn rotate_carries_the_crop_with_the_image() {
+ use lightcraft_geom::Rect;
+ let mut s = demo();
+ let id = s.active().unwrap();
+ let mut d = active_dev(&s);
+ let left_half = Rect { x0: 0.0, y0: 0.0, x1: 0.5, y1: 1.0 };
+ d.crop.geometry.rect = left_half;
+ d.crop.aspect = Some((2, 3));
+ s.set_develop(id, d, "Crop").unwrap();
+ s.execute("photo.rotateRight", &json!({})).unwrap();
+ let c = active_dev(&s).crop;
+ assert_eq!(c.geometry.rect, Rect { x0: 0.0, y0: 0.0, x1: 1.0, y1: 0.5 });
+ assert_eq!(c.aspect, Some((3, 2)), "a locked aspect turns too");
+ s.execute("photo.rotateLeft", &json!({})).unwrap();
+ let c = active_dev(&s).crop;
+ assert_eq!(c.geometry.rect, left_half);
+ assert_eq!(c.aspect, Some((2, 3)));
+}