PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Web: a requested download no longer marks the document saved (#4962)
Higgs325847bc556ec02aa
diff --git a/apps/photocraft-web/src/web.rs b/apps/photocraft-web/src/web.rsindex eb350d17e..7084396cb 100644--- a/apps/photocraft-web/src/web.rs+++ b/apps/photocraft-web/src/web.rs@@ -379,6 +379,8 @@ fn services(inbox: Inbox) -> Services {}})),write: Some(Box::new(|path: &str, bytes: &[u8]| download(path, bytes))),+ // A download may be cancelled and never reports back: it doesn't save the document.+ write_unconfirmed: true,encode_png: Some(Box::new(|w, h, rgba| {let img = Image::from_u8(w, h, ChannelLayout::Rgba, rgba.to_vec()).map_err(|e| e.to_string())?;photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &EncodeOptions::default()).map_err(|e| e.to_string())diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex bde51ff27..4802fbe91 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -354,6 +354,13 @@ pub type WriteFn = Box<dyn FnMut(&str, &[u8]) -> Result<(), String>>;/// [`jobs_ui::SaveCtl::commit`] right before replacing the file, and write nothing if that fails.pub type SaveFileFn = std::sync::Arc<dyn Fn(&Document, &str, &ExportSettings, &jobs_ui::SaveCtl) -> Result<Vec<String>, String> + Send + Sync>;+/// The revision a document counts as saved at after `written` went to its writer: that one when+/// the writer confirms the bytes were stored, else still `previous`, so the document stays+/// unsaved. A browser download is only a request the user can cancel (#4962).+pub(crate) fn saved_revision_after_write(confirmed: bool, written: u64, previous: u64) -> u64 {+ if confirmed { written } else { previous }+}+/// Who a finished save serves.#[derive(Clone, Copy)]pub(crate) enum SaveKind {@@ -446,6 +453,10 @@ pub struct Services {pub file_dialog: Option<FileDialogFn>,/// Write bytes to a path (native) or trigger a download (web).pub write: Option<WriteFn>,+ /// `write` only asks for the bytes to be kept (a browser download, which the user can+ /// cancel and which reports nothing back), so a save through it names the document but+ /// leaves its changes unsaved and the leave-page guard armed (#4962).+ pub write_unconfirmed: bool,/// Encode and write in one step on a worker thread, so a large save doesn't freeze the/// window (#2017). With background jobs on, saves use it instead of `export` and `write`.pub save_file: Option<SaveFileFn>,@@ -1592,11 +1603,12 @@ impl PhotocraftApp {return jobs_ui::start_save(self, path, settings.clone(), copy, save, false);}let st = self.session.active().ok_or("no document")?;- let (doc, revision) = (st.doc.id, st.revision);+ let (doc, written, previous) = (st.doc.id, st.revision, st.saved_revision);let export = self.services.export.as_ref().ok_or("no exporter configured")?;let (bytes, warnings) = export(&st.doc, &path, settings)?;let write = self.services.write.as_mut().ok_or("no writer configured")?;write(&path, &bytes)?;+ let revision = saved_revision_after_write(!self.services.write_unconfirmed, written, previous);self.saved(doc, revision, &path, &warnings, copy);Ok(Some((path, warnings)))}diff --git a/crates/ui-egui/src/save_identity_tests.rs b/crates/ui-egui/src/save_identity_tests.rsindex c947afc9b..3396dfcdd 100644--- a/crates/ui-egui/src/save_identity_tests.rs+++ b/crates/ui-egui/src/save_identity_tests.rs@@ -254,3 +254,21 @@ fn save_settings_start_at_the_opened_files_encoding() {app.open_bytes("art.webp", &lossless).unwrap();assert!(app.save_settings().webp_lossless);}++/// A browser download is only requested: the document takes the name but keeps its unsaved+/// changes, so the leave-page guard stays armed (#4962). A confirmed write still saves it.+#[test]+fn unconfirmed_download_names_the_document_but_keeps_it_unsaved() {+ assert_eq!(crate::saved_revision_after_write(true, 7, 3), 7);+ assert_eq!(crate::saved_revision_after_write(false, 7, 3), 3);+ let mut app = app();+ app.services.write_unconfirmed = true;+ let (_, _, revision, saved) = identity(&app);+ app.save_as(Some("download.psd".into())).unwrap();+ assert_eq!(identity(&app), ("download.psd".into(), Some("download.psd".into()), revision, saved));+ assert!(app.session.active().unwrap().is_dirty());+ assert!(app.has_unsaved_work());+ app.services.write_unconfirmed = false;+ app.save_as(Some("download.psd".into())).unwrap();+ assert!(!app.has_unsaved_work());+}