PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Esc deselects when no active operation needs cancelling (#2765)
mroxso4db47f2bcc2a
diff --git a/crates/ui-egui/src/input_tests.rs b/crates/ui-egui/src/input_tests.rsindex df51d69d5..35794f128 100644--- a/crates/ui-egui/src/input_tests.rs+++ b/crates/ui-egui/src/input_tests.rs@@ -651,3 +651,45 @@ fn esc_ends_a_pen_path_and_keeps_it() {assert_eq!(path.subpaths[0].knots.len(), 3);assert_eq!(h.state().ui.selected_path.as_deref(), Some("work"));}++/// Esc with no active operation deselects (#2674), like ⌘D.+#[test]+fn escape_deselects_when_nothing_else_needs_cancelling() {+ let mut h = harness();+ h.state_mut().run("select.all", json!({})).unwrap();+ h.run_steps(1);+ assert!(h.state().session.active().unwrap().doc.selection.is_some());+ h.key_press(Key::Escape);+ h.run_steps(2);+ assert!(h.state().session.active().unwrap().doc.selection.is_none(), "Esc deselects");+ assert!(!h.state().ui.status_error, "no error: {}", h.state().ui.status);+ // With nothing selected, Esc stays untouched (no history step, no error).+ let steps = h.state().session.active().unwrap().history.entries().len();+ h.key_press(Key::Escape);+ h.run_steps(2);+ assert_eq!(h.state().session.active().unwrap().history.entries().len(), steps, "no Deselect step");+}++/// The dialog owns Esc: it cancels the dialog and the selection stays.+#[test]+fn escape_with_a_dialog_open_cancels_the_dialog_not_the_selection() {+ let mut h = harness();+ h.state_mut().run("select.all", json!({})).unwrap();+ open_dialog(&mut h);+ h.key_press(Key::Escape);+ h.run_steps(2);+ assert!(h.state().ui.dialogs.is_empty(), "Esc cancelled the dialog");+ assert!(h.state().session.active().unwrap().doc.selection.is_some(), "the selection stays");+}++/// The command palette owns Esc: it closes the palette and the selection stays.+#[test]+fn escape_with_the_palette_open_does_not_deselect() {+ let mut h = harness();+ h.state_mut().run("select.all", json!({})).unwrap();+ h.state_mut().ui.palette_open = true;+ h.run_steps(1);+ h.key_press(Key::Escape);+ h.run_steps(2);+ assert!(h.state().session.active().unwrap().doc.selection.is_some(), "Esc belongs to the palette");+}diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rsindex 6ff4f2850..6584dfa04 100644--- a/crates/ui-egui/src/shortcuts.rs+++ b/crates/ui-egui/src/shortcuts.rs@@ -451,6 +451,32 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {return;}}+ // Esc with no active operation deselects (#2674), like ⌘D. The key is only taken when a+ // selection exists, and never from a state that handles Esc later in the frame: menus,+ // dialogs and inline type returned above, transform, pen, lasso and crop consumed it, and+ // these own theirs: full screen (Esc returns), the palette, the canvas and layer menus, the+ // brush picker, Rotate View, Adaptive Wide Angle, the Filter Gallery, the TIFF prompt, any+ // open egui popup and the toolbar tool flyout. A canvas drag in progress (a marquee being+ // drawn) keeps the selection too.+ let esc_taken = app.ui.view.hides_chrome()+ || app.drag.is_some()+ || app.ui.palette_open+ || app.ui.canvas_tool_menu.is_some()+ || app.ui.layer_menu.is_some()+ || app.ui.brush_picker.is_some()+ || app.ui.tool == Tool::RotateView+ || app.wide_angle.is_some()+ || app.tiff_options.is_some()+ || app.distort.gallery.is_some()+ || ctx.any_popup_open()+ || ctx.data(|d| d.get_temp::<(egui::Id, egui::Rect)>(egui::Id::new("tool-flyout")).is_some());+ if !esc_taken && app.session.active().is_some_and(|st| st.doc.selection.is_some()) && pressed(Key::Escape) {+ if let Err(e) = app.run("select.deselect", json!({})) {+ app.ui.status = e;+ app.ui.status_error = true;+ }+ return;+ }// Tool keys; pressing the key of the current group cycles within it. With Preferences ›// Tools › Use Shift Key for Tool Switch, only ⇧+key cycles and the plain key keeps the// group's current tool. From another group, the key picks the group's last-used tool.