From fc8575ecfbadc07a6c489ce1f9a890256d4994fe Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 15:18:52 -0800 Subject: [PATCH] Keep Export As attached to the original drawing --- crates/ui-egui/src/dialogs/export_as.rs | 23 +++++++++++++++++----- crates/ui-egui/src/dialogs/export_host.rs | 7 +++++-- crates/ui-egui/src/dialogs/tests_export.rs | 18 +++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/dialogs/export_as.rs b/crates/ui-egui/src/dialogs/export_as.rs index 6e8f8bc1d2..30af709740 100644 --- a/crates/ui-egui/src/dialogs/export_as.rs +++ b/crates/ui-egui/src/dialogs/export_as.rs @@ -19,7 +19,7 @@ use craft::ui::style::TypeRole; use serde_json::{Value, json}; use vectorcraft_engine::cmd::fileio::{self, ArtboardPick, Format}; -use super::export_host::{self, After, Export, Open}; +use super::export_host::{self, After, Export}; use super::{dxf_options, eps_options, form, hpgl_options, plugin, png_options, save_pdf, svg_options, text_export}; use crate::{VectorcraftApp, io}; @@ -111,7 +111,9 @@ pub fn open(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) { fn open_dialog(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) -> Result<(), String> { let ctx = app.ui_ctx.clone().ok_or("no window")?; - let n = app.session.active().map_or(0, |s| s.doc.artboards.len()); + let source = app.session.active().ok_or("no document")?; + let document = source.uid; + let n = source.doc.artboards.len(); let missing = format.and_then(|f| UNAVAILABLE.iter().find(|u| u.0.eq_ignore_ascii_case(f.trim_start_matches('.')))); let format = match missing { Some((id, _)) => id, @@ -133,12 +135,20 @@ fn open_dialog(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) -> Res }) .ok_label(Label::from("Export…")) }); - export_host::open(app, Export::As, false, dialog).map(|_| ()).ok_or_else(|| "no window".into()) + export_host::open(app, Export::As { document }, false, dialog).map(|_| ()).ok_or_else(|| "no window".into()) +} + +fn require_source(app: &VectorcraftApp, document: u64) -> Result<(), String> { + if app.session.active().is_none_or(|source| source.uid != document) { + return Err(tl!("Return to the original drawing to export it").into()); + } + Ok(()) } /// Check the artboard range and that there is something to export; the save panel and the format's options or /// the file follow once the dialog has closed. -pub(super) fn confirm(app: &mut VectorcraftApp, _open: &Open, fields: &DialogFields) -> Result { +pub(super) fn confirm(app: &mut VectorcraftApp, document: u64, fields: &DialogFields) -> Result { + require_source(app, document)?; if let Some((_, hint)) = unavailable(fields) { return Err(hint.into()); } @@ -162,7 +172,10 @@ pub(super) fn confirm(app: &mut VectorcraftApp, _open: &Open, fields: &DialogFie } let range = if all { "all".to_string() } else { range }; let pages = Pages { use_artboards, all, range, selection: false }; - Ok(Some(Box::new(move |app| io::export_path(app, None, f, move |app, path| options_or_write(app, f, plugin, path, pages))))) + Ok(Some(Box::new(move |app| { + require_source(app, document)?; + io::export_path(app, None, f, move |app, path| options_or_write(app, f, plugin, path, pages)) + }))) } /// `params` of an options dialog for [`io::export`]: with [`io::EXPORT_SELECTION`] when diff --git a/crates/ui-egui/src/dialogs/export_host.rs b/crates/ui-egui/src/dialogs/export_host.rs index efdc6c9f6d..2d45ad40ca 100644 --- a/crates/ui-egui/src/dialogs/export_host.rs +++ b/crates/ui-egui/src/dialogs/export_host.rs @@ -21,7 +21,10 @@ pub(crate) type After = Option Result Result<(), String> fn confirm(app: &mut VectorcraftApp, open: &Open, fields: &DialogFields) -> Result { match &open.kind { - Export::As => export_as::confirm(app, open, fields), + Export::As { document } => export_as::confirm(app, *document, fields), Export::Raster { .. } => png_options::confirm(app, open, fields), Export::Svg { .. } => svg_options::confirm(app, open, fields), Export::Eps => eps_options::confirm(app, open, fields), diff --git a/crates/ui-egui/src/dialogs/tests_export.rs b/crates/ui-egui/src/dialogs/tests_export.rs index 016cf865aa..521cb19043 100644 --- a/crates/ui-egui/src/dialogs/tests_export.rs +++ b/crates/ui-egui/src/dialogs/tests_export.rs @@ -344,6 +344,24 @@ fn a_bad_range_keeps_export_as_open() { assert!(written.borrow().is_empty()); } +#[test] +fn export_as_waits_for_the_drawing_that_opened_it() { + let (mut app, written) = app(2); + app.run("file.exportAs", json!({"format": "png", "useArtboards": true, "range": "1"})).unwrap(); + app.run("file.new", json!({ "width": 200, "height": 100 })).unwrap(); + let error = try_confirm(&mut app).expect_err("Export As must refuse the other drawing"); + assert!(error.contains("original drawing"), "{error}"); + assert_eq!(kind(&app).as_deref(), Some("exportAs")); + assert_eq!(fields(&app)["range"], "1"); + PANEL.with_borrow(|panel| assert!(panel.as_ref().unwrap().picker.requests().is_empty())); + app.run("document.activate", json!({ "index": 0 })).unwrap(); + confirm(&mut app); + assert_eq!(kind(&app).as_deref(), Some("pngOptions")); + confirm(&mut app); + assert_eq!(written.borrow().len(), 1); + assert_eq!(&written.borrow()[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).