Save PDF: Reset with a deleted preset is an error

When the saved preset the Save PDF dialog has chosen is deleted while
the dialog is open, Reset now returns an error naming it, shown in the
status bar, and leaves every field, passwords included, as it is. A
dialog's reset callback returns a Result for this; Flare Tool Options
and Objects on Path Options return Ok. The ui.dialogReset params text
and docs/mcp.md name the error.
This commit is contained in:
dotCooCoo
2026-11-16 13:21:14 -08:00
parent 51efeba7b4
commit 4557e10248
8 changed files with 32 additions and 21 deletions
+1 -1
View File
@@ -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",
+8 -5
View File
@@ -179,6 +179,9 @@ use crate::{VectorcraftApp, widgets};
type DialogResult = Result<Value, String>;
/// 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<fn(&VectorcraftApp, &mut Dialog)>,
/// dialog stays open. An error leaves the dialog as it was.
pub reset: Option<ResetFn>,
pub min_width: f32,
pub max_width: Option<f32>,
/// 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)
+2 -1
View File
@@ -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.
@@ -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
}
+5 -5
View File
@@ -531,11 +531,11 @@ fn replace_settings(d: &mut Dialog, settings: Map<String, Value>) {
/// 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).
+7 -4
View File
@@ -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]
+1 -1
View File
@@ -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",
+2 -1
View File
@@ -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