From d14d2137c5aa91fd82a00da83d4d7e475f609580 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 16:54:57 -0800 Subject: [PATCH] Plug-ins: a command dialog runs only on the document it opened on A plug-in command's dialog now records the uid of the document it opened on (`__doc`). Object ids come from a per-document counter, so the ids in `__ids` can name a different object in another document. Once another document is the active one, the dialog closes when it is next drawn, and OK closes it without running the command. Closing it that way leaves the active document's interaction in progress. --- crates/ui-egui/src/dialogs.rs | 5 ++- crates/ui-egui/src/dialogs/plugin.rs | 32 ++++++++++++----- crates/ui-egui/src/tests/plugins.rs | 53 ++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index a24755ab12..461b9b5e86 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -826,7 +826,10 @@ pub fn cancel(app: &mut VectorcraftApp) { // that was already active before opening them. The generic parameter dialog has a spec // without a preview, yet previews some commands (Grid Repeat Options) when asked to. let owns_preview = d.kind != flare_options::KIND || d.fields.contains_key(form::PREVIEWED); - if (spec(&d.kind).preview && owns_preview) || d.fields.contains_key(form::PREVIEWED) { + // A plug-in command's dialog previews on the document it opened on, so closing it while + // another document is active leaves that document's interaction alone. + let elsewhere = d.kind == plugin::KIND && !plugin::on_its_document(app, &d); + if !elsewhere && ((spec(&d.kind).preview && owns_preview) || d.fields.contains_key(form::PREVIEWED)) { let _ = app.session.cancel_interaction(); } app.ui.dialog = parent(&d); diff --git a/crates/ui-egui/src/dialogs/plugin.rs b/crates/ui-egui/src/dialogs/plugin.rs index d41036d131..2dd4a29bf5 100644 --- a/crates/ui-egui/src/dialogs/plugin.rs +++ b/crates/ui-egui/src/dialogs/plugin.rs @@ -346,22 +346,24 @@ fn described(ui: &mut egui::Ui, d: &Dialog) -> Option> { Some(plugin) } +/// The field of a plug-in command's dialog holding the uid of the document it opened on. +const DOC: &str = "__doc"; + /// Opens the dialog of plug-in command `id` (`plugin..`), given no params /// or only `ids` (a context menu's objects), when it has parameters and a document is open: its -/// fields at their defaults (`__command` is the command id, `__ids` the objects given) with a live -/// Preview, and OK keeps the run on those objects as one undo step. False for any other command or -/// params, which then run as they were given. +/// fields at their defaults (`__command` is the command id, `__ids` the objects given, `__doc` the +/// uid of the active document) with a live Preview, and OK keeps the run on those objects as one +/// undo step. False for any other command or params, which then run as they were given. pub fn open_command(app: &mut VectorcraftApp, id: &str, p: &Value) -> bool { let only_ids = p.as_object().is_none_or(|o| o.keys().all(|k| k == "ids")) && p.get("ids").is_none_or(Value::is_array); if !only_ids { return false; } let Some((plugin, c)) = vectorcraft_engine::cmd::plugin::command_of(id) else { return false }; - if c.params.is_empty() || app.session.active().is_none() { - return false; - } + let Some(doc) = app.session.active().map(|st| st.uid).filter(|_| !c.params.is_empty()) else { return false }; let mut fields = fields_for(&plugin.manifest().for_command(&c)); fields.insert("__command".into(), json!(id)); + fields.insert(DOC.into(), json!(doc)); if let Some(ids) = p.get("ids") { fields.insert("__ids".into(), ids.clone()); } @@ -378,6 +380,12 @@ fn command_of(d: &Dialog) -> Option { Some(plugin.manifest().for_command(&c)) } +/// Whether dialog `d` may run on the active document: a plug-in command's dialog runs only on the +/// document it opened on, and a filter's dialog runs on any document. +pub(super) fn on_its_document(app: &VectorcraftApp, d: &Dialog) -> bool { + d.str("__command").is_empty() || app.session.active().is_some_and(|st| d.fields.get(DOC).and_then(Value::as_u64) == Some(st.uid)) +} + /// The command dialog `d` runs: its plug-in command, or `plugin.run`. fn command(d: &Dialog) -> String { match d.str("__command") { @@ -416,11 +424,12 @@ fn run(app: &mut VectorcraftApp, plugin: &Plugin, p: Value) -> Result bool { let Some(plugin) = described(ui, d) else { return true }; let run_with = command_of(d); - if run_with.is_none() && !d.str("__command").is_empty() { + if (run_with.is_none() && !d.str("__command").is_empty()) || !on_its_document(app, d) { return true; } form::schema_fields(app, ui, d, run_with.as_ref().unwrap_or(plugin.manifest())); @@ -442,8 +451,13 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { /// OK: a filter keeps its previewed run as one undo step; a heavy one, which has no preview, and a /// light one that runs in the background ([`in_background`]) with Preview off or a failed preview, /// start in the background ([`run`]) and the dialog closes. A preview still shown when Preview was -/// just turned off, or a preview that failed, is rolled back first. +/// just turned off, or a preview that failed, is rolled back first. Once another document is +/// the active one, a command dialog closes and changes no document. fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { + if !on_its_document(app, d) { + app.ui.dialog = None; + return Ok(json!({})); + } let own_run = d.str("__command").is_empty(); let kept = |plugin: &Plugin| { plugin.manifest().budget == Budget::Light && d.bool("preview") && app.session.in_interaction() && !d.fields.contains_key(form::REFUSED) diff --git a/crates/ui-egui/src/tests/plugins.rs b/crates/ui-egui/src/tests/plugins.rs index 4bcbd0be2b..4cb2ef5e37 100644 --- a/crates/ui-egui/src/tests/plugins.rs +++ b/crates/ui-egui/src/tests/plugins.rs @@ -961,6 +961,59 @@ fn plugin_commands_are_listed_in_the_canvas_and_layers_context_menus() { assert!(rows(&[other.0]).is_empty()); } +/// A command dialog opened for objects of one document changes no document once another document +/// is the active one, even one holding an object with the same id: drawn, the dialog closes and +/// leaves that document's edit in progress, and OK closes it. On its own document, OK runs the +/// command on those objects. +#[test] +fn a_plugin_command_dialog_stays_with_its_document() { + let mut app = app(); + let id = "test.ui.context.doc"; + let size = vectorcraft_engine::cmd::plugin::command_id(id, "size"); + let commands = r#"[{"id":"size","label":"Size","params":{"by":{"type":"number","min":0,"max":10,"default":2}}}]"#; + app.run("plugin.install", json!({"dataBase64": context_plugin(id, commands)})).unwrap(); + let target = red_rect(&mut app); + app.run("layer.setProps", json!({"ids": [target.0], "name": "Target"})).unwrap(); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let twin = red_rect(&mut app); + app.run("layer.setProps", json!({"ids": [twin.0], "name": "Target"})).unwrap(); + assert_eq!(twin, target, "the second document's object has the first one's id"); + let ids = json!({"ids": [target.0]}); + let undos = |app: &VectorcraftApp| app.session.documents().iter().map(|st| st.history.undo.len()).collect::>(); + let kept = |app: &VectorcraftApp| app.session.documents().iter().map(|st| st.doc.node(target).is_some()).collect::>(); + + // Drawn while the other document is active, the dialog closes. + app.run("document.activate", json!({"index": 0})).unwrap(); + menus::invoke(&mut app, &size, ids.clone()); + assert_eq!(app.ui.dialog.as_ref().map(|d| d.str("__command")), Some(size.clone())); + let before = undos(&app); + frame(&mut app); + app.run("document.activate", json!({"index": 1})).unwrap(); + app.begin_interaction("Edit").unwrap(); + frame(&mut app); + assert!(app.ui.dialog.is_none(), "the dialog closes on another document"); + assert!(app.session.in_interaction(), "closing it leaves the other document's edit in progress"); + app.session.cancel_interaction().unwrap(); + assert_eq!((undos(&app), kept(&app)), (before.clone(), vec![true, true])); + + // OK while the other document is active closes the dialog and changes neither document. + app.run("document.activate", json!({"index": 0})).unwrap(); + menus::invoke(&mut app, &size, ids.clone()); + frame(&mut app); + app.run("document.activate", json!({"index": 1})).unwrap(); + assert_eq!(dialogs::confirm(&mut app).unwrap(), json!({})); + assert!(app.ui.dialog.is_none()); + assert_eq!((undos(&app), kept(&app)), (before.clone(), vec![true, true])); + + // On its own document, OK runs the command on the objects it opened for. + app.run("document.activate", json!({"index": 0})).unwrap(); + menus::invoke(&mut app, &size, ids); + frame(&mut app); + dialogs::confirm(&mut app).unwrap(); + assert_eq!((undos(&app), kept(&app)), (vec![before[0] + 1, before[1]], vec![false, true])); + app.run("plugin.remove", json!({"id": id})).unwrap(); +} + /// A shortcut the user gives a plug-in command runs it. #[test] fn a_shortcut_runs_a_plugin_command() {