From 87c5278d032115b95520069aa9a20b46dcce7111 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 13:31:32 -0800 Subject: [PATCH] Shortcuts: a command listed only with a shortcut keeps a hold shortcut only with one A command that needs a value, such as Transparency, is listed in the editor only while it has a shortcut. shortcuts.set now refuses a hold shortcut for such a command without a shortcut, and the command loses its hold shortcut when its shortcut is cleared or given to another entry, through shortcuts.set, the Keyboard Shortcuts dialog or an imported set. Its key no longer runs a command the editor does not show, and it is free for another entry without a conflict. --- crates/mcp/src/ui_catalog.rs | 2 +- crates/ui-egui/src/menus/ui_commands.rs | 2 +- crates/ui-egui/src/shortcut_editor.rs | 50 +++++++++++++++++++++++-- docs/mcp.md | 4 +- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index 2974846982..543026c385 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -177,7 +177,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "shortcuts.set", "Set Keyboard Shortcut", "", - "{id: command id or tool:, shortcut?: \"Cmd+Shift+K\" | \"\" (none) | null (default), hold?: a character key alone or with Shift (\"X\", \"Shift+X\", \".\") | \"\" | null (none), force?} → {id, shortcut, hold, removedFrom}; `hold` alone leaves the shortcut as it is; a hold shortcut acts while the holdShortcuts preference is on", + "{id: command id or tool:, shortcut?: \"Cmd+Shift+K\" | \"\" (none) | null (default), hold?: a character key alone or with Shift (\"X\", \"Shift+X\", \".\") | \"\" | null (none), force?} → {id, shortcut, hold, removedFrom}; `hold` alone leaves the shortcut as it is; a command listed only with a shortcut takes a hold only with one and loses it with the shortcut; a hold shortcut acts while the holdShortcuts preference is on", ), ("shortcuts.list", "List Keyboard Shortcuts", "", "{query?} → [{id, label, group, shortcut, hold, default, overridden}]"), ("shortcuts.conflicts", "Keyboard Shortcut Conflicts", "", "{} → [{shortcut, ids}]"), diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index 6d6fb85307..b2647ed3eb 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -178,7 +178,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "shortcuts.set", "Set Keyboard Shortcut", "", - "{id: command id or tool:, shortcut?: \"Cmd+Shift+K\" | \"\" (none) | null (default), hold?: a character key alone or with Shift (\"X\", \"Shift+X\", \".\") | \"\" | null (none), force?} → {id, shortcut, hold, removedFrom}; `hold` alone leaves the shortcut as it is; a hold shortcut acts while the holdShortcuts preference is on", + "{id: command id or tool:, shortcut?: \"Cmd+Shift+K\" | \"\" (none) | null (default), hold?: a character key alone or with Shift (\"X\", \"Shift+X\", \".\") | \"\" | null (none), force?} → {id, shortcut, hold, removedFrom}; `hold` alone leaves the shortcut as it is; a command listed only with a shortcut takes a hold only with one and loses it with the shortcut; a hold shortcut acts while the holdShortcuts preference is on", ), ("shortcuts.list", "List Keyboard Shortcuts", "", "{query?} → [{id, label, group, shortcut, hold, default, overridden}]"), ("shortcuts.conflicts", "Keyboard Shortcut Conflicts", "", "{} → [{shortcut, ids}]"), diff --git a/crates/ui-egui/src/shortcut_editor.rs b/crates/ui-egui/src/shortcut_editor.rs index a1cb43d874..106c04405b 100644 --- a/crates/ui-egui/src/shortcut_editor.rs +++ b/crates/ui-egui/src/shortcut_editor.rs @@ -1165,9 +1165,20 @@ pub fn hold_of(holds: &Holds, key: &str) -> Option { } /// The entry whose usable hold shortcut is `chord`: the first in `holds`' order when several are. -/// An entry no longer listed (a removed plug-in's) keeps its text but does not hold. +/// An entry no longer listed (a removed plug-in's, or one that [`needs_a_value`] whose shortcut +/// was taken away) keeps its text but does not hold. pub(crate) fn hold_for<'a>(holds: &'a Holds, chord: &craft_keymap::Chord) -> Option<&'a str> { - holds.iter().find(|(k, t)| hold_chord(t).is_ok_and(|c| c == *chord) && entry(k).is_some()).map(|(k, _)| k.as_str()) + let mirror = current(); + holds + .iter() + .find(|(k, t)| hold_chord(t).is_ok_and(|c| c == *chord) && entry(k).is_some_and(|e| listed(mirror.keymap.overrides(), &e))) + .map(|(k, _)| k.as_str()) +} + +/// Removes the hold shortcuts of the entries the editor does not list under `overrides`: one that +/// [`needs_a_value`] loses its hold with its shortcut. A removed plug-in's entry keeps its hold. +fn drop_unlisted_holds(holds: &mut Holds, overrides: &Overrides) { + holds.retain(|k, _| entry(k).is_none_or(|e| listed(overrides, &e))); } /// Sets (`Some`) or removes (`None` or `""`) the hold shortcut of `key`. Another entry that holds @@ -1540,6 +1551,10 @@ pub fn run_command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option Option Option Err("no file reader".into()), } }; - data.and_then(|v| Ok((import_json(&v)?, import_holds(&v)?))).map(|((set, ov), holds)| { + data.and_then(|v| Ok((import_json(&v)?, import_holds(&v)?))).map(|((set, ov), mut holds)| { let n = ov.len(); + drop_unlisted_holds(&mut holds, &ov); app.ui.shortcut_overrides = ov; app.ui.shortcut_holds = holds; app.ui.shortcut_set = set; @@ -2066,6 +2083,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { if let Cow::Owned(km) = &km { d.fields.insert("overrides".into(), stored::to_value(&saved_overrides(km.overrides(), &saved))); } + drop_unlisted_holds(&mut holds, km.overrides()); d.fields.insert("holds".into(), json!(holds)); if d.str("__recording").is_empty() { d.fields.insert("__recordingHold".into(), json!(false)); @@ -3580,6 +3598,32 @@ mod tests { assert_eq!(app.ui.shortcut_holds.keys().collect::>(), ["select.none"]); } + /// A command the editor lists only with a shortcut (Transparency needs a value) takes a hold + /// shortcut only with one, and loses its hold, as it stops being listed, when the shortcut is + /// taken away: by clearing it or by giving it to another command. + #[test] + fn a_hold_shortcut_needs_a_listed_entry() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + assert!(needs_a_value("transparency.set")); + let r = app.run("shortcuts.set", json!({"id": "transparency.set", "hold": "Q"})); + assert!(r.is_err(), "{r:?}"); + assert!(app.ui.shortcut_holds.is_empty() && app.ui.shortcut_overrides.is_empty()); + let q = vc_chord("Q").unwrap(); + app.run("shortcuts.set", json!({"id": "transparency.set", "shortcut": "Cmd+Alt+Shift+F9", "hold": "Q"})).unwrap(); + assert_eq!(hold_for(&app.ui.shortcut_holds, &q), Some("transparency.set")); + let rows = with_steady_plugins(|| app.run("shortcuts.list", json!({"query": "transparency.set"})).unwrap()); + assert!(rows.as_array().unwrap().iter().any(|r| r["id"] == "transparency.set" && r["hold"] == "Q"), "{rows}"); + app.run("shortcuts.set", json!({"id": "transparency.set", "shortcut": ""})).unwrap(); + assert_eq!(hold_for(&app.ui.shortcut_holds, &q), None); + assert!(app.ui.shortcut_holds.is_empty(), "{:?}", app.ui.shortcut_holds); + // The key is free for another command, with no conflict left behind. + app.run("shortcuts.set", json!({"id": "paint.swap", "hold": "Q"})).unwrap(); + assert_eq!(app.run("shortcuts.conflicts", json!({})).unwrap(), json!([])); + app.run("shortcuts.set", json!({"id": "transparency.set", "shortcut": "Cmd+Alt+Shift+F9", "hold": "Shift+Q"})).unwrap(); + app.run("shortcuts.set", json!({"id": "edit.duplicate", "shortcut": "Cmd+Alt+Shift+F9", "force": true})).unwrap(); + assert_eq!(app.ui.shortcut_holds.keys().collect::>(), ["paint.swap"]); + } + /// Reset All and a preset clear the hold shortcuts; two entries holding one key are a conflict. #[test] fn hold_shortcut_conflicts_reset_and_presets() { diff --git a/docs/mcp.md b/docs/mcp.md index 6b5e55116c..ea0ce74230 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6649,7 +6649,9 @@ has a hold shortcut, or a key the active tool uses itself (E with the Extend Pat `shortcuts.set {id, hold}` sets a hold shortcut, and `hold: ""` or `null` removes it; `hold` alone leaves the entry's shortcut as it is, and a key that another entry already holds is refused unless `force` is true. Hold shortcuts are kept apart from shortcuts: Use Default, Clear and `shortcut: null` leave them, and `overridden` in `shortcuts.list` is -about the shortcut only. `shortcuts.list` rows give the hold shortcut as `hold`, `shortcuts.conflicts` lists a key two +about the shortcut only. A command the editor lists only while it has a shortcut (one that needs a value, such as +`transparency.set`) takes a hold shortcut only with a shortcut, and loses its hold shortcut when its shortcut is cleared +or given to another entry. `shortcuts.list` rows give the hold shortcut as `hold`, `shortcuts.conflicts` lists a key two entries hold as `"Hold X"`, and Reset to Defaults and a preset remove every hold shortcut. `shortcuts.export` writes them as a `holds` object (entry id to key) when there are any, and `shortcuts.import` reads it; a file without `holds` has none, and a `holds` value that is not a usable hold shortcut fails the import, which then changes nothing.