diff --git a/crates/dialogs/src/save.rs b/crates/dialogs/src/save.rs index d6fce4da1..f2c3b6e16 100644 --- a/crates/dialogs/src/save.rs +++ b/crates/dialogs/src/save.rs @@ -89,8 +89,8 @@ pub enum SaveStep { path: PathBuf, /// Whether the document takes `path` as its file or keeps its own. target: SaveTarget, - /// The index of the file type picked in the save panel, when it reported one; `None` for the document's own - /// file. + /// The index of the file type picked in the save panel, when it reported one; `None` for a save in place, + /// which opens no panel. file_type: Option, }, /// The user cancelled (the picker or the question); nothing was written. @@ -106,9 +106,22 @@ pub enum SaveStep { /// The state of one save. #[derive(Clone, Debug)] enum Pending { - Picking { document: SaveDocument, target: SaveTarget, ticket: PickId }, - Asking { document: SaveDocument, target: SaveTarget, path: PathBuf }, - Writing { key: D, target: SaveTarget }, + Picking { + document: SaveDocument, + target: SaveTarget, + ticket: PickId, + }, + /// The changed-on-disk question, with the file type the save panel picked for the path, if any. + Asking { + document: SaveDocument, + target: SaveTarget, + path: PathBuf, + file_type: Option, + }, + Writing { + key: D, + target: SaveTarget, + }, } /// Save and Save As for an app's documents, one save at a time. @@ -154,7 +167,7 @@ impl SaveFlow { return SaveStep::Busy; } match document.path.clone() { - Some(path) if target == SaveTarget::InPlace && !is_mail_temp_copy(&path) => self.confirm(watch, document, target, path), + Some(path) if target == SaveTarget::InPlace && !is_mail_temp_copy(&path) => self.confirm(watch, document, target, path, None), _ => self.open_picker(files, document, target), } } @@ -171,7 +184,7 @@ impl SaveFlow { let Some(Pending::Picking { document, target, .. }) = self.pending.take() else { return None }; let Picked::Saved { path, file_type } = picked else { return Some(SaveStep::Cancelled { key: document.key }) }; if document.path.as_deref() == Some(path.as_path()) { - return Some(self.confirm(watch, document, target, path)); + return Some(self.confirm(watch, document, target, path, file_type)); } self.pending = Some(Pending::Writing { key: document.key.clone(), target }); Some(SaveStep::Write { key: document.key, path, target, file_type }) @@ -181,9 +194,9 @@ impl SaveFlow { /// when no such question is open. // pdfcraft: crates/ui-egui/src/editing/save.rs:141 overwrite_changed_file pub fn replace(&mut self) -> SaveStep { - let Some(Pending::Asking { document, target, path }) = self.pending.take() else { return SaveStep::Busy }; + let Some(Pending::Asking { document, target, path, file_type }) = self.pending.take() else { return SaveStep::Busy }; self.pending = Some(Pending::Writing { key: document.key.clone(), target }); - SaveStep::Write { key: document.key, path, target, file_type: None } + SaveStep::Write { key: document.key, path, target, file_type } } /// The changed-on-disk question's Save As: leave the file on disk as it is and ask where to @@ -245,15 +258,22 @@ impl SaveFlow { } impl SaveFlow { - /// Writes `path`, the document's own file, unless another app changed it. - fn confirm(&mut self, watch: &FileWatch, document: SaveDocument, target: SaveTarget, path: PathBuf) -> SaveStep { + /// Writes `path`, the document's own file, as `file_type` (the save panel's pick), unless another app changed it. + fn confirm( + &mut self, + watch: &FileWatch, + document: SaveDocument, + target: SaveTarget, + path: PathBuf, + file_type: Option, + ) -> SaveStep { if watch.disk_differs(&document.key, &path, document.saved.as_deref().map(Vec::as_slice)) { let key = document.key.clone(); - self.pending = Some(Pending::Asking { document, target, path: path.clone() }); + self.pending = Some(Pending::Asking { document, target, path: path.clone(), file_type }); return SaveStep::AskChangedOnDisk { key, path }; } self.pending = Some(Pending::Writing { key: document.key.clone(), target }); - SaveStep::Write { key: document.key, path, target, file_type: None } + SaveStep::Write { key: document.key, path, target, file_type } } fn open_picker(&mut self, files: &dyn Files, document: SaveDocument, target: SaveTarget) -> SaveStep { diff --git a/crates/dialogs/src/save_tests.rs b/crates/dialogs/src/save_tests.rs index b0dc528ba..0ce6af043 100644 --- a/crates/dialogs/src/save_tests.rs +++ b/crates/dialogs/src/save_tests.rs @@ -144,6 +144,34 @@ fn a_file_changed_on_disk_is_asked_about_once() { std::fs::remove_dir_all(dir).ok(); } +// Tc-9 (4): Save As onto the document's own file writes the file type the panel picked, straight away or after the +// changed-on-disk question's Replace: the own-file path dropped the pick, so the document saved in its old type. +#[trial] +fn save_as_onto_the_own_file_keeps_the_picked_type() { + let dir = scratch("own-type"); + let file = dir.join("a.pdf"); + std::fs::write(&file, b"one").unwrap(); + let (files, mut watch, mut flow) = (ScriptedFiles::default(), watch(), SaveFlow::new()); + watch.known_now(7, &file); + let write = SaveStep::Write { key: 7, path: file.clone(), target: SaveTarget::As, file_type: Some(1) }; + for changed in [false, true] { + if changed { + std::fs::write(&file, b"another app wrote more").unwrap(); + } + assert_eq!(flow.start(&files, &watch, document(Some(&file), "a.pdf"), SaveTarget::As), SaveStep::Picking); + files.answer(files.last_ticket(), Picked::Saved { path: file.clone(), file_type: Some(1) }); + let step = flow.take_pick(&files, &watch); + if changed { + assert_eq!(step, Some(SaveStep::AskChangedOnDisk { key: 7, path: file.clone() }), "a changed file asks first"); + assert_eq!(flow.replace(), write, "Replace writes the picked type"); + } else { + assert_eq!(step, Some(write.clone()), "the own file writes the picked type"); + } + flow.written(&mut watch, 7, &file); + } + std::fs::remove_dir_all(dir).ok(); +} + // A write that finishes for another document (its save was cancelled) leaves a newer save alone. #[trial] fn a_late_write_does_not_free_another_save() {