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.
This commit is contained in:
dotCooCoo committed 2026-11-22 16:54:57 -08:00
1 parent abc0872cd0
commit d14d2137c5
3 files changed
+80 -10

No files matched your search

+4 -1
View File
@@ -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);
+23 -9
View File
@@ -346,22 +346,24 @@ fn described(ui: &mut egui::Ui, d: &Dialog) -> Option<Arc<Plugin>> {
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.<plug-in id>.<command id>`), 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<Manifest> {
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<Value, Str
/// The plug-in's description and fields (a command's: its own parameters), then the live
/// Preview; a heavy filter has a note instead that it runs in the background, since a run may take
/// minutes. A command dialog closes once the plug-in no longer has the command.
/// minutes. A command dialog closes once the plug-in no longer has the command, or once another
/// document is the active one.
fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> 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<Value, String> {
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)
+53
View File
@@ -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::<Vec<_>>();
let kept = |app: &VectorcraftApp| app.session.documents().iter().map(|st| st.doc.node(target).is_some()).collect::<Vec<_>>();
// 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() {