diff --git a/crates/ui-egui/src/dialogs/export_host.rs b/crates/ui-egui/src/dialogs/export_host.rs index 94377ed572..9730de0da1 100644 --- a/crates/ui-egui/src/dialogs/export_host.rs +++ b/crates/ui-egui/src/dialogs/export_host.rs @@ -64,6 +64,8 @@ pub(crate) enum Export { pub(crate) struct Open { /// The hosted dialog's id. pub id: DialogId, + /// 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. @@ -88,6 +90,22 @@ impl ExportDialogs { } } +impl Open { + fn check_source(&self, app: &VectorcraftApp) -> Result<(), String> { + if matches!(self.kind, Export::As { .. } | Export::Hpgl { .. }) { + return 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()) + } +} + /// `ids` paired with `labels` as the options of a choice row. pub(super) fn options>(ids: &[&'static str], labels: &[&'static str]) -> Vec<(String, L)> { ids.iter().zip(labels).map(|(id, label)| (id.to_string(), L::from(label))).collect() @@ -96,13 +114,18 @@ pub(super) fn options>(ids: &[&'static str], labels: &[&'s /// Opens `dialog` as the export dialog `kind` and records it; `None` without a window. A dialog of the same key /// replaces the one open, and its record. pub(crate) fn open(app: &mut VectorcraftApp, kind: Export, selection: bool, dialog: HostedParamDialog) -> Option { + let document = match &kind { + Export::As { .. } | Export::Hpgl { .. } => None, + Export::EpsCustom { parent } => app.export_dialogs.find(*parent)?.document, + _ => app.session.active().map(|doc| (doc.uid, doc.reverts)), + }; let options = match &kind { Export::EpsCustom { parent } => HeldOptions { parent: Some(*parent), cancel: None }, _ => HeldOptions::default(), }; let id = host::open_held_with(app, dialog, answer, options)?; app.export_dialogs.0.retain(|o| o.id != id); - app.export_dialogs.0.push(Open { id, kind, selection, after: None }); + app.export_dialogs.0.push(Open { id, document, kind, selection, after: None }); Some(id) } @@ -117,9 +140,10 @@ pub(super) fn set_fields(app: &VectorcraftApp, id: DialogId, fields: &[(&str, Va /// OK of the dialog `result` is from: its kind's `confirm`, and the step that follows kept for [`pump`]. fn answer(app: &mut VectorcraftApp, result: &ParamResult) -> Result<(), String> { - let (id, kind, selection) = - app.export_dialogs.find(result.id).map(|o| (o.id, o.kind.clone(), o.selection)).ok_or("this export dialog is no longer open")?; - let open = Open { id, kind, selection, after: None }; + let open = app.export_dialogs.find(result.id).ok_or("this export dialog is no longer open")?; + open.check_source(app)?; + let id = open.id; + let open = Open { id, document: open.document, kind: open.kind.clone(), selection: open.selection, after: None }; let after = confirm(app, &open, &result.fields)?; if let Some(record) = app.export_dialogs.0.iter_mut().find(|o| o.id == id) { record.after = after; @@ -197,14 +221,20 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { let Some((footer, actions)) = taken else { // The dialog closed: the step after an OK runs, a cancelled one has none. if let Some(i) = app.export_dialogs.0.iter().position(|o| o.id == id) { - let record = app.export_dialogs.0.remove(i); - if let Some(after) = record.after { - let result = after(app); + let mut record = app.export_dialogs.0.remove(i); + if let Some(after) = record.after.take() { + let result = record.check_source(app).and_then(|()| after(app)); report(app, result); } } continue; }; + if let Err(error) = open.check_source(app) { + if footer.is_some() || !actions.is_empty() { + app.status(error); + } + continue; + } if let Export::Svg { mode } = kind { if footer.as_deref() == Some(svg_options::BROWSER_ACTION) && let Some(fields) = fields_of(app, id) diff --git a/crates/ui-egui/src/dialogs/save_options/tests.rs b/crates/ui-egui/src/dialogs/save_options/tests.rs index 200b91f458..9076f18d21 100644 --- a/crates/ui-egui/src/dialogs/save_options/tests.rs +++ b/crates/ui-egui/src/dialogs/save_options/tests.rs @@ -513,6 +513,24 @@ fn eps_save_transparency_follows_the_color_preview() { assert_eq!(dialog(&mut app).bool("embedFonts"), Some(true)); } +#[test] +fn a_native_save_options_draft_cannot_redirect_its_path_to_another_drawing() { + let (mut app, written, _) = desktop("original.vectorcraft"); + assert_eq!(save_as(&mut app, "file.saveAs").as_deref(), Some(KIND)); + let draft_path = dialog(&mut app).text("path").unwrap().to_owned(); + app.run("file.new", json!({"width": 200, "height": 120})).unwrap(); + app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 30, "height": 20})).unwrap(); + assert!(!ok(&mut app)); + assert_eq!(dialog(&mut app).text("path"), Some(draft_path.as_str())); + assert!(written.borrow().is_empty()); + assert!(app.session.active().unwrap().path.is_none()); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert!(ok(&mut app), "{}", app.ui.status); + assert_eq!(written.borrow()[0].0, draft_path); + assert!(app.session.documents()[1].path.is_none()); +} + /// #4931: a native file's Version is a menu of the versions VectorCraft writes, the current one /// first and chosen, each named by the releases that read it. An older version without /// compression saves plain, whatever Use Compression held. @@ -557,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"); + } +} diff --git a/crates/ui-egui/src/dialogs/tests_export.rs b/crates/ui-egui/src/dialogs/tests_export.rs index ea383bd6c5..f11039d48a 100644 --- a/crates/ui-egui/src/dialogs/tests_export.rs +++ b/crates/ui-egui/src/dialogs/tests_export.rs @@ -378,6 +378,28 @@ fn export_as_waits_for_the_drawing_that_opened_it() { assert_eq!(&written.borrow()[0].1[16..20], 60u32.to_be_bytes()); } +#[test] +fn png_options_wait_for_the_drawing_that_opened_them() { + let (mut app, written) = app(1); + app.run("file.export.png", json!({})).unwrap(); + answer_saves(&mut app); + assert_eq!(kind(&app).as_deref(), Some("pngOptions")); + let draft = fields(&app); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap(); + let error = try_confirm(&mut app).expect_err("PNG Options must refuse the other drawing"); + assert!(error.contains("original drawing"), "{error}"); + assert_eq!(kind(&app).as_deref(), Some("pngOptions")); + assert_eq!(fields(&app)["path"], draft["path"]); + assert!(written.borrow().is_empty()); + + app.run("document.activate", json!({"index": 0})).unwrap(); + confirm(&mut app); + let w = written.borrow(); + assert_eq!(w.len(), 1); + assert_eq!(&w[0].1[16..20], 60u32.to_be_bytes()); +} + /// Each format's Export As continues in its options dialog, writes nothing before that dialog's OK, and then /// writes its files: (case, artboards, Export As params, the options' key, the files, the pages of a PDF, the /// page size of a PDF of the art's bounds).