Skip to content

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

Save: a tab whose file another save writes over is detached and marked unsaved (#6597)

zuujiscomdc5006e79e00
diff --git a/crates/ui-egui/src/file_dialog/tests.rs b/crates/ui-egui/src/file_dialog/tests.rs
index bb827274f..7c761dcb6 100644
--- a/crates/ui-egui/src/file_dialog/tests.rs
+++ b/crates/ui-egui/src/file_dialog/tests.rs
@@ -869,6 +869,45 @@ fn same_folder_quick_export_asks_before_replacing_the_documents_own_file() {
std::fs::remove_dir_all(&dir).ok();
}
+/// #6597: once Save As or Save a Copy writes over the file another open document is bound to,
+/// that document no longer has the file and reads as unsaved (so closing it asks, and its Save
+/// asks for a name), and a notice says so. Documents bound to other files are left alone.
+#[test]
+fn saving_over_the_file_of_another_open_document_detaches_it() {
+ let dir = std::env::temp_dir().join(format!("photocraft-save-over-tab-{}", std::process::id()));
+ let (a, d) = (dir.join("a.psd").to_string_lossy().into_owned(), dir.join("d.psd").to_string_lossy().into_owned());
+ let (mut app, open, written) = app();
+ let saved_as = |app: &mut PhotocraftApp, path: &str| {
+ let st = app.session.active_mut().unwrap();
+ st.path = Some(path.to_string());
+ st.saved_revision = st.revision;
+ };
+ // A lives in a.psd and D in d.psd, both saved.
+ saved_as(&mut app, &a);
+ app.run("file.new", json!({"width": 4, "height": 4})).unwrap();
+ saved_as(&mut app, &d);
+ // B, in another tab, is saved as a.psd.
+ app.run("file.new", json!({"width": 4, "height": 4})).unwrap();
+ app.save_as(Some(a.clone())).unwrap();
+ let docs = app.session.documents();
+ assert_eq!((docs[0].path.as_deref(), docs[0].is_dirty()), (None, true), "A lost a.psd and is unsaved");
+ assert_eq!((docs[1].path.as_deref(), docs[1].is_dirty()), (Some(d.as_str()), false), "D is untouched");
+ assert_eq!(docs[2].path.as_deref(), Some(a.as_str()), "B lives in a.psd");
+ let notice = app.ui.notices.last().expect("a notice");
+ assert!(notice.title.contains("a.psd was saved over") && notice.lines.len() == 1, "{} {:?}", notice.title, notice.lines);
+ // C's Save a Copy onto a.psd detaches B the same way, and C doesn't take the file.
+ app.run("file.new", json!({"width": 4, "height": 4})).unwrap();
+ let ctx = egui::Context::default();
+ menus::invoke(&mut app, &ctx, "file.saveACopy", json!({})).unwrap();
+ app.poll_file_dialog(&ctx, None);
+ answer(&open, Some(FileDialogAnswer::SaveTo(a.clone())));
+ app.poll_file_dialog(&ctx, None);
+ let docs = app.session.documents();
+ assert_eq!((docs[2].path.as_deref(), docs[2].is_dirty()), (None, true), "B lost a.psd and is unsaved");
+ assert_eq!(docs[3].path, None, "a copy doesn't bind C to a.psd");
+ assert_eq!(*written.borrow(), [a.clone(), a]);
+}
+
#[test]
fn cancelling_multi_layer_quick_export_writes_nothing() {
let (mut app, open, written) = app();
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 8bf2be90d..ed46d7934 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1761,6 +1761,50 @@ impl PhotocraftApp {
Ok(Some((path, warnings)))
}
+ /// After `saver` was written to `path`, any other open document bound to that file no longer
+ /// matches it: it loses the file and is marked unsaved, so it can't stay "saved" while its file
+ /// holds something else, closing it asks first, and its own Save asks for a name instead of
+ /// writing over the new file (#6597). Paths are compared as files, so another letter case on a
+ /// case-insensitive volume or a hard link counts. A notice names the documents affected.
+ pub(crate) fn detach_others_from_file(&mut self, path: &str, saver: DocId) {
+ let others: Vec<(DocId, String)> = self
+ .session
+ .documents()
+ .iter()
+ .filter(|st| st.doc.id != saver && st.path.as_deref().is_some_and(|p| photocraft_engine::file_cmds::same_file(p, path)))
+ .map(|st| (st.doc.id, st.doc.name.clone()))
+ .collect();
+ let mut detached = Vec::new();
+ for (id, name) in others {
+ let done = self.with_document(id, |app| {
+ let st = app.session.active_mut().ok_or("no document")?;
+ st.path = None;
+ // No revision ever equals this, so the document reads as unsaved until saved again.
+ st.saved_revision = u64::MAX;
+ Ok(())
+ });
+ if done.is_ok() {
+ detached.push(name);
+ }
+ }
+ if detached.is_empty() {
+ return;
+ }
+ let name = file_open::display_name(path);
+ let title = {
+ let template = "{name} was saved over from another tab";
+ crate::i18n::fmt(tl!(template), &[("name", name.as_str())])
+ };
+ let lines = detached
+ .iter()
+ .map(|doc| {
+ let template = "\"{doc}\" no longer has a file and is marked unsaved: save it under a new name to keep it";
+ crate::i18n::fmt(tl!(template), &[("doc", doc.as_str())])
+ })
+ .collect();
+ notices::post(self, title, lines, false, None);
+ }
+
/// Record a written save of document `doc` as it was at `revision`: its name, path and saved
/// state (unless a copy), the status, script events and the warnings.
pub(crate) fn saved(&mut self, doc: DocId, revision: u64, path: &str, warnings: &[String], copy: bool) {
@@ -1783,6 +1827,8 @@ impl PhotocraftApp {
}
};
self.ui.status = crate::i18n::fmt(tl!("Saved {name}"), &[("name", path)]);
+ // The file was written, copy or not: another tab bound to it no longer matches it.
+ self.detach_others_from_file(path, doc);
if !copy {
// A background save may finish while another document is active (or after its
// document was closed, when there is nothing left to record).
diff --git a/crates/ui-egui/src/view_cmds.rs b/crates/ui-egui/src/view_cmds.rs
index 1cb51a6ea..53a687149 100644
--- a/crates/ui-egui/src/view_cmds.rs
+++ b/crates/ui-egui/src/view_cmds.rs
@@ -1501,6 +1501,7 @@ pub(crate) fn write_copy(app: &mut PhotocraftApp, doc: &photocraft_doc::Document
let (bytes, warnings) = export(doc, path, &settings)?;
let write = app.services.write.as_mut().ok_or("no writer configured")?;
write(path, &bytes)?;
+ app.detach_others_from_file(path, doc.id);
let template = "Saved a copy as {path}";
app.ui.status = crate::i18n::fmt(tl!(template), &[("path", path)]);
app.ui.status_error = false;