mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Integration: Save As from an opened .pgm suggests .pnm on every platform (#3609)
CodeRabbit on #9775: the Linux portal swaps the extension for the picked type, but macOS and rfd keep the suggested name, so an opened image.pgm could still be saved as image.pgm holding P6 data. The suggested name itself now takes .pnm for any Netpbm subtype; the services test fails without it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
8b0a8da8b1
commit
f56ae19522
2 files changed
+15
No files matched your search
@@ -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<String>)> {
|
||||
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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user