mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 12:21:08 +01:00
Merge pull request #9104 from dotCooCoo/feat/plugin-api-last-filter
Plug-ins: Apply Last Filter and Last Filter… repeat the last filter plug-in
This commit is contained in:
17 files changed
+711
-23
No files matched your search
@@ -150,7 +150,7 @@ pub fn describe(p: &Plugin) -> Value {
|
||||
|
||||
/// The plug-in's own parameters from command params: `params` if given, else the top-level keys
|
||||
/// minus the ones the command itself uses.
|
||||
fn plugin_params(p: &Value) -> Value {
|
||||
pub fn plugin_params(p: &Value) -> Value {
|
||||
if let Some(v) = p.get("params") {
|
||||
return v.clone();
|
||||
}
|
||||
|
||||
@@ -1986,7 +1986,19 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"plugin.dialog",
|
||||
"Plug-in…",
|
||||
"",
|
||||
"{id: an object filter plug-in (plugin.list), params?: {…}} Object › Plug-ins: run the filter at once when it takes no parameters or params are given (plugin.run), else open its dialog (dialog `plugin`: a field per parameter, `__plugin` the id, live preview; OK runs plugin.run as one undo step). A heavy filter, and a filter that lists `progress` and no commandAllowlist, run in the background when they run at once or OK runs them without a kept preview → {background: true, status} (see plugin.cancelRuns)",
|
||||
"{id: an object filter plug-in (plugin.list), params?: {…}} Object › Plug-ins: run the filter at once when it takes no parameters or params are given (plugin.run), else open its dialog (dialog `plugin`: a field per parameter at the value the filter last ran with in this session, else its default, `__plugin` the id, live preview; OK runs plugin.run as one undo step). A heavy filter, and a filter that lists `progress` and no commandAllowlist, run in the background when they run at once or OK runs them without a kept preview → {background: true, status} (see plugin.cancelRuns)",
|
||||
),
|
||||
(
|
||||
"plugin.applyLastFilter",
|
||||
"Apply Last Filter",
|
||||
"",
|
||||
"{} Object › Plug-ins › Apply <filter name>: run the object filter that last ran in this session (a plugin.run that succeeded: its menu item, its dialog's OK, a request, a command.batch step or an action.batch step; not a plug-in's own commands) again on the selection with the same parameters, without its dialog, as one undo step; it runs in the background where its menu item would → plugin.run's result | {background: true, status}. An error with no document open, when no filter ran yet, when that filter was removed or installed again (the same module too) since, or when its manifest no longer takes those parameters as they are",
|
||||
),
|
||||
(
|
||||
"plugin.lastFilter",
|
||||
"Last Filter…",
|
||||
"",
|
||||
"{} Object › Plug-ins › <filter name>…: open the dialog of the object filter that last ran in this session (see plugin.applyLastFilter) with the parameters it ran with (dialog `plugin`); OK runs it as one undo step and Cancel changes nothing. A filter without parameters runs at once. The same errors as plugin.applyLastFilter",
|
||||
),
|
||||
(
|
||||
"ui.installPlugin",
|
||||
|
||||
@@ -185,6 +185,7 @@ impl VectorcraftApp {
|
||||
titlebar_inset: 0.0,
|
||||
last_effect: None,
|
||||
last_pathfinder: None,
|
||||
last_filters: Default::default(),
|
||||
character_focus: None,
|
||||
run_count: 0,
|
||||
channel: None,
|
||||
|
||||
@@ -237,6 +237,7 @@ impl VectorcraftApp {
|
||||
return Err(e);
|
||||
}
|
||||
let spot_base = panels::color_guide::before_command(self);
|
||||
let plugins_before = dialogs::plugin::before_command();
|
||||
let r = self.session.execute(id, ¶ms).map_err(|e| e.to_string());
|
||||
let r = panels::actions::after_recorded_command(self, r);
|
||||
if r.is_ok()
|
||||
@@ -244,6 +245,8 @@ impl VectorcraftApp {
|
||||
{
|
||||
self.last_pathfinder = Some(op.to_string());
|
||||
}
|
||||
// A filter plug-in run becomes Object › Plug-ins › Apply Last Filter.
|
||||
dialogs::plugin::after_command(self, id, ¶ms, &r, plugins_before);
|
||||
let copied = r.is_ok() && self.publish_copy(id);
|
||||
self.sync_views();
|
||||
self.refresh_replace_font_warning(false);
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
//! filter or live effect with commands of its own is a submenu named after it, holding its item and
|
||||
//! then its commands; a command with parameters opens the same dialog for them ([`open_command`]).
|
||||
//! A command whose manifest names another menu is listed in that menu instead ([`place_commands`]).
|
||||
//! Object › Plug-ins starts with Apply Last Filter and Last Filter…, which run the filter that last
|
||||
//! ran again with the same parameters or open its dialog with them, and a filter's dialog opens with
|
||||
//! the parameters it last ran with in the session ([`LastFilters`]).
|
||||
//! Exporters are formats of File › Export › Export As; one with parameters opens dialog
|
||||
//! `pluginExport` once the file is picked, and its Export button writes the file. Importers open the
|
||||
//! files File › Open finds no built-in format for, and File › Open With lists them; one with
|
||||
@@ -335,14 +338,20 @@ fn build(key: &'static str, m: Manifest, all: Arc<[Param]>, look: Look) -> impl
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the hosted dialog `key` for plug-in manifest `m` (an instance per `instance`), closing the
|
||||
/// one open before → its id on the host and its rows.
|
||||
fn open_hosted(app: &mut VectorcraftApp, key: &'static str, instance: &str, m: Manifest, look: Look) -> Result<(DialogId, Arc<[Param]>), String> {
|
||||
/// Opens the hosted dialog `key` for plug-in manifest `m` (an instance per `instance`) with its rows
|
||||
/// at `values`, closing the one open before → its id on the host and its rows.
|
||||
fn open_hosted(
|
||||
app: &mut VectorcraftApp,
|
||||
key: &'static str,
|
||||
instance: &str,
|
||||
m: Manifest,
|
||||
values: Map<String, Value>,
|
||||
look: Look,
|
||||
) -> Result<(DialogId, Arc<[Param]>), String> {
|
||||
let (Some(ctx), Some(dialogs)) = (app.ui_ctx.clone(), app.dialogs()) else { return Err("no window".into()) };
|
||||
close_open(app);
|
||||
let defaults = m.defaults();
|
||||
let all: Arc<[Param]> = form::plugin_params(&m, &defaults).into();
|
||||
let fields = form::fields_of(&all, &defaults);
|
||||
let all: Arc<[Param]> = form::plugin_params(&m, &values).into();
|
||||
let fields = form::fields_of(&all, &values);
|
||||
let dialog = HostedParamDialog::new(&ctx, key, fields, build(key, m, Arc::clone(&all), look)).instance(instance.to_string());
|
||||
let id = dialogs.with(|host| host.open(dialog));
|
||||
Ok((id, all))
|
||||
@@ -359,8 +368,186 @@ fn close_open(app: &mut VectorcraftApp) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Object › Plug-ins' first items: Apply Last Filter, which runs the filter that last ran again with
|
||||
/// the same parameters, and Last Filter…, which opens its dialog with them. Each is named after the
|
||||
/// filter once one has run ([`last_filter_name`]).
|
||||
pub fn last_filter_menu() -> Vec<Node> {
|
||||
vec![c("Apply Last Filter", APPLY_LAST), c("Last Filter…", LAST), Sep]
|
||||
}
|
||||
|
||||
/// Apply Last Filter's command id.
|
||||
pub const APPLY_LAST: &str = "plugin.applyLastFilter";
|
||||
|
||||
/// Last Filter…'s command id.
|
||||
pub const LAST: &str = "plugin.lastFilter";
|
||||
|
||||
/// The error of Apply Last Filter and Last Filter… when no filter can run again: none ran in this
|
||||
/// session, or the one that ran last was removed or installed again, or its parameters no longer fit it.
|
||||
pub const NO_LAST_FILTER: &str = "no plug-in filter to repeat: run one from Object › Plug-ins first";
|
||||
|
||||
/// The most filters whose last parameters [`LastFilters`] keeps.
|
||||
const MAX_REMEMBERED: usize = 64;
|
||||
|
||||
/// The parameters filter `id` last ran with, as its manifest resolved them, and the installation
|
||||
/// that ran with them (its [`Plugin::serial`]; installing the plug-in again gives it a new one).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Used {
|
||||
id: String,
|
||||
serial: u64,
|
||||
params: Map<String, Value>,
|
||||
}
|
||||
|
||||
/// The parameters each object filter last ran with in this session (`plugin.run`), the filter that
|
||||
/// ran last at the end, for at most [`MAX_REMEMBERED`] filters. They are never saved.
|
||||
#[derive(Default)]
|
||||
pub struct LastFilters {
|
||||
used: Vec<Used>,
|
||||
}
|
||||
|
||||
impl LastFilters {
|
||||
/// The id of the filter that ran last, whether or not it can run again.
|
||||
pub fn last_id(&self) -> Option<&str> {
|
||||
self.used.last().map(|u| u.id.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// Notes a `plugin.run` with params `p` that succeeded: its filter ran last, with the
|
||||
/// parameters its manifest resolves from `p`. Anything else (not a filter, parameters the manifest
|
||||
/// refuses) is not noted.
|
||||
pub(crate) fn record(app: &mut VectorcraftApp, p: &Value) {
|
||||
let Some(plugin) = p.get("id").and_then(Value::as_str).and_then(registry::get) else { return };
|
||||
let m = plugin.manifest();
|
||||
if m.kind != Kind::Filter {
|
||||
return;
|
||||
}
|
||||
let Ok(params) = m.resolve_params(&vectorcraft_engine::cmd::plugin::plugin_params(p)) else { return };
|
||||
let used = &mut app.last_filters.used;
|
||||
used.retain(|u| u.id != m.id);
|
||||
used.push(Used { id: m.id.clone(), serial: plugin.serial(), params });
|
||||
if used.len() > MAX_REMEMBERED {
|
||||
used.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// The plug-in registry's revision before a command runs, for [`after_command`].
|
||||
pub(crate) fn before_command() -> u64 {
|
||||
registry::revision()
|
||||
}
|
||||
|
||||
/// After command `id` with `params` ran with result `r`, the registry at revision `before` when it
|
||||
/// 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<Value, String>, 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);
|
||||
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();
|
||||
// `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),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// The command id of batch step `s`.
|
||||
fn step_id(s: &Value) -> Option<&str> {
|
||||
s.get("command").and_then(Value::as_str)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn forget(app: &mut VectorcraftApp, p: &Value) {
|
||||
let Some(id) = p.get("id").and_then(Value::as_str) else { return };
|
||||
let used = &mut app.last_filters.used;
|
||||
used.retain(|u| u.id != id);
|
||||
// Serial 0 belongs to no installed plug-in.
|
||||
used.push(Used { id: id.to_string(), serial: 0, params: Map::new() });
|
||||
if used.len() > MAX_REMEMBERED {
|
||||
used.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter `id`'s last parameters, while the installation that ran with them is still installed under
|
||||
/// its id and its manifest takes them as they are.
|
||||
fn remembered(app: &VectorcraftApp, id: &str) -> Option<(Arc<Plugin>, Map<String, Value>)> {
|
||||
let used = app.last_filters.used.iter().find(|u| u.id == id)?;
|
||||
let plugin = registry::get(id).filter(|p| p.manifest().kind == Kind::Filter && p.serial() == used.serial)?;
|
||||
let params = Value::Object(used.params.clone());
|
||||
plugin.manifest().resolve_params(¶ms).ok().filter(|r| *r == used.params)?;
|
||||
Some((plugin, used.params.clone()))
|
||||
}
|
||||
|
||||
/// The filter that ran last and its parameters, while it can run again with them ([`remembered`]).
|
||||
pub(crate) fn last_filter(app: &VectorcraftApp) -> Option<(Arc<Plugin>, Map<String, Value>)> {
|
||||
remembered(app, app.last_filters.last_id()?)
|
||||
}
|
||||
|
||||
/// The name Apply Last Filter and Last Filter… show once a filter ran that can run again
|
||||
/// ([`last_filter`]): the filter's own name, without an ellipsis at its end and with hidden
|
||||
/// characters shown as escapes, as a submenu named after a plug-in shows it. `None` before.
|
||||
pub(crate) fn last_filter_name(app: &VectorcraftApp) -> Option<String> {
|
||||
let (plugin, _) = last_filter(app)?;
|
||||
let name = plugin.manifest().name.trim().trim_end_matches('…').trim_end().to_string();
|
||||
Some(permissions::display_text(&name, permissions::MAX_SHOWN_NAME))
|
||||
}
|
||||
|
||||
/// `plugin.applyLastFilter`: runs the filter that ran last again, with the same parameters, on the
|
||||
/// selection, without its dialog, as its menu item runs it ([`run`]).
|
||||
pub fn apply_last(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
app.session.active().ok_or("no document open")?;
|
||||
let (plugin, params) = last_filter(app).ok_or(NO_LAST_FILTER)?;
|
||||
let p = json!({"id": plugin.id(), "params": params});
|
||||
run(app, &plugin, p)
|
||||
}
|
||||
|
||||
/// `plugin.lastFilter`: opens the dialog of the filter that ran last with the parameters it ran
|
||||
/// with; one without parameters runs at once, as its menu item runs it.
|
||||
pub fn open_last(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
app.session.active().ok_or("no document open")?;
|
||||
let (plugin, params) = last_filter(app).ok_or(NO_LAST_FILTER)?;
|
||||
if plugin.manifest().params.is_empty() {
|
||||
return run(app, &plugin, json!({"id": plugin.id(), "params": params}));
|
||||
}
|
||||
let m = plugin.manifest();
|
||||
open_filter(app, &plugin, m.clone(), None, m.name.trim_end_matches('…').to_string(), params)?;
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
/// `plugin.dialog {id, params?}`: run filter `id` at once when it takes no parameters or `params`
|
||||
/// are given, else open its dialog prefilled with its defaults.
|
||||
/// are given, else open its dialog prefilled with the parameters it last ran with in this session
|
||||
/// ([`remembered`]), or with its defaults.
|
||||
pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
let id = p.get("id").and_then(Value::as_str).ok_or("missing `id` (see plugin.list)")?;
|
||||
let plugin = registry::get(id).ok_or_else(|| format!("no plug-in {id:?} is installed"))?;
|
||||
@@ -375,18 +562,26 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
if m.params.is_empty() || given.is_some() {
|
||||
return run(app, &plugin, json!({"id": id, "params": given.cloned().unwrap_or(Value::Null)}));
|
||||
}
|
||||
open_filter(app, &plugin, m.clone(), None, m.name.trim_end_matches('…').to_string())?;
|
||||
let values = remembered(app, id).map(|(_, params)| params).unwrap_or_else(|| m.defaults());
|
||||
open_filter(app, &plugin, m.clone(), None, m.name.trim_end_matches('…').to_string(), values)?;
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
/// Opens the dialog of filter `plugin`, or of its command `command`, with `m` the manifest it runs
|
||||
/// with, headed `heading`: the rows at their defaults, with a live Preview for a light filter.
|
||||
fn open_filter(app: &mut VectorcraftApp, plugin: &Plugin, m: Manifest, command: Option<String>, heading: String) -> Result<(), String> {
|
||||
/// with, headed `heading`: the rows at `values`, with a live Preview for a light filter.
|
||||
fn open_filter(
|
||||
app: &mut VectorcraftApp,
|
||||
plugin: &Plugin,
|
||||
m: Manifest,
|
||||
command: Option<String>,
|
||||
heading: String,
|
||||
values: Map<String, Value>,
|
||||
) -> Result<(), String> {
|
||||
let own = plugin.manifest();
|
||||
let light = own.budget == Budget::Light;
|
||||
let look = Look { title: heading.clone(), description: own.description.clone(), previews: light, note: !light, ok: None };
|
||||
let instance = command.as_deref().unwrap_or(plugin.id()).to_string();
|
||||
let (id, params) = open_hosted(app, KIND, &instance, m, look)?;
|
||||
let (id, params) = open_hosted(app, KIND, &instance, m, values, look)?;
|
||||
let kind = Opened::Filter {
|
||||
plugin: plugin.id().to_string(),
|
||||
command,
|
||||
@@ -409,7 +604,9 @@ pub fn open_command(app: &mut VectorcraftApp, id: &str) -> bool {
|
||||
if c.params.is_empty() || app.session.active().is_none() {
|
||||
return false;
|
||||
}
|
||||
if let Err(e) = open_filter(app, &plugin, plugin.manifest().for_command(&c), Some(id.to_string()), c.shown_label().to_string()) {
|
||||
let m = plugin.manifest().for_command(&c);
|
||||
let defaults = m.defaults();
|
||||
if let Err(e) = open_filter(app, &plugin, m, Some(id.to_string()), c.shown_label().to_string(), defaults) {
|
||||
app.status(e);
|
||||
}
|
||||
true
|
||||
@@ -457,7 +654,7 @@ pub fn open_export(app: &mut VectorcraftApp, plugin: &Plugin, export: Value) {
|
||||
note: false,
|
||||
ok: Some(tl!("Export")),
|
||||
};
|
||||
match open_hosted(app, EXPORT_KIND, plugin.id(), m.clone(), look) {
|
||||
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 } }),
|
||||
Err(e) => app.status(e),
|
||||
}
|
||||
@@ -475,7 +672,7 @@ pub fn open_import(app: &mut VectorcraftApp, plugin: &Plugin, open: Value) {
|
||||
note: false,
|
||||
ok: Some(tl!("Open")),
|
||||
};
|
||||
match open_hosted(app, IMPORT_KIND, plugin.id(), m.clone(), look) {
|
||||
match open_hosted(app, IMPORT_KIND, plugin.id(), m.clone(), m.defaults(), look) {
|
||||
Ok((id, params)) => app.plugin_dialog = Some(PluginDialog { id, kind: Opened::Import { plugin: plugin.id().to_string(), open, params } }),
|
||||
Err(e) => app.status(e),
|
||||
}
|
||||
@@ -511,7 +708,15 @@ fn confirm_filter(app: &mut VectorcraftApp, kind: Opened, values: Map<String, Va
|
||||
preview.cancel(app);
|
||||
run(app, &found, p)
|
||||
}
|
||||
_ => preview.commit(app, &cmd, p),
|
||||
_ => {
|
||||
let r = preview.commit(app, &cmd, p.clone());
|
||||
// A kept preview commits without running `plugin.run` again ([`after_command`]); for
|
||||
// the filter's own run, `p` is `plugin.run`'s `{id, params}`.
|
||||
if r.is_ok() && own.is_none() {
|
||||
record(app, &p);
|
||||
}
|
||||
r
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -163,6 +163,8 @@ pub struct VectorcraftApp {
|
||||
pub last_effect: Option<(String, serde_json::Value)>,
|
||||
/// Last successful destructive Pathfinder operation, repeated on the current selection.
|
||||
pub last_pathfinder: Option<String>,
|
||||
/// The parameters each object filter plug-in last ran with (Object › Plug-ins › Apply Last Filter).
|
||||
pub last_filters: dialogs::plugin::LastFilters,
|
||||
/// The Character panel field that takes the keyboard focus when the panel next draws it
|
||||
/// ([`panels::character::highlight`]).
|
||||
pub(crate) character_focus: Option<panels::character::Field>,
|
||||
|
||||
@@ -99,6 +99,12 @@ fn command_label(app: &VectorcraftApp, id: &str, label: &str) -> Option<String>
|
||||
Some((e, _)) => format!("Last Effect: {}", vectorcraft_effects::effect_info(e).map(|i| i.label).unwrap_or(e.as_str())),
|
||||
None => label.into(),
|
||||
},
|
||||
// Apply Last Filter and Last Filter… are named after the filter that ran last.
|
||||
crate::dialogs::plugin::APPLY_LAST | crate::dialogs::plugin::LAST => match crate::dialogs::plugin::last_filter_name(app) {
|
||||
Some(name) if id == crate::dialogs::plugin::APPLY_LAST => format!("Apply {name}"),
|
||||
Some(name) => format!("{name}…"),
|
||||
None => label.into(),
|
||||
},
|
||||
// Edit Contents reads Edit Envelope while an envelope's contents are being edited.
|
||||
"object.envelope.editContents" => {
|
||||
let editing = app.session.active().is_some_and(|st| {
|
||||
@@ -195,6 +201,12 @@ pub fn display_label(app: &VectorcraftApp, id: &str, label: &str) -> String {
|
||||
}
|
||||
None => crate::i18n::tr_id(lang, id, label).to_string(),
|
||||
},
|
||||
// The filter's name is the plug-in's own text, never translated.
|
||||
crate::dialogs::plugin::APPLY_LAST | crate::dialogs::plugin::LAST => match crate::dialogs::plugin::last_filter_name(app) {
|
||||
Some(name) if id == crate::dialogs::plugin::APPLY_LAST => fmt(tl!("Apply {filter}"), &[("filter", &name)]),
|
||||
Some(name) => format!("{name}…"),
|
||||
None => crate::i18n::tr_id(lang, id, label).to_string(),
|
||||
},
|
||||
_ => {
|
||||
let shown = dynamic_label(app, id, label);
|
||||
// PLAN: is_builtin needs the UI state, which shows_a_name has no access to; a custom
|
||||
|
||||
@@ -75,7 +75,7 @@ pub(crate) fn translate_label(english: &'static str) -> String {
|
||||
}
|
||||
|
||||
/// What the menus' rows depend on, cheaply (no allocation): commands run, the active document,
|
||||
/// its revision (edits, selection and view changes), history and clipboard, the language,
|
||||
/// its revision (edits, selection and view changes), history and clipboard, the filter plug-in that ran last, the language,
|
||||
/// shortcuts, plug-ins, the Scripts folder's listing, an open file dialog and the fonts (Type › Font lists the loaded fonts
|
||||
/// until the installed ones are scanned, then all of them, #5483). `input` forces a change on frames with a click or
|
||||
/// a key press, which covers what the hash doesn't list (panels shown, a tool picked). While an
|
||||
@@ -96,6 +96,8 @@ pub(crate) fn state_key(app: &VectorcraftApp, input: Option<u64>) -> u64 {
|
||||
(d.uid, revision, d.history.undo.len(), d.history.redo.len(), d.transparency_grid, d.print_tiling).hash(&mut h);
|
||||
}
|
||||
(app.system_paste, app.last_effect.is_some(), app.ui.recent_files.len(), app.ui.recent_files.first(), app.recent_fonts().len()).hash(&mut h);
|
||||
// Apply Last Filter and Last Filter… are named after the filter that ran last.
|
||||
app.last_filters.last_id().hash(&mut h);
|
||||
app.file_dialog_open().hash(&mut h);
|
||||
(crate::i18n::current().code(), plugin_revision(), crate::scripts_menu::revision(), crate::dialogs::proof_setup::saved_revision()).hash(&mut h);
|
||||
hash_listed_names(&app.ui, &mut h);
|
||||
|
||||
@@ -1212,6 +1212,8 @@ fn run_ui_part13(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option<Result
|
||||
}
|
||||
"ui.printPresetDialog" => crate::dialogs::print::open_preset(app, p),
|
||||
"plugin.dialog" => crate::dialogs::plugin::open(app, p),
|
||||
crate::dialogs::plugin::APPLY_LAST => crate::dialogs::plugin::apply_last(app),
|
||||
crate::dialogs::plugin::LAST => crate::dialogs::plugin::open_last(app),
|
||||
"ui.installPlugin" => io::install_plugin(app, p),
|
||||
"plugin.cancelRuns" => Ok(crate::plugin_runs::cancel_all(app)),
|
||||
"plugin.hideRuns" => Ok(crate::plugin_runs::hide_all(app)),
|
||||
|
||||
@@ -385,6 +385,10 @@ fn ui_enabled(app: &VectorcraftApp, id: &str) -> bool {
|
||||
"view.guides" | "view.grid" => !app.ui.view.trim_view,
|
||||
"view.refreshMonitorProfile" => app.session.prefs.monitor_profile_display && app.display_profile.has_reader(),
|
||||
"plugin.dialog" => app.session.active().is_some(),
|
||||
// A filter may take no objects and add new ones, so no selection is needed.
|
||||
crate::dialogs::plugin::APPLY_LAST | crate::dialogs::plugin::LAST => {
|
||||
app.session.active().is_some() && crate::dialogs::plugin::last_filter(app).is_some()
|
||||
}
|
||||
"ui.perspectiveGridDialog" | "ui.savePerspectivePreset" => app.session.active().is_some(),
|
||||
id if id.starts_with(crate::dialogs::perspective_presets::SLOT) => {
|
||||
app.session.active().is_some() && crate::dialogs::perspective_presets::slot_preset(app, id).is_some()
|
||||
|
||||
@@ -435,7 +435,10 @@ fn built_in_tree(english_names: bool, listed: &Listed) -> Vec<(&'static str, Vec
|
||||
],
|
||||
),
|
||||
Sep,
|
||||
sub("Plug-ins", [crate::addons::plugin_menu(), crate::dialogs::plugin::object_menu()].concat()),
|
||||
sub(
|
||||
"Plug-ins",
|
||||
[crate::dialogs::plugin::last_filter_menu(), crate::addons::plugin_menu(), crate::dialogs::plugin::object_menu()].concat(),
|
||||
),
|
||||
],
|
||||
),
|
||||
(
|
||||
|
||||
@@ -1987,7 +1987,19 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"plugin.dialog",
|
||||
"Plug-in…",
|
||||
"",
|
||||
"{id: an object filter plug-in (plugin.list), params?: {…}} Object › Plug-ins: run the filter at once when it takes no parameters or params are given (plugin.run), else open its dialog (dialog `plugin`: a field per parameter, `__plugin` the id, live preview; OK runs plugin.run as one undo step). A heavy filter, and a filter that lists `progress` and no commandAllowlist, run in the background when they run at once or OK runs them without a kept preview → {background: true, status} (see plugin.cancelRuns)",
|
||||
"{id: an object filter plug-in (plugin.list), params?: {…}} Object › Plug-ins: run the filter at once when it takes no parameters or params are given (plugin.run), else open its dialog (dialog `plugin`: a field per parameter at the value the filter last ran with in this session, else its default, `__plugin` the id, live preview; OK runs plugin.run as one undo step). A heavy filter, and a filter that lists `progress` and no commandAllowlist, run in the background when they run at once or OK runs them without a kept preview → {background: true, status} (see plugin.cancelRuns)",
|
||||
),
|
||||
(
|
||||
"plugin.applyLastFilter",
|
||||
"Apply Last Filter",
|
||||
"",
|
||||
"{} Object › Plug-ins › Apply <filter name>: run the object filter that last ran in this session (a plugin.run that succeeded: its menu item, its dialog's OK, a request, a command.batch step or an action.batch step; not a plug-in's own commands) again on the selection with the same parameters, without its dialog, as one undo step; it runs in the background where its menu item would → plugin.run's result | {background: true, status}. An error with no document open, when no filter ran yet, when that filter was removed or installed again (the same module too) since, or when its manifest no longer takes those parameters as they are",
|
||||
),
|
||||
(
|
||||
"plugin.lastFilter",
|
||||
"Last Filter…",
|
||||
"",
|
||||
"{} Object › Plug-ins › <filter name>…: open the dialog of the object filter that last ran in this session (see plugin.applyLastFilter) with the parameters it ran with (dialog `plugin`); OK runs it as one undo step and Cancel changes nothing. A filter without parameters runs at once. The same errors as plugin.applyLastFilter",
|
||||
),
|
||||
(
|
||||
"ui.installPlugin",
|
||||
|
||||
@@ -14,6 +14,7 @@ mod fonts;
|
||||
mod guide_escape;
|
||||
mod home;
|
||||
pub(super) mod labels;
|
||||
mod last_filter;
|
||||
mod lifecycle;
|
||||
mod nativemenu;
|
||||
mod nativeoptions;
|
||||
|
||||
@@ -0,0 +1,405 @@
|
||||
//! Object › Plug-ins › Apply Last Filter and Last Filter…: the object filter that last ran in the
|
||||
//! session runs again with the same parameters, or opens its dialog with them, and each filter's
|
||||
//! dialog opens with the parameters it last ran with. Both items are off before a filter ran and
|
||||
//! once that filter was removed or installed again.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_engine::Session;
|
||||
use vectorcraft_plugins::wat as tpl;
|
||||
|
||||
use super::plugins::{Window, at, window};
|
||||
use crate::dialogs::plugin::{APPLY_LAST, LAST, NO_LAST_FILTER};
|
||||
use crate::{VectorcraftApp, dialogs, menus};
|
||||
|
||||
/// The whole-number parameter `n` of [`filter`]: 0 to 10, 3 by default.
|
||||
const N: &str = r#"{"n":{"type":"int","min":0,"max":10,"default":3}}"#;
|
||||
|
||||
/// An app with a new 200 × 200 pt document.
|
||||
fn app() -> VectorcraftApp {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.allow_plugin_grants_from_requests();
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
/// A filter `id` named `name` with `params` that adds a 30 pt wide triangle when it is given `n` 7,
|
||||
/// and a 50 pt square otherwise.
|
||||
fn filter(id: &str, name: &str, params: &str) -> Vec<u8> {
|
||||
let manifest = format!(r#"{{"id":"{id}","name":"{name}","version":"1","kind":"filter","params":{params}}}"#);
|
||||
let triangle = r#"{"objects":[{"path":{"subpaths":[{"closed":true,"anchors":[{"p":[0,0]},{"p":[30,0]},{"p":[15,20]}]}]}}]}"#;
|
||||
let square = r#"{"objects":[{"path":{"subpaths":[{"closed":true,"anchors":[{"p":[0,0]},{"p":[50,0]},{"p":[50,50]},{"p":[0,50]}]}]}}]}"#;
|
||||
wat::parse_str(tpl::when_given_else(&manifest, tpl::Given::Params, r#""n":7"#, triangle, square)).unwrap()
|
||||
}
|
||||
|
||||
/// Installs `wasm`, in place of a plug-in with its id if there is one.
|
||||
fn install(app: &mut VectorcraftApp, wasm: &[u8]) {
|
||||
app.run("plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(wasm), "replace": true})).unwrap();
|
||||
}
|
||||
|
||||
/// The width of what a filter run returned as `ids`, in points.
|
||||
fn width(app: &VectorcraftApp, ran: &Value) -> f64 {
|
||||
let ids: Vec<vectorcraft_doc::NodeId> = ran["ids"].as_array().unwrap().iter().map(|i| vectorcraft_doc::NodeId(i.as_u64().unwrap())).collect();
|
||||
let r = app.session.doc().unwrap().doc.bounds_of(&ids, false).unwrap();
|
||||
r.x1 - r.x0
|
||||
}
|
||||
|
||||
fn undo_len(app: &VectorcraftApp) -> usize {
|
||||
app.session.doc().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
/// The id of the dialog on top of the host.
|
||||
fn top(app: &VectorcraftApp) -> Option<egui::Id> {
|
||||
app.dialogs()?.with(|host| host.open_dialogs().last().map(|d| d.id))
|
||||
}
|
||||
|
||||
/// The values of the dialog on top of the host, as automation lists them.
|
||||
fn fields(app: &VectorcraftApp) -> Value {
|
||||
app.dialogs().and_then(|d| d.with(|host| host.open_dialogs().last().and_then(|d| d.fields.clone()))).unwrap_or(Value::Null)
|
||||
}
|
||||
|
||||
/// Draws a frame a tenth of a second after the one before.
|
||||
fn step(h: &mut Window) {
|
||||
let time = h.input_mut().time.unwrap_or(0.0) + 0.1;
|
||||
at(h, time);
|
||||
}
|
||||
|
||||
/// Sets `field` of the top dialog as `ui.dialog.set` does, and draws the frame that previews it.
|
||||
fn set(h: &mut Window, field: &str, value: Value) {
|
||||
let (ctx, id) = (h.ctx.clone(), top(h.state()).expect("a dialog is open"));
|
||||
h.state().dialogs().expect("the dialog host").with(|host| host.set_json_field(&ctx, id, field, value)).unwrap();
|
||||
step(h);
|
||||
}
|
||||
|
||||
/// Answers the top dialog with OK (`ok`) or Cancel and draws the frames that take the answer.
|
||||
fn answer(h: &mut Window, ok: bool) {
|
||||
let (ctx, id) = (h.ctx.clone(), top(h.state()).expect("a dialog is open"));
|
||||
let done = h.state().dialogs().expect("the dialog host").with(|host| if ok { host.confirm_by_id(&ctx, id) } else { host.cancel_by_id(&ctx, id) });
|
||||
assert!(done, "the dialog took the answer");
|
||||
for _ in 0..3 {
|
||||
step(h);
|
||||
if top(h.state()) != Some(id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
panic!("the answered dialog stayed open");
|
||||
}
|
||||
|
||||
/// Runs `cmd` with `p`, then draws the frame that shows the dialog it opened.
|
||||
fn open(h: &mut Window, cmd: &str, p: Value) {
|
||||
h.state_mut().run(cmd, p).unwrap();
|
||||
step(h);
|
||||
assert!(top(h.state()).is_some(), "{cmd} opened a dialog");
|
||||
}
|
||||
|
||||
/// The value of `n` filter `id`'s own dialog opens with, after which the dialog is canceled.
|
||||
fn opens_with(h: &mut Window, id: &str) -> Value {
|
||||
open(h, "plugin.dialog", json!({"id": id}));
|
||||
let n = fields(h.state())["n"].clone();
|
||||
answer(h, false);
|
||||
n
|
||||
}
|
||||
|
||||
/// Both items are off, and say so when run, until a filter ran; then they are named after it, and
|
||||
/// Apply runs it again with its parameters on what is selected now (nothing, here), without a
|
||||
/// dialog, as one undo step.
|
||||
#[test]
|
||||
fn apply_last_filter_runs_the_last_filter_again_with_its_parameters() {
|
||||
let mut h = window(app());
|
||||
let app = h.state_mut();
|
||||
let id = "test.ui.last.apply";
|
||||
install(app, &filter(id, "Grow Shape", N));
|
||||
let entries = menus::menu_entries(app);
|
||||
let under: Vec<&str> = entries.iter().filter(|e| e.path == ["Object", "Plug-ins"]).map(|e| e.label.as_str()).take(2).collect();
|
||||
assert_eq!(under, ["Apply Last Filter", "Last Filter…"], "the first items of Object › Plug-ins");
|
||||
for cmd in [APPLY_LAST, LAST] {
|
||||
assert!(!menus::enabled(app, cmd), "{cmd} before any filter ran");
|
||||
assert_eq!(app.run(cmd, json!({})), Err(NO_LAST_FILTER.to_string()));
|
||||
}
|
||||
assert_eq!(menus::dynamic_label(app, APPLY_LAST, "Apply Last Filter"), "Apply Last Filter");
|
||||
|
||||
let first = app.run("plugin.dialog", json!({"id": id, "params": {"n": 7}})).unwrap();
|
||||
assert_eq!(width(app, &first), 30.0);
|
||||
assert_eq!(app.last_filters.last_id(), Some(id));
|
||||
assert!(menus::enabled(app, APPLY_LAST) && menus::enabled(app, LAST));
|
||||
assert_eq!(menus::dynamic_label(app, APPLY_LAST, "Apply Last Filter"), "Apply Grow Shape");
|
||||
assert_eq!(menus::dynamic_label(app, LAST, "Last Filter…"), "Grow Shape…");
|
||||
assert_eq!(menus::display_label(app, APPLY_LAST, "Apply Last Filter"), "Apply Grow Shape");
|
||||
|
||||
app.run("select.none", json!({})).unwrap();
|
||||
let undo = undo_len(app);
|
||||
let again = app.run(APPLY_LAST, json!({})).unwrap();
|
||||
step(&mut h);
|
||||
assert!(top(h.state()).is_none(), "no dialog");
|
||||
let app = h.state();
|
||||
assert_eq!(width(app, &again), 30.0, "the same parameters: n 7");
|
||||
assert_ne!(again["ids"], first["ids"], "a new run");
|
||||
let st = app.session.doc().unwrap();
|
||||
assert_eq!((st.history.undo.len(), st.history.undo.last().unwrap().label.as_str()), (undo + 1, "Grow Shape"));
|
||||
}
|
||||
|
||||
/// Last Filter… opens the dialog with the parameters the filter last ran with; Cancel changes
|
||||
/// nothing, and OK runs it, with a kept preview or without one, the values it ran with becoming
|
||||
/// the last ones.
|
||||
#[test]
|
||||
fn last_filter_opens_its_dialog_with_the_last_parameters() {
|
||||
let mut h = window(app());
|
||||
let id = "test.ui.last.dialog";
|
||||
install(h.state_mut(), &filter(id, "Grow Dialog…", N));
|
||||
h.state_mut().run("plugin.dialog", json!({"id": id, "params": {"n": 7}})).unwrap();
|
||||
assert_eq!(menus::dynamic_label(h.state(), LAST, "Last Filter…"), "Grow Dialog…", "one ellipsis");
|
||||
h.state_mut().run("select.none", json!({})).unwrap();
|
||||
let undo = undo_len(h.state());
|
||||
|
||||
open(&mut h, LAST, json!({}));
|
||||
assert_eq!(fields(h.state())["n"], json!(7), "the last value");
|
||||
answer(&mut h, false);
|
||||
assert_eq!(undo_len(h.state()), undo, "Cancel changes nothing");
|
||||
|
||||
// OK with the preview kept.
|
||||
open(&mut h, LAST, json!({}));
|
||||
set(&mut h, "n", json!(5));
|
||||
assert!(h.state().session.in_interaction(), "previewing");
|
||||
answer(&mut h, true);
|
||||
assert_eq!(undo_len(h.state()), undo + 1);
|
||||
open(&mut h, LAST, json!({}));
|
||||
assert_eq!(fields(h.state())["n"], json!(5), "OK made 5 the last value");
|
||||
|
||||
// OK without a preview.
|
||||
set(&mut h, "n", json!(7));
|
||||
set(&mut h, "preview", json!(false));
|
||||
assert!(!h.state().session.in_interaction(), "the preview is off");
|
||||
answer(&mut h, true);
|
||||
assert_eq!(undo_len(h.state()), undo + 2);
|
||||
h.state_mut().run("select.none", json!({})).unwrap();
|
||||
let again = h.state_mut().run(APPLY_LAST, json!({})).unwrap();
|
||||
assert_eq!(width(h.state(), &again), 30.0);
|
||||
}
|
||||
|
||||
/// Each filter's own dialog opens with the parameters that filter last ran with, never another
|
||||
/// filter's; one that hasn't run opens with its defaults.
|
||||
#[test]
|
||||
fn each_filter_dialog_opens_with_its_own_last_parameters() {
|
||||
let mut h = window(app());
|
||||
let (a, b, c) = ("test.ui.last.own.a", "test.ui.last.own.b", "test.ui.last.own.c");
|
||||
for (id, name) in [(a, "Own A"), (b, "Own B"), (c, "Own C")] {
|
||||
install(h.state_mut(), &filter(id, name, N));
|
||||
}
|
||||
for (id, n) in [(a, 7), (b, 2), (a, 8), (b, 1)] {
|
||||
h.state_mut().run("plugin.dialog", json!({"id": id, "params": {"n": n}})).unwrap();
|
||||
}
|
||||
assert_eq!(h.state().last_filters.last_id(), Some(b));
|
||||
for (id, n) in [(a, 8), (b, 1), (c, 3)] {
|
||||
assert_eq!(opens_with(&mut h, id), json!(n), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A batch step, a direct `plugin.run` and an Action Batch step count; a failed run, a plug-in's own
|
||||
/// command and a live effect don't.
|
||||
#[test]
|
||||
fn only_successful_filter_runs_become_the_last_filter() {
|
||||
let mut h = window(app());
|
||||
let id = "test.ui.last.kinds";
|
||||
install(h.state_mut(), &filter(id, "Kinds", N));
|
||||
assert!(h.state_mut().run("plugin.run", json!({"id": id, "params": {"n": "seven"}})).is_err());
|
||||
assert_eq!(h.state().last_filters.last_id(), None, "a run that failed");
|
||||
|
||||
let steps = json!({"commands": [{"command": "plugin.run", "params": {"id": id, "params": {"n": 7}}}]});
|
||||
h.state_mut().run("command.batch", steps).unwrap();
|
||||
assert_eq!(h.state().last_filters.last_id(), Some(id), "a batch step");
|
||||
assert_eq!(opens_with(&mut h, id), json!(7));
|
||||
|
||||
let other = "test.ui.last.kinds.other";
|
||||
install(h.state_mut(), &filter(other, "Other", N));
|
||||
h.state_mut().run("plugin.run", json!({"id": other, "n": 2})).unwrap();
|
||||
assert_eq!(h.state().last_filters.last_id(), Some(other), "a direct run, its parameters as top-level keys");
|
||||
assert_eq!(opens_with(&mut h, other), json!(2));
|
||||
|
||||
let app = h.state_mut();
|
||||
let cmds = "test.ui.last.cmds";
|
||||
let manifest = format!(r#"{{"id":"{cmds}","name":"Cmds","version":"1","kind":"filter","commands":[{{"id":"grow","label":"Grow"}}]}}"#);
|
||||
let triangle = r#"{"objects":[{"path":{"subpaths":[{"closed":true,"anchors":[{"p":[0,0]},{"p":[5,0]},{"p":[5,5]}]}]}}]}"#;
|
||||
install(app, &wat::parse_str(tpl::returning(&manifest, triangle)).unwrap());
|
||||
app.run("plugin.test.ui.last.cmds.grow", json!({})).unwrap();
|
||||
assert_eq!(app.last_filters.last_id(), Some(other), "a plug-in's own command");
|
||||
|
||||
let fx = tpl::module(&tpl::manifest("test.ui.last.fx", "effect", "{}"), tpl::ECHO, "", 1);
|
||||
install(app, &wat::parse_str(fx).unwrap());
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 40, "height": 40})).unwrap();
|
||||
app.run("effect.apply", json!({"effect": "plugin.test.ui.last.fx"})).unwrap();
|
||||
assert_eq!(app.last_filters.last_id(), Some(other), "a live effect");
|
||||
|
||||
// Action Batch over the document's data sets counts once a data set ran the step.
|
||||
app.run("dataset.new", json!({"name": "a"})).unwrap();
|
||||
let steps = json!([{"command": "plugin.run", "params": {"id": id, "params": {"n": 5}}}]);
|
||||
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(id), "an Action Batch step");
|
||||
assert_eq!(opens_with(&mut h, id), json!(5));
|
||||
}
|
||||
|
||||
/// 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 h = window(app());
|
||||
let (a, b) = ("test.ui.last.batch.a", "test.ui.last.batch.b");
|
||||
install(h.state_mut(), &filter(a, "Batch A", N));
|
||||
install(h.state_mut(), &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}}},
|
||||
]);
|
||||
h.state_mut().run("command.batch", json!({"commands": steps})).unwrap();
|
||||
assert_eq!(h.state().last_filters.last_id(), Some(b), "the last filter step");
|
||||
for (id, n) in [(a, 7), (b, 6)] {
|
||||
assert_eq!(opens_with(&mut h, id), json!(n), "{id}");
|
||||
}
|
||||
|
||||
// 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}}},
|
||||
]);
|
||||
h.state_mut().run("command.batch", json!({"commands": steps})).unwrap();
|
||||
assert_eq!(h.state().last_filters.last_id(), Some(d), "the last filter step");
|
||||
for (id, n) in [(c, 7), (d, 2)] {
|
||||
assert_eq!(opens_with(&mut h, id), json!(n), "{id}");
|
||||
}
|
||||
|
||||
// The same over an Action Batch, with the filters in the other order.
|
||||
h.state_mut().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 = h.state_mut().run("action.batch", json!({"source": "datasets", "commands": steps})).unwrap();
|
||||
assert_eq!((r["count"].clone(), r["failed"].clone()), (json!(1), json!(0)), "{r}");
|
||||
assert_eq!(h.state().last_filters.last_id(), Some(a), "the last Action Batch filter step");
|
||||
for (id, n) in [(a, 9), (b, 1)] {
|
||||
assert_eq!(opens_with(&mut h, id), json!(n), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
#[test]
|
||||
fn the_last_filter_is_off_once_its_plugin_is_removed_or_replaced() {
|
||||
let mut h = window(app());
|
||||
let app = h.state_mut();
|
||||
let id = "test.ui.last.stale";
|
||||
let wasm = filter(id, "Stale", N);
|
||||
install(app, &wasm);
|
||||
app.run("plugin.dialog", json!({"id": id, "params": {"n": 7}})).unwrap();
|
||||
install(app, &filter("test.ui.last.unrelated", "Unrelated", N));
|
||||
assert!(menus::enabled(app, APPLY_LAST), "another plug-in installed");
|
||||
install(app, &wasm);
|
||||
assert!(!menus::enabled(app, APPLY_LAST), "the same module again");
|
||||
app.run("plugin.dialog", json!({"id": id, "params": {"n": 7}})).unwrap();
|
||||
|
||||
// Another module and then the first one again: the run with the first one doesn't come back.
|
||||
install(app, &filter(id, "Stale", r#"{"n":{"type":"int","min":0,"max":20,"default":3}}"#));
|
||||
install(app, &wasm);
|
||||
assert!(!menus::enabled(app, APPLY_LAST), "replaced, then the module that ran installed again");
|
||||
|
||||
install(app, &filter(id, "Stale", r#"{"n":{"type":"choice","options":["a","b"],"default":"b"}}"#));
|
||||
for cmd in [APPLY_LAST, LAST] {
|
||||
assert!(!menus::enabled(app, cmd), "{cmd} once the parameters no longer fit");
|
||||
assert_eq!(app.run(cmd, json!({})), Err(NO_LAST_FILTER.to_string()));
|
||||
}
|
||||
assert_eq!(menus::dynamic_label(app, APPLY_LAST, "Apply Last Filter"), "Apply Last Filter");
|
||||
assert_eq!(opens_with(&mut h, id), json!("b"), "the new module's default");
|
||||
|
||||
let app = h.state_mut();
|
||||
install(app, &filter(id, "Stale", r#"{"n":{"type":"int","min":0,"max":20,"default":3}}"#));
|
||||
assert!(!menus::enabled(app, APPLY_LAST), "another module, though it takes n 7");
|
||||
app.run("plugin.dialog", json!({"id": id, "params": {"n": 7}})).unwrap();
|
||||
assert!(menus::enabled(app, APPLY_LAST));
|
||||
app.run("plugin.remove", json!({"id": id})).unwrap();
|
||||
assert!(!menus::enabled(app, APPLY_LAST) && !menus::enabled(app, LAST), "removed");
|
||||
assert_eq!(app.run(APPLY_LAST, json!({})), Err(NO_LAST_FILTER.to_string()));
|
||||
|
||||
// A batch that runs the filter and then replaces its module.
|
||||
install(app, &wasm);
|
||||
let other = filter(id, "Stale", r#"{"n":{"type":"int","min":0,"max":20,"default":3}}"#);
|
||||
let steps = json!([
|
||||
{"command": "plugin.run", "params": {"id": id, "params": {"n": 7}}},
|
||||
{"command": "plugin.install", "params": {"dataBase64": vectorcraft_format::base64_encode(&other), "replace": true}},
|
||||
]);
|
||||
app.run("command.batch", json!({"commands": steps})).unwrap();
|
||||
assert_eq!(app.last_filters.last_id(), Some(id));
|
||||
assert!(!menus::enabled(app, APPLY_LAST) && !menus::enabled(app, LAST), "replaced after it ran in the batch");
|
||||
|
||||
// A batch that runs the filter and then installs another plug-in keeps it.
|
||||
install(app, &wasm);
|
||||
let unrelated = filter("test.ui.last.unrelated", "Unrelated", N);
|
||||
let steps = json!([
|
||||
{"command": "plugin.run", "params": {"id": id, "params": {"n": 7}}},
|
||||
{"command": "plugin.install", "params": {"dataBase64": vectorcraft_format::base64_encode(&unrelated), "replace": true}},
|
||||
]);
|
||||
app.run("command.batch", json!({"commands": steps})).unwrap();
|
||||
assert!(menus::enabled(app, APPLY_LAST), "another plug-in installed after it ran in the batch");
|
||||
|
||||
// A batch that runs the filter, removes it and installs the same module again.
|
||||
let steps = json!([
|
||||
{"command": "plugin.run", "params": {"id": id, "params": {"n": 7}}},
|
||||
{"command": "plugin.remove", "params": {"id": id}},
|
||||
{"command": "plugin.install", "params": {"dataBase64": vectorcraft_format::base64_encode(&wasm), "replace": true}},
|
||||
]);
|
||||
app.run("command.batch", json!({"commands": steps})).unwrap();
|
||||
assert!(!menus::enabled(app, APPLY_LAST), "installed again in the batch after it ran");
|
||||
}
|
||||
|
||||
/// A filter that runs in the background becomes the last filter only when its result is applied:
|
||||
/// starting it or canceling it doesn't. Apply Last Filter starts it in the background as its menu
|
||||
/// item does. With no document open both items are off.
|
||||
#[test]
|
||||
fn a_background_run_becomes_the_last_filter_only_when_applied() {
|
||||
let mut app = app();
|
||||
let id = "test.ui.last.heavy";
|
||||
let manifest = format!(r#"{{"id":"{id}","name":"Heavy Last","version":"1","kind":"filter","budget":"heavy"}}"#);
|
||||
let wasm = wat::parse_str(tpl::spinning(&manifest, 1_000_000_000, r#"{"objects":[]}"#)).unwrap();
|
||||
app.run("plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "allowHeavy": true})).unwrap();
|
||||
assert_eq!(app.run("plugin.dialog", json!({"id": id})).unwrap()["background"], true);
|
||||
assert_eq!(app.last_filters.last_id(), None, "started");
|
||||
crate::plugin_runs::cancel_all(&mut app);
|
||||
crate::plugin_runs::poll(&mut app);
|
||||
assert_eq!(app.last_filters.last_id(), None, "canceled");
|
||||
|
||||
// As its result would be applied: `plugin.run` with its parameters.
|
||||
dialogs::plugin::record(&mut app, &json!({"id": id, "params": {}}));
|
||||
assert_eq!(app.run(APPLY_LAST, json!({})).unwrap()["background"], true, "in the background, as its menu item runs it");
|
||||
crate::plugin_runs::cancel_all(&mut app);
|
||||
|
||||
// A filter with parameters, so Last Filter… would open its dialog.
|
||||
let with_params = "test.ui.last.nodoc";
|
||||
install(&mut app, &filter(with_params, "No Document", N));
|
||||
let mut empty = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default()));
|
||||
dialogs::plugin::record(&mut empty, &json!({"id": with_params, "params": {"n": 7}}));
|
||||
assert_eq!(empty.last_filters.last_id(), Some(with_params));
|
||||
assert!(!menus::enabled(&empty, APPLY_LAST) && !menus::enabled(&empty, LAST), "no document");
|
||||
assert_eq!(dialogs::plugin::apply_last(&mut empty), Err("no document open".into()));
|
||||
assert_eq!(dialogs::plugin::open_last(&mut empty), Err("no document open".into()));
|
||||
assert!(top(&empty).is_none(), "no dialog opens without a document");
|
||||
}
|
||||
|
||||
/// The native menu reads its rows again when the filter that ran last changes, also when nothing
|
||||
/// else changed.
|
||||
#[test]
|
||||
fn the_native_menu_follows_the_last_filter() {
|
||||
let mut app = app();
|
||||
let (a, b) = ("test.ui.last.native.a", "test.ui.last.native.b");
|
||||
install(&mut app, &filter(a, "Native A", N));
|
||||
install(&mut app, &filter(b, "Native B", N));
|
||||
dialogs::plugin::record(&mut app, &json!({"id": a, "params": {}}));
|
||||
let before = crate::menus::model::state_key(&app, None);
|
||||
dialogs::plugin::record(&mut app, &json!({"id": b, "params": {}}));
|
||||
assert_ne!(crate::menus::model::state_key(&app, None), before);
|
||||
}
|
||||
+8
-1
@@ -13759,7 +13759,14 @@ A plug-in of API level 2 (`api: 2` in its entry) that lists `progress` may repor
|
||||
log lines: `notifications/cancelled` makes its `vc_cancelled` return 1, and its log lines go to the server's log
|
||||
under its id. With a desktop app connected, `plugin.dialog` runs such a filter (one without a `commandAllowlist`) in the
|
||||
background when it runs at once or OK runs it with Preview off, and returns `{background: true, status}`;
|
||||
`plugin.cancelRuns` stops it. One that lists `settings` or `cache` keeps settings or a cache of its own in the `Plug-ins` folder
|
||||
`plugin.cancelRuns` stops it. With a desktop app connected, `plugin.applyLastFilter {}` (Object › Plug-ins › Apply
|
||||
Last Filter) runs the object filter whose `plugin.run` last succeeded in the app's session again on the selection, with
|
||||
the same parameters and without a dialog, and returns `plugin.run`'s result, or `{background: true, status}` for a
|
||||
filter that runs in the background; `plugin.lastFilter {}` (Last Filter…) opens its dialog with those parameters
|
||||
instead. Both return an error with no document open, before a filter ran and once that filter was removed, installed
|
||||
again (the same module too) or no longer takes the parameters as they are; `plugin.dialog {id}` opens a filter's dialog with the
|
||||
parameters it last ran with. A plug-in's own commands and live effects don't change what they run. A plug-in that
|
||||
lists `settings` or `cache` keeps settings or a cache of its own in the `Plug-ins` folder
|
||||
(VectorCraft's own caps: 256 settings and 1 MiB, 512 MiB of cache): `plugin.list` and `plugin.info` give the cache's
|
||||
size as `cacheBytes`, `plugin.clearCache {id?}` empties the cache of one plug-in or of all of them and returns
|
||||
`{cleared: [id], bytesFreed}`, replacing a plug-in keeps both, and `plugin.remove` deletes both (also for an id that
|
||||
|
||||
+18
-2
@@ -83,7 +83,22 @@ In the app, installed filters appear under **Object › Plug-ins**, next to **In
|
||||
Plug-ins**; live effects under **Effect › Plug-ins** and in the Appearance panel's effect menu. A plug-in with
|
||||
parameters gets a dialog generated from its schema (numbers, whole numbers, checkboxes, dropdowns, text, lists
|
||||
of numbers and colors, labelled, explained and sorted into sections as its manifest says) with a live preview; without
|
||||
parameters it runs at once. Edit › Keyboard Shortcuts lists each installed filter under Object › Plug-ins, where
|
||||
parameters it runs at once. A filter's dialog opens with the parameters that filter last ran with in the session,
|
||||
else with its defaults. Object › Plug-ins starts with **Apply Last Filter** (`plugin.applyLastFilter`), which runs
|
||||
the filter that last ran again with the same parameters on the selection, without its dialog, as one undo step, and
|
||||
**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 (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 or removes
|
||||
that same plug-in, or reloads plug-ins. Both items are grayed out with no document open, before any filter ran, and once the filter that
|
||||
ran last was removed, installed again (the same module too) or no longer takes those parameters as they are;
|
||||
installing another plug-in keeps them. They don't need a selection: a filter given no objects can still add new ones. The parameters are
|
||||
kept for the session only, for at most 64 filters. Where the two items sit, when they are grayed out and how long the
|
||||
parameters are kept are VectorCraft's own rules. Edit › Keyboard Shortcuts lists each installed filter under Object ›
|
||||
Plug-ins, where
|
||||
it can be given a shortcut (`plugin:<id>`); the shortcut stays saved while the plug-in is removed and applies again
|
||||
when it is reinstalled. Parameters too many for the window's height scroll inside the dialog, which keeps its
|
||||
heading and buttons in view. Exporters are formats of **File › Export › Export As**, where Use Artboards is greyed out
|
||||
@@ -1089,4 +1104,5 @@ per change of their input, parameters or paints (results are cached), not per fr
|
||||
- Live effects that change opacity, blending or effects (at `api` 2 one may [paint](#painted-live-effects) the pieces
|
||||
it returns), and filters that change type, images or symbol instances (a filter at `api` 2 reads them, see
|
||||
[`inputs`](#reading-type-images-and-symbols)).
|
||||
- Apply Last Effect and Last Effect work for plug-in effects; there is no "last filter" for object filters yet.
|
||||
- Scripts can't run object filters (`plugin.*` commands are refused there), so a script doesn't change what Apply
|
||||
Last Filter runs.
|
||||
@@ -2704,6 +2704,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- **Changed plug-in gradients lose spot plates (G17):** a gradient that a filter or importer changes loses each stop's swatch link, tint and process `plates` together, in fills and strokes, and keeps the stop's color, offset, opacity and midpoint; a gradient returned unchanged keeps all three. Regressions: `crates/plugins/src/objects/tests.rs` (`changed_gradient_fill_and_stroke_drop_spot_plates`, `unchanged_gradient_paint_keeps_spot_plates`). Scores and hours unchanged.
|
||||
- **Plug-in data kept with the document (G17):** a filter writes JSON of at most 1 MiB with `documentData` (one undo step with its objects; `null` removes it) and reads it back in `_context.documentData`, which no other filter is given. It is the document's foreign data (`unknown.pluginData`, by plug-in id), as the perspective grid is, so the native file keeps it with no section of its own; an exporter is given the whole document, so it can read every plug-in's. Regressions: `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_export.rs` (the exporter), `crates/doc/src/plugin_data.rs`, `crates/plugins/src/objects/tests.rs`. Scores and hours unchanged.
|
||||
- **Heavy filter plug-ins (G17):** a filter's manifest may ask for `"budget": "heavy"`: 1 G instructions (20 times a light run's) and 300 s a run, for routing, packing or simulation. Install Plug-in… and File › Open ask the user first, `plugin.install` needs `allowHeavy: true` (`plugin.inspect` tells beforehand), the Additional Plug-ins Folder skips such filters, and the plug-in store keeps the answer. Its dialog has no Preview; the app runs it on a thread of its own with a progress window (Cancel, Continue in Background) and applies the result as `plugin.run` with the same parameters, one undo step, unless the document or the plug-in changed meanwhile. Regressions: `crates/plugins/tests/abi.rs`, `crates/plugins/src/manifest.rs`, `crates/engine/src/tests/plugins.rs`, `crates/ui-egui/src/tests/plugin_runs.rs`. Scores and hours unchanged.
|
||||
- **Apply Last Filter and Last Filter… for filter plug-ins (G17):** Object › Plug-ins starts with Apply Last Filter (`plugin.applyLastFilter`), which runs the filter plug-in that last ran in the session again with the same parameters on the selection, without its dialog, as one undo step, and Last Filter… (`plugin.lastFilter`), which opens that filter's dialog with those parameters; once a filter has run, both items are named after it. [The help page](https://help.adobe.com/archive/en_US/illustrator/cs3/illustrator_cs3_help.pdf#page=357) describes the same pair for filters: one item repeats the last filter with its last settings, the other opens its options. Each filter's own dialog opens with the parameters that filter last ran with. A heavy filter repeats in the background as its menu item runs it. Both items are grayed out with no document open, before a filter ran, and once that filter was removed, installed again or no longer takes the parameters as they are; they don't need a selection (VectorCraft's own rules). Regressions: `crates/ui-egui/src/tests/last_filter.rs`. Scores and hours unchanged.
|
||||
- **Plug-in instruction budget preference (G17, VectorCraft's own setting):** Preferences › Plug-ins & Scratch Disks › Plug-in Instruction Budget (`pluginInstructionBudget`) is Normal by default, which keeps the 50 M instructions a light filter run gets, or Large, which gives each object filter run 250 M plus the 4,000 per input byte, with the 60 s time budget, Cancel and the other limits unchanged. A heavy filter keeps its 1 G and 300 s with either choice; live effects, exporters and importers keep their own budgets. A change applies to the next run of an installed filter; `prefs.set` refuses any other value, and a saved file with another value reads as Normal. Regressions: `crates/plugins/tests/abi.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/src/tests/prefs.rs`, `crates/ui-egui/src/prefs_dialog/tests.rs`. Scores and hours unchanged.
|
||||
- **Safe Mode for plug-ins (G17):** `vectorcraft --safe-mode` and `vectorcraft-cli --safe-mode <subcommand>` start without plug-ins. When a launch stopped while a plug-in was loading or drawing its first live effect, the next launch names that plug-in, without saying it caused the stop, and offers Disable and Continue, Start Normally or Start in Safe Mode. Help › Safe Mode lists the disabled plug-ins with Enable on Re-launch, which takes effect at the next launch, and the app bar and the window title show Safe Mode. The help page (https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf, pages 65 to 69) describes the same choice between diagnosing and starting normally, a Safe Mode with the problem item off and the rest of the app working, and turning the item back on at the next launch. In Safe Mode no plug-in loads or runs, and a live-effect plug-in draws its object's geometry unchanged while the object keeps the effect, which the Appearance panel marks "(Safe Mode)" with the plug-in's id in its tooltip and `effect.list` marks `skipped`; Expand Appearance refuses such an object. The web build keeps no plug-ins between page loads and has no marker. Each desktop launch with preferences keeps a marker in the `Launch` folder next to them; `vectorcraft-cli` and a desktop launch without preferences keep none, so a stop there leads to no choice at the next launch. Disabled plug-ins are listed in `Plug-ins/disabled.json`. `plugin.safeMode` reports Safe Mode and the disabled plug-ins, and `plugin.setEnabled` changes the list. Regressions: `crates/plugins/src/safe_mode.rs`, `crates/plugins/tests/safe_mode.rs`, `crates/engine/tests/plugin_safe_mode.rs`, `crates/ui-egui/tests/safe_mode.rs`. Scores and hours unchanged.
|
||||
- **Hang report and Copy Troubleshooting Info:** when the desktop app's interface thread stays in one part of a frame for 3 s (VectorCraft's own value), a watching thread writes a hang report to the log and to `logs/hang-report-<process id>.txt`, and removes the file when the interface answers again. A later launch that finds a report no process holds writes it to the log, keeps it as `logs/hang-report.1.txt` and, unless another start-up message is showing, says so in the status bar. Help › Copy Troubleshooting Info (`help.copyTroubleshootingInfo`) copies the platform facts, the recent commands and frame times, and the reports of this session and the last one. Regressions: `apps/vectorcraft/src/hang_watch/tests.rs`, `crates/ui-egui/src/hang_beacon.rs`, `crates/ui-egui/src/troubleshooting.rs`. Scores and hours unchanged.
|
||||
|
||||
Reference in new issue
Block a user