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.
This commit is contained in:
dotCooCoo
2026-11-16 13:31:32 -08:00
parent 2caaabbc8f
commit 87c5278d03
4 changed files with 52 additions and 6 deletions
+1 -1
View File
@@ -177,7 +177,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"shortcuts.set",
"Set Keyboard Shortcut",
"",
"{id: command id or tool:<id>, 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:<id>, 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}]"),
+1 -1
View File
@@ -178,7 +178,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
"shortcuts.set",
"Set Keyboard Shortcut",
"",
"{id: command id or tool:<id>, 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:<id>, 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}]"),
+47 -3
View File
@@ -1165,9 +1165,20 @@ pub fn hold_of(holds: &Holds, key: &str) -> Option<String> {
}
/// 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<Resu
assign(&mut ov, &key, shortcut.as_deref(), force)
};
let r = r.and_then(|mut removed| {
// An entry the editor lists only with a shortcut takes a hold shortcut only with one.
if hold.as_deref().is_some_and(|h| !h.is_empty()) && entry(&key).is_some_and(|e| !listed(&ov, &e)) {
return Err(format!("`{key}` needs a shortcut before it can have a hold shortcut"));
}
if has_hold {
for k in assign_hold(&mut holds, &key, hold.as_deref(), force)? {
if !removed.contains(&k) {
@@ -1547,6 +1562,7 @@ pub fn run_command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option<Resu
}
}
}
drop_unlisted_holds(&mut holds, &ov);
Ok(removed)
});
r.map(|removed| {
@@ -1742,8 +1758,9 @@ pub fn run_command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option<Resu
(_, None) => 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::<Vec<_>>(), ["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::<Vec<_>>(), ["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() {
+3 -1
View File
@@ -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.