PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix #1272: a reassigned shortcut runs its new command (#1330)
BFlatd836e3458d77
diff --git a/crates/ui-egui/src/shortcut_capture_tests.rs b/crates/ui-egui/src/shortcut_capture_tests.rsindex 796c99c23..994a6e9f5 100644--- a/crates/ui-egui/src/shortcut_capture_tests.rs+++ b/crates/ui-egui/src/shortcut_capture_tests.rs@@ -104,3 +104,40 @@ fn a_shortcut_in_use_is_reported_and_moved_on_ok() {crate::shortcut_dispatch::bindings(app).into_iter().filter(|(_, sc)| *sc == crate::shortcuts::parse("Cmd+J").unwrap()).map(|(id, _)| id).collect();assert_eq!(bound, ["edit.search"], "one owner for ⌘J");}++#[test]+fn a_function_key_moved_off_a_panel_toggle_runs_its_new_command() {+ // #1272: F5 given to Select › Modify › Feather… is taken from Window › Brush Settings, whose+ // row in the dialog is Photoshop's `window.panel.brushSettings` while the key ran the shell's+ // `window.toggle.brushSettings`.+ let (mut h, id) = dialog(1.0, "select.modify.feather", "feather");+ capture(&mut h, &crate::shortcuts::pretty("Shift+F6"), Key::ShiftLeft, Modifiers::NONE, Key::F5);+ let message = field(&h, id, "message");+ assert!(message.as_str().unwrap().contains("already in use"), "{message}");+ crate::dialogs::confirm(h.state_mut(), id).unwrap();+ h.run_steps(2);+ let f5 = crate::shortcuts::parse("F5").unwrap();+ let bound: Vec<String> = crate::shortcut_dispatch::bindings(h.state()).into_iter().filter(|(_, sc)| *sc == f5).map(|(id, _)| id).collect();+ assert_eq!(bound, ["select.modify.feather"], "one owner for F5");+ // Pressing F5 opens Feather, not Brush Settings.+ let ctx = h.ctx.clone();+ crate::shortcut_dispatch::set_dry_run(&ctx, true);+ key(&mut h, Key::F5, true, Modifiers::NONE);+ h.run_steps(1);+ key(&mut h, Key::F5, false, Modifiers::NONE);+ h.run_steps(1);+ let log = crate::shortcut_dispatch::take_log(&ctx);+ assert!(log.iter().any(|(id, _)| id == "select.modify.feather"), "{log:?}");+ assert!(!h.state().ui.panels.brush_settings);+}++#[test]+fn a_user_shortcut_wins_over_a_default_it_was_not_moved_from() {+ // A key assigned by hand (here through the prefs file, so no row was cleared) still reaches+ // the command it was given to rather than the built-in owner.+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("edit.keyboardShortcuts", json!({"set": {"select.modify.feather": "F7"}, "allowUnknown": true, "removeConflicts": false})).unwrap();+ let f7 = crate::shortcuts::parse("F7").unwrap();+ let bound: Vec<String> = crate::shortcut_dispatch::bindings(&app).into_iter().filter(|(_, sc)| *sc == f7).map(|(id, _)| id).collect();+ assert_eq!(bound, ["select.modify.feather"]);+}diff --git a/crates/ui-egui/src/shortcut_dispatch.rs b/crates/ui-egui/src/shortcut_dispatch.rsindex 14fa6d04b..f304a1659 100644--- a/crates/ui-egui/src/shortcut_dispatch.rs+++ b/crates/ui-egui/src/shortcut_dispatch.rs@@ -50,7 +50,16 @@ pub const SECONDARY: &[(&str, &str)] = &[("edit.fill", "Shift+Backspace")];/// their overrides apply. Held temporary tools (Space…) are not here: see [`crate::hold_keys`].pub fn bindings(app: &PhotocraftApp) -> Vec<(String, KeyboardShortcut)> {let prefs = app.session.prefs();- let ui = crate::menus::UI_COMMANDS.iter().map(|(id, _, _, sc)| (*id, prefs.shortcut(id, *sc)));+ // The Keyboard Shortcuts dialog lists Photoshop's Window › <panel> item (`window.panel.*`)+ // in place of the shell command it runs (`window.toggle.*`, see `menus::panel_alias`), so an+ // edit to that row reassigns or removes the shell command's default too (#1272).+ let aliased = |id: &str, sc: Option<&str>| {+ !prefs.shortcuts.contains_key(id)+ && prefs.shortcuts.keys().any(|k| {+ crate::menus::panel_alias(k) == Some(id) && crate::menu_catalog::CATALOG.iter().any(|c| c.3 == k.as_str() && c.2.is_some() && c.2 == sc)+ })+ };+ let ui = crate::menus::UI_COMMANDS.iter().map(|(id, _, _, sc)| (*id, if aliased(id, *sc) { None } else { prefs.shortcut(id, *sc) }));let engine = photocraft_engine::command_specs().iter().map(|c| (c.id, prefs.shortcut(c.id, c.shortcut)));let own: std::collections::HashSet<&str> = crate::menus::UI_COMMANDS.iter()@@ -73,8 +82,13 @@ pub fn bindings(app: &PhotocraftApp) -> Vec<(String, KeyboardShortcut)> {.map(|(id, sc)| (id.as_str(), Some(sc.as_str())));// Photoshop's second shortcuts, kept while the command's main one is the default.let secondary = SECONDARY.iter().filter(|(id, _)| !prefs.shortcuts.contains_key(*id)).map(|&(id, sc)| (id, Some(sc)));+ // A key the user assigned in Edit › Keyboard Shortcuts belongs to that command, ahead of any+ // default that still names it (#1272): OK clears the old owner's row, but that row can be+ // another id for the same item, or a default the dialog doesn't list.+ let mut candidates: Vec<(&str, Option<&str>)> = ui.chain(engine).chain(catalog).chain(overrides).chain(secondary).collect();+ candidates.sort_by_key(|(id, sc)| !(sc.is_some() && prefs.shortcuts.get(*id).map(String::as_str) == *sc));let mut all: Vec<(String, KeyboardShortcut)> = Vec::new();- for (id, sc) in ui.chain(engine).chain(catalog).chain(overrides).chain(secondary) {+ for (id, sc) in candidates {let Some(sc) = sc.and_then(parse) else { continue };if !all.iter().any(|(_, b)| *b == sc) {all.push((id.to_string(), sc));