From 643f048a895e4b28a1d99396449e22e4e59b6f93 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 16:16:42 -0800 Subject: [PATCH] =?UTF-8?q?Presets:=20Export=E2=80=A6=20suggests=20the=20p?= =?UTF-8?q?reset's=20name=20as=20the=20file=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Export… in the Print Presets, PDF Presets, Transparency Flattener Presets and Perspective Grid Presets dialogs suggested the document's name (Untitled.vcprintpresets). It now suggests the selected preset's name with the format's extension, made safe as a file name the way Export Library… makes a library's. A built-in preset's file is named without its brackets ([Default] gives Default.vcprintpresets). The four dialogs share one helper, preset_manager::export, and the web downloads the file under the same name. --- crates/ui-egui/src/dialogs/flattener_presets.rs | 7 +++---- crates/ui-egui/src/dialogs/pdf_presets.rs | 7 +++---- .../ui-egui/src/dialogs/perspective_presets.rs | 2 +- crates/ui-egui/src/dialogs/preset_manager.rs | 17 +++++++++++++++++ crates/ui-egui/src/dialogs/print_presets.rs | 5 +---- crates/ui-egui/src/dialogs/tests_perspective.rs | 17 +++++++++++++++++ .../ui-egui/src/dialogs/tests_print_presets.rs | 8 ++++++++ crates/ui-egui/src/io/export.rs | 4 ++-- crates/ui-egui/src/tests/saveext.rs | 2 +- 9 files changed, 53 insertions(+), 16 deletions(-) diff --git a/crates/ui-egui/src/dialogs/flattener_presets.rs b/crates/ui-egui/src/dialogs/flattener_presets.rs index b59f3b2a15..5346bf06f6 100644 --- a/crates/ui-egui/src/dialogs/flattener_presets.rs +++ b/crates/ui-egui/src/dialogs/flattener_presets.rs @@ -90,10 +90,7 @@ fn run(app: &mut VectorcraftApp, event: &ManagerEvent, current: &str) -> Result< Ok(app.session.flattener_presets().get(above).map(|p| p.name.clone())) } ManagerEvent::Action(ManagerAction::Import) => preset_manager::import(app, KEY, "flattener.presets.import", "Flattener presets", PRESET_EXTS), - ManagerEvent::Action(ManagerAction::Export) => { - crate::io::save_command_output(app, "flattener.presets.export", PRESET_FORMAT, json!({ "names": [current] }), |_, _| Ok(Value::Null))?; - Ok(None) - } + ManagerEvent::Action(ManagerAction::Export) => preset_manager::export(app, "flattener.presets.export", PRESET_FORMAT, current), // A new name that is taken or a built-in one is refused by the command. ManagerEvent::Rename(name) if name.trim() == current => Ok(None), ManagerEvent::Rename(name) => Ok(select(app.run("flattener.presets.save", json!({ "name": current, "newName": name }))?)), @@ -216,6 +213,8 @@ mod tests { let picker = crate::tests::picks::scripted(&mut app); open(&mut app, Some("Mine")); press(&mut app, ManagerAction::Export); + // The save panel suggests the preset's name. + assert_eq!(picker.requests().last().and_then(|r| r.file_name.clone()).as_deref(), Some("Mine.vcflattener")); // The answered save panel writes the file in the next frame. picker.answer_saved(&file, None); frame(&mut app); diff --git a/crates/ui-egui/src/dialogs/pdf_presets.rs b/crates/ui-egui/src/dialogs/pdf_presets.rs index a097f11537..e0e68d0eda 100644 --- a/crates/ui-egui/src/dialogs/pdf_presets.rs +++ b/crates/ui-egui/src/dialogs/pdf_presets.rs @@ -105,10 +105,7 @@ fn run(app: &mut VectorcraftApp, event: &ManagerEvent, current: &str) -> Result< Ok(app.session.pdf_presets().get(above).map(|p| p.name.clone())) } ManagerAction::Import => preset_manager::import(app, KEY, "pdf.preset.import", "PDF presets", PRESET_EXTS), - ManagerAction::Export => { - crate::io::save_command_output(app, "pdf.preset.export", PRESET_FORMAT, json!({ "names": [current] }), |_, _| Ok(Value::Null))?; - Ok(None) - } + ManagerAction::Export => preset_manager::export(app, "pdf.preset.export", PRESET_FORMAT, current), } } @@ -234,6 +231,8 @@ mod tests { app.run("pdf.preset.save", json!({"name": "Mine", "thumbnails": true})).unwrap(); open(&mut app, Some("Mine")); press(&mut app, ManagerAction::Export); + // The save panel suggests the preset's name. + assert_eq!(picker.requests().last().and_then(|r| r.file_name.clone()).as_deref(), Some("Mine.vcpdfpresets")); picker.answer_saved(&file, None); let ctx = ctx(&app); crate::tests_picks::poll(&mut app, &ctx); diff --git a/crates/ui-egui/src/dialogs/perspective_presets.rs b/crates/ui-egui/src/dialogs/perspective_presets.rs index c6d7ecb778..9d3f81d15d 100644 --- a/crates/ui-egui/src/dialogs/perspective_presets.rs +++ b/crates/ui-egui/src/dialogs/perspective_presets.rs @@ -307,7 +307,7 @@ pub(super) fn run(app: &mut VectorcraftApp, act: Action, current: &str) -> Resul }); } Action::Export => { - crate::io::save_command_output(app, "perspective.presets.export", PRESET_FORMAT, json!({ "names": [current] }), |_, _| Ok(Value::Null))?; + super::preset_manager::export(app, "perspective.presets.export", PRESET_FORMAT, current)?; } } Ok(()) diff --git a/crates/ui-egui/src/dialogs/preset_manager.rs b/crates/ui-egui/src/dialogs/preset_manager.rs index 1ec0cead83..319d3a49b3 100644 --- a/crates/ui-egui/src/dialogs/preset_manager.rs +++ b/crates/ui-egui/src/dialogs/preset_manager.rs @@ -183,6 +183,23 @@ pub(crate) fn import( Ok(None) } +/// Export…: run `command` on the preset `name` and write its file through a save panel that suggests +/// [`export_file_name`] (the web downloads it under that name). Returns at once: the answer arrives later. +pub(crate) fn export(app: &mut VectorcraftApp, command: &str, ext: &str, name: &str) -> Result, String> { + let file = export_file_name(name, ext); + crate::io::save_command_output_named(app, command, &file, serde_json::json!({ "names": [name] }), |_, _| Ok(Value::Null))?; + Ok(None) +} + +/// The file name Export… suggests for the preset `name`: the name without a built-in preset's brackets +/// (`[Default]`), made safe as a file name as Export Library… makes a library's +/// (`library::file_stem_for`), with the extension `ext`; "Preset" when no name is left. +pub(crate) fn export_file_name(name: &str, ext: &str) -> String { + let stem = name.trim().trim_start_matches('[').trim_end_matches(']').trim(); + let stem = if stem.is_empty() { "Preset" } else { stem }; + format!("{}.{ext}", vectorcraft_engine::cmd::library::file_stem_for(stem)) +} + /// Opens the manager of `source` on the preset `selected` (the first one when none has that name). pub(crate) fn open(app: &mut VectorcraftApp, source: &'static Source, selected: &str) { let rows = (source.rows)(app); diff --git a/crates/ui-egui/src/dialogs/print_presets.rs b/crates/ui-egui/src/dialogs/print_presets.rs index e69e7f5e7e..fc5e1b1d0b 100644 --- a/crates/ui-egui/src/dialogs/print_presets.rs +++ b/crates/ui-egui/src/dialogs/print_presets.rs @@ -77,9 +77,6 @@ fn run(app: &mut VectorcraftApp, event: &ManagerEvent, current: &str) -> Result< Ok(rows(app).get(preset_manager::above(i, 1)).map(|row| row.name.clone())) } ManagerAction::Import => preset_manager::import(app, KEY, "print.presets.import", "Print presets", PRESET_EXTS), - ManagerAction::Export => { - crate::io::save_command_output(app, "print.presets.export", PRESET_FORMAT, json!({ "names": [current] }), |_, _| Ok(Value::Null))?; - Ok(None) - } + ManagerAction::Export => preset_manager::export(app, "print.presets.export", PRESET_FORMAT, current), } } diff --git a/crates/ui-egui/src/dialogs/tests_perspective.rs b/crates/ui-egui/src/dialogs/tests_perspective.rs index 81ecca1d9b..775b3e1374 100644 --- a/crates/ui-egui/src/dialogs/tests_perspective.rs +++ b/crates/ui-egui/src/dialogs/tests_perspective.rs @@ -187,6 +187,23 @@ fn the_presets_manager_creates_edits_and_deletes_presets() { assert!(app.run("ui.perspectiveUserPreset3.1", json!({})).is_err()); } +/// Export… suggests the selected preset's name as the file name, a built-in one's without its brackets. +#[test] +fn the_presets_manager_exports_to_a_file_named_after_the_preset() { + use super::perspective_presets::{self, Action}; + let mut app = app(); + let picker = crate::tests_picks::scripted(&mut app); + app.run("perspective.presets.save", json!({"name": "Street"})).unwrap(); + app.run("ui.perspectivePresetsDialog", json!({"selected": "Street"})).unwrap(); + frame(&mut app); + for (preset, file) in [("Street", "Street.vcperspective"), ("[2P-High View]", "2P-High View.vcperspective")] { + perspective_presets::run(&mut app, Action::Export, preset).unwrap(); + assert_eq!(picker.requests().last().and_then(|r| r.file_name.clone()).as_deref(), Some(file), "{preset}"); + picker.cancel(); + frame(&mut app); + } +} + #[test] fn perspective_preset_menus_list_the_views_of_each_type() { let app = app(); diff --git a/crates/ui-egui/src/dialogs/tests_print_presets.rs b/crates/ui-egui/src/dialogs/tests_print_presets.rs index 65f4d2217f..a14ad40635 100644 --- a/crates/ui-egui/src/dialogs/tests_print_presets.rs +++ b/crates/ui-egui/src/dialogs/tests_print_presets.rs @@ -131,6 +131,8 @@ fn presets_files_import_and_export() { app.run("ui.printPresetsDialog", json!({"selected": "Mine"})).unwrap(); pump(&mut app); press(&mut app, ManagerAction::Export); + // The save panel suggests the preset's name. + assert_eq!(picker.requests().last().and_then(|r| r.file_name.clone()).as_deref(), Some("Mine.vcprintpresets")); picker.answer_saved(&file, None); let ctx = app.ui_ctx.clone().expect("the test app has a window"); crate::tests_picks::poll(&mut app, &ctx); @@ -141,5 +143,11 @@ fn presets_files_import_and_export() { assert_eq!(names, ["Mine", "Mine 2"]); assert_eq!(app.session.prefs.print_presets[1].settings.copies, 2); assert!(app.ui.status.contains("print presets"), "{}", app.ui.status); + // The built-in preset's file is named without its brackets. + app.run("ui.printPresetsDialog", json!({"selected": DEFAULT_PRESET})).unwrap(); + pump(&mut app); + press(&mut app, ManagerAction::Export); + assert_eq!(picker.requests().last().and_then(|r| r.file_name.clone()).as_deref(), Some("Default.vcprintpresets")); + picker.cancel(); let _ = std::fs::remove_dir_all(dir); } diff --git a/crates/ui-egui/src/io/export.rs b/crates/ui-egui/src/io/export.rs index 8f19aaef13..f78f3147b5 100644 --- a/crates/ui-egui/src/io/export.rs +++ b/crates/ui-egui/src/io/export.rs @@ -227,8 +227,8 @@ pub(crate) fn exported_with_notes(what: &str, n: usize, first: &str) -> String { } /// Run an engine command that returns `{dataBase64}` and write the bytes to a picked path -/// (Export Selection, Save for Office Documents, Text Export Options and the presets dialogs' -/// Export…), reported with the command's warnings ([`report_saved`]); `then` gets where they went. +/// (Export Selection, Save for Office Documents, Text Export Options and the Tracing Presets +/// dialog's Export…), reported with the command's warnings ([`report_saved`]); `then` gets where they went. // PLAN: callers outside io (print.rs, dialogs/office_export.rs, text_export.rs, the presets dialogs, menus/run.rs, // composite_fonts.rs) get the path from this synchronously; their owners move the code after it into `then`. pub fn save_command_output( diff --git a/crates/ui-egui/src/tests/saveext.rs b/crates/ui-egui/src/tests/saveext.rs index d3ed6f1a3e..c13165ff2e 100644 --- a/crates/ui-egui/src/tests/saveext.rs +++ b/crates/ui-egui/src/tests/saveext.rs @@ -216,7 +216,7 @@ fn save_panels_show_the_file_type_they_write() { ]; for (save, params, export, label, exts) in presets { app.run(save, params).unwrap(); - let run = |a: &mut VectorcraftApp| io::save_command_output(a, export, exts[0], json!({"names": ["Mine"]}), |_, _| Ok(Value::Null)); + let run = |a: &mut VectorcraftApp| crate::dialogs::preset_manager::export(a, export, exts[0], "Mine").map(|_| Value::Null); shows(&mut app, &picker, run, &[(label, exts)], &format!("{export}: the presets dialog's Export…")); } }