PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Preferences: old settings files take Shift-to-switch-tools on (#5647)
Higgs32584d86069c0393d
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rsindex d6b40178d..e30e99901 100644--- a/crates/engine/src/prefs.rs+++ b/crates/engine/src/prefs.rs@@ -1531,6 +1531,10 @@ impl PrefsStore {}}+/// The preferences file format written by [`Session::prefs_value`]. 2: Use Shift Key for Tool+/// Switch stored as chosen (before, a stored `false` was the old default; see `load_prefs_json`).+pub const PREFS_VERSION: u64 = 2;+impl Session {/// The current preferences.pub fn prefs(&self) -> &Preferences {@@ -1571,7 +1575,7 @@ impl Session {let mut v = self.prefs().to_json();if let Value::Object(m) = &mut v {m.insert("colorSettings".into(), serde_json::to_value(&self.color.settings).unwrap_or(Value::Null));- m.insert("version".into(), json!(1));+ m.insert("version".into(), json!(PREFS_VERSION));m.insert("presets".into(), self.presets.to_json(self));}v@@ -1581,6 +1585,16 @@ impl Session {/// defaults and unknown keys are ignored, so files from older and newer versions load.pub fn load_prefs_json(&mut self, s: &str) -> std::result::Result<(), String> {let mut v: Value = serde_json::from_str(s).map_err(|e| format!("preferences: {e}"))?;+ // Files before version 2 hold Use Shift Key for Tool Switch off because that was the+ // default when they were written: every key is saved, chosen or not. Like Photoshop it is+ // on by default now (#3528), so those files take the new default (#5647: a tool letter+ // pressed again kept cycling its group). A choice made since is saved as version 2.+ if v.get("version").and_then(Value::as_u64).unwrap_or(0) < 2+ && let Some(tools) = v.get_mut("tools").and_then(Value::as_object_mut)+ && tools.get("useShiftKeyForToolSwitch") == Some(&json!(false))+ {+ tools.remove("useShiftKeyForToolSwitch");+ }// Saved preferences before appearance modes had one concrete theme. Preserve its// appearance instead of silently switching established users to Auto.if let Some(interface) = v.get_mut("interface").and_then(Value::as_object_mut)diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rsindex bf2fe52ef..d4259b0bd 100644--- a/crates/engine/src/prefs/tests.rs+++ b/crates/engine/src/prefs/tests.rs@@ -827,3 +827,26 @@ fn shift_key_tool_switch_is_on_by_default_and_a_stored_false_is_kept() {let stored: Tools = serde_json::from_value(json!({"useShiftKeyForToolSwitch": false})).unwrap();assert!(!stored.use_shift_key_for_tool_switch);}++/// #5647: a settings file from before version 2 stored the switch off only because that was the+/// old default, so it loads with the new default (on); one saved since keeps an explicit off.+#[test]+fn old_settings_files_take_the_shift_key_tool_switch_default() {+ for old in+ [json!({"tools": {"useShiftKeyForToolSwitch": false}}), json!({"version": 1, "tools": {"useShiftKeyForToolSwitch": false, "showTooltips": false}})]+ {+ let mut s = Session::new();+ s.load_prefs_json(&old.to_string()).unwrap();+ assert!(s.prefs().tools.use_shift_key_for_tool_switch, "{old}");+ }+ let mut s = Session::new();+ s.load_prefs_json(&json!({"version": 1, "tools": {"showTooltips": false}}).to_string()).unwrap();+ assert!(!s.prefs().tools.show_tooltips, "other tool settings are kept");+ // Chosen off and saved now: kept on the next start.+ s.edit_prefs(|p| p.tools.use_shift_key_for_tool_switch = false);+ let saved = s.prefs_to_json();+ assert_eq!(serde_json::from_str::<Value>(&saved).unwrap()["version"], json!(PREFS_VERSION));+ let mut restored = Session::new();+ restored.load_prefs_json(&saved).unwrap();+ assert!(!restored.prefs().tools.use_shift_key_for_tool_switch);+}