Guard deferred format saves against native File Revert

This commit is contained in:
dotCooCoo committed 2026-11-26 19:05:21 -08:00
1 parent cc7654aa53
commit bbe11d8d8b
2 files changed
+55 -8

No files matched your search

+14 -8
View File
@@ -64,8 +64,8 @@ pub(crate) enum Export {
pub(crate) struct Open {
/// The hosted dialog's id.
pub id: DialogId,
/// The drawing whose options and output this dialog controls.
document: Option<u64>,
/// The drawing and loaded contents whose options and output this dialog controls.
document: Option<(u64, u64)>,
/// Which dialog it is.
pub kind: Export,
/// Whether Export Selection opened it: OK then writes the selection alone.
@@ -92,10 +92,17 @@ impl ExportDialogs {
impl Open {
fn check_source(&self, app: &VectorcraftApp) -> Result<(), String> {
if !matches!(self.kind, Export::As { .. } | Export::Hpgl { .. }) && self.document != app.session.active().map(|doc| doc.uid) {
return Err(tl!("Return to the original drawing to continue").into());
if matches!(self.kind, Export::As { .. } | Export::Hpgl { .. }) {
return Ok(());
}
Ok(())
let active = app.session.active().map(|doc| (doc.uid, doc.reverts));
if active == self.document {
return Ok(());
}
if active.zip(self.document).is_some_and(|((uid, _), (source, _))| uid == source) {
return Err(tl!("The document was reverted. Reopen this dialog to continue.").into());
}
Err(tl!("Return to the original drawing to continue").into())
}
}
@@ -108,10 +115,9 @@ pub(super) fn options<L: From<&'static str>>(ids: &[&'static str], labels: &[&'s
/// replaces the one open, and its record.
pub(crate) fn open(app: &mut VectorcraftApp, kind: Export, selection: bool, dialog: HostedParamDialog) -> Option<DialogId> {
let document = match &kind {
Export::As { document } => *document,
Export::Hpgl { doc } => Some(*doc),
Export::As { .. } | Export::Hpgl { .. } => None,
Export::EpsCustom { parent } => app.export_dialogs.find(*parent)?.document,
_ => app.session.active().map(|doc| doc.uid),
_ => app.session.active().map(|doc| (doc.uid, doc.reverts)),
};
let options = match &kind {
Export::EpsCustom { parent } => HeldOptions { parent: Some(*parent), cancel: None },
@@ -575,3 +575,44 @@ fn a_refused_save_says_so_in_an_alert() {
let st = app.session.active().unwrap();
assert!(st.is_dirty() && st.path.is_none(), "not saved");
}
#[test]
fn native_save_options_do_not_write_a_copy_after_file_revert() {
use craft::ui::dialog::HeldOk;
for accepted_before_revert in [false, true] {
let (mut app, written, _) = desktop("revision-copy.vectorcraft");
let dir = vectorcraft_testkit::TempDir::new("save-options-revert");
let source = dir.join("source.vectorcraft");
let record_outcome = app.services.write.take();
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string())));
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string())));
app.run("file.saveAs", json!({"path": source})).unwrap();
app.services.write = record_outcome;
app.run("shape.ellipse", json!({"x": 10, "y": 10, "width": 15, "height": 15})).unwrap();
assert_eq!(save_as(&mut app, "file.saveAs").as_deref(), Some(KIND));
let (ctx, id) = (app.ui_ctx.clone().unwrap(), top(&mut app));
assert!(!host(&app, |h| h.confirm_by_id(&ctx, id)));
assert_eq!(host(&app, |h| h.held_state(id)), Some(HeldOk::Waiting));
if accepted_before_revert {
super::super::host::pump(&mut app, &ctx);
assert_eq!(host(&app, |h| h.held_state(id)), Some(HeldOk::Accepted));
assert!(host(&app, |h| h.confirm_by_id(&ctx, id)));
}
assert!(written.borrow().is_empty());
app.run("file.revert", json!({"confirmed": true})).unwrap();
super::super::host::pump(&mut app, &ctx);
assert!(written.borrow().is_empty(), "the old options must not write restored contents");
if accepted_before_revert {
assert!(app.ui.status.contains("document was reverted"));
} else {
assert!(matches!(host(&app, |h| h.held_state(id)), Some(HeldOk::Refused(_))));
host(&app, |h| h.cancel_by_id(&ctx, id));
super::super::host::pump(&mut app, &ctx);
}
assert_eq!(save_as(&mut app, "file.saveAs").as_deref(), Some(KIND));
assert!(ok(&mut app));
assert_eq!(written.borrow()[0].0, "revision-copy.vectorcraft");
}
}