Plug-ins: an outside command acts on the selection an add-on preview shows

During an add-on dialog's preview, a command from outside the dialog
now uses the selection on show, without the objects the preview made,
instead of the selection from before the preview, so Clear no longer
deletes art that was not shown selected. A failed preview runs again
when an outside command changes only the selection, and the new-art
paints a selection command loads stay with the selection it made.
This commit is contained in:
dotCooCoo committed 2026-12-04 17:52:09 -08:00
1 parent 632598888f
commit dc65d03200
4 files changed
+103 -19

No files matched your search

+16 -9
View File
@@ -344,8 +344,10 @@ impl Session {
/// no drag in progress.
///
/// A dialog's live preview ([`crate::Interaction::dialog`]) is set aside the same way, except
/// for a command that acts on the drawing on show ([`reads_preview`]). The
/// interaction stays as it is, and the command runs inside it, for an interaction a panel or
/// for a command that acts on the drawing on show ([`reads_preview`]). Outside commands use
/// the shown selection with objects absent from the base document removed. A selection of
/// only preview objects becomes empty. The interaction stays as it is, and the command runs
/// inside it, for an interaction a panel or
/// another dialog began (it builds its preview from commands), for typing in progress
/// ([`Session::edit`] keeps it as a step of its own), for the Quick Pen (an edit made outside
/// it ends the path it draws), and for a copy made with `object.transform` during a drag that
@@ -370,7 +372,11 @@ impl Session {
let shown = previewed.then(|| (st.doc.clone(), st.selection.clone()));
if previewed {
st.doc = interaction.doc.clone();
st.selection.clone_from(&interaction.selection);
if interaction.dialog {
st.selection.prune(&st.doc);
} else {
st.selection.clone_from(&interaction.selection);
}
st.revision += 1;
cmd::globaledit::follow_history(st);
}
@@ -387,19 +393,20 @@ impl Session {
/// Put back the interaction [`Session::outside_interaction`] set aside. When the command left
/// the document and the selection as it found them, the preview on show before it is on show
/// again. Otherwise the interaction starts from the document the command left and from its
/// own selection, without the objects the command removed, hid or locked, and the preview that Real-time
/// Drawing showed is applied to it again. The warning or the error of that preview goes to
/// again. Otherwise a tool drag starts from the document the command left and from its own
/// selection, without the objects the command removed, hid or locked. The preview that
/// Real-time Drawing showed is applied to it again. The warning or the error of that preview goes to
/// the command's result ([`Session::note_settled_warning`]); a preview that fails leaves the
/// interaction with nothing to commit. An interaction whose document the command closed ends,
/// and so does one with anchors or segments selected on a path the command changed in more
/// than a move. A document
/// the command left an interaction of its own on keeps that one.
/// than a move. A document the command left an interaction of its own on keeps that one.
/// A dialog adopts the command's selection and paints and reruns its preview on its next pass.
pub(crate) fn put_back_interaction(&mut self) {
let Some(crate::HeldInteraction { uid, interaction: mut it, shown, state, paint }) = self.held_interaction.take() else { return };
// A selection command loaded the paints of the selection it made into the fill and stroke
// proxies. The drag goes on with its own selection, so they show its paints again.
if self.paint != paint
if !it.dialog
&& self.paint != paint
&& let Some(st) = self.active_mut().filter(|d| d.uid == uid && d.interaction.is_none())
{
let own = drag_selection(&it.selection, &st.doc);
@@ -318,6 +318,82 @@ fn opening_a_second_add_on_dialog_closes_the_first_and_rolls_back_its_preview()
assert_eq!(x0(&app, 1), 100.0, "the first preview rolled back");
}
#[test]
fn clearing_a_dialog_preview_uses_only_the_shown_base_objects() {
for include_base_object in [false, true] {
let mut app = two_rects_filled();
let original = app.session.active().unwrap().doc.clone();
let selected = app.session.active().unwrap().selection.objects.clone();
let undo = undo_len(&app);
let mut preview = crate::dialogs::preview::LivePreview::default();
let params = json!({});
assert!(preview.run(&mut app, true, "Flood Fill Artboards", "addon.document.floodFillArtboards", &params).unwrap().is_ok());
preview.keep_outside_edits(&mut app);
assert!(app.session.active().unwrap().selection.objects.iter().all(|id| original.node(*id).is_none()));
if include_base_object {
app.session.active_mut().unwrap().selection.add(selected[0]);
}
let result = app.run("edit.clear", json!({}));
if include_base_object {
result.unwrap();
}
preview.cancel(&mut app);
let state = app.session.active().unwrap();
assert!(state.doc.node(selected[1]).is_some(), "Clear must keep hidden selected artwork");
if include_base_object {
assert!(state.doc.node(selected[0]).is_none(), "Clear deletes a shown object present in the base document");
assert_eq!(undo_len(&app), undo + 1);
app.run("edit.undo", json!({})).unwrap();
assert_eq!(app.session.active().unwrap().doc, original);
} else {
assert_eq!(state.doc, original, "Cancel keeps all artwork when only preview objects were selected");
assert_eq!(undo_len(&app), undo);
}
}
}
#[test]
fn a_failed_dialog_preview_retries_after_an_outside_selection_change() {
let mut app = two_rects();
app.run("select.none", json!({})).unwrap();
let mut preview = crate::dialogs::preview::LivePreview::default();
let params = json!({"offset": 5});
assert!(preview.run(&mut app, true, "Contour", "addon.contour.set", &params).unwrap().is_err());
preview.keep_outside_edits(&mut app);
assert!(preview.run(&mut app, true, "Contour", "addon.contour.set", &params).is_none());
let document = app.session.active().unwrap().doc.clone();
app.run("select.all", json!({})).unwrap();
assert!(std::sync::Arc::ptr_eq(&app.session.active().unwrap().doc, &document));
assert!(preview.run(&mut app, true, "Contour", "addon.contour.set", &params).unwrap().is_ok());
assert!(count(&app) > 2);
preview.cancel(&mut app);
assert_eq!(app.session.active().unwrap().doc, document);
}
#[test]
fn an_outside_dialog_selection_keeps_its_paints_after_cancel() {
let mut app = two_rects();
let ids = app.session.active().unwrap().selection.objects.clone();
app.run("select.set", json!({"ids": [ids[0].0]})).unwrap();
app.run("paint.setFill", json!({"color": "#ff0000"})).unwrap();
app.run("select.set", json!({"ids": [ids[1].0]})).unwrap();
app.run("paint.setFill", json!({"color": "#0000ff"})).unwrap();
let expected = app.session.paint.clone();
app.run("select.set", json!({"ids": [ids[0].0]})).unwrap();
assert_ne!(app.session.paint, expected);
let mut preview = crate::dialogs::preview::LivePreview::default();
assert!(preview.run(&mut app, true, "Move", "object.move", &json!({"dx": 10, "dy": 0})).unwrap().is_ok());
preview.keep_outside_edits(&mut app);
app.run("select.set", json!({"ids": [ids[1].0]})).unwrap();
assert_eq!(app.session.active().unwrap().selection.objects, vec![ids[1]]);
assert_eq!(app.session.paint, expected);
preview.cancel(&mut app);
assert_eq!(app.session.active().unwrap().selection.objects, vec![ids[1]]);
assert_eq!(app.session.paint, expected);
}
#[test]
fn an_add_on_preview_keeps_an_edit_made_outside_the_dialog() {
let mut app = two_rects();
+10 -9
View File
@@ -15,8 +15,8 @@ pub(crate) struct LivePreview {
previewed: Option<(String, Value)>,
/// The last preview ran without an error, so its result is on show.
shown: bool,
/// The document the last preview ran on: a command from outside the dialog that changes it has the preview run
/// again, even one that failed.
/// The document the last preview ran on. A change to it or the owner's selection snapshot
/// reruns the preview, including one that failed.
snapshot: Weak<Document>,
owner: Option<Owner>,
}
@@ -52,8 +52,9 @@ impl LivePreview {
}
/// While `on`, runs `cmd` with `params` on the interaction's snapshot whenever they differ from the last preview
/// or its interaction has ended; `label` names the undo step. Returns the command's answer when this call ran a
/// preview, and `None` when nothing ran: the same values again (a pass reruns nothing), a preview that could not
/// or its interaction has ended or changed its document or selection snapshot; `label` names the undo step.
/// Returns the command's answer when this call ran a preview, and `None` when nothing ran:
/// the same values and snapshots again, a preview that could not
/// begin, another edit in progress, or `on` off, which rolls a shown preview back. Typing in progress is kept as
/// its own step first. A refusal's text is written for the user, without the command's id.
pub(crate) fn run(&mut self, app: &mut VectorcraftApp, on: bool, label: &str, cmd: &str, params: &Value) -> Option<Result<Value, String>> {
@@ -66,11 +67,11 @@ impl LivePreview {
return None;
}
// A command from outside the dialog took a shown preview off its interaction: it runs again.
let taken_off = app
.session
.active()
.and_then(|state| state.interaction.as_ref())
.is_some_and(|it| (self.shown && it.preview.is_none()) || self.snapshot.as_ptr() != Arc::as_ptr(&it.doc));
let taken_off = app.session.active().and_then(|state| state.interaction.as_ref()).is_some_and(|it| {
(self.shown && it.preview.is_none())
|| self.snapshot.as_ptr() != Arc::as_ptr(&it.doc)
|| self.owner.as_ref().is_some_and(|owner| owner.selection.as_ptr() != Arc::as_ptr(&it.selection))
});
let same = self.previewed.as_ref().is_some_and(|(c, p)| c == cmd && p == params);
if same && !taken_off {
return None;
+1 -1
View File
@@ -889,7 +889,7 @@ ends the wait, so the sampler can be shown again. The BSDs and headless sessions
| `transform` | `{ids?, dx?, dy?, rotate?, scale?, scaleX?, scaleY?, reflect?, shear?, origin?, copy?}` | Runs move, rotate, scale, reflect, shear in that order. `rotate` is in degrees counter-clockwise; a positive `shear` moves the top to the right. With `copy`, the first step duplicates. A `scale`, `reflect` or `shear` that its command would refuse returns that error before any step runs; nothing changes and no undo step is recorded. The same holds for a step that is neither a number nor null, a `reflect` that is not a string, a `copy` that is not true or false, and an `origin` that is not two numbers. A null `rotate`, `scale`, `scaleX`, `scaleY`, `shear`, `reflect`, `copy` or `origin` is left out; a null `dx` or `dy` counts as 0, so it still runs the move. |
| `create_graph` | `{type?, x, y, width, height, csv? \| series?, categories?, rows?}` | The nine Illustrator graph types: column, stacked column, bar, stacked bar, line, area, scatter, pie and radar. An empty CSV cell or a `null` in `rows` is a blank value; a number in straight quotes is a label. Edit later with `graph.setData` / `graph.setType` via `run_command`. `graph.column {design, columnType: vertical\|uniform\|repeating\|sliding, each, fractions: chop\|scale}` draws the columns or bars of the series picked with Group Selection (or `seriesIndexes`, or all of them) with a graph design saved by `graph.design`; with no `design` it returns the series' current design and options. Type in the design that holds a `%` followed by two digits shows each column's value, with at least the first digit's places before the decimal point and the second digit's places after it. |
| `text_wrap` | `{ids?, offset?, invert?, release?}` | Area type below the objects (same layer) flows around them. A type object in `ids` with another of them above it in its layer is the text to wrap, not a wrap object, so the objects and the type can be selected together. The reply has a `warning` when no area type lies below any of the wrap objects. `offset` is in points, from -1000 to 1000 (default 6). An offset outside that range or not a number, or an `invert` or `release` that is not true or false, is an error and changes nothing; `object.textWrap.make` and `object.textWrap.options` refuse the same values. |
| `undo` / `redo` | `{}` | Many calls that change nothing record no undo step, for example `object.align` with nothing to move, `object.move` by 0 without `copy`, `path.moveAnchors` by 0, `layer.setProps` with every row already as given and `object.showAll` with nothing hidden. `undo` (`edit.undo`) after such a call undoes the step before it. Settings that change without an undo step stay as they are when `undo` or `redo` brings back a step that did not change them: the perspective grid's view options and active plane, the grid preferences (`gridlineEvery`, `gridSubdivisions`), the export settings (`assets.settings.set`), and the current brush and symbol. A step that changed one of them, such as a perspective grid preset that shows the grid, takes that change back. The point Automatic Plane Positioning holds comes back with the step, and is let go when the active plane kept is another one. `inspect_document` lists the undo steps as `history`. `run_command` with `edit.clearHistory` removes every undo and redo step of the active document and returns `{cleared}`, the number of steps removed; the document stays as it is, and the command records no undo step. `run_command` with `edit.newDocumentFromState` and `{index?, created?}` makes a new document holding the art as it was at that state and makes it the active document, and returns `{index, title}`. `index` is a row of the History panel, with 0 the oldest state kept, and an omitted or null `index` uses the current state. An `index` that names no state fails with `no history state N`, one that is not a whole number with `` `index` must be a whole number ``, and either changes nothing. `created` is File Info's created date in Unix seconds or null, and defaults to now. The new document takes the next `Untitled-N` name, is marked unsaved, has nothing selected and has no steps to undo or redo. It is out of symbol, opacity-mask and pattern editing, and the art drawn in a pattern's tile is stored in the pattern, as `object.pattern.done` stores it. The source document and its steps stay as they are, except that typing or a drag in progress is first kept as a step. When a command keeps a drag whose preview Real-time Drawing off had not applied yet (`edit.undo`, `edit.redo`, `edit.clearHistory`, `edit.newDocumentFromState` and the other commands that end a drag first), and applying that preview gives a warning, the command's result carries it as `previewWarning`, and as `warning` too when the command gives no warning of its own; the app's status bar shows `warning`. Any other command that changes the document during a tool's drag, from `run_command`, a panel or a menu, records its own undo step before the drag's. The command acts on the document without the drag's preview, and the drag then continues from the command's result: with Real-time Drawing on, the drag's preview is applied again at once, and with it off, the release applies it as before. The release records the drag's step after the command's, `undo` takes back the drag first and the command next, and Escape cancels only the drag. The drag acts on the objects it started with, so a selection the command makes (`select.all`, `edit.duplicate`) does not change what the drag moves, and the drag's release and Escape set the selection as they do without the command (with Real-time Drawing on, so does each move of the drag); a command that records no undo step, such as `select.none`, adds no step. When the command deletes or changes the art being dragged, the drag continues with the art the command left, and a drag left with nothing to change records no step. A warning or an error from the drag's preview applied again after the command is in the command's result as `previewWarning`. Typing in progress is kept as a step of its own before the command. Two cases run the command inside the drag, which then keeps the command's edit as part of its own step: a press of the Quick Pen (an edit to its path ends the path), and `object.transform` with `copy` during an Alt-drag, which copies the art as the drag's move left it. A command run while a dialog's or a panel's preview is open runs inside that preview, except during an add-on dialog's preview (Object › Plug-ins, other than Edit Points Only): there a command that changes the document records its own undo step, acts on the document without the preview, and stays after the dialog's Cancel or OK, and the dialog previews again from the command's result and selection. `edit.copy` and the `file.*` commands other than `file.documentColorMode` still run on the document as the preview shows it. While the Type tool edits type, `undo` and `redo` put its caret, with no characters selected, at the end of the text the step changed: after the text it puts back or retypes, or where the text it takes out was. A step that changes no characters, such as a formatting change, leaves the caret and the selection where they are. |
| `undo` / `redo` | `{}` | Many calls that change nothing record no undo step, for example `object.align` with nothing to move, `object.move` by 0 without `copy`, `path.moveAnchors` by 0, `layer.setProps` with every row already as given and `object.showAll` with nothing hidden. `undo` (`edit.undo`) after such a call undoes the step before it. Settings that change without an undo step stay as they are when `undo` or `redo` brings back a step that did not change them: the perspective grid's view options and active plane, the grid preferences (`gridlineEvery`, `gridSubdivisions`), the export settings (`assets.settings.set`), and the current brush and symbol. A step that changed one of them, such as a perspective grid preset that shows the grid, takes that change back. The point Automatic Plane Positioning holds comes back with the step, and is let go when the active plane kept is another one. `inspect_document` lists the undo steps as `history`. `run_command` with `edit.clearHistory` removes every undo and redo step of the active document and returns `{cleared}`, the number of steps removed; the document stays as it is, and the command records no undo step. `run_command` with `edit.newDocumentFromState` and `{index?, created?}` makes a new document holding the art as it was at that state and makes it the active document, and returns `{index, title}`. `index` is a row of the History panel, with 0 the oldest state kept, and an omitted or null `index` uses the current state. An `index` that names no state fails with `no history state N`, one that is not a whole number with `` `index` must be a whole number ``, and either changes nothing. `created` is File Info's created date in Unix seconds or null, and defaults to now. The new document takes the next `Untitled-N` name, is marked unsaved, has nothing selected and has no steps to undo or redo. It is out of symbol, opacity-mask and pattern editing, and the art drawn in a pattern's tile is stored in the pattern, as `object.pattern.done` stores it. The source document and its steps stay as they are, except that typing or a drag in progress is first kept as a step. When a command keeps a drag whose preview Real-time Drawing off had not applied yet (`edit.undo`, `edit.redo`, `edit.clearHistory`, `edit.newDocumentFromState` and the other commands that end a drag first), and applying that preview gives a warning, the command's result carries it as `previewWarning`, and as `warning` too when the command gives no warning of its own; the app's status bar shows `warning`. Any other command that changes the document during a tool's drag, from `run_command`, a panel or a menu, records its own undo step before the drag's. The command acts on the document without the drag's preview, and the drag then continues from the command's result: with Real-time Drawing on, the drag's preview is applied again at once, and with it off, the release applies it as before. The release records the drag's step after the command's, `undo` takes back the drag first and the command next, and Escape cancels only the drag. The drag acts on the objects it started with, so a selection the command makes (`select.all`, `edit.duplicate`) does not change what the drag moves, and the drag's release and Escape set the selection as they do without the command (with Real-time Drawing on, so does each move of the drag); a command that records no undo step, such as `select.none`, adds no step. When the command deletes or changes the art being dragged, the drag continues with the art the command left, and a drag left with nothing to change records no step. A warning or an error from the drag's preview applied again after the command is in the command's result as `previewWarning`. Typing in progress is kept as a step of its own before the command. Two cases run the command inside the drag, which then keeps the command's edit as part of its own step: a press of the Quick Pen (an edit to its path ends the path), and `object.transform` with `copy` during an Alt-drag, which copies the art as the drag's move left it. A command run while a dialog's or a panel's preview is open runs inside that preview, except during an add-on dialog's preview (Object › Plug-ins, other than Edit Points Only): there a command that changes the document records its own undo step, acts on the document without the preview using the shown selection restricted to objects present in that document, and stays after the dialog's Cancel or OK. A shown selection containing only preview objects becomes empty for the command. The dialog previews again from the command's result and selection, including after a failed preview when only the selection changed. An outside selection command's selection and paint defaults stay together after Cancel or OK. `edit.copy` and the `file.*` commands other than `file.documentColorMode` still run on the document as the preview shows it. While the Type tool edits type, `undo` and `redo` put its caret, with no charactLine truncated
| `agent_folders` | `{}` | Where VectorCraft opens and saves files for this session: `{read_folder, write_folder}`, each a path or `null`. In the open window, the agent folders of Preferences › AI Integrations (`Documents/ArtCraft/Agents/VectorCraft` for an empty one); in a headless session started for agents, the folders it was confined to (`read_folders` / `write_folders` list them all when a kind has several); a session started without automation roots answers `unrestricted: true`. An app reached with the deprecated `--connect` answers an error: its own `--automation-*-root` flags, if any, confine it. |
For example, create editable Japanese area type with the engine's vertical writing mode: