PhotoCraft: An open-source, photo editor written by scratch in pure Rust
View: Esc leaves full screen modes (part of #4116) (#4145)
FredApps7015f2916db4
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 1092f4081..a144477cd 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1710,9 +1710,6 @@ impl eframe::App for PhotocraftApp {// system title bar this is where the document name lives, as the in-app title is hidden.panels::sync_window_title(self, &ctx);let chrome = !self.ui.view.hides_chrome();- if !chrome && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) {- let _ = menus::invoke(self, &ctx, "view.screenMode.standard", serde_json::json!({}));- }if chrome && self.ui.panels.menu_bar {panels::title_bar(self, ui);}diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rsindex 7c0c8145f..a156a6ce3 100644--- a/crates/ui-egui/src/shortcuts.rs+++ b/crates/ui-egui/src/shortcuts.rs@@ -531,15 +531,13 @@ 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()+ // Esc with no active operation leaves a full screen mode, else deselects (#2674), like ⌘D.+ // The key is never taken from a state that handles Esc itself: menus, dialogs, text fields+ // and inline type returned above, transform, pen, lasso and crop consumed it, and these own+ // theirs later in the frame: 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 it too.+ let esc_taken = app.drag.is_some()|| app.ui.palette_open|| app.ui.canvas_tool_menu.is_some()|| app.ui.layer_menu.is_some()@@ -550,6 +548,15 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {|| 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());+ // Both full screen modes (with and without the menu bar) return to Standard, as in Photoshop+ // (#4116), through the menu command so the window and the Screen Mode check follow.+ if !esc_taken && app.ui.view.screen_mode != "standard" && pressed(Key::Escape) {+ if let Err(e) = crate::menus::invoke(app, ctx, "view.screenMode.standard", json!({})) {+ app.ui.status = e;+ app.ui.status_error = true;+ }+ return;+ }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;diff --git a/crates/ui-egui/src/view_cmds/tests.rs b/crates/ui-egui/src/view_cmds/tests.rsindex 34c93ccc2..3de1a20b4 100644--- a/crates/ui-egui/src/view_cmds/tests.rs+++ b/crates/ui-egui/src/view_cmds/tests.rs@@ -369,3 +369,47 @@ fn load_selection_without_alpha_channels_offers_the_composite() {assert_eq!(app.ui.dialog_mut(dialog).unwrap().fields["channel"], "RGB");crate::dialogs::confirm(&mut app, dialog).unwrap();}++/// One frame of the shortcut handler with a plain Esc press.+fn press_esc(app: &mut PhotocraftApp, ctx: &egui::Context) {+ 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()+ };+ ctx.run_ui(input, |ui| crate::shortcuts::handle(app, ui.ctx())).textures_delta.clear();+}++#[test]+fn esc_leaves_full_screen_modes_unless_something_else_owns_it() {+ let (mut app, ctx) = app_with(1);+ // Both full screen modes return to Standard, and the menu check follows (#4116).+ for mode in ["fullScreen", "fullScreenWithMenuBar"] {+ menu(&mut app, &ctx, &format!("view.screenMode.{mode}"), json!({})).unwrap();+ press_esc(&mut app, &ctx);+ assert_eq!(app.ui.view.screen_mode, "standard", "{mode}");+ assert_eq!(checked(&app, "view.screenMode.standard"), Some(true));+ }+ // In Standard, Esc leaves the mode alone.+ press_esc(&mut app, &ctx);+ assert_eq!(app.ui.view.screen_mode, "standard");+ // An open dialog takes Esc first.+ menu(&mut app, &ctx, "view.screenMode.fullScreen", json!({})).unwrap();+ menu(&mut app, &ctx, "image.canvasSize", json!({})).unwrap();+ assert_eq!(app.ui.dialogs.len(), 1);+ press_esc(&mut app, &ctx);+ assert_eq!(app.ui.view.screen_mode, "fullScreen");+ app.ui.dialogs.clear();+ // So does Free Transform: Esc cancels it and the mode stays.+ app.run("select.all", json!({})).unwrap();+ crate::transform_tool::begin(&mut app, &ctx).unwrap();+ assert!(app.ui.transform.is_some());+ press_esc(&mut app, &ctx);+ assert!(app.ui.transform.is_none());+ assert_eq!(app.ui.view.screen_mode, "fullScreen");+ // A selection stays while Esc leaves full screen; the next Esc deselects.+ press_esc(&mut app, &ctx);+ assert_eq!(app.ui.view.screen_mode, "standard");+ assert!(app.session.active().unwrap().doc.selection.is_some());+}