diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs index 18f15fe3ea..48d78aa922 100644 --- a/apps/photocraft/src/services.rs +++ b/apps/photocraft/src/services.rs @@ -116,6 +116,15 @@ const SAVE_FILTERS: &[(&str, &[&str])] = &[ /// never swaps their extension for a document format's. const OTHER_SAVE_FILTERS: &[(&str, &[&str])] = &[("Color Swatches", &["aco"]), ("Color Table", &["act"]), ("Swatch Exchange", &["ase"])]; +/// `suggested` with a Netpbm subtype's extension (an opened .pgm) as the neutral .pnm, the name +/// every save dialog starts from: the encoder picks P5, P6, P7 or PFM from the image, so the old +/// suffix could name other data, and macOS and rfd keep the suggested name as it is. +fn netpbm_as_pnm(suggested: &str) -> String { + let path = Path::new(suggested); + let subtype = path.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).is_some_and(|e| ["pbm", "pgm", "ppm", "pam", "pfm"].contains(&e.as_str())); + if subtype { path.with_extension("pnm").to_string_lossy().into_owned() } else { suggested.to_string() } +} + /// 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(); @@ -223,6 +232,7 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>, Box::pin(async move { picked.await.map(|folder| answer_one(folder.path(), true, FileDialogAnswer::FolderTo)) }) } FileDialogRequest::Save { suggested } => { + let suggested = netpbm_as_pnm(&suggested); let filters = save_filters(&suggested); let name = dialog_file_name(&suggested, cfg!(windows)); let dir = Path::new(&suggested).parent().filter(|p| !p.as_os_str().is_empty()).map(Path::to_path_buf); diff --git a/apps/photocraft/src/services/tests.rs b/apps/photocraft/src/services/tests.rs index af77bdfa9f..c71074572e 100644 --- a/apps/photocraft/src/services/tests.rs +++ b/apps/photocraft/src/services/tests.rs @@ -301,6 +301,11 @@ fn save_as_offers_only_writable_formats_including_ico() { // neutral .pnm (#3609; CodeRabbit on #9775): a .pbm, .pgm or .pfm name would hold other data. let netpbm = save_filters("Untitled").into_iter().find(|(name, _)| name == "Netpbm").map(|(_, exts)| exts); assert_eq!(netpbm, Some(vec!["pnm".to_string()])); + // The suggested name itself becomes .pnm, as macOS and rfd keep it (CodeRabbit on #9775). + assert_eq!(netpbm_as_pnm("scans/image.pgm"), Path::new("scans/image.pnm").to_string_lossy()); + assert_eq!(netpbm_as_pnm("image.PFM"), "image.pnm"); + assert_eq!(netpbm_as_pnm("image.png"), "image.png"); + assert_eq!(netpbm_as_pnm("Untitled-1"), "Untitled-1"); // Also when Save As starts from a subtype's name: no filter offers that subtype again. for name in ["image.pbm", "image.pgm", "image.ppm", "image.pam", "image.pfm"] { let filters = save_filters(name);