Skip to content

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

Preferences: reset a single shortcut or menu colour override (#988) (#1064)

TiredIsa619727023011
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index f4bb48505..d19f4d652 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -1080,6 +1080,18 @@ impl Preferences {
*self = Preferences::default();
return Ok(());
};
+ // These maps have no stored defaults; removing an override restores the fallback.
+ match keyed(path) {
+ Some(("shortcuts", id)) => {
+ self.shortcuts.remove(id);
+ return Ok(());
+ }
+ Some(("menus.colors", id)) => {
+ self.menus.colors.remove(id);
+ return Ok(());
+ }
+ _ => {}
+ }
let def = Preferences::default().get(path).ok_or_else(|| format!("unknown preference `{path}`"))?;
if path == "shortcuts" {
self.shortcuts.clear();
@@ -1404,7 +1416,12 @@ fn prefs_reset(s: &mut Session, p: &Value) -> Result<Value> {
}
s.prefs.edit(|_| ());
s.apply_prefs();
- prefs_get(s, &json!({"path": path.unwrap_or("")}))
+ if path.is_some_and(|path| keyed(path).is_some()) {
+ // The removed override is absent, so reading its old path would report an error.
+ Ok(Value::Null)
+ } else {
+ prefs_get(s, &json!({"path": path.unwrap_or("")}))
+ }
}
/// `edit.preferences.<section>`: the section's values (the GUI opens the dialog on it instead).
diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs
index 24363b291..ffcfb1b47 100644
--- a/crates/engine/src/prefs/tests.rs
+++ b/crates/engine/src/prefs/tests.rs
@@ -57,6 +57,84 @@ fn reset_one_colour_setting() {
assert!(s.execute("prefs.reset", json!({"path": "colorSettings.notAField"})).is_err());
}
+#[test]
+fn reset_one_keyed_override() {
+ let mut s = session();
+ s.execute(
+ "prefs.set",
+ json!({"values": {
+ "shortcuts.edit.undo": "Ctrl+Alt+Z", "shortcuts.edit.redo": "Ctrl+Alt+Y",
+ "menus.colors.edit.fill": "red", "menus.colors.filter.blur.gaussianBlur": "blue",
+ "interface.theme": "studioLight"
+ }}),
+ )
+ .unwrap();
+ let undo = crate::commands::find("edit.undo").unwrap().shortcut;
+ assert_eq!(s.prefs().shortcut("edit.undo", undo), Some("Ctrl+Alt+Z"));
+ for path in ["shortcuts.edit.undo", "menus.colors.filter.blur.gaussianBlur"] {
+ let revision = s.prefs.rev();
+ assert_eq!(s.execute("prefs.reset", json!({"path": path})).unwrap(), Value::Null);
+ assert!(s.prefs.rev() > revision);
+ assert!(s.execute("prefs.get", json!({"path": path})).is_err(), "removed override: {path}");
+ let mut restored = Session::new();
+ restored.load_prefs_json(&s.prefs_to_json()).unwrap();
+ assert_eq!(restored.prefs(), s.prefs(), "removal survives saving: {path}");
+ }
+ assert_eq!(s.execute("prefs.get", json!({"path": "shortcuts"})).unwrap(), json!({"edit.redo": "Ctrl+Alt+Y"}));
+ assert_eq!(s.execute("prefs.get", json!({"path": "menus.colors"})).unwrap(), json!({"edit.fill": "red"}));
+ assert_eq!(s.execute("prefs.get", json!({"path": "interface.theme"})).unwrap(), json!("studioLight"));
+ assert_eq!(s.prefs().shortcut("edit.undo", undo), undo);
+}
+
+#[test]
+fn reset_keyed_overrides_is_idempotent_and_restores_disabled_shortcuts() {
+ let mut s = session();
+ s.execute("prefs.set", json!({"path": "shortcuts.edit.undo", "value": ""})).unwrap();
+ let undo = crate::commands::find("edit.undo").unwrap().shortcut;
+ assert_eq!(s.prefs().shortcut("edit.undo", undo), None);
+ for path in ["shortcuts.edit.undo", "menus.colors.edit.fill"] {
+ for _ in 0..2 {
+ assert_eq!(s.execute("prefs.reset", json!({"section": path})).unwrap(), Value::Null);
+ }
+ }
+ assert_eq!(s.prefs().shortcut("edit.undo", undo), undo);
+ assert_eq!(s.prefs(), &Preferences::default());
+}
+
+#[test]
+fn preferences_reset_removes_keyed_overrides() {
+ let mut p = Preferences::default();
+ for (path, value) in [("shortcuts.edit.undo", json!("Ctrl+Z")), ("menus.colors.edit.fill", json!("red"))] {
+ p.set(path, value).unwrap();
+ assert!(p.get(path).is_some());
+ p.reset(Some(path)).unwrap();
+ assert!(p.get(path).is_none());
+ p.reset(Some(path)).unwrap();
+ }
+ assert_eq!(p, Preferences::default());
+}
+
+#[test]
+fn keyed_reset_keeps_section_resets_and_unknown_path_errors() {
+ let mut s = session();
+ s.execute("prefs.set", json!({"values": {"shortcuts.edit.undo": "Ctrl+Z", "menus.colors.edit.fill": "red"}})).unwrap();
+ let saved = s.prefs_value();
+ let revision = s.prefs.rev();
+ for path in ["shortcut.edit.undo", "menus.color.edit.fill", "menus.notAField", "general.notAField"] {
+ assert!(matches!(s.execute("prefs.reset", json!({"path": path})), Err(EngineError::BadParams { .. })));
+ assert_eq!(s.prefs_value(), saved, "rejected reset changes nothing: {path}");
+ assert_eq!(s.prefs.rev(), revision);
+ }
+ assert_eq!(s.execute("prefs.reset", json!({"path": "shortcuts"})).unwrap(), json!({}));
+ assert_eq!(s.execute("prefs.get", json!({"path": "menus.colors"})).unwrap(), json!({"edit.fill": "red"}));
+ assert_eq!(s.execute("prefs.reset", json!({"path": "menus.colors"})).unwrap(), json!({}));
+ s.execute("prefs.set", json!({"path": "shortcuts.edit.undo", "value": "Ctrl+Z"})).unwrap();
+ let all = s.execute("prefs.reset", json!({})).unwrap();
+ assert_eq!(all["shortcuts"], json!({}));
+ assert_eq!(all["menus"]["colors"], json!({}));
+ assert_eq!(s.prefs(), &Preferences::default());
+}
+
#[test]
fn set_validates_and_is_all_or_nothing() {
let mut s = session();
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index ed2d67782..abc176f8c 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -115,7 +115,7 @@ the same commands work in the app, the CLI and headless MCP. Paths are dotted ca
|---|---|
| `prefs.get` | `{"path":"performance.historyStates"}`; no path returns everything |
| `prefs.set` | `{"path":"cursors.painting","value":"precise"}` or `{"values":{"unitsAndRulers.rulers":"cm","guidesGridAndSlices.gridColor":"#ff8800"}}`. Values are validated (choices, ranges, `#rrggbb` colours, shortcut syntax); a batch applies all or nothing. A section path takes an object and merges it key by key |
-| `prefs.reset` | `{"path":"performance"}` (a section or key); no path resets everything |
+| `prefs.reset` | `{"path":"performance"}` (a section or key); no path resets everything. An individual `shortcuts.<id>` or `menus.colors.<id>` reset removes the override and returns `null`, including when it is already absent |
| `edit.preferences.<section>` | the section's values; in the app (no params) it opens the Preferences dialog on that section |
| `edit.keyboardShortcuts` | `{"set":{"edit.fill":"Cmd+Shift+F"},"reset":true\|["id",…],"removeConflicts":true,"filter":"blur","list":false}`: returns overrides, matching commands and conflicts. A shortcut moved to another command is removed from its old owner unless `removeConflicts` is false. `""` removes a shortcut, `null` restores the default. The held temporary tools are bindable too: `tools.temporary.hand` (Space; also repositions a selection being drawn), `tools.temporary.zoomIn` (Cmd+Space), `tools.temporary.zoomOut` (Cmd+Alt+Space); they list with `"hold": true`. In the app it opens Keyboard Shortcuts and Menus |
| `edit.menus` / `edit.toolbar` | `{"hide":["edit.fade"],"show":[…],"color":{"edit.fill":"red"},"reset":false}` / `{"hidden":["Sponge"],"order":[…]}` |