mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Web: a requested download no longer marks the document saved (#4962)
The web writer only triggers an anchor download, which the user can cancel and which reports nothing back, yet Save treated it as a confirmed write: the document was marked clean and the leave-page guard disarmed. Services gains `write_unconfirmed` (set by the web shell); a save through such a writer names the document but keeps its changes unsaved. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
b630d7cb9a
commit
7bc556ec02
3 files changed
+33
-1
No files matched your search
@@ -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())
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
Reference in new issue
Block a user