From 2ea588d42ac6d669ff30e52c0396ffa09ff917b0 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:12:07 +0100 Subject: [PATCH] Crop: Esc hides the crop frame until a press or re-picking the tool (#10370) Esc cancelled the crop but the next frame made the default frame again, so the borders never went away. Esc, the options bar's cancel and the crop menu's Cancel now hide the frame (crop_ui::dismiss); a press on the canvas draws a new one, and leaving and re-picking the tool brings the default frame back. Co-Authored-By: Claude Sonnet 5.5 --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 2 +- crates/ui-egui/src/input/shortcuts.rs | 2 +- crates/ui-egui/src/panels/options_bar.rs | 2 +- crates/ui-egui/src/tools/crop_ui.rs | 21 ++++++++ crates/ui-egui/src/tools/crop_ui/tests.rs | 53 +++++++++++++++++++ 5 files changed, 77 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index 9d5b84651f..b61afcc7f0 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -313,7 +313,7 @@ fn choose_crop(app: &mut PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> use crate::crop_overlay::{CropOverlay, OverlayShow}; use crate::crop_presets::{Choice, apply_choice}; match command { - CROP_CANCEL => crate::crop_ui::cancel(app), + CROP_CANCEL => crate::crop_ui::dismiss(app), CROP_COMMIT => crate::canvas::commit_crop(app), CROP_NEW => apply_choice(app, Choice::New), CROP_DELETE => apply_choice(app, Choice::Delete), diff --git a/crates/ui-egui/src/input/shortcuts.rs b/crates/ui-egui/src/input/shortcuts.rs index e6a20bae55..277ca0af29 100644 --- a/crates/ui-egui/src/input/shortcuts.rs +++ b/crates/ui-egui/src/input/shortcuts.rs @@ -819,7 +819,7 @@ fn handle_registry_and_tool_keys(app: &mut PhotocraftApp, ctx: &egui::Context, f app.ui.polygon.clear(); app.ui.polygon_mode.clear(); app.ui.polygon_doc = None; - crate::crop_ui::cancel(app); + crate::crop_ui::dismiss(app); return true; } } diff --git a/crates/ui-egui/src/panels/options_bar.rs b/crates/ui-egui/src/panels/options_bar.rs index e03fa24e7a..eba8fd1d24 100644 --- a/crates/ui-egui/src/panels/options_bar.rs +++ b/crates/ui-egui/src/panels/options_bar.rs @@ -539,7 +539,7 @@ fn selection_specific_options(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: crate::canvas::commit_crop(app); } if icons::button(ui, "ban", 26.0, false, tl!("Cancel current crop operation (Esc)")).clicked() { - crate::crop_ui::cancel(app); + crate::crop_ui::dismiss(app); } }); } diff --git a/crates/ui-egui/src/tools/crop_ui.rs b/crates/ui-egui/src/tools/crop_ui.rs index 3a05ba2cc7..350296a690 100644 --- a/crates/ui-egui/src/tools/crop_ui.rs +++ b/crates/ui-egui/src/tools/crop_ui.rs @@ -51,6 +51,10 @@ pub struct CropState { pub default_frame: bool, /// The document (index and size) the default frame was made for. frame_for: Option<(usize, u32, u32)>, + /// The document (index and size) whose frame Esc (or the options bar's cancel) hid: with the + /// Crop tool still picked no default frame is made for it until a press on the canvas, or + /// leaving and re-picking the tool, as in Photoshop (#10370). + dismissed: Option<(usize, u32, u32)>, /// The frame is being edited (pressed since it was made): the canvas shows what lies past it. pub editing: bool, /// The document the pending frame (or gesture) belongs to ([`cancel_stale`], #1918). @@ -350,6 +354,7 @@ pub fn ensure_frame(app: &mut PhotocraftApp) { fn frame(app: &mut PhotocraftApp) { if app.ui.tool != Tool::Crop { app.crop.editing = false; + app.crop.dismissed = None; crate::crop_straighten::reset(app); // Leaving the tool drops an untouched default frame and, as in Photoshop, commits a // drawn one (#3368). @@ -377,6 +382,11 @@ fn frame(app: &mut PhotocraftApp) { if app.ui.crop_rect.is_some() || app.crop.drag.is_some() { return; } + // Esc hid the frame: it stays hidden for this document until the next press. + if key.is_some() && app.crop.dismissed == key { + return; + } + app.crop.dismissed = None; let Some(st) = app.session.active() else { return }; let canvas = st.doc.bounds(); // Computed once per new frame, not per frame drawn. @@ -433,6 +443,16 @@ pub fn cancel(app: &mut PhotocraftApp) { crate::crop_straighten::reset(app); } +/// Esc or the options bar's cancel: [`cancel`], then, with the Crop tool picked, the frame stays +/// hidden (no new default frame) until a press on the canvas or re-picking the tool, as in +/// Photoshop (#10370). +pub fn dismiss(app: &mut PhotocraftApp) { + cancel(app); + if app.ui.tool == Tool::Crop { + app.crop.dismissed = app.session.active_index().zip(app.session.active()).map(|(i, st)| (i, st.doc.size.width, st.doc.size.height)); + } +} + /// Drop the box's own Undo/Redo (leaving the tool, cancelling or committing a crop). pub fn clear_history(app: &mut PhotocraftApp) { app.crop.undo.clear(); @@ -815,6 +835,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; 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 6fba4e57d4..86c67c42bf 100644 --- a/crates/ui-egui/src/tools/crop_ui/tests.rs +++ b/crates/ui-egui/src/tools/crop_ui/tests.rs @@ -1144,3 +1144,56 @@ fn readout_shows_the_frame_size_while_drawing_or_resizing() { assert_eq!(frame_size([f64::NAN, 0.0, 1.0, 1.0]), None); assert_eq!(frame_size([-f64::MAX, 0.0, f64::MAX, 1.0]), None); } + +/// Esc through the real shortcut handler. +fn press_esc(app: &mut PhotocraftApp) { + let key = egui::Key::Escape; + let input = egui::RawInput { + focused: true, + events: vec![egui::Event::Key { key, physical_key: Some(key), pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }], + ..Default::default() + }; + egui::Context::default().run_ui(input, |ui| crate::shortcuts::handle(app, ui.ctx())).textures_delta.clear(); +} + +#[test] +fn esc_hides_the_frame_until_a_press_or_re_picking_the_tool() { + // #10370: Esc cancelled the crop but the next frame made the default frame again. + for classic in [false, true] { + let mut app = app(SampleType::U8); + app.ui.tool_options.crop_shield.classic_mode = classic; + ensure_frame(&mut app); + assert!(app.ui.crop_rect.is_some() && app.crop.default_frame); + // The untouched default frame: Esc hides it, and it stays hidden frame after frame. + press_esc(&mut app); + for _ in 0..3 { + ensure_frame(&mut app); + assert_eq!(app.ui.crop_rect, None, "classic={classic}"); + } + // A press on the canvas draws a new frame. + drag(&mut app, &[[50.0, 20.0], [80.0, 40.0], [120.0, 70.0]], NONE); + assert_eq!(app.ui.crop_rect, Some([50.0, 20.0, 120.0, 70.0])); + // A drawn frame: Esc hides it too, and crops nothing. + press_esc(&mut app); + ensure_frame(&mut app); + assert_eq!(app.ui.crop_rect, None); + assert_eq!(app.session.active().unwrap().doc.size, Size::new(200, 100)); + // Re-picking the tool brings the default frame back. + app.ui.tool = Tool::Brush; + ensure_frame(&mut app); + app.ui.tool = Tool::Crop; + ensure_frame(&mut app); + assert!(app.ui.crop_rect.is_some() && app.crop.default_frame); + // The options bar's cancel hides it as well; a plain cancel (stale document) does not. + dismiss(&mut app); + ensure_frame(&mut app); + assert_eq!(app.ui.crop_rect, None); + app.ui.tool = Tool::Brush; + ensure_frame(&mut app); + app.ui.tool = Tool::Crop; + ensure_frame(&mut app); + cancel(&mut app); + ensure_frame(&mut app); + assert!(app.ui.crop_rect.is_some()); + } +}