From 477844c36d06631c3d7963f1c0e9b1c1082de248 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 12:50:32 -0800 Subject: [PATCH] Plug-in export: write the document the dialog opened on An exporter with parameters opens its dialog after Export As picks the file. Export then wrote whichever drawing was active when it was pressed, so switching tabs while the dialog was open exported the other drawing under the first drawing's file name. The dialog now records the drawing it opened on and its revert count. Export makes that drawing active again and writes it, as the file dialogs' continuations do (file_asks.rs, now through the shared `back_to`). A drawing closed or reverted while the dialog was open is not exported, and the status bar says why. The same pattern was used for the Transform dialogs in #9584. --- crates/ui-egui/src/dialogs/plugin.rs | 28 +++++- .../src/dialogs/tests_plugin_export.rs | 91 ++++++++++++++++++- crates/ui-egui/src/file_asks.rs | 11 ++- 3 files changed, 122 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/dialogs/plugin.rs b/crates/ui-egui/src/dialogs/plugin.rs index 94b82a5259..54ddd836b4 100644 --- a/crates/ui-egui/src/dialogs/plugin.rs +++ b/crates/ui-egui/src/dialogs/plugin.rs @@ -264,7 +264,13 @@ pub(crate) enum Opened { params: Arc<[Param]>, }, /// An exporter's parameters, with Export As's own parameters (`format`, `path`, …) kept for Export. - Export { plugin: String, export: Value, params: Arc<[Param]> }, + Export { + plugin: String, + export: Value, + /// The drawing the dialog opened on and its revert count then: Export writes that drawing. + document: Option<(u64, u64)>, + params: Arc<[Param]>, + }, /// An importer's parameters, with Open With's own parameters (`path`, `importer`, `picked`) kept for Open. Import { plugin: String, open: Value, params: Arc<[Param]> }, } @@ -654,8 +660,11 @@ pub fn open_export(app: &mut VectorcraftApp, plugin: &Plugin, export: Value) { note: false, ok: Some(tl!("Export")), }; + let document = app.session.active().map(|st| (st.uid, st.reverts)); match open_hosted(app, EXPORT_KIND, plugin.id(), m.clone(), m.defaults(), look) { - Ok((id, params)) => app.plugin_dialog = Some(PluginDialog { id, kind: Opened::Export { plugin: plugin.id().to_string(), export, params } }), + Ok((id, params)) => { + app.plugin_dialog = Some(PluginDialog { id, kind: Opened::Export { plugin: plugin.id().to_string(), export, document, params } }) + } Err(e) => app.status(e), } } @@ -678,8 +687,17 @@ pub fn open_import(app: &mut VectorcraftApp, plugin: &Plugin, open: Value) { } } -/// Writes the file with the export's parameters and the plug-in's, as `params`. -fn export_with(app: &mut VectorcraftApp, export: Value, values: Map) -> Result { +/// Writes the file with the export's parameters and the plug-in's, as `params`, of `document`, the +/// drawing the dialog opened on, which is made active again first. A drawing closed or reverted +/// since is not exported, and the status bar says why. +fn export_with(app: &mut VectorcraftApp, export: Value, document: Option<(u64, u64)>, values: Map) -> Result { + if let Some((uid, reverts)) = document { + let closed = tl!("The drawing was closed while its export options were open"); + let reverted = tl!("The drawing was reverted while its export options were open"); + if !crate::file_asks::back_to(app, uid, Some(reverts), closed, reverted) { + return Ok(Value::Null); + } + } let mut p = Some(export).filter(Value::is_object).ok_or("no format to export to")?; p["params"] = Value::Object(values); let format = p.get("format").and_then(Value::as_str).map(str::to_string); @@ -774,7 +792,7 @@ fn pump_dialog(app: &mut VectorcraftApp, ctx: &egui::Context) { // The dialog is already closed, so a failure goes to the status line. let done = match open.kind { Opened::Filter { .. } => confirm_filter(app, open.kind, values), - Opened::Export { export, .. } => export_with(app, export, values), + Opened::Export { export, document, .. } => export_with(app, export, document, values), Opened::Import { open, .. } => import_with(app, open, values), }; if let Err(e) = done { diff --git a/crates/ui-egui/src/dialogs/tests_plugin_export.rs b/crates/ui-egui/src/dialogs/tests_plugin_export.rs index 873715cbea..4943339805 100644 --- a/crates/ui-egui/src/dialogs/tests_plugin_export.rs +++ b/crates/ui-egui/src/dialogs/tests_plugin_export.rs @@ -6,7 +6,7 @@ use serde_json::{Value, json}; use vectorcraft_plugins::wat::{self as tpl, Echo}; -use super::tests_export::{answer_saves, app, app_saving, fields, frame, kind, names, set, try_confirm, try_set}; +use super::tests_export::{Written, answer_saves, app, app_saving, fields, frame, kind, names, set, try_confirm, try_set}; use super::*; use crate::VectorcraftApp; @@ -72,6 +72,95 @@ fn an_exporter_with_parameters_asks_for_them_once_the_file_is_picked() { app.run("plugin.remove", json!({"id": "test.ui.params"})).unwrap(); } +/// [`app`] with a second drawing that has no ellipse, the first active and saved in `dir`, so it +/// closes without asking and can be reverted. +fn two_drawings(dir: &vectorcraft_testkit::TempDir) -> (VectorcraftApp, Written) { + let (mut app, written) = app(1); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("first.vectorcraft")})).unwrap(); + let w = written.clone(); + app.services.write = Some(Box::new(move |p: &str, b: &[u8]| { + w.borrow_mut().push((p.to_string(), b.to_vec())); + Ok(()) + })); + app.run("file.new", json!({"width": 60, "height": 40})).unwrap(); + app.run("document.activate", json!({"index": 0})).unwrap(); + (app, written) +} + +/// Installs exporter `id` (extension `tus`), which has parameters and writes the document it is +/// handed. +fn install_source_exporter(app: &mut VectorcraftApp, id: &str) { + let params = r#"{"density":{"type":"number","min":0,"max":10,"default":4}}"#; + install(app, &tpl::echo_exporter(&tpl::exporter(id, r#"["tus"]"#, params), Echo::Input)); +} + +/// Export As with exporter `id`, up to its parameters dialog, opened on the active drawing. +fn open_source_exporter(app: &mut VectorcraftApp, id: &str) { + app.run("file.exportAs", json!({"format": format!("plugin.{id}")})).unwrap(); + try_confirm(app).unwrap(); + assert_eq!(kind(app).as_deref(), Some(plugin::EXPORT_KIND)); + frame(app); +} + +/// The exporter's files. +fn exported(written: &Written) -> Vec<(String, Vec)> { + written.borrow().iter().filter(|(p, _)| p.ends_with(".tus")).cloned().collect() +} + +/// Export writes the drawing the dialog opened on, made active again, not the one active when +/// Export is pressed. +#[test] +fn an_exporter_writes_the_drawing_its_dialog_opened_on() { + let dir = vectorcraft_testkit::TempDir::new("plugin-export-opening-drawing"); + let (mut app, written) = two_drawings(&dir); + let id = "test.ui.opening"; + install_source_exporter(&mut app, id); + open_source_exporter(&mut app, id); + app.run("document.activate", json!({"index": 1})).unwrap(); + frame(&mut app); + try_confirm(&mut app).unwrap(); + assert!(kind(&app).is_none()); + assert_eq!(app.session.active_index(), Some(0), "the opening drawing is active again"); + // The second drawing, exported from its own dialog, is another file. + app.run("document.activate", json!({"index": 1})).unwrap(); + open_source_exporter(&mut app, id); + try_confirm(&mut app).unwrap(); + let files = exported(&written); + assert_eq!(files.len(), 2, "{:?}", files.iter().map(|(p, _)| p).collect::>()); + assert_ne!(files[0].1, files[1].1, "the first export wrote the first drawing, not the second"); + app.run("plugin.remove", json!({"id": id})).unwrap(); +} + +/// A drawing reverted or closed while its exporter's dialog was open is not exported, and the +/// status bar says why; the drawing active then is not exported either. +#[test] +fn an_exporter_writes_nothing_once_its_drawing_is_reverted_or_closed() { + for close in [false, true] { + let dir = vectorcraft_testkit::TempDir::new("plugin-export-reverted-drawing"); + let (mut app, written) = two_drawings(&dir); + let id = if close { "test.ui.closed" } else { "test.ui.reverted" }; + install_source_exporter(&mut app, id); + open_source_exporter(&mut app, id); + if close { + app.run("document.activate", json!({"index": 1})).unwrap(); + app.run("file.close", json!({"index": 0})).unwrap(); + assert_eq!(app.session.documents().len(), 1, "the saved drawing closes without asking"); + } else { + app.run("shape.rectangle", json!({"x": 2, "y": 2, "width": 10, "height": 10})).unwrap(); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + } + frame(&mut app); + try_confirm(&mut app).unwrap(); + assert!(kind(&app).is_none()); + assert!(exported(&written).is_empty(), "nothing exported"); + let said = if close { "closed" } else { "reverted" }; + assert!(app.ui.status.contains(said), "{}", app.ui.status); + app.run("plugin.remove", json!({"id": id})).unwrap(); + } +} + /// A plug-in that declines keeps nothing written and says why. #[test] fn a_declining_exporter_writes_nothing() { diff --git a/crates/ui-egui/src/file_asks.rs b/crates/ui-egui/src/file_asks.rs index f70ee645e4..babf4e1890 100644 --- a/crates/ui-egui/src/file_asks.rs +++ b/crates/ui-egui/src/file_asks.rs @@ -266,12 +266,19 @@ pub(crate) fn open_params(picked: &Picked) -> Option> { /// Make the original drawing active before running a continuation. The status bar explains why /// a closed drawing or a different loaded generation cannot receive the result. fn on_document(app: &mut VectorcraftApp, uid: u64, generation: Option) -> bool { + back_to(app, uid, generation, closed(), tl!("The drawing was reverted while its file dialog was open")) +} + +/// Makes drawing `uid` active again, at its revert count `generation` when one is given. When the +/// drawing is closed, or was reverted since, the status bar shows `closed` or `reverted` and the +/// answer is false. +pub(crate) fn back_to(app: &mut VectorcraftApp, uid: u64, generation: Option, closed: &str, reverted: &str) -> bool { let Some((i, state)) = app.session.documents().iter().enumerate().find(|(_, d)| d.uid == uid) else { - app.status(closed()); + app.status(closed); return false; }; if generation.is_some_and(|reverts| state.reverts != reverts) { - app.status(tl!("The drawing was reverted while its file dialog was open")); + app.status(reverted); return false; } if app.session.active_index() != Some(i)