diff --git a/crates/ui-egui/src/dialogs/plugin.rs b/crates/ui-egui/src/dialogs/plugin.rs index 02d7ca9dd3..0cd94a4c55 100644 --- a/crates/ui-egui/src/dialogs/plugin.rs +++ b/crates/ui-egui/src/dialogs/plugin.rs @@ -190,34 +190,37 @@ pub(crate) fn before_command() -> u64 { } /// After command `id` with `params` ran with result `r`, the registry at revision `before` when it -/// started: a `plugin.run` that succeeded, or the last `plugin.run` step of a batch that succeeded -/// (`command.batch`, or `action.batch` on at least one file or data set), is noted as the filter -/// that ran last ([`record`]). +/// started: a `plugin.run` that succeeded, or each `plugin.run` step of a batch that succeeded +/// (`command.batch`, or `action.batch` on at least one file or data set) in the order they ran, is +/// noted ([`record`]), so the last one is the filter that ran last. pub(crate) fn after_command(app: &mut VectorcraftApp, id: &str, params: &Value, r: &Result, before: u64) { let Ok(reply) = r else { return }; // Action Batch returns success even when files or data sets fail, and runs every step on each // one that doesn't. let played = |v: &Value| v.get("count").and_then(Value::as_u64).unwrap_or(0) > v.get("failed").and_then(Value::as_u64).unwrap_or(0); - let ran = match id { - CMD => Some(params), + match id { + CMD => record(app, params), "command.batch" | "action.batch" if id == "command.batch" || played(reply) => { let steps = params.get("commands").and_then(Value::as_array).map(Vec::as_slice).unwrap_or_default(); - let Some(at) = steps.iter().rposition(|s| step_id(s) == Some(CMD)) else { return }; - let p = steps[at].get("params").unwrap_or(&Value::Null); - // The plug-in installed under the id now is the one that ran when no later step changed - // the plug-ins, or when it was installed before the batch (installing gives a new serial). - let changed_later = steps[at + 1..].iter().any(|s| step_id(s).is_some_and(|c| REGISTRY_CHANGES.contains(&c))); - let installed_before = p.get("id").and_then(Value::as_str).and_then(registry::get).is_some_and(|q| q.serial() <= before); - if changed_later && !installed_before { - forget(app, p); - return; + // `command.batch` returns each step's result; Action Batch returns none. + let results = reply.get("results").and_then(Value::as_array); + // Every filter step is noted in the order the steps ran, so the last one is the filter + // that ran last and a filter that ran twice keeps its newest parameters. + for (at, s) in steps.iter().enumerate().filter(|(_, s)| step_id(s) == Some(CMD)) { + let p = s.get("params").unwrap_or(&Value::Null); + let ran = p.get("id").and_then(Value::as_str).unwrap_or_default(); + // The plug-in installed under the id now is the one that ran when no later step may + // have changed it, or when it was installed before the batch (installing gives a new + // serial). + let changed_later = (at + 1..steps.len()).any(|i| may_change(&steps[i], results.and_then(|r| r.get(i)), ran)); + let installed_before = registry::get(ran).is_some_and(|q| q.serial() <= before); + match changed_later && !installed_before { + true => forget(app, p), + false => record(app, p), + } } - Some(p) } - _ => None, - }; - if let Some(p) = ran.cloned() { - record(app, &p); + _ => {} } } @@ -226,8 +229,17 @@ fn step_id(s: &Value) -> Option<&str> { s.get("command").and_then(Value::as_str) } -/// The commands that install, remove or reload plug-ins. -const REGISTRY_CHANGES: [&str; 3] = ["plugin.install", "plugin.remove", "plugin.reload"]; +/// Whether batch step `s`, which returned `result` if the batch reports it, may have installed, +/// removed or reloaded plug-in `id`: a removal names its id, an install reports the id it installed, +/// and a reload, or an install whose result is not known, may change any plug-in. +fn may_change(s: &Value, result: Option<&Value>, id: &str) -> bool { + match step_id(s) { + Some("plugin.remove") => s.get("params").and_then(|p| p.get("id")).and_then(Value::as_str) == Some(id), + Some("plugin.install") => result.and_then(|r| r.get("id")).and_then(Value::as_str).is_none_or(|i| i == id), + Some("plugin.reload") => true, + _ => false, + } +} /// Notes the filter `plugin.run` params `p` name as the one that ran last, without parameters or a /// module, so that Apply Last Filter and Last Filter… stay off until a filter runs again. diff --git a/crates/ui-egui/src/tests/last_filter.rs b/crates/ui-egui/src/tests/last_filter.rs index 20b761062f..b546771d83 100644 --- a/crates/ui-egui/src/tests/last_filter.rs +++ b/crates/ui-egui/src/tests/last_filter.rs @@ -199,6 +199,62 @@ fn only_successful_filter_runs_become_the_last_filter() { dialogs::cancel(&mut app); } +/// Every filter step of a batch counts, in the order the steps ran: the last one becomes the last +/// filter, each filter's dialog opens with the parameters it last ran with in the batch, and a +/// filter that ran twice keeps the newer ones. +#[test] +fn every_filter_step_of_a_batch_counts() { + let mut app = app(); + let (a, b) = ("test.ui.last.batch.a", "test.ui.last.batch.b"); + install(&mut app, &filter(a, "Batch A", N)); + install(&mut app, &filter(b, "Batch B", N)); + let steps = json!([ + {"command": "plugin.run", "params": {"id": a, "params": {"n": 4}}}, + {"command": "plugin.run", "params": {"id": b, "params": {"n": 2}}}, + {"command": "plugin.run", "params": {"id": a, "params": {"n": 7}}}, + {"command": "plugin.run", "params": {"id": b, "params": {"n": 6}}}, + ]); + app.run("command.batch", json!({"commands": steps})).unwrap(); + assert_eq!(app.last_filters.last_id(), Some(b), "the last filter step"); + for (id, n) in [(a, 7), (b, 6)] { + app.run("plugin.dialog", json!({"id": id})).unwrap(); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields.get("n"), Some(&json!(n)), "{id}"); + dialogs::cancel(&mut app); + } + + // Filters installed in the batch: installing another plug-in after a filter ran keeps that + // filter's parameters. + let (c, d) = ("test.ui.last.batch.c", "test.ui.last.batch.d"); + let steps = json!([ + {"command": "plugin.install", "params": {"dataBase64": vectorcraft_format::base64_encode(&filter(c, "Batch C", N)), "replace": true}}, + {"command": "plugin.run", "params": {"id": c, "params": {"n": 7}}}, + {"command": "plugin.install", "params": {"dataBase64": vectorcraft_format::base64_encode(&filter(d, "Batch D", N)), "replace": true}}, + {"command": "plugin.run", "params": {"id": d, "params": {"n": 2}}}, + ]); + app.run("command.batch", json!({"commands": steps})).unwrap(); + assert_eq!(app.last_filters.last_id(), Some(d), "the last filter step"); + for (id, n) in [(c, 7), (d, 2)] { + app.run("plugin.dialog", json!({"id": id})).unwrap(); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields.get("n"), Some(&json!(n)), "{id}"); + dialogs::cancel(&mut app); + } + + // The same over an Action Batch, with the filters in the other order. + app.run("dataset.new", json!({"name": "a"})).unwrap(); + let steps = json!([ + {"command": "plugin.run", "params": {"id": b, "params": {"n": 1}}}, + {"command": "plugin.run", "params": {"id": a, "params": {"n": 9}}}, + ]); + let r = app.run("action.batch", json!({"source": "datasets", "commands": steps})).unwrap(); + assert_eq!((r["count"].clone(), r["failed"].clone()), (json!(1), json!(0)), "{r}"); + assert_eq!(app.last_filters.last_id(), Some(a), "the last Action Batch filter step"); + for (id, n) in [(a, 9), (b, 1)] { + app.run("plugin.dialog", json!({"id": id})).unwrap(); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields.get("n"), Some(&json!(n)), "{id}"); + dialogs::cancel(&mut app); + } +} + /// Installing the plug-in again, even the same module, another module with its id, one whose /// manifest no longer takes the parameters, and removing it turn both items off, and the new /// module's dialog opens with its own defaults. Installing another plug-in keeps them. diff --git a/docs/plugins.md b/docs/plugins.md index fe3e2a4161..7a881396f0 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -64,7 +64,7 @@ the filter that last ran again with the same parameters on the selection, withou **Last Filter…** (`plugin.lastFilter`), which opens that filter's dialog with those parameters, its OK running it as a new run. Once a filter has run they read **Apply** followed by the filter's name and the filter's name with an ellipsis. A run counts once it succeeds: from its menu item, its dialog's OK, a shortcut, `plugin.run` or a -`command.batch` step (the last such step of the batch), an Action Batch (`action.batch`) step once a file or data set +`command.batch` step (each such step in the order the batch ran them, so the last one runs again), an Action Batch (`action.batch`) step once a file or data set ran it, and a background run once its result is applied. A preview, Cancel, a failed or canceled run, a plug-in's own commands and live effects don't count, and neither does a batch step whose filter the batch installed when a later step of the batch installs, removes or