From 231d90614b70787a6e700f413e12f4df54cafcf3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 05:32:37 -0800 Subject: [PATCH] Docs: the save-changes question takes choice dontSave, not discard The question that Close, Close All and Quit open for a modified document takes `choice` (`save`, `dontSave` or `cancel`) and, while more documents wait, `applyToAll`. A `discard` field is refused with "unknown field `discard`". docs/control-protocol.md, the answer_dialog row of docs/mcp.md and the control module's doc comment still told clients to set `discard: true`. Each text now gives `{field: "choice", value: "dontSave"}`, lists the values `choice` takes and describes `applyToAll`. The answer_dialog row gives the choice in one call and the confirm in a second one: the app answers a `set` with `action` by returning the confirm's result alone, and answer_dialog then sends a second confirm, which can fail after the document closed or answer the next document's question. A test in unsaved.rs checks the requests on the lines that describe the `{"pending": "saveChanges"}` reply in both docs and sends the same requests through the control handler: the modified documents close and no file is written. --- crates/ui-egui/Cargo.toml | 2 + crates/ui-egui/src/control.rs | 3 +- crates/ui-egui/src/unsaved.rs | 76 +++++++++++++++++++++++++++++++++++ docs/control-protocol.md | 2 +- docs/mcp.md | 2 +- 5 files changed, 82 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 49fad8d56f..1582da283e 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -116,6 +116,8 @@ toml = "1" reads = [ "crates/engine/src/cmd/addons/cutting/banner.rs", "crates/plugins/tests/fixtures/desaturate.wasm", + "docs/control-protocol.md", + "docs/mcp.md", "packaging/macos/nightly-lib.sh", "packaging/macos/nightly_plist.py", ] diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 158cf852f8..ddf6055c2c 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -28,7 +28,8 @@ //! a path, or with `data: true`, as `pngBase64`) //! - `app.open {path}` (any readable format) / `app.save {path?, svg?: {…SVG options}}` / `app.quit` //! (`file.close`, `file.closeAll` and `app.quit` first open a `saveChanges` dialog for each -//! modified document: `ui.dialog.confirm` saves, set `discard: true` then confirm to discard) +//! modified document: `ui.dialog.confirm` saves, set `choice: "dontSave"` then confirm to close +//! without saving) //! - `app.export {path?, format?, …document.export options}`: encoded by the engine, written through //! the host; the document keeps its path. No path → `{dataBase64, format, bytes}` (as headless) //! - `wait: true` on `app.save` and `app.export`: the file is written before the reply, even with diff --git a/crates/ui-egui/src/unsaved.rs b/crates/ui-egui/src/unsaved.rs index 9d5a7230a0..252f3cc8a5 100644 --- a/crates/ui-egui/src/unsaved.rs +++ b/crates/ui-egui/src/unsaved.rs @@ -399,6 +399,82 @@ mod tests { assert_eq!((docs(&app), app.views.len()), (1, 1)); } + const CONTROL_DONT_SAVE: &str = r#"`ui.dialog.set {field: "choice", value: "dontSave"}` then confirm"#; + const CONTROL_APPLY_TO_ALL: &str = r#"`ui.dialog.set {field: "applyToAll", value: true}` before confirm"#; + const MCP_DONT_SAVE: &str = r#"`{set: [{field: "choice", value: "dontSave"}]}` followed by a second call with `{action: "confirm"}`"#; + const MCP_APPLY_TO_ALL: &str = r#"adding `{field: "applyToAll", value: true}` to `set`"#; + + /// Each documented Don't Save sequence closes the requested documents without writing a file, + /// including Apply to All for multiple modified documents. The documented requests are read + /// from the lines describing the `{"pending": "saveChanges"}` reply. + #[test] + fn the_documented_dont_save_answer_closes_without_saving() { + for (name, text, snippets) in [ + ("docs/control-protocol.md", include_str!("../../../docs/control-protocol.md"), [CONTROL_DONT_SAVE, CONTROL_APPLY_TO_ALL]), + ("docs/mcp.md", include_str!("../../../docs/mcp.md"), [MCP_DONT_SAVE, MCP_APPLY_TO_ALL]), + ] { + // Check the requests on every line describing the pending Save Changes reply. + let lines: Vec<_> = text.lines().filter(|line| line.contains(r#"{"pending": "saveChanges"}"#)).collect(); + assert!(!lines.is_empty(), "{name}: missing a line describing the pending Save Changes reply"); + for line in lines { + for snippet in snippets { + assert!(line.contains(snippet), "{name}: missing {snippet}; the test's requests must be updated with the docs"); + } + assert!(!line.contains("\"discard\""), "{name}: saveChanges must use choice instead of discard: {line}"); + } + } + + let control = |app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, params: Value| { + let (req, _) = craft::automation::ControlRequest::new(method, params); + let craft::automation::Reply::Done(r) = crate::control::handle(app, ctx, &req) else { panic!("{method} answers at once") }; + r + }; + for (name, apply_to_all, requests) in [ + ( + "control protocol Don't Save", + false, + vec![("ui.dialog.set", json!({ "field": "choice", "value": "dontSave" })), ("ui.dialog.confirm", json!({}))], + ), + ( + "control protocol applyToAll", + true, + vec![ + ("ui.dialog.set", json!({ "field": "choice", "value": "dontSave" })), + ("ui.dialog.set", json!({ "field": "applyToAll", "value": true })), + ("ui.dialog.confirm", json!({})), + ], + ), + ( + "MCP Don't Save", + false, + vec![("ui.dialog.set", json!({ "fields": [{ "field": "choice", "value": "dontSave" }] })), ("ui.dialog.confirm", json!({}))], + ), + ( + "MCP applyToAll", + true, + vec![ + ("ui.dialog.set", json!({ "fields": [{ "field": "choice", "value": "dontSave" }, { "field": "applyToAll", "value": true }] })), + ("ui.dialog.confirm", json!({})), + ], + ), + ] { + let mut app = app(); + new_doc(&mut app, true); + new_doc(&mut app, apply_to_all); + let (ctx, picker) = ready_with_picks(&mut app); + let (method, params) = if apply_to_all { ("file.closeAll", json!({})) } else { ("file.close", json!({ "index": 0 })) }; + assert_eq!(app.run(method, params).unwrap(), json!({ "pending": KIND }), "{name}: closing asks first"); + for (method, params) in requests { + let r = control(&mut app, &ctx, method, params.clone()); + assert!(r.get("error").is_none_or(Value::is_null), "{name}: {method} {params} is refused: {r}"); + } + pump(&mut app, &ctx); + assert_eq!(docs(&app), if apply_to_all { 0 } else { 1 }, "{name}: the requested documents close"); + assert!(picker.written().is_empty(), "{name}: no file is written"); + assert!(app.closing.is_none() && !question_open(&app), "{name}: no question remains open"); + } + } + #[test] fn closing_a_modified_background_tab_keeps_the_front_document_after_dont_save() { let mut app = app(); diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 71e3d0fae9..783c686f45 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -107,7 +107,7 @@ the two can still win that race. | `ui.render` | `{path?, scale?, artboard?, data?, traceViews?}` | render an artboard headlessly (PNG with the pixels of `app.export {format: "png", background: "white"}` at the same scale; `artboard`: its 0-based index, default the first; `traceViews: true`: Image Trace objects draw in their View, as on the canvas, instead of their tracing result): written to `path`, else (or with `data: true` as well) sent back as `pngBase64` | | `ui.resize` | `{width?, height?}` | request the window's inner size; omitted or null `width` and `height` default to 1440 and 900. Returns `null` | | `ui.focus` | `{}` | request keyboard focus for the window. Returns `null` | -| `app.open` / `app.save` / `app.export` / `app.quit` | `{path, importer?, params?}` / `{path?}` / `{path?, format?, artboard?, range?, scale?, …}` | `app.open` reads every format `document.open` reads (see `document.formats`) and returns its result (`{index, title, format, warnings, …}`; `warnings` say what didn't come in as it was, such as an EPS shown as its preview image and why its PostScript couldn't be read), or null when a dialog asks first or a library loads. The status bar (`ui.inspect` → `ui.status`), after a line about the document's linked files when there is one (Missing linked files below), counts the `warnings` and names the first that says reading the file left something out (the notes that make Save ask before writing over it), else the first; with several, hovering it lists them in that order, as many as fit in the window (at most 30), then how many more. After `app.save`, and `app.export` with a `path`, the status bar counts the reply's `warnings` and names the first, and with several, hovering it lists them in the reply's order (a save's notes start with what the format loses). With `importer` (an importer plug-in's format id, `plugin.`) it runs `file.openWith`: the file opens with that importer, also when a built-in format reads its extension, with the importer's parameters `params` (their defaults when omitted; no dialog asks for them). Without `importer`, `params` go to the importer that opens the file, and a built-in format does not read them. `app.export` encodes through the engine's `document.export` (same options; the document keeps its path) and writes `path` through the host; without `path` it returns `{dataBase64, format, bytes}`, as headless mode does. It refuses a key that no format takes, and an unknown key inside a PDF or EPS option object, before it writes anything (`docs/mcp.md`, Parameter keys). It accepts a key that only another format takes and does not use it: the reply's `warnings` and the status bar name the key ("`quality` was not used: PDF files don't read it"). `app.save` refuses a key that no save format takes, beside the path or in `options`, the same way. `app.quit`, `file.close` and `file.closeAll` first open a `saveChanges` dialog for each modified document (they return `{"pending": "saveChanges"}`): `ui.dialog.confirm` saves, `ui.dialog.set {field: "discard", value: true}` then confirm discards, `ui.dialog.cancel` cancels the whole close or quit | +| `app.open` / `app.save` / `app.export` / `app.quit` | `{path, importer?, params?}` / `{path?}` / `{path?, format?, artboard?, range?, scale?, …}` | `app.open` reads every format `document.open` reads (see `document.formats`) and returns its result (`{index, title, format, warnings, …}`; `warnings` say what didn't come in as it was, such as an EPS shown as its preview image and why its PostScript couldn't be read), or null when a dialog asks first or a library loads. The status bar (`ui.inspect` → `ui.status`), after a line about the document's linked files when there is one (Missing linked files below), counts the `warnings` and names the first that says reading the file left something out (the notes that make Save ask before writing over it), else the first; with several, hovering it lists them in that order, as many as fit in the window (at most 30), then how many more. After `app.save`, and `app.export` with a `path`, the status bar counts the reply's `warnings` and names the first, and with several, hovering it lists them in the reply's order (a save's notes start with what the format loses). With `importer` (an importer plug-in's format id, `plugin.`) it runs `file.openWith`: the file opens with that importer, also when a built-in format reads its extension, with the importer's parameters `params` (their defaults when omitted; no dialog asks for them). Without `importer`, `params` go to the importer that opens the file, and a built-in format does not read them. `app.export` encodes through the engine's `document.export` (same options; the document keeps its path) and writes `path` through the host; without `path` it returns `{dataBase64, format, bytes}`, as headless mode does. It refuses a key that no format takes, and an unknown key inside a PDF or EPS option object, before it writes anything (`docs/mcp.md`, Parameter keys). It accepts a key that only another format takes and does not use it: the reply's `warnings` and the status bar name the key ("`quality` was not used: PDF files don't read it"). `app.save` refuses a key that no save format takes, beside the path or in `options`, the same way. `app.quit`, `file.close` and `file.closeAll` first open a `saveChanges` dialog for each modified document (they return `{"pending": "saveChanges"}`): `ui.dialog.confirm` saves, `ui.dialog.set {field: "choice", value: "dontSave"}` then confirm closes the document without saving, `ui.dialog.cancel` cancels the whole close or quit. `choice` takes `save`, `dontSave` or `cancel`, and any other field or value is an error. While more modified documents wait to be asked about, the dialog also has `applyToAll`: `ui.dialog.set {field: "applyToAll", value: true}` before confirm gives the same answer for each of them | | `app.export` with `useArtboards` | `{path, useArtboards: true, range? \| artboards?, …}` | Export As: PNG, JPEG and WebP write one file per chosen artboard (default all; SVG does so for a `range`), `-.`, and return `{path, files: [path…]}`; a PDF keeps them as pages of `path`. `useArtboards: false` covers the bounds of the visible art. The menu's Export As… is `file.exportAs`: the `exportAs` dialog (`format`, `useArtboards`, `all`, `range`), then a save dialog and the format's options: `pngOptions` / `jpgOptions` / `webpOptions`, `svgOptions`, or `savePdf` for a PDF | | `ui.dialog.*` on `gradientStop` | `{field: "color" \| "opacity" \| "location", value}` | double-clicking a stop on the Gradient tool's annotator opens its popover (fields `index`, `x`, `y`, `tab`); set `color` (hex), `opacity` or `location` (percentages) and `ui.dialog.confirm` to apply them to the selected stop. `gradient.selectStop {index}` picks the stop the annotator, the panels and Delete/←/→ (`ui.key`) act on | | `engine.execute` `ui.savePdfDialog` | `{path?, preset?, range?, …document.exportPdf options}` | opens the Save PDF dialog (`file.export.pdf` with no params opens it too). Its fields are the `document.exportPdf` options: set `compatibility` with `ui.dialog.set {field: "compatibility", value: "1.5"}`, a section with `{field: "compression", value: {…}}` (the options it leaves out keep the dialog's values, in Save PDF and in the preset editor, and in an object inside a section such as `compression.color`; a value that isn't an object, null included, is an error), the artboards with `__allArtboards: false` and `range`, the visible section with `__section` (General, Compression, Marks and Bleeds, Output, Advanced, Security, Summary), and the bleed link with `__bleedLinked` (true gives every side the top value; any value other than true or false is an error; setting `bleed` turns the link on when its four sides are equal and off when they differ). In Advanced, Custom… beside Flattener preset (enabled when the file is flattened: PDF 1.3, PDF/X-1a, PDF/X-3) opens the `pdfFlattenerOptions` dialog over Save PDF on the flattener options the save would use; its fields are those options, with `clipComplexRegions`, `preserveAlpha` and `preserveOverprints` set by the writer and Overprint. Its `ui.dialog.confirm` sets `flattener` (the preset then shows [Custom]; options equal to the preset's leave it unset) and goes back to Save PDF, and `ui.dialog.cancel` goes back unchanged. Setting `flattenerPreset` drops `flattener`. `useArtboards: false` opens it for one page of the visible art's bounds, as Export As with Use Artboards off does: the dialog has no Artboards section and its `__artBounds` field is true. `ui.dialog.confirm` writes `path` (else asks for one), returns `{path, bytes, warnings}` and opens the file when `viewAfterSaving` is set. With `useArtboards: false` it writes the page as `app.export` does and returns `{path, warnings}`, or `{background: true, status}` while Background Export writes it | diff --git a/docs/mcp.md b/docs/mcp.md index 85e35043df..2eb69df0ef 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -818,7 +818,7 @@ ends the wait, so the sampler can be shown again. The BSDs and headless sessions | `set_paint` | `{fill?, stroke?, strokeWidth?, ids?}` | Applies to the selection (or `ids`) and becomes the default for new art. Returns `{applied, objects}`: `objects` is the number of objects painted, as the `objects` of the `paint.setFill`, `paint.setStroke` and `stroke.set` replies (the largest of them): a selected group counts the objects inside it. When it is 0, a `note` says that only the paint for new art changed. When some of those commands painted objects and others painted none (a fill while the Shape Builder is the active tool, say), `unpainted` lists the commands that painted none, with a `note`. | | `press_key` | `{key, mods?}` | Remote: a real key event. Headless: runs the command or tool bound to that shortcut, or sends the key to the busy tool (digits too: `5` while dragging with the Perspective Selection tool; `e` while dragging a Selection or Group Selection marquee; `c` and `f` while dragging out an arc; `f`, `v`, `x` and `c` while dragging out a rectangular or polar grid). | | `type_text` | `{text}` | Remote only. | -| `answer_dialog` | `{set?: [{field, value?}], action?}` | Remote only. Sets fields of the app's open dialog in the order given with one `ui.dialog.set {fields, action?}` (a missing `value` is `null`). The app stores all of them or none, then answers that same dialog with `action` in the same request: `confirm` (OK) or `cancel`. Another connection cannot open a dialog between the two steps. When the app refuses the `ui.dialog.set`, for example because no dialog is open, the call is an error and the dialog is not confirmed or canceled. `set` cannot change a `__` field other than the dialog's controls held under a `__` name (such as Save PDF's `__section` or the Preferences `__search`): the `__command` and `__params` of a `confirm` or `command` dialog, and every other `__` field that holds what OK acts on, are refused with "the `` dialog cannot set protected field ``", so OK runs what the dialog was opened for. `action` alone is sent as `ui.dialog.confirm` or `ui.dialog.cancel`. Give `set`, `action` or both; `set` holds at most 256 fields. `inspect_ui` shows the open dialog and its fields as `ui.dialog`. Returns `{dialog, result}`: the dialog after the fields were set (or `null`) and the reply to `confirm` or `cancel` (or `null`). After `invoke_menu` `app.quit`, `file.close` or `file.closeAll` replies `{"pending": "saveChanges"}`, `{action: "confirm"}` saves, `{set: [{field: "discard", value: true}], action: "confirm"}` closes without saving and `{action: "cancel"}` cancels the close or quit. | +| `answer_dialog` | `{set?: [{field, value?}], action?}` | Remote only. Sets fields of the app's open dialog in the order given with one `ui.dialog.set {fields, action?}` (a missing `value` is `null`). The app stores all of them or none, then answers that same dialog with `action` in the same request: `confirm` (OK) or `cancel`. Another connection cannot open a dialog between the two steps. When the app refuses the `ui.dialog.set`, for example because no dialog is open, the call is an error and the dialog is not confirmed or canceled. `set` cannot change a `__` field other than the dialog's controls held under a `__` name (such as Save PDF's `__section` or the Preferences `__search`): the `__command` and `__params` of a `confirm` or `command` dialog, and every other `__` field that holds what OK acts on, are refused with "the `` dialog cannot set protected field ``", so OK runs what the dialog was opened for. `action` alone is sent as `ui.dialog.confirm` or `ui.dialog.cancel`. Give `set`, `action` or both; `set` holds at most 256 fields. `inspect_ui` shows the open dialog and its fields as `ui.dialog`. Returns `{dialog, result}`: the dialog after the fields were set (or `null`) and the reply to `confirm` or `cancel` (or `null`). After `invoke_menu` `app.quit`, `file.close` or `file.closeAll` replies `{"pending": "saveChanges"}`, `{action: "confirm"}` saves, `{set: [{field: "choice", value: "dontSave"}]}` followed by a second call with `{action: "confirm"}` closes without saving, and `{action: "cancel"}` cancels the close or quit. `choice` takes `save`, `dontSave` or `cancel`; while more modified documents wait to be asked about, adding `{field: "applyToAll", value: true}` to `set` gives the same answer for each of them. The second call answers whichever dialog is open when it arrives, so a dialog that another client or the user opens between the two calls is answered instead. | | `invoke_menu` | `{command, params?}` | Invokes a menu item by command id. UI commands such as `view.*` and `window.*` are in `command_list` with `ui: true`; running them needs the desktop app (`--connect`). The app refuses `attributes.openUrl`, `file.export.svg.browser`, `file.saveForWeb.browser` and `file.showPackage` for every control and MCP request, since each opens another app, so the headless error for them does not suggest `--connect`. | | `open_panel` | `{panel}` | Remote only. `panel` is a panel id (`layers`, `swatches`, `colorGuide`, …, as `window.panel` takes) or its display label (`"Color Guide"`), in any case. | | `screenshot` | `{path?, scale?, artboard?, window?, max_side?, traceViews?}` | Returns MCP image content (`image/png`, base64) plus a text block. Renders the artboard with the pixels of a PNG `export` with `background: "white"` at the same scale; `traceViews:true` draws Image Trace objects in the view set with `imageTrace.setView`, as in the app's window; `window:true` captures the app window (remote only), as `ui_screenshot` does, and `max_side` (1–4096, window captures only) scales the returned image down as described for `ui_screenshot`. With `path` the backend (the headless session or the app) writes the PNG there too; a window capture written there keeps its full size with `max_side`. An app that does not send the image back writes a window capture without `path` to a file in a new folder inside the system's temporary folder (on Unix only the user can open it); the server reads the file and then removes the folder, also when the capture or the read fails. |