From 4974b14c538d382f96ddf72f7fbf9e17ee6c1d16 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 14:46:15 -0800 Subject: [PATCH] Keep Package tied to its opening document --- crates/ui-egui/src/app.rs | 1 + crates/ui-egui/src/dialogs/package.rs | 22 +++++++++++++-- crates/ui-egui/src/dialogs/tests_package.rs | 31 +++++++++++++++++++++ crates/ui-egui/src/lib.rs | 2 ++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 9c787b603e..8717905b90 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -241,6 +241,7 @@ impl VectorcraftApp { three_d_dialog: None, three_d: Default::default(), export_dialogs: Default::default(), + package_document: None, web_export: Default::default(), flattener_preview: Default::default(), flatten_dialog: Default::default(), diff --git a/crates/ui-egui/src/dialogs/package.rs b/crates/ui-egui/src/dialogs/package.rs index 61397b4076..606d5a8bde 100644 --- a/crates/ui-egui/src/dialogs/package.rs +++ b/crates/ui-egui/src/dialogs/package.rs @@ -55,6 +55,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result { ); return Ok(Value::Null); }; + let document = st.uid; let p = std::path::Path::new(&path); let stem = p.file_stem().map_or_else(|| "Untitled".into(), |s| s.to_string_lossy().into_owned()); let folder = p.parent().map(|f| f.to_string_lossy().into_owned()).unwrap_or_default(); @@ -73,6 +74,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result { .footer_note(Label::from(tl!("Fonts whose licence doesn't allow embedding are not copied."))) }); crate::dialogs::host::open_held(app, dialog, |app, answer| confirm(app, &answer.fields).map(|_| ())).ok_or("no window")?; + app.package_document = Some(document); Ok(Value::Null) } @@ -96,13 +98,27 @@ fn params(web: bool) -> Vec { pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { let Some(dialogs) = app.dialogs() else { return }; let id = dialog_id(KEY); - let Some(request) = dialogs.with(|host| host.get_mut_by_id::(id).and_then(HostedParamDialog::pick_path)) else { return }; - file_asks::browse(app, id, request, PickRequest::folder(PURPOSE), file_asks::answer_path); + let Some(request) = dialogs.with(|host| host.get_mut_by_id::(id).map(HostedParamDialog::pick_path)) else { + app.package_document = None; + return; + }; + if let Some(request) = request { + file_asks::browse(app, id, request, PickRequest::folder(PURPOSE), file_asks::answer_path); + } +} + +fn require_package_document(app: &VectorcraftApp, document: u64) -> Result<(), String> { + if app.session.active().is_none_or(|st| st.uid != document) { + return Err(tl!("Package can only save and collect the document where it was opened.").to_owned()); + } + Ok(()) } /// What OK does: save a dirty document, then write the package (on the worker with Background Export on) or /// download it. `Err` refuses the OK and keeps the dialog open; the reply says whether the package is in the background. pub(super) fn confirm(app: &mut VectorcraftApp, fields: &DialogFields) -> Result { + let document = app.package_document.ok_or("no Package dialog open")?; + require_package_document(app, document)?; let text = |key: &str| fields.text(key).unwrap_or_default(); let mut params = json!({ "folder": text("folder"), "name": text("name") }); for (key, _) in OPTIONS { @@ -144,6 +160,8 @@ pub(super) fn confirm(app: &mut VectorcraftApp, fields: &DialogFields) -> Result return r; } } + // A completed background job may have changed the active document while Package waited. + require_package_document(app, document)?; let package = app.session.prepare_package(¶ms).map_err(|e| e.to_string()).inspect_err(|e| app.ui.status = e.clone())?; let label = format!("Packaging {}", text("name")); let later = Rc::new(Cell::new(false)); diff --git a/crates/ui-egui/src/dialogs/tests_package.rs b/crates/ui-egui/src/dialogs/tests_package.rs index e0d0e87e72..f958d395e5 100644 --- a/crates/ui-egui/src/dialogs/tests_package.rs +++ b/crates/ui-egui/src/dialogs/tests_package.rs @@ -224,6 +224,37 @@ fn package_dialog(app: &mut VectorcraftApp, out: &str) { set(app, "folder", json!(out)).unwrap(); } +#[test] +fn package_opening_drawing_is_required_before_saving_or_packaging() { + let dir = Folder::new("opening-drawing"); + let mut app = saved(&dir); + let source = app.session.active().unwrap().uid; + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + let destination = app.session.active().unwrap().uid; + let destination_path = dir.path("destination.vectorcraft"); + app.run("document.save", json!({"path": destination_path})).unwrap(); + let saved_destination = std::fs::read(&destination_path).unwrap(); + app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30})).unwrap(); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert_eq!(app.session.active().unwrap().uid, source); + let out = dir.path("out"); + package_dialog(&mut app, &out); + app.run("document.activate", json!({"index": 1})).unwrap(); + assert_eq!(app.session.active().unwrap().uid, destination); + let accepted = press_ok(&mut app); + assert!(app.session.active().unwrap().is_dirty(), "Package leaves the different drawing unsaved"); + assert_eq!(std::fs::read(&destination_path).unwrap(), saved_destination); + let package = std::path::Path::new(&out).join("card Folder"); + assert!(!package.exists()); + assert!(!accepted && matches!(held_state(&app), Some(HeldOk::Refused(_)))); + + app.run("document.activate", json!({"index": 0})).unwrap(); + assert!(press_ok(&mut app)); + assert!(package.join("card.vectorcraft").is_file()); + assert!(!package.join("destination.vectorcraft").exists()); +} + #[test] fn package_is_written_in_the_background_as_it_is_at_once() { let dir = Folder::new("background"); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index b3f9cd31ac..816a402835 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -286,6 +286,8 @@ pub struct VectorcraftApp { pub(crate) three_d: panels::three_d::Model, /// The open export option dialogs and what runs after each. pub(crate) export_dialogs: dialogs::export_host::ExportDialogs, + /// The drawing the open Package dialog saves and collects. + pub(crate) package_document: Option, /// Save for Web's renders and colour table. pub(crate) web_export: dialogs::save_for_web::WebExport, /// The Flattener Preview panel's model.