From 05124f65d800b399ed44bd5d8f643be887b8da9f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 13:05:16 -0800 Subject: [PATCH] File Info: a template saved without .xmp gets it, and OK waits for the merge choice Save Metadata Template adds .xmp to a name typed without it, so the templates list shows the file, and asks before replacing a file already there under the completed name. File Info's OK and Enter wait while a template being imported shows its merge choice, so the template is not dropped. docs/mcp.md now says the title is left out of a template only when it is blank or a new document's Untitled-N name. --- crates/ui-egui/src/dialogs/file_info.rs | 65 +++++++++++++++++++++++-- docs/mcp.md | 4 +- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs/file_info.rs b/crates/ui-egui/src/dialogs/file_info.rs index b9c12777a9..29ada4d271 100644 --- a/crates/ui-egui/src/dialogs/file_info.rs +++ b/crates/ui-egui/src/dialogs/file_info.rs @@ -37,8 +37,16 @@ const XMP_EXT: &str = "xmp"; /// Width of the text fields. const FIELD: f32 = 320.0; -pub(super) const SPEC: DialogSpec = - DialogSpec { heading: |_| tl!("File Info").into(), body, confirm, min_width: 500.0, max_width: Some(500.0), ..DialogSpec::FORM }; +pub(super) const SPEC: DialogSpec = DialogSpec { + heading: |_| tl!("File Info").into(), + body, + confirm, + // OK and Enter wait while a template being imported waits for its merge choice. + ok_enabled: Some(|_, d| !d.fields.get(IMPORT).is_some_and(Value::is_object)), + min_width: 500.0, + max_width: Some(500.0), + ..DialogSpec::FORM +}; /// Open File Info on the active document's. pub fn open(app: &mut VectorcraftApp) -> Result { @@ -168,7 +176,9 @@ fn templates_menu(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) { } /// Save Metadata Template…: the fields as they are in the dialog, to a file picked in a save -/// dialog that starts in the metadata templates folder. +/// dialog that starts in the metadata templates folder. A name typed without `.xmp` takes it, so +/// the templates list shows the file; a file already there under that name is replaced only after +/// a question. fn save_template(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { let mut d2 = d.clone(); add_keywords(&mut d2); @@ -176,7 +186,12 @@ fn save_template(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> let name = format!("{}.{XMP_EXT}", d2.str("title").trim()); let pick = FilePick { name, folder, ..xmp_pick() }; // None: canceled, or the dialog is still open off the UI thread and runs this again. - let Some(path) = crate::picks::save(app, &pick) else { return Ok(()) }; + let Some(picked) = crate::picks::save(app, &pick) else { return Ok(()) }; + let path = if picked.to_ascii_lowercase().ends_with(&format!(".{XMP_EXT}")) { picked.clone() } else { format!("{picked}.{XMP_EXT}") }; + // The save dialog did not ask about the completed name: ask here, and the answer runs this again. + if path != picked && crate::picks::ask_to_replace(app, PickRequest::Save(pick), &picked, None, &path) { + return Ok(()); + } app.run("file.info.saveTemplate", json!({ "path": path, "fields": form::params(&d2) })).map(|_| ()) } @@ -449,4 +464,46 @@ mod tests { assert_eq!((st.doc.metadata.author.as_str(), st.doc.metadata.rating), ("Ada", 3)); assert_eq!(st.history.undo.len(), 1); } + + #[test] + fn ok_waits_for_the_merge_choice_of_a_template_being_imported() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("ui.fileInfoDialog", json!({})).unwrap(); + let mut d = app.ui.dialog.clone().unwrap(); + let ok = |app: &VectorcraftApp, d: &crate::state::Dialog| super::SPEC.ok_enabled.is_none_or(|f| f(app, d)); + assert!(ok(&app, &d)); + d.fields.insert(super::IMPORT.into(), json!({"name": "Studio"})); + assert!(!ok(&app, &d), "OK and Enter wait while the merge choice is shown"); + d.fields.remove(super::IMPORT); + assert!(ok(&app, &d)); + } + + #[test] + fn a_template_saved_without_an_extension_gets_xmp_and_is_listed() { + let dir = vectorcraft_testkit::TempDir::new("ui-file-info-save-ext"); + let folder = dir.join("Metadata Templates"); + let typed = folder.join("Studio").to_string_lossy().into_owned(); + let services = crate::Services { + write: Some(Box::new(|p: &str, b: &[u8]| std::fs::write(p, b).map_err(|e| e.to_string()))), + exists: Some(std::sync::Arc::new(|p: &str| std::path::Path::new(p).exists())), + pick_save: Some(Box::new(move |_: &crate::FilePick| Some(typed.clone()))), + ..Default::default() + }; + let mut app = VectorcraftApp::new(Session::new(), services); + app.session.attach_metadata_templates_dir(folder.clone()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("ui.fileInfoDialog", json!({})).unwrap(); + let mut d = app.ui.dialog.clone().unwrap(); + d.fields.insert("author".into(), json!("Ada")); + crate::picks::in_dialog(&mut app, &mut d, super::save_template).unwrap(); + assert!(folder.join("Studio.xmp").is_file() && !folder.join("Studio").exists(), "the typed name takes .xmp"); + let listed = app.session.execute("file.info.templates", &json!({})).unwrap(); + assert!(listed.to_string().contains("Studio"), "{listed}"); + // A file already there under the completed name is not replaced without asking. + std::fs::write(folder.join("Studio.xmp"), b"kept").unwrap(); + crate::picks::in_dialog(&mut app, &mut d, super::save_template).unwrap(); + assert_eq!(std::fs::read(folder.join("Studio.xmp")).unwrap(), b"kept"); + assert_eq!(d.kind, crate::dialogs::confirm::REPLACE, "the dialog asks before replacing it"); + } } diff --git a/docs/mcp.md b/docs/mcp.md index 549797968f..2fc50fdba7 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7609,8 +7609,8 @@ A PDF, `.ai` or `.ait` also opens with the `authorTitle` and `rating` its XMP pa Metadata templates (the menu at the top of File Info: Save Metadata Template…, Import…, the saved templates and Templates Folder; https://help.adobe.com/archive/en_US/illustrator/cs5/illustrator_cs5_help.pdf#page=278) keep File -Info fields in an XMP file. `file.info.saveTemplate {name?, path?, fields?}` writes the `title` (when it is not the -document's name), `author`, `authorTitle`, `description`, `keywords`, `rating` (from 1), `copyrightStatus`, +Info fields in an XMP file. `file.info.saveTemplate {name?, path?, fields?}` writes the `title` (when it is not blank +or the `Untitled-N` name a new document gets), `author`, `authorTitle`, `description`, `keywords`, `rating` (from 1), `copyrightStatus`, `copyrightNotice` and `copyrightUrl` that have a value as an XMP packet, using the properties listed above; the dates are left out. `fields` takes `file.info`'s editable keys in place of the document's values. `name` saves into the metadata templates folder as `.xmp` (the folder is made when it is missing; a name with characters file systems reject, a dot at either end, or a