Integration: keep the save base complete after the prefs migration (#5878)

The save base after loading kept only the keys the file had, so a key an
older file lacks was written as this instance's default over another
instance's change (two instances on one v1 file: B sets the theme, A saves
anything, the theme reverts). A version 3 file was also written back as
version 2. The base is now what was loaded, with only the version and
tools.useShiftKeyForToolSwitch put back as the file had them (a newer
version is left alone), the way repair_invalid patches keys.

Tests: an_old_sparse_file_keeps_another_instances_change,
a_newer_files_version_is_kept_on_save (both fail with the previous base).

Co-authored-by: Higgs32584 <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
committed 2026-10-22 21:15:53 +02:00
1 parent 76d7d27c52
commit cc498e537e
1 file changed
+56 -7
+56 -7
View File
@@ -267,15 +267,30 @@ pub fn load(app: &mut PhotocraftApp) {
}
}
/// The save base after loading `stored`: what the file holds, limited to the keys `loaded` (this
/// version's preferences) has. Whatever loading changed there (a migrated default, the file's
/// version) then differs from the base, so the next save writes it rather than keeping the old
/// value under a newer version (#5647). Keys only the file has stay out, so the merge keeps them.
/// The save base after loading `stored`: what was loaded, with the keys the shift tool switch
/// migration may change (#5647) put back as the file had them, the way [`repair_invalid`] puts
/// back unreadable values. The migrated value and the new version then differ from the base, so
/// the next save writes them rather than keeping the old value under a newer version. Every other
/// key keeps its loaded value, so the three-way merge leaves another instance's changes alone,
/// also for keys the file doesn't have yet. A file from a newer version keeps its version.
fn stored_base(loaded: &Value, stored: &Value) -> Value {
match (loaded, stored) {
(Value::Object(l), Value::Object(s)) => Value::Object(s.iter().filter_map(|(k, sv)| l.get(k).map(|lv| (k.clone(), stored_base(lv, sv)))).collect()),
_ => stored.clone(),
let mut base = loaded.clone();
let Some(obj) = base.as_object_mut() else {
return base;
};
match stored.get("version") {
Some(v) if v.as_u64().is_some_and(|v| v > prefs::PREFS_VERSION) => {}
Some(v) => {
obj.insert("version".into(), v.clone());
}
None => {
obj.remove("version");
}
}
if let (Some(v), Some(tools)) = (stored.pointer("/tools/useShiftKeyForToolSwitch"), obj.get_mut("tools").and_then(Value::as_object_mut)) {
tools.insert("useShiftKeyForToolSwitch".into(), v.clone());
}
base
}
/// Some saved values couldn't be read and took their defaults (#6611): say which, and write
@@ -3241,6 +3256,40 @@ mod tests {
}
}
#[test]
fn an_old_sparse_file_keeps_another_instances_change() {
// Two instances on a version 1 file that lacks most keys: one changes a key the file
// doesn't have, the other then saves an unrelated change and must not write its default
// over it.
let saved = json!({"version": 1, "tools": {"useShiftKeyForToolSwitch": false}});
let store = Arc::new(Mutex::new(Some(saved.to_string())));
let ctx = egui::Context::default();
let (mut a, mut b) = (app_on(&store, Arc::default()), app_on(&store, Arc::default()));
tick(&mut a, &ctx);
tick(&mut b, &ctx);
b.run("prefs.set", json!({"values": {"interface.theme": "studio"}})).unwrap();
tick(&mut b, &ctx);
assert_eq!(stored(&store)["interface"]["theme"], "studio");
a.run("prefs.set", json!({"values": {"performance.historyStates": 12}})).unwrap();
tick(&mut a, &ctx);
let v = stored(&store);
assert_eq!((v["interface"]["theme"].as_str(), v["performance"]["historyStates"].as_u64()), (Some("studio"), Some(12)));
}
#[test]
fn a_newer_files_version_is_kept_on_save() {
let saved = json!({"version": prefs::PREFS_VERSION + 1, "tools": {"useShiftKeyForToolSwitch": false}});
let (mut app, store) = app_with_saved(Some(saved.to_string()));
let ctx = egui::Context::default();
tick(&mut app, &ctx);
assert!(!app.session.prefs().tools.use_shift_key_for_tool_switch, "a newer file isn't migrated");
app.run("prefs.set", json!({"values": {"performance.historyStates": 12}})).unwrap();
tick(&mut app, &ctx);
let v = stored(&store);
assert_eq!(v["version"], json!(prefs::PREFS_VERSION + 1));
assert_eq!(v["performance"]["historyStates"], 12);
}
#[test]
fn merge_keeps_changes_made_elsewhere() {
let base = json!({"a": {"x": 1, "y": 1}, "list": [1], "gone": 1, "kept": 1});