diff --git a/crates/ui-egui/src/dialogs/dxf_import.rs b/crates/ui-egui/src/dialogs/dxf_import.rs index 91d8a8cd5a..a01076a838 100644 --- a/crates/ui-egui/src/dialogs/dxf_import.rs +++ b/crates/ui-egui/src/dialogs/dxf_import.rs @@ -491,12 +491,16 @@ fn options(c: &DxfChoices) -> Value { /// OK: open or place the drawing with the choices; an error keeps the dialog open. fn confirm(app: &mut VectorcraftApp, source: &Source, choices: &DxfChoices) -> Result<(), String> { let dxf = options(choices); - app.ui.dxf_import = Value::Object(REMEMBERED.iter().map(|k| (k.to_string(), dxf[*k].clone())).collect()); + let remembered = Value::Object(REMEMBERED.iter().map(|k| (k.to_string(), dxf[*k].clone())).collect()); match &source.place { Some(place) => { + let placement = source.converted.as_ref().and_then(|c| c.placement.as_ref()); + if let Some(placement) = placement { + placement.check(app)?; + } + app.ui.dxf_import = remembered; let mut p = with_file(place, source.path.as_deref(), &source.name, &source.file); p["dxf"] = dxf; - let placement = source.converted.as_ref().and_then(|c| c.placement.as_ref()); // Where new art goes now is taken before Replace selects the object it swaps again. let current = placement.and_then(|_| crate::placing::Insertion::of(app)); // A drawing converted off the UI thread replaces the object selected when Place @@ -527,7 +531,10 @@ fn confirm(app: &mut VectorcraftApp, source: &Source, choices: &DxfChoices) -> R } r } - None => io::open_document(app, &source.name, &source.file.bytes, source.path.clone(), &json!({ "dxf": dxf })).map(|_| ()), + None => { + app.ui.dxf_import = remembered; + io::open_document(app, &source.name, &source.file.bytes, source.path.clone(), &json!({ "dxf": dxf })).map(|_| ()) + } } } @@ -1017,6 +1024,31 @@ mod tests { }); } + /// Switching documents while the import dialog is open leaves both documents unchanged. + #[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))] + #[test] + fn a_dwg_drawing_cannot_be_placed_after_switching_documents_in_its_dialog() { + vectorcraft_engine::dwg::allow_in_process(); + let (_dir, plan) = dwg_file("dxf-import-dwg-place-dialog-switch"); + crate::opening::tests::with_threads(|| { + let mut app = host::app(); + app.services.read = Some(Box::new(|p: &str| std::fs::read(p).map_err(|e| e.to_string()))); + app.run("file.new", json!({"width": 600, "height": 400})).unwrap(); + let first = app.session.doc().unwrap().uid; + let before = app.session.doc().unwrap().history.undo.len(); + crate::place::run_for_user(&mut app, &json!({ "path": plan })).unwrap(); + finish_conversions(&mut app); + assert!(host::is_open(&app, KIND)); + app.run("file.new", json!({"width": 600, "height": 400})).unwrap(); + let other_before = app.session.doc().unwrap().history.undo.len(); + let error = host::confirm(&mut app, KIND).unwrap_err(); + assert!(error.contains("not the active document"), "{error}"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), other_before); + let first = app.session.documents().iter().find(|st| st.uid == first).unwrap(); + assert_eq!(first.history.undo.len(), before); + }); + } + #[test] fn placing_a_drawing_asks_then_places_one_group() { let mut app = host::app(); diff --git a/crates/ui-egui/src/opening.rs b/crates/ui-egui/src/opening.rs index 214b0e56f2..5c08b2e2fe 100644 --- a/crates/ui-egui/src/opening.rs +++ b/crates/ui-egui/src/opening.rs @@ -80,6 +80,20 @@ pub(crate) struct Placement { pub(crate) replaced: Option<(vectorcraft_doc::NodeId, vectorcraft_doc::Node)>, } +impl Placement { + /// Is the document it was placed in still open, unreverted and active? + pub(crate) fn check(&self, app: &VectorcraftApp) -> Result<(), String> { + let st = app.session.documents().iter().find(|st| st.uid == self.doc).ok_or("the document it was placed in was closed")?; + if st.reverts != self.reverts { + return Err("the document it was placed in was reverted".into()); + } + if !app.session.doc().is_ok_and(|st| st.uid == self.doc) { + return Err("the document it was placed in is not the active document".into()); + } + Ok(()) + } +} + /// The files being read. #[derive(Default)] pub struct Opening { @@ -292,18 +306,12 @@ pub fn poll(app: &mut VectorcraftApp) { // placed when that document was closed or reverted meanwhile. if let Some(placement) = &c.placed_in { let name = fileio::file_name(&c.name); - match app.session.documents().iter().position(|st| st.uid == placement.doc) { - None => { - app.status(format!("Couldn't place {name}: the document it was placed in was closed")); - continue; - } - Some(d) if app.session.documents()[d].reverts != placement.reverts => { - app.status(format!("Couldn't place {name}: the document it was placed in was reverted")); - continue; - } - Some(d) => { - app.session.set_active(d); - } + if let Some(d) = app.session.documents().iter().position(|st| st.uid == placement.doc && st.reverts == placement.reverts) { + app.session.set_active(d); + } + if let Err(e) = placement.check(app) { + app.status(format!("Couldn't place {name}: {e}")); + continue; } } c.scope.run(|| crate::dialogs::dxf_import::converted(app, &c.name, r, c.path, c.place.as_ref(), c.placed_in));