Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(ui): a save name without an extension gets the suggested one (#5039) (#5049)

mroxso26e003059bc0
diff --git a/crates/ui-egui/src/file_dialog.rs b/crates/ui-egui/src/file_dialog.rs
index 486a13dee..7cdff50af 100644
--- a/crates/ui-egui/src/file_dialog.rs
+++ b/crates/ui-egui/src/file_dialog.rs
@@ -149,9 +149,17 @@ impl PhotocraftApp {
{
suggested = dir.join(suggested);
}
+ // The format comes from the extension, so a name typed without one (common where file
+ // extensions are hidden, #5039) gets the suggested file's extension, as Photoshop's
+ // Save As appends the extension of the chosen file type.
+ let suggested_ext = suggested.extension().and_then(|e| e.to_str()).map(|e| e.to_ascii_lowercase());
self.ask_file(FileDialogRequest::Save { suggested: suggested.to_string_lossy().into_owned() }, move |app, answer| match answer {
FileDialogAnswer::SaveTo(path) => {
let path = Self::lowercased_extension(app, path);
+ let path = match &suggested_ext {
+ Some(ext) if Path::new(&path).extension().is_none() => Path::new(&path).with_extension(ext).to_string_lossy().into_owned(),
+ _ => path,
+ };
// The web answers a download name, which has no folder to remember.
if let Some(doc) = doc
&& let Some(dir) = Path::new(&path).parent().filter(|p| !p.as_os_str().is_empty())
diff --git a/crates/ui-egui/src/file_dialog/tests.rs b/crates/ui-egui/src/file_dialog/tests.rs
index 8e19c67b4..c3f41b487 100644
--- a/crates/ui-egui/src/file_dialog/tests.rs
+++ b/crates/ui-egui/src/file_dialog/tests.rs
@@ -290,6 +290,26 @@ fn save_paths_get_a_lowercase_extension_when_the_preference_is_on() {
assert_eq!(*written.borrow(), ["/pics/CAT.PSD"], "off keeps the user's spelling");
}
+/// #5039: a name typed without an extension (where file extensions are hidden) gets the
+/// suggested file's extension, so the save picks its format instead of failing.
+#[test]
+fn save_paths_without_an_extension_get_the_suggested_one() {
+ let (mut app, open, written) = app();
+ let ctx = egui::Context::default();
+ menus::invoke(&mut app, &ctx, "file.saveAs", json!({})).unwrap();
+ app.poll_file_dialog(&ctx, None);
+ answer(&open, Some(FileDialogAnswer::SaveTo("/pics/cat".into())));
+ app.poll_file_dialog(&ctx, None);
+ assert_eq!(*written.borrow(), ["/pics/cat.psd"]);
+ assert_eq!(app.session.active().unwrap().path.as_deref(), Some("/pics/cat.psd"));
+ // A name that already has an extension is left alone.
+ menus::invoke(&mut app, &ctx, "file.saveAs", json!({})).unwrap();
+ app.poll_file_dialog(&ctx, None);
+ answer(&open, Some(FileDialogAnswer::SaveTo("/pics/dog.png".into())));
+ app.poll_file_dialog(&ctx, None);
+ assert_eq!(*written.borrow(), ["/pics/cat.psd", "/pics/dog.png"]);
+}
+
#[test]
fn save_and_export_dialogs_start_beside_the_document() {
let (mut app, open, _) = app();