Skip to content

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

Save As on Linux: the one file type offered gives the name its extension

Codex5c48fa987d72
diff --git a/apps/photocraft/src/portal_save.rs b/apps/photocraft/src/portal_save.rs
index 42588e2bc..d16432193 100644
--- a/apps/photocraft/src/portal_save.rs
+++ b/apps/photocraft/src/portal_save.rs
@@ -156,6 +156,18 @@ fn file_uri_path(uri: &str) -> Option<PathBuf> {
Some(PathBuf::from(std::ffi::OsString::from_vec(out)))
}
+/// Which file type's extensions belong on the name the dialog returned: the one it reported, or,
+/// when the dialog offered only one type, that one. It reports no pick then, and the name it was
+/// given is the stem (`services::dialog_file_name`), so a bare `Swatches` has to come back as
+/// `Swatches.aco`. `None` when the answer names no type this dialog offered.
+fn chosen_type(filters: &[Filter], picked: Option<usize>) -> Option<usize> {
+ match picked {
+ Some(i) if i < filters.len() => Some(i),
+ None if filters.len() == 1 => Some(0),
+ _ => None,
+ }
+}
+
/// Save As on Linux: the portal's dialog, with the name given the extension of the file type
/// picked. When that changes the name to one that already exists, the dialog opens again under
/// the new name, so the portal asks before replacing it (it asked only about the typed name).
@@ -163,10 +175,10 @@ pub fn save_as(conn: &zbus::blocking::Connection, mut req: SaveRequest) -> Resul
loop {
let Some(saved) = save_file(conn, &req)? else { return Ok(None) };
let picked = saved.filter.as_deref().and_then(|label| req.filters.iter().position(|(l, _)| l == label));
- // With one type offered there is nothing to pick, and a typed extension is the user's.
- let exts: &[String] = match picked {
- Some(i) if req.filters.len() > 1 => &req.filters[i].1,
- _ => &[],
+ // A typed extension is the user's own: `with_chosen_type` keeps one it recognises.
+ let exts: &[String] = match chosen_type(&req.filters, picked) {
+ Some(i) => &req.filters[i].1,
+ None => &[],
};
let path = crate::services::with_chosen_type(&saved.path, req.suggested.as_deref(), exts);
if path == saved.path || !path.exists() {
diff --git a/apps/photocraft/src/portal_save/tests.rs b/apps/photocraft/src/portal_save/tests.rs
index ed44a10d9..76243c32b 100644
--- a/apps/photocraft/src/portal_save/tests.rs
+++ b/apps/photocraft/src/portal_save/tests.rs
@@ -230,3 +230,33 @@ fn a_single_file_type_keeps_the_typed_extension() {
assert_eq!(save_as(&conn, req).unwrap(), Some(PathBuf::from("/tmp/photocraft-none/Swatches.ase")));
daemon.kill().ok();
}
+
+/// The same single type, with the name left as the dialog shows it: the bare stem comes back, and
+/// that one type gives it its extension instead of a file named `Swatches` being written.
+#[test]
+fn a_single_file_type_gives_the_bare_name_its_extension() {
+ let typed: &'static str = "file:///tmp/photocraft-none/Swatches";
+ let Some((conn, _seen, mut daemon)) = mock_portal(vec![(0, Some((typed, "Color Swatches")))]) else {
+ eprintln!("no dbus-daemon: skipped");
+ return;
+ };
+ let req = SaveRequest { filters: vec![("Color Swatches".into(), vec!["aco".into()])], ..request("Swatches.aco") };
+ assert_eq!(save_as(&conn, req).unwrap(), Some(PathBuf::from("/tmp/photocraft-none/Swatches.aco")));
+ daemon.kill().ok();
+}
+
+/// Which type's extensions the returned name gets. The portal cases above need a `dbus-daemon`,
+/// so the rule itself is checked here too, which needs none.
+#[test]
+fn the_type_that_gives_the_extension_is_the_one_picked() {
+ let one: Vec<Filter> = vec![("Color Swatches".into(), vec!["aco".into()])];
+ // One type offered: the dialog reports no pick, and it was given the stem.
+ assert_eq!(chosen_type(&one, None), Some(0));
+ assert_eq!(chosen_type(&one, Some(0)), Some(0));
+ let many: Vec<Filter> = request("test").filters;
+ assert_eq!(chosen_type(&many, Some(1)), Some(1));
+ // Several types and no pick: nothing to add, as before.
+ assert_eq!(chosen_type(&many, None), None);
+ // An answer naming a type this dialog never offered is not a pick either.
+ assert_eq!(chosen_type(&many, Some(99)), None);
+}