Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(engine): keyboard shortcut bulk set reports in-call clashes (#719) (#799)

bfenty38d23b621fdf
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index 68819fc94..0aa3adf19 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -1432,7 +1432,14 @@ fn keyboard_shortcuts(s: &mut Session, p: &Value) -> Result<Value> {
for (id, v) in m {
let Some(sc) = v.as_str().and_then(normalize_shortcut) else { continue };
for (c, def) in bindable() {
- if c != id && next.shortcut(c, def).and_then(normalize_shortcut).as_deref() == Some(sc.as_str()) {
+ // Never strip a command that this same call is assigning:
+ // two entries for one key are a clash inside the call, which
+ // the returned conflicts list reports (#719). Stripping
+ // each other here unbound both silently.
+ if c == id || m.contains_key(c) {
+ continue;
+ }
+ if next.shortcut(c, def).and_then(normalize_shortcut).as_deref() == Some(sc.as_str()) {
next.shortcuts.insert(c.to_string(), String::new());
}
}
diff --git a/crates/engine/tests/keyboard_shortcut_bulk.rs b/crates/engine/tests/keyboard_shortcut_bulk.rs
new file mode 100644
index 000000000..d016291c4
--- /dev/null
+++ b/crates/engine/tests/keyboard_shortcut_bulk.rs
@@ -0,0 +1,36 @@
+//! Regression tests for issue #719: a bulk `edit.keyboardShortcuts` set that
+//! assigns the same key to two commands must not silently unbind both; the
+//! clash inside the call must be reported in `conflicts`.
+use photocraft_engine::Session;
+use serde_json::{Value, json};
+
+fn shortcut_of(v: &Value, id: &str) -> Option<String> {
+ v["commands"].as_array()?.iter().find(|c| c["id"].as_str() == Some(id))?.get("shortcut").and_then(|s| s.as_str()).map(str::to_string)
+}
+
+#[test]
+fn bulk_set_same_key_twice_reports_conflict_and_keeps_bindings() {
+ let mut s = Session::new();
+ // edit.undo's default is already Cmd+Z; the bulk set also gives Cmd+Z to
+ // edit.fill. On main the conflict pass made each entry blank the other:
+ // both ended up unbound and conflicts was empty.
+ let r = s.execute("edit.keyboardShortcuts", json!({"set": {"edit.undo": "Cmd+Z", "edit.fill": "Cmd+Z"}, "list": true})).unwrap();
+ let fill = shortcut_of(&r, "edit.fill").unwrap_or_default();
+ let undo = shortcut_of(&r, "edit.undo").unwrap_or_default();
+ assert_eq!(fill, "Cmd+Z", "edit.fill must keep the key it was just given (empty on main)");
+ assert_eq!(undo, "Cmd+Z", "edit.undo must keep the key it was just given (empty on main)");
+ let conflicts = r["conflicts"].as_array().unwrap();
+ let clash = conflicts.iter().any(|c| c["shortcut"].as_str() == Some("Cmd+Z") && c["commands"].as_array().is_some_and(|ids| ids.len() == 2));
+ assert!(clash, "the in-call clash must appear in conflicts, got {}", r["conflicts"]);
+}
+
+#[test]
+fn single_set_still_steals_from_old_owner() {
+ let mut s = Session::new();
+ // Control: the documented single-assignment behaviour must be unchanged —
+ // moving Cmd+Z to edit.fill takes it away from edit.undo.
+ let r = s.execute("edit.keyboardShortcuts", json!({"set": {"edit.fill": "Cmd+Z"}, "list": true})).unwrap();
+ assert_eq!(shortcut_of(&r, "edit.fill").as_deref(), Some("Cmd+Z"));
+ let undo = shortcut_of(&r, "edit.undo").unwrap_or_default();
+ assert_ne!(undo, "Cmd+Z", "edit.undo must lose the key when it moves to edit.fill");
+}