diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs index 486088ec2de..4de185a412c 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs @@ -1014,7 +1014,7 @@ fn the_crop_row_crops_and_no_frame_shows_until_the_next_press() { assert!(h.state().ui.canvas_tool_menu.is_none()); assert_eq!(h.state().ui.tool, Tool::Crop); assert_eq!(h.state().ui.crop_rect, None, "no frame around the cropped image"); - // A click on the canvas brings back the tool's default frame. + // A click on the canvas brings back a frame around the whole cropped image. let center = h.state().last_canvas_rect.center(); h.event(egui::Event::PointerMoved(center)); h.run_steps(1); @@ -1022,7 +1022,7 @@ fn the_crop_row_crops_and_no_frame_shows_until_the_next_press() { h.run_steps(1); h.event(egui::Event::PointerButton { pos: center, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); h.run_steps(2); - assert!(h.state().ui.crop_rect.is_some() && h.state().crop.default_frame, "the press brings a frame back"); + assert_eq!(h.state().ui.crop_rect, Some([0.0, 0.0, f64::from(size.width), f64::from(size.height)]), "the click brings a frame back"); } #[test] diff --git a/crates/ui-egui/src/tool_memory.rs b/crates/ui-egui/src/tool_memory.rs index 2b05c523d63..1464052aad3 100644 --- a/crates/ui-egui/src/tool_memory.rs +++ b/crates/ui-egui/src/tool_memory.rs @@ -26,6 +26,10 @@ pub(crate) fn select(app: &mut PhotocraftApp, tool: Tool) { app.stylus.tool_before_eraser = None; app.ui.spring = None; app.ui.tool = tool; + // Picking the Crop tool, even the one already picked, brings back a frame a commit hid. + if tool == Tool::Crop { + crate::crop_ui::show_frame(app); + } } /// Temporary canvas overrides do not change `ui.tool`. Spring-loaded letters and the pen's diff --git a/crates/ui-egui/src/tools/crop_ui.rs b/crates/ui-egui/src/tools/crop_ui.rs index 5e6625a2ede..a59e9ca019c 100644 --- a/crates/ui-egui/src/tools/crop_ui.rs +++ b/crates/ui-egui/src/tools/crop_ui.rs @@ -248,6 +248,8 @@ pub(crate) fn apply_ratio(app: &mut PhotocraftApp) { if app.ui.tool != Tool::Crop || app.crop.drag.is_some() { return; } + // A ratio or size chosen after a commit fits a frame, not nothing. + show_frame(app); frame(app); fit_frame(app); crate::crop_mode::sync_frame(app); @@ -452,6 +454,14 @@ pub fn hide_frame(app: &mut PhotocraftApp) { } } +/// Ends [`hide_frame`] and makes the default frame now, so what follows acts on it as on any +/// default frame: a press records it as the box's Undo step, a ratio fits it. +pub(crate) fn show_frame(app: &mut PhotocraftApp) { + if app.crop.dismissed.take().is_some() { + frame(app); + } +} + /// The active document's identity for [`CropState::dismissed`]. fn dismiss_key(app: &PhotocraftApp) -> Option<(photocraft_doc::DocId, u32, u32)> { app.session.active().map(|st| (st.doc.id, st.doc.size.width, st.doc.size.height)) @@ -823,7 +833,7 @@ fn straighten(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers, p: [f64; // A press it takes brings the hidden frame back like any other press, even when the line is // too short to turn the frame. if matches!(ev, ToolEvent::Down { .. }) { - app.crop.dismissed = None; + show_frame(app); } claim(app); // Default mode: the image turns so the line ends up level. @@ -844,7 +854,7 @@ fn straighten(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers, p: [f64; /// Starts the drag a press at `p` selects: resize, move, turn or draw. fn begin_drag(app: &mut PhotocraftApp, p: [f64; 2]) { - app.crop.dismissed = None; + show_frame(app); claim(app); app.crop.editing = true; app.crop.press_step = Some(step_of(app)); diff --git a/crates/ui-egui/src/tools/crop_ui/tests.rs b/crates/ui-egui/src/tools/crop_ui/tests.rs index babe3fe85a8..ea456ab6a42 100644 --- a/crates/ui-egui/src/tools/crop_ui/tests.rs +++ b/crates/ui-egui/src/tools/crop_ui/tests.rs @@ -138,6 +138,41 @@ fn enter_on_the_selection_frame_crops_to_it_at_8_and_16_bit() { } } +/// A commit hides the frame; picking the Crop tool again, a ratio, or a press brings back the +/// default frame, and the press's crop keeps its box Undo and Redo. +#[test] +fn a_hidden_frame_comes_back_on_re_picking_a_ratio_or_a_press_with_redo_intact() { + let committed = || { + let mut app = app(SampleType::U8); + app.ui.crop_rect = Some([20.0, 10.0, 120.0, 60.0]); + crate::canvas::commit_crop(&mut app); + ensure_frame(&mut app); + assert_eq!(app.ui.crop_rect, None, "hidden after the commit"); + app + }; + let whole = Some([0.0, 0.0, 100.0, 50.0]); + // Picking the Crop tool while it is still picked. + let mut app = committed(); + crate::tool_memory::select(&mut app, Tool::Crop); + assert_eq!(app.ui.crop_rect, whole); + assert!(app.crop.default_frame); + // A ratio fits the frame it brings back. + let mut app = committed(); + app.ui.tool_options.crop_ratio = "1:1".into(); + apply_ratio(&mut app); + assert_eq!(app.ui.crop_rect, Some([25.0, 0.0, 75.0, 50.0])); + // A drawn crop undoes to the default frame, and redoes. + let mut app = committed(); + drag(&mut app, &[[10.0, 10.0], [40.0, 30.0]], NONE); + let drawn = app.ui.crop_rect; + assert_eq!(drawn, Some([10.0, 10.0, 40.0, 30.0])); + assert!(intercept(&mut app, "edit.undo").is_some()); + ensure_frame(&mut app); + assert_eq!(app.ui.crop_rect, whole, "undo goes back to the default frame"); + assert!(intercept(&mut app, "edit.redo").is_some()); + assert_eq!(app.ui.crop_rect, drawn, "and redo still has the drawn frame"); +} + #[test] fn the_frame_is_edited_from_a_press_until_commit_cancel_or_another_tool() { let mut app = app(SampleType::U8);