diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs index e7faf24f21..e377302f10 100644 --- a/apps/photocraft/src/services.rs +++ b/apps/photocraft/src/services.rs @@ -32,13 +32,17 @@ const SAVE_FILTERS: &[(&str, &[&str])] = &[ ("OpenEXR", &["exr"]), ]; -/// [`SAVE_FILTERS`] with the one for `suggested`'s extension first. -fn save_filters(suggested: &str) -> Vec<(&'static str, &'static [&'static str])> { +/// Lists the save dialog's file types with the `suggested` type first (added if unlisted), so the dialog keeps that extension instead of .psd. +fn save_filters(suggested: &str) -> Vec<(String, Vec)> { let ext = Path::new(suggested).extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default(); - let mut v = SAVE_FILTERS.to_vec(); - if let Some(i) = v.iter().position(|(_, exts)| exts.contains(&ext.as_str())) { - let f = v.remove(i); - v.insert(0, f); + let mut v: Vec<(String, Vec)> = SAVE_FILTERS.iter().map(|(name, exts)| (name.to_string(), exts.iter().map(|e| e.to_string()).collect())).collect(); + match v.iter().position(|(_, exts)| exts.contains(&ext)) { + Some(i) => { + let f = v.remove(i); + v.insert(0, f); + } + None if !ext.is_empty() => v.insert(0, (ext.to_ascii_uppercase(), vec![ext])), + None => {} } v } @@ -180,7 +184,7 @@ pub fn native(automation: Option) -> let p = std::path::Path::new(suggested); let mut d = rfd::FileDialog::new(); for (name, exts) in save_filters(suggested) { - d = d.add_filter(name, exts); + d = d.add_filter(name, &exts); } if let Some(name) = p.file_name() { d = d.set_file_name(name.to_string_lossy()); @@ -310,14 +314,75 @@ mod tests { use photocraft_engine::Session; use photocraft_format::list_recovery; use photocraft_ui_egui::{PhotocraftApp, prefs_ui}; - use serde_json::json; + use serde_json::{Value, json}; - /// "Export As" formats must lead with their own filter, or the save panel appends the first one's extension (`photo.webp.psd`). + /// Tests every native save dialog and records the suggested file name. Each name's file type must come first + /// in the save panel, or the panel appends the first type's extension (`photo.webp.psd`, `photo.gif.psd`). #[test] - fn save_filters_lead_with_every_export_format() { - for ext in ["png", "jpg", "webp", "tif", "tga"] { - assert!(save_filters(&format!("photo.{ext}"))[0].1.contains(&ext), "{ext}"); + fn every_save_dialog_leads_with_its_own_extension() { + let asked: Rc>> = Rc::default(); + let log = asked.clone(); + let services = Services { + pick_save: Some(Box::new(move |s: &str| { + log.borrow_mut().push(s.to_string()); + None + })), + ..Default::default() + }; + let mut app = PhotocraftApp::new(Session::new(), services); + let ctx = egui::Context::default(); + let invoke = |app: &mut PhotocraftApp, id: &str| photocraft_ui_egui::menus::invoke(app, &ctx, id, json!({})); + let dialog = |app: &mut PhotocraftApp, id: &str, fields: Value| { + let d = invoke(app, id).unwrap()["dialog"].as_u64().unwrap(); + for (k, v) in fields.as_object().unwrap() { + app.ui.dialog_mut(d).unwrap().fields.insert(k.clone(), v.clone()); + } + let _ = photocraft_ui_egui::dialogs::confirm(app, d); + }; + new_doc(&mut app, "#ff0000"); + // Save As, Save a Copy. + // Files in a format Save As can't write, like .dng, should suggest .psd instead. + let _ = invoke(&mut app, "file.saveAs"); + for name in ["cat.pcraft", "cat.jpeg", "cat.gif", "cat.bmp", "cat.dng"] { + app.session.active_mut().unwrap().path = Some(name.into()); + let _ = invoke(&mut app, "file.saveAs"); } + let _ = invoke(&mut app, "file.saveACopy"); + // Export As, Quick Export, Save for Web (with and without slices). + for format in ["png", "jpg", "webp", "tif", "tga"] { + dialog(&mut app, "file.export.exportAs", json!({"format": format})); + } + for format in ["png", "jpg", "gif", "webp"] { + app.run("prefs.set", json!({"path": "export.quickExportFormat", "value": format})).unwrap(); + let _ = invoke(&mut app, "file.export.quickExportAsPng"); + } + for format in ["gif", "png8", "png24", "jpeg", "wbmp"] { + dialog(&mut app, "file.export.saveForWebLegacy", json!({"format": format})); + } + app.run("slice.new", json!({"x": 0, "y": 0, "width": 2, "height": 2})).unwrap(); + dialog(&mut app, "file.export.saveForWebLegacy", json!({"html": true})); + // Measurement Log, Export/Import Presets. + app.run("image.analysis.recordMeasurements", json!({})).unwrap(); + let _ = invoke(&mut app, "measurementLog.export"); + dialog(&mut app, "edit.presets.exportImportPresets", json!({"action": "export"})); + let asked = asked.borrow(); + let exts: Vec<&str> = asked.iter().filter_map(|s| s.rsplit_once('.').map(|(_, e)| e)).collect(); + // The extension each save above should suggest, in order. + let want = [ + "psd", // Save As untitled + "pcraft jpeg gif bmp psd", // Save As opened .pcraft .jpeg .gif .bmp .dng + "psd", // Save a Copy + "png jpg webp tif tga", // Export As + "png jpg gif webp", // Quick Export + "gif png png jpg wbmp html", // Save for Web: gif png8 png24 jpeg wbmp, then with slices + "csv pcpresets", // Measurement Log, Export Presets + ] + .join(" "); + assert_eq!(exts, want.split(' ').collect::>()); + for (name, ext) in asked.iter().zip(exts) { + assert!(save_filters(name)[0].1.iter().any(|e| e == ext), "{name}: {:?}", save_filters(name)[0]); + } + assert_eq!(save_filters("Untitled")[0].0, "Photoshop", "no extension keeps the default"); } const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index c6edf19a41..6c3931ef0b 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -835,8 +835,15 @@ impl PhotocraftApp { return Ok(("smart object".into(), Vec::new())); } let st = self.session.active().ok_or("no document")?; - // Documents are named after their file ("cat.png"): suggest "cat.psd", not "cat.png.psd". - let suggested = st.path.clone().unwrap_or_else(|| format!("{}.psd", st.doc.name.rsplit_once('.').map_or(st.doc.name.as_str(), |(stem, _)| stem))); + // Suggest the file's own name if Save As can write its format, otherwise switch to .psd. + let ext = st.path.as_deref().and_then(|p| std::path::Path::new(p).extension()).map(|e| e.to_string_lossy().to_ascii_lowercase()); + let writable = ext.is_some_and(|e| { + matches!(e.as_str(), photocraft_format::EXTENSION | "psd" | "psb") || photocraft_codecs::from_extension(&e).is_some_and(|f| f.caps().write) + }); + let suggested = match &st.path { + Some(p) if writable => p.clone(), + p => std::path::Path::new(p.as_deref().unwrap_or(&st.doc.name)).with_extension("psd").to_string_lossy().into_owned(), + }; let path = match path { Some(p) => p, None => self.services.pick_save.as_mut().and_then(|f| f(&suggested)).ok_or("cancelled")?,