diff --git a/crates/ui-egui/src/tools/crop_ui.rs b/crates/ui-egui/src/tools/crop_ui.rs index 97111fde6b..39fcfa7cf5 100644 --- a/crates/ui-egui/src/tools/crop_ui.rs +++ b/crates/ui-egui/src/tools/crop_ui.rs @@ -75,6 +75,8 @@ struct CropStep { rect: Option<[f64; 4]>, angle: f64, default_frame: bool, + /// The canvas shows the pixels past the frame ([`shows_beyond_canvas`]). + editing: bool, } /// A crop gesture in progress. Rects are `[x0, y0, x1, y1]` in document coordinates. @@ -521,6 +523,8 @@ pub fn reset(app: &mut PhotocraftApp) { app.crop.press_step = None; claim(app); crate::crop_straighten::reset(app); + // As a new default frame is: held to the ratio preset (or output size) picked in the bar. + fit_frame(app); if step_of(app) != before { app.crop.undo.push(before); app.crop.redo.clear(); @@ -538,7 +542,7 @@ pub fn clear_history(app: &mut PhotocraftApp) { /// The frame's current state, as an undo step. fn step_of(app: &PhotocraftApp) -> CropStep { - CropStep { rect: app.ui.crop_rect, angle: app.ui.crop_angle, default_frame: app.crop.default_frame } + CropStep { rect: app.ui.crop_rect, angle: app.ui.crop_angle, default_frame: app.crop.default_frame, editing: app.crop.editing } } /// Put a frame state back. @@ -546,6 +550,7 @@ fn apply_step(app: &mut PhotocraftApp, s: CropStep) { app.ui.crop_rect = s.rect; app.ui.crop_angle = s.angle; app.crop.default_frame = s.default_frame; + app.crop.editing = s.editing; } /// The pending frame, its box Undo/Redo and the view's turn, kept while `image.crop` runs so a diff --git a/crates/ui-egui/src/tools/crop_ui/tests.rs b/crates/ui-egui/src/tools/crop_ui/tests.rs index 5d0f05040c..b4ae5e93f3 100644 --- a/crates/ui-egui/src/tools/crop_ui/tests.rs +++ b/crates/ui-egui/src/tools/crop_ui/tests.rs @@ -127,8 +127,10 @@ fn reset_frames_the_canvas_again_and_can_be_undone() { assert_eq!(app.session.active().unwrap().doc.size, Size::new(200, 100), "nothing is cropped"); // The box's own Undo brings the drawn frame back, and Redo resets it again. + assert!(!shows_beyond_canvas(&app), "the reset frame is not being edited"); assert!(intercept(&mut app, "edit.undo").is_some()); assert_eq!(app.ui.crop_rect, Some([50.0, 20.0, 120.0, 70.0])); + assert!(shows_beyond_canvas(&app), "the drawn frame is being edited again: the canvas shows the pixels past it"); assert!(intercept(&mut app, "edit.redo").is_some()); assert_eq!(app.ui.crop_rect, Some([0.0, 0.0, 200.0, 100.0])); @@ -150,6 +152,23 @@ fn reset_frames_the_canvas_again_and_can_be_undone() { assert!(!modified(&app)); } +/// Reset frames the canvas as a new frame is: held to the ratio preset picked in the bar. +#[test] +fn reset_holds_the_frame_to_the_ratio_preset() { + let mut app = app(SampleType::U8); + ensure_frame(&mut app); + drag(&mut app, &[[50.0, 20.0], [80.0, 40.0], [120.0, 70.0]], NONE); + let drawn = Some([50.0, 20.0, 120.0, 70.0]); + assert_eq!(app.ui.crop_rect, drawn); + // A 1:1 preset on the 200 x 100 canvas: the first frame is the largest square, centred. + app.ui.tool_options.crop_ratio = "1:1".into(); + reset(&mut app); + assert_eq!(app.ui.crop_rect, Some([50.0, 0.0, 150.0, 100.0]), "not the whole canvas, a 2:1 frame under a 1:1 preset"); + assert!(!modified(&app) && app.crop.default_frame); + assert!(intercept(&mut app, "edit.undo").is_some()); + assert_eq!(app.ui.crop_rect, drawn, "Undo brings the drawn frame back"); +} + /// #9730: the Crop tool's options bar has a Reset button beside Cancel and Commit, live once /// the frame was changed. #[test]