Skip to content

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

prefs.reset resets a single colorSettings key (#445) (#467)

HEOJUNFO533566c5adc1
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index 0c56ada65..41e50afab 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -1351,6 +1351,14 @@ fn prefs_reset(s: &mut Session, p: &Value) -> Result<Value> {
let path = p.get("path").or_else(|| p.get("section")).and_then(Value::as_str);
match path {
Some("colorSettings") => s.color.settings = Default::default(),
+ // One colour setting: `Preferences` has no `colorSettings`, so take the default
+ // from `ColorSettings` and route it like `prefs.set` does.
+ Some(path) if path.starts_with("colorSettings.") => {
+ let defaults = serde_json::to_value(crate::color_cmds::ColorSettings::default()).map_err(|e| bad("prefs.reset", e.to_string()))?;
+ let key = path.strip_prefix("colorSettings.").unwrap_or(path);
+ let def = get_path(&defaults, key).cloned().ok_or_else(|| bad("prefs.reset", format!("unknown preference `{path}`")))?;
+ s.set_pref(path, def).map_err(|e| bad("prefs.reset", e))?;
+ }
None => {
s.color.settings = Default::default();
s.edit_prefs(|p| p.reset(None)).map_err(|e| bad("prefs.reset", e))?;
diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs
index eb21c8276..78bd564a4 100644
--- a/crates/engine/src/prefs/tests.rs
+++ b/crates/engine/src/prefs/tests.rs
@@ -45,6 +45,18 @@ fn get_set_reset_by_path() {
assert_eq!(s.active().unwrap().history.max_states, 50);
}
+#[test]
+fn reset_one_colour_setting() {
+ let mut s = session();
+ s.execute("prefs.set", json!({"values": {"colorSettings.bpc": false, "colorSettings.workingRgb": "display-p3"}})).unwrap();
+ assert_eq!(s.execute("prefs.reset", json!({"path": "colorSettings.bpc"})).unwrap(), json!(true));
+ assert!(s.color.settings.bpc);
+ assert_eq!(s.color.settings.working_rgb, "display-p3", "other colour settings are untouched");
+ s.execute("prefs.reset", json!({"path": "colorSettings.workingRgb"})).unwrap();
+ assert_eq!(s.color.settings, crate::color_cmds::ColorSettings::default());
+ assert!(s.execute("prefs.reset", json!({"path": "colorSettings.notAField"})).is_err());
+}
+
#[test]
fn set_validates_and_is_all_or_nothing() {
let mut s = session();