PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Shortcuts: a command shortcut leaves the focused menu bar and runs (#6573)
David Mann65dc117b4b83
diff --git a/crates/ui-egui/src/menu_keys.rs b/crates/ui-egui/src/menu_keys.rsindex 5762b2a94..2767db828 100644--- a/crates/ui-egui/src/menu_keys.rs+++ b/crates/ui-egui/src/menu_keys.rs@@ -61,6 +61,40 @@ pub fn is_focused(ctx: &egui::Context) -> bool {ctx.data(|d| d.get_temp::<u64>(focus_id())).is_some_and(|seen| seen + 2 >= pass)}+/// A command shortcut pressed while the keyboard is on the bar with no menu open (⌘D, ⌘⇧I, F7…)+/// takes it off the bar, so the shortcut runs in the same press, as a native menu bar does+/// (#6573). Keys the bar uses (arrows, ↩, Esc, letters, Alt+letter, F10) keep driving it, and+/// an open menu still swallows shortcuts. True when the bar was left.+pub fn leave_for_shortcut(ctx: &egui::Context) -> bool {+ if !is_focused(ctx) || crate::menu_nav::is_open(ctx) {+ return false;+ }+ let shortcut = ctx.input(|i| i.events.iter().any(|e| matches!(e, Event::Key { key, pressed: true, modifiers, .. } if is_command_key(*key, *modifiers))));+ if !shortcut {+ return false;+ }+ let mut keys = Keys::load(ctx);+ keys.focus = None;+ keys.underline = false;+ keys.store(ctx);+ ctx.request_repaint();+ true+}++/// A key press that is a command shortcut rather than a key of the bar: ⌘ / Ctrl with a key+/// (not the modifier keys themselves), or a function key other than F10 (which toggles the bar).+fn is_command_key(key: Key, mods: Modifiers) -> bool {+ let modifier_key = matches!(+ key,+ Key::ShiftLeft | Key::ShiftRight | Key::ControlLeft | Key::ControlRight | Key::AltLeft | Key::AltRight | Key::SuperLeft | Key::SuperRight+ );+ if modifier_key {+ return false;+ }+ let function_key = key != Key::F10 && key.name().strip_prefix('F').is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()));+ mods.command || mods.ctrl || mods.mac_cmd || function_key+}+/// The mnemonic a key press stands for: the letter or digit of a plain key.fn key_char(key: Key) -> Option<char> {let name = key.name();@@ -296,6 +330,21 @@ mod tests {assert_eq!(key_char(Key::ArrowDown), None);assert_eq!(key_char(Key::Space), None);}++ #[test]+ fn command_keys_are_command_combos_and_function_keys_but_f10() {+ assert!(is_command_key(Key::D, Modifiers::COMMAND));+ assert!(is_command_key(Key::I, Modifiers::COMMAND | Modifiers::SHIFT));+ assert!(is_command_key(Key::F7, Modifiers::NONE));+ assert!(is_command_key(Key::F12, Modifiers::SHIFT));+ assert!(!is_command_key(Key::F10, Modifiers::NONE), "F10 toggles the bar");+ assert!(!is_command_key(Key::ControlLeft, Modifiers::CTRL), "Ctrl going down is no shortcut yet");+ assert!(!is_command_key(Key::E, Modifiers::NONE), "a letter is a mnemonic");+ assert!(!is_command_key(Key::E, Modifiers::ALT), "Alt+letter opens a menu");+ assert!(!is_command_key(Key::ArrowRight, Modifiers::NONE));+ assert!(!is_command_key(Key::Enter, Modifiers::NONE));+ assert!(!is_command_key(Key::Escape, Modifiers::NONE));+ }}#[cfg(test)]diff --git a/crates/ui-egui/src/menu_keys_tests.rs b/crates/ui-egui/src/menu_keys_tests.rsindex 1f226054e..6b5aaa836 100644--- a/crates/ui-egui/src/menu_keys_tests.rs+++ b/crates/ui-egui/src/menu_keys_tests.rs@@ -210,3 +210,29 @@ fn the_shortcuts_wait_while_the_keyboard_is_on_the_bar() {h.run_steps(3);assert_ne!(h.state().ui.tool, tool, "off the bar, E picks the Eraser again");}++/// The whole app: with the bar focused by an Alt tap, a command shortcut leaves the bar and runs+/// in the same press (#6573); the bar's own keys still drive it.+#[test]+fn a_command_shortcut_leaves_the_bar_and_runs() {+ let mut h = Harness::builder().with_size(egui::vec2(1280.0, 800.0)).with_max_steps(64).build_eframe(|cc| {+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());+ let mut app = app();+ app.services.native_menu = None;+ app+ });+ h.run_steps(4);+ h.state_mut().run("select.all", json!({})).unwrap();+ h.run_steps(2);+ alt_tap(&mut h);+ assert_eq!(Keys::current(&h.ctx).focus, Some(0), "Alt puts the keyboard on File");+ key(&mut h, egui::Key::ArrowRight);+ assert_eq!(Keys::current(&h.ctx).focus, Some(1), "→ still moves along the bar");+ assert!(h.state().session.active().unwrap().doc.selection.is_some());+ h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::D);+ h.run_steps(3);+ assert!(h.state().session.active().unwrap().doc.selection.is_none(), "⌘D deselects");+ assert_eq!(Keys::current(&h.ctx).focus, None, "and takes the keyboard off the bar");+ assert!(!crate::menu_keys::is_focused(&h.ctx));+ assert!(!is_open(&h.ctx), "no menu opened");+}diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rsindex e8645a151..ccb48561c 100644--- a/crates/ui-egui/src/shortcuts.rs+++ b/crates/ui-egui/src/shortcuts.rs@@ -343,7 +343,9 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {}// An open menu owns the keyboard (arrows, ↩, Esc), like a native menu; so does the menu bar// once Alt or F10 put the keyboard on it (#4988), and an open dropdown list, whose letters- // pick an option rather than a tool (#1481).+ // pick an option rather than a tool (#1481). A command shortcut (⌘D, F7…) takes the keyboard+ // off the bar first and runs, as with a native menu bar (#6573).+ crate::menu_keys::leave_for_shortcut(ctx);if crate::menu_nav::is_open(ctx) || crate::menu_keys::is_focused(ctx) || crate::widgets::dropdown_list_open(ctx) {return;}