diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs index f892de31cd2..891e251bf10 100644 --- a/apps/photocraft/src/services.rs +++ b/apps/photocraft/src/services.rs @@ -82,8 +82,11 @@ fn save_filters(suggested: &str) -> Vec<(String, Vec)> { 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); + let (name, mut exts) = v.remove(i); + // Windows adds the first type's first extension to a bare name: `.psb`, not `.psd`. + exts.retain(|e| *e != ext); + exts.insert(0, ext); + v.insert(0, (name, exts)); } None if !ext.is_empty() => v.insert(0, (ext.to_ascii_uppercase(), vec![ext])), None => {} @@ -91,6 +94,21 @@ fn save_filters(suggested: &str) -> Vec<(String, Vec)> { v } +/// The name the save dialog starts with. On Windows a visible extension stays put when the user +/// picks another type (with File name extensions shown, `image.psd` saved as PhotoCraft stayed +/// `image.psd`, #4394), while a bare name gets the chosen type's extension, so there the dialog +/// gets the stem and the suggested type (listed first by [`save_filters`]) supplies `.psd`. A +/// stem that still has a dot (`scan.jpg.psd`: Windows could keep `scan.jpg`) or an upper-case +/// extension (the user's spelling) keeps the whole name. Elsewhere the whole name is kept: the +/// macOS panel has no type list, and on Linux rfd doesn't report which type was picked. +fn dialog_file_name(suggested: &str, windows: bool) -> Option { + let path = Path::new(suggested); + let name = path.file_name()?.to_string_lossy().into_owned(); + let (Some(stem), Some(ext)) = (path.file_stem(), path.extension().and_then(|e| e.to_str())) else { return Some(name) }; + let bare = windows && Path::new(stem).extension().is_none() && !ext.is_empty() && ext == ext.to_ascii_lowercase(); + Some(if bare { stem.to_string_lossy().into_owned() } else { name }) +} + /// Shows an Open or Save dialog without blocking the event loop (#673, #574): rfd's async dialog /// is created here, on the main thread, with the window as its parent (a sheet on macOS, an owned /// modal window elsewhere), and a helper thread waits for it and hands the answer to the app. @@ -132,8 +150,8 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>, for (name, exts) in save_filters(&suggested) { dialog = dialog.add_filter(name, &exts); } - if let Some(name) = Path::new(&suggested).file_name() { - dialog = dialog.set_file_name(name.to_string_lossy()); + if let Some(name) = dialog_file_name(&suggested, cfg!(windows)) { + dialog = dialog.set_file_name(name); } if let Some(dir) = Path::new(&suggested).parent().filter(|p| !p.as_os_str().is_empty()) { dialog = dialog.set_directory(dir); @@ -669,7 +687,8 @@ mod tests { .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]); + // The first type's first extension is the one Windows adds to the bare name. + assert_eq!(save_filters(name)[0].1.first().map(String::as_str), Some(ext), "{name}: {:?}", save_filters(name)[0]); } assert_eq!(save_filters("Untitled")[0].0, "PSD Document", "no extension keeps the default"); } @@ -701,6 +720,33 @@ mod tests { assert!(err.contains("256"), "{err}"); } + /// #4394: with File name extensions shown, Windows kept `image.psd` when the type was changed to + /// PhotoCraft. Given the bare `image`, it adds the extension of whichever type is picked + /// (checked by hand with rfd 0.17.2 on Windows 11), defaulting to the first type's. + #[test] + fn the_windows_save_dialog_gets_a_bare_name_so_the_type_picks_the_extension() { + for (suggested, name, first) in [ + ("/pics/image.psd", "image", "psd"), + ("/pics/image.psb", "image", "psb"), + ("/pics/image.pcraft", "image", "pcraft"), + ("/pics/sky.fit", "sky", "fit"), + ("cat.jpeg", "cat", "jpeg"), + ("swatches.aco", "swatches", "aco"), + ] { + assert_eq!(dialog_file_name(suggested, true).as_deref(), Some(name), "{suggested}"); + assert_eq!(save_filters(suggested)[0].1[0], first, "{suggested}"); + // Elsewhere the dialogs keep the whole name (see `dialog_file_name`). + let whole = Path::new(suggested).file_name().unwrap().to_string_lossy().into_owned(); + assert_eq!(dialog_file_name(suggested, false), Some(whole), "{suggested}"); + } + // Kept whole: a second dot (Windows could keep `scan.jpg`), the user's upper case, no + // extension, and a dotfile. + for name in ["scan.jpg.psd", "my.photo.psd", "transparent.PNG", "Untitled", ".psd"] { + assert_eq!(dialog_file_name(name, true).as_deref(), Some(name)); + } + assert_eq!(dialog_file_name("", true), None); + } + #[test] fn block_on_waits_for_a_wake_from_another_thread() { let (tx, rx) = std::sync::mpsc::channel::();