Skip to content

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

Crop: in default mode, box Undo/Redo keeps the box in place (#7088)

David Manncb5b1f8e9e13
diff --git a/crates/ui-egui/src/crop_mode.rs b/crates/ui-egui/src/crop_mode.rs
index 676c97dbe..a5669f12c 100644
--- a/crates/ui-egui/src/crop_mode.rs
+++ b/crates/ui-egui/src/crop_mode.rs
@@ -424,6 +424,42 @@ mod tests {
assert!(r.iter().zip([20.0, 30.0, 80.0, 70.0]).all(|(a, b)| (a - b).abs() < 1e-3), "{r:?}");
}
+ /// #7088: the box's own Undo/Redo of a move (or a turn) in default mode puts the frame back
+ /// and moves the image, not the box: it stays put on screen (Auto Center Preview off) or goes
+ /// to the middle of the canvas (on).
+ #[test]
+ fn box_undo_and_redo_keep_the_box_put() {
+ for auto in [false, true] {
+ let mut app = app();
+ app.ui.tool_options.crop_shield.auto_center_preview = auto;
+ with_frame(&mut app);
+ // A quarter turn, then a move inside: two box steps.
+ screen_drag(&mut app, &[pos2(450.0, 300.0), pos2(370.0, 380.0)], NONE);
+ let turned = app.ui.crop_rect;
+ screen_drag(&mut app, &[pos2(370.0, 300.0), pos2(380.0, 305.0), pos2(390.0, 310.0)], NONE);
+ let moved = app.ui.crop_rect;
+ assert_ne!(moved, turned);
+ let want = if auto { pos2(400.0, 300.0) } else { pos2(370.0, 300.0) };
+ let check = |app: &PhotocraftApp, what: &str| {
+ let q = screen_box(app);
+ assert_upright(q);
+ assert!(mid_of(q).distance(want) < 0.05, "auto={auto} {what}: the box moved on screen to {:?}", mid_of(q));
+ };
+ app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(app.ui.crop_rect, turned, "auto={auto}: the move is undone");
+ check(&app, "undo move");
+ app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(app.ui.crop_rect, Some([40.0, 30.0, 100.0, 70.0]));
+ assert!(crate::crop_ui::angle(&app).abs() < 1e-9, "the turn is undone");
+ check(&app, "undo turn");
+ app.run("edit.redo", json!({})).unwrap();
+ check(&app, "redo turn");
+ app.run("edit.redo", json!({})).unwrap();
+ assert_eq!(app.ui.crop_rect, moved, "auto={auto}: the move is redone");
+ check(&app, "redo move");
+ }
+ }
+
fn mid_of(q: [Pos2; 4]) -> Pos2 {
pos2((q[0].x + q[2].x) / 2.0, (q[0].y + q[2].y) / 2.0)
}
diff --git a/crates/ui-egui/src/crop_ui.rs b/crates/ui-egui/src/crop_ui.rs
index f1df63ff1..0081f463b 100644
--- a/crates/ui-egui/src/crop_ui.rs
+++ b/crates/ui-egui/src/crop_ui.rs
@@ -462,7 +462,19 @@ fn step(app: &mut PhotocraftApp, redo: bool) -> serde_json::Value {
} else {
app.crop.redo.push(now);
}
+ // Default mode: the box stays put on screen, so the view follows the frame's centre (#7088).
+ if let (true, Some(a), Some(b)) = (crate::crop_mode::moves_image(app), now.rect, s.rect) {
+ let (a, b) = (center(a), center(b));
+ let d = [(b[0] - a[0]) as f32, (b[1] - a[1]) as f32];
+ if let Some(v) = app.session.active_index().and_then(|i| app.ui.views.get_mut(i)) {
+ let c = [v.center[0] + d[0], v.center[1] + d[1]];
+ if c.iter().all(|x| x.is_finite()) {
+ v.center = c;
+ }
+ }
+ }
crate::crop_mode::sync_frame(app);
+ crate::crop_mode::auto_center(app);
serde_json::Value::Null
}