diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index 8234aa1ba4..211efdb935 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -1336,7 +1336,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "ui.dialogReset", "Reset", "", - "{} the open dialog's Reset, which its Cancel button reads while Alt (Option on macOS) is held (Flare Tool Options, dialog `flareOptions`; Save PDF, dialog `savePdf`): its fields go back to their defaults (Save PDF: the chosen preset's settings, with the path and artboards kept) and the dialog stays open, so OK applies them → the dialog; an error when no dialog is open or the open one has no Reset", + "{} the open dialog's Reset, which its Cancel button reads while Alt (Option on macOS) is held (Flare Tool Options, dialog `flareOptions`; Save PDF, dialog `savePdf`): its fields go back to their defaults (Save PDF: the chosen preset's settings, with the path and artboards kept) and the dialog stays open, so OK applies them → the dialog; an error when no dialog is open, the open one has no Reset, or Save PDF's chosen preset was deleted (the fields stay as they are)", ), ( "ui.justification", diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 30b02e4380..94cbc7672a 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -179,6 +179,9 @@ use crate::{VectorcraftApp, widgets}; type DialogResult = Result; +/// A dialog's Reset ([`DialogSpec::reset`]). +type ResetFn = fn(&VectorcraftApp, &mut Dialog) -> Result<(), String>; + /// The shared dialog frame's inner margin, and the least room it leaves at the window's edges. const MARGIN: i8 = 22; const EDGE_GAP: f32 = 8.0; @@ -219,8 +222,8 @@ pub(crate) struct DialogSpec { pub alternate: Option<(&'static str, &'static str)>, /// Puts the fields back to their defaults. While Alt (Option on macOS) is held, the Cancel /// button of a dialog with an `ok` reads Reset; Reset (or `ui.dialogReset`) runs this and the - /// dialog stays open. - pub reset: Option, + /// dialog stays open. An error leaves the dialog as it was. + pub reset: Option, pub min_width: f32, pub max_width: Option, /// The body runs a live preview interaction that Cancel rolls back. @@ -722,12 +725,12 @@ pub(crate) fn can_reset(app: &VectorcraftApp) -> bool { } /// Reset (`ui.dialogReset`, or Cancel while Alt or Option is held): the open dialog's fields go -/// back to their defaults and it stays open → the dialog; an error when no dialog is open or the -/// open one has no Reset. +/// back to their defaults and it stays open → the dialog; an error when no dialog is open, the +/// open one has no Reset, or its Reset fails (the dialog is then left as it was). pub(crate) fn reset(app: &mut VectorcraftApp) -> DialogResult { let Some(mut d) = app.ui.dialog.clone() else { return Err("no dialog open".into()) }; let Some(reset) = spec(&d.kind).reset else { return Err("the open dialog has no Reset".into()) }; - reset(app, &mut d); + reset(app, &mut d)?; let out = serde_json::to_value(&d).unwrap_or_default(); app.ui.dialog = Some(d); Ok(out) diff --git a/crates/ui-egui/src/dialogs/flare_options.rs b/crates/ui-egui/src/dialogs/flare_options.rs index b94f6a7c2e..9c374f3cf5 100644 --- a/crates/ui-egui/src/dialogs/flare_options.rs +++ b/crates/ui-egui/src/dialogs/flare_options.rs @@ -136,10 +136,11 @@ fn preview_command(app: &VectorcraftApp, d: &Dialog) -> Option<(String, Value)> /// Reset (Cancel with Alt or Option held): every option goes back to the Flare tool's default /// (`FLARE_OPTIONS`, with Rays and Rings on). The dialog keeps a click's `x` and `y`, so OK still draws there. -fn reset(_: &VectorcraftApp, d: &mut Dialog) { +fn reset(_: &VectorcraftApp, d: &mut Dialog) -> Result<(), String> { if let Value::Object(defaults) = vectorcraft_tools::create("flare").options() { d.fields.extend(defaults); } + Ok(()) } /// OK: set the tool's options; opened by a click, draw a flare there with them. diff --git a/crates/ui-egui/src/dialogs/objects_on_path_options.rs b/crates/ui-egui/src/dialogs/objects_on_path_options.rs index 8a02cd37cf..4726e0762a 100644 --- a/crates/ui-egui/src/dialogs/objects_on_path_options.rs +++ b/crates/ui-egui/src/dialogs/objects_on_path_options.rs @@ -40,9 +40,10 @@ pub fn open(app: &mut VectorcraftApp) -> Value { } /// Reset: the center and 0. -fn reset(_: &VectorcraftApp, d: &mut Dialog) { +fn reset(_: &VectorcraftApp, d: &mut Dialog) -> Result<(), String> { d.fields.insert("pivot".into(), json!(CENTER)); d.fields.insert("rotate".into(), json!(0.0)); + Ok(()) } fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { @@ -61,8 +62,10 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { ui.add_space(8.0); let r = widgets::flat_button(ui, tl!("Reset"), 64.0); r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), tl!("Reset"))); - if r.clicked() { - reset(app, d); + if r.clicked() + && let Err(e) = reset(app, d) + { + app.status(e); } false } diff --git a/crates/ui-egui/src/dialogs/save_pdf.rs b/crates/ui-egui/src/dialogs/save_pdf.rs index 428a8768ff..9a4cbfa364 100644 --- a/crates/ui-egui/src/dialogs/save_pdf.rs +++ b/crates/ui-egui/src/dialogs/save_pdf.rs @@ -531,11 +531,11 @@ fn replace_settings(d: &mut Dialog, settings: Map) { /// Reset (Cancel with Alt or Option held): the options go back to the chosen preset's settings, as /// choosing that preset sets them, and the dialog stays open. A preset that can no longer be found -/// (a saved one deleted while the dialog is open) leaves the dialog as it is. -fn reset(app: &VectorcraftApp, d: &mut Dialog) { - if let Ok(settings) = settings_fields(app, &json!({ "preset": d.str("preset") })) { - replace_settings(d, settings); - } +/// (a saved one deleted while the dialog is open) is an error, and the dialog is left as it is. +fn reset(app: &VectorcraftApp, d: &mut Dialog) -> Result<(), String> { + let settings = settings_fields(app, &json!({ "preset": d.str("preset") }))?; + replace_settings(d, settings); + Ok(()) } /// General: the options, then the artboards (Save PDF) or the description (preset editor). diff --git a/crates/ui-egui/src/dialogs/save_pdf/tests.rs b/crates/ui-egui/src/dialogs/save_pdf/tests.rs index 9315b29f23..656d88b870 100644 --- a/crates/ui-egui/src/dialogs/save_pdf/tests.rs +++ b/crates/ui-egui/src/dialogs/save_pdf/tests.rs @@ -396,19 +396,22 @@ fn reset_puts_the_chosen_presets_settings_back_and_keeps_the_path_and_artboards( assert_eq!(vectorcraft_pdf::import(&w[0].1).unwrap().artboards.len(), 1, "range 2"); } -/// Reset with a saved preset deleted while the dialog is open leaves the options as they are, -/// rather than switching to another preset's. +/// Reset with a saved preset deleted while the dialog is open is an error naming the preset, and +/// it leaves the options and the password as they are rather than switching to another preset's. #[test] -fn reset_with_a_deleted_preset_keeps_the_options() { +fn reset_with_a_deleted_preset_is_an_error_and_keeps_the_options() { let (mut app, _, _) = app(); app.run("pdf.preset.save", json!({"name": "Gone", "compatibility": "1.5"})).unwrap(); app.run("ui.savePdfDialog", json!({"preset": "Gone"})).unwrap(); set_field(&mut app, "compatibility", json!("1.6")); + set_field(&mut app, OPEN_PASSWORD, json!("secret")); app.run("pdf.preset.delete", json!({"name": "Gone"})).unwrap(); - app.run("ui.dialogReset", json!({})).unwrap(); + let e = app.run("ui.dialogReset", json!({})).unwrap_err(); + assert!(e.contains("unknown PDF preset `Gone`"), "{e}"); let d = app.ui.dialog.as_ref().expect("the dialog stays open"); assert_eq!(d.str("preset"), "Gone"); assert_eq!(get(d, "compatibility"), "1.6"); + assert_eq!(get(d, OPEN_PASSWORD), "secret"); } #[test] diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index 5e43e8576c..d4ddd46ea2 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -1337,7 +1337,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "ui.dialogReset", "Reset", "", - "{} the open dialog's Reset, which its Cancel button reads while Alt (Option on macOS) is held (Flare Tool Options, dialog `flareOptions`; Save PDF, dialog `savePdf`): its fields go back to their defaults (Save PDF: the chosen preset's settings, with the path and artboards kept) and the dialog stays open, so OK applies them → the dialog; an error when no dialog is open or the open one has no Reset", + "{} the open dialog's Reset, which its Cancel button reads while Alt (Option on macOS) is held (Flare Tool Options, dialog `flareOptions`; Save PDF, dialog `savePdf`): its fields go back to their defaults (Save PDF: the chosen preset's settings, with the path and artboards kept) and the dialog stays open, so OK applies them → the dialog; an error when no dialog is open, the open one has no Reset, or Save PDF's chosen preset was deleted (the fields stay as they are)", ), ( "ui.justification", diff --git a/docs/mcp.md b/docs/mcp.md index ac1d935cfe..0b044a547c 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -1261,7 +1261,8 @@ option skips the options dialog, but without `path` the save panel still picks t dialog, pass `path`. `file.exportAs {path, exportSelection: true, …options}` writes the selection the same way. In the Save PDF dialog, `ui.dialogReset {}` (Cancel while Alt, or Option on macOS, is held) puts every option back to the chosen preset's settings, clears the passwords, and leaves the dialog open with its preset, path, artboard range and -section; `answer_dialog {action: "cancel"}` still closes it without saving. +section; `answer_dialog {action: "cancel"}` still closes it without saving. If the chosen saved preset was deleted +while the dialog is open, `ui.dialogReset` returns an error naming it and leaves every field, passwords included, as it is. Raster exports default to the document's raster effects resolution. An explicit `scale` sets pixels per point (72 times that value in ppi); `options.ppi` wins over either scale option. CLI `run --export` and