diff --git a/crates/ui-egui/src/dialogs/export_as.rs b/crates/ui-egui/src/dialogs/export_as.rs index 30af709740..7894461e9a 100644 --- a/crates/ui-egui/src/dialogs/export_as.rs +++ b/crates/ui-egui/src/dialogs/export_as.rs @@ -111,9 +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 source = app.session.active().ok_or("no document")?; - let document = source.uid; - let n = source.doc.artboards.len(); + let source = app.session.active(); + let document = source.map(|source| source.uid); + let n = source.map_or(0, |source| 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, @@ -138,7 +138,8 @@ fn open_dialog(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) -> Res 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> { +fn require_source(app: &VectorcraftApp, document: Option) -> Result<(), String> { + let document = document.ok_or_else(|| tl!("Open Export As from the drawing you want to export").to_owned())?; if app.session.active().is_none_or(|source| source.uid != document) { return Err(tl!("Return to the original drawing to export it").into()); } @@ -147,7 +148,7 @@ fn require_source(app: &VectorcraftApp, document: u64) -> Result<(), String> { /// 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, document: u64, fields: &DialogFields) -> Result { +pub(super) fn confirm(app: &mut VectorcraftApp, document: Option, fields: &DialogFields) -> Result { require_source(app, document)?; if let Some((_, hint)) = unavailable(fields) { return Err(hint.into()); diff --git a/crates/ui-egui/src/dialogs/export_host.rs b/crates/ui-egui/src/dialogs/export_host.rs index 2d45ad40ca..fb68f64c51 100644 --- a/crates/ui-egui/src/dialogs/export_host.rs +++ b/crates/ui-egui/src/dialogs/export_host.rs @@ -22,8 +22,8 @@ pub(crate) type After = Option Result, }, /// The options of the raster format `format`. Raster { diff --git a/crates/ui-egui/src/dialogs/tests_export.rs b/crates/ui-egui/src/dialogs/tests_export.rs index 521cb19043..7ab85da4ae 100644 --- a/crates/ui-egui/src/dialogs/tests_export.rs +++ b/crates/ui-egui/src/dialogs/tests_export.rs @@ -344,6 +344,22 @@ fn a_bad_range_keeps_export_as_open() { assert!(written.borrow().is_empty()); } +#[test] +fn export_as_opened_without_a_drawing_does_not_take_a_later_drawing() { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Services::default())); + save_panel(&mut app, |_| Some("/out/untitled.png".into())); + app.run("file.exportAs", json!({"format": "png"})).unwrap(); + assert_eq!(kind(&app).as_deref(), Some("exportAs")); + assert_eq!(fields(&app)["format"], "png"); + + 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("the old Export As draft has no source drawing"); + assert!(error.contains("Open Export As from the drawing"), "{error}"); + assert_eq!(kind(&app).as_deref(), Some("exportAs")); + PANEL.with_borrow(|panel| assert!(panel.as_ref().unwrap().picker.requests().is_empty())); +} + #[test] fn export_as_waits_for_the_drawing_that_opened_it() { let (mut app, written) = app(2);