Skip to content

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

fix(engine): disambiguate colliding document identities (#114)

MikeeI18631641826b
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index fc0a8a975..badafb2a1 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -279,7 +279,13 @@ impl Session {
}
/// Add a document (from File → New, an import, etc.) and make it active.
- pub fn add_document(&mut self, doc: Document, path: Option<String>) -> usize {
+ /// Preserve its identity unless another open document already owns it.
+ /// Identity-dependent callers must read the admitted document at the returned index.
+ pub fn add_document(&mut self, mut doc: Document, path: Option<String>) -> usize {
+ // Persisted IDs can overlap the allocator, so check every replacement too.
+ while self.docs.iter().any(|st| st.doc.id == doc.id) {
+ doc.id = photocraft_doc::DocId::fresh();
+ }
let mut st = DocState::new(doc, path);
st.history.max_states = self.prefs.get().performance.history_states.max(1) as usize;
st.history.max_bytes = self.prefs.get().performance.history_budget_bytes();
diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs
index debcb61f4..b7dc666ad 100644
--- a/crates/engine/src/smart_cmds.rs
+++ b/crates/engine/src/smart_cmds.rs
@@ -618,8 +618,9 @@ fn edit_contents(s: &mut Session, p: &Value) -> Result<Value> {
// Bundles keep their document id; each open copy needs its own.
child.id = DocId::fresh();
child.name = name;
- let child_id = child.id;
let index = s.add_document(child, None);
+ // Admission may replace an ID already owned by another open document.
+ let child_id = s.documents().get(index).ok_or(EngineError::NoDocument)?.doc.id;
s.smart_links.retain(|l| l.child != child_id);
s.smart_links.push(SmartLink { child: child_id, parent, layer: id });
Ok(json!({"document": index, "parentLayer": id.0}))
diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rs
index 0bb95e5c6..a1b439165 100644
--- a/crates/ui-egui/src/prefs_ui.rs
+++ b/crates/ui-egui/src/prefs_ui.rs
@@ -1171,11 +1171,26 @@ mod tests {
autosave_now(&mut app);
tick(&mut app, &ctx);
assert_eq!(saved.lock().unwrap().len(), 1, "unchanged since the last autosave");
+
+ // Loaded copies can have the same persisted identity and dirty revision.
+ let original = app.session.active().unwrap().doc.clone();
+ let original_id = original.id;
+ app.session.add_document(original.as_ref().clone(), None);
+ app.run("edit.fill", json!({"color": "#0000ff"})).unwrap();
+ autosave_now(&mut app);
+ tick(&mut app, &ctx);
+ {
+ let saved = saved.lock().unwrap();
+ assert_eq!(saved.len(), 2, "both dirty copies need independent autosaves");
+ assert_eq!(saved[0].0, original_id.0);
+ assert_ne!(saved[0].0, saved[1].0, "recovery ownership must be distinct");
+ assert_eq!(saved[0].1, saved[1].1, "the revisions must match to reproduce suppression");
+ }
app.run("prefs.set", json!({"path": "fileHandling.autosave", "value": false})).unwrap();
app.run("edit.fill", json!({"color": "#00ff00"})).unwrap();
autosave_now(&mut app);
tick(&mut app, &ctx);
- assert_eq!(saved.lock().unwrap().len(), 1, "autosave off");
+ assert_eq!(saved.lock().unwrap().len(), 2, "autosave off");
}
#[test]