PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix multi-file open dialog (#595) (#657)
Alex Gorevski12460d0345e2
diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rsindex 6767384bd..9d62a5366 100644--- a/apps/photocraft/src/services.rs+++ b/apps/photocraft/src/services.rs@@ -160,6 +160,13 @@ pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) ->let bytes = photocraft_format::read_file(&path).map_err(|e| e.to_string());Some((path.to_string_lossy().to_string(), bytes))})),+ pick_open_paths: Some(Box::new(|| {+ rfd::FileDialog::new()+ .add_filter("All Formats", OPEN_EXTS)+ .add_filter("PhotoCraft", &["pcraft"])+ .pick_files()+ .map(|paths| paths.into_iter().map(|path| path.to_string_lossy().into_owned()).collect())+ })),pick_save: Some(Box::new(|suggested: &str| {let p = std::path::Path::new(suggested);let mut d = rfd::FileDialog::new();diff --git a/crates/ui-egui/src/file_open/tests.rs b/crates/ui-egui/src/file_open/tests.rsindex 6eab293c1..0967e72d1 100644--- a/crates/ui-egui/src/file_open/tests.rs+++ b/crates/ui-egui/src/file_open/tests.rs@@ -77,6 +77,40 @@ fn file_open_dialog_sets_path_so_save_writes_in_place() {assert_eq!(written.borrow().last().map(|(p, _)| p.clone()).as_deref(), Some("/pics/cat.psd"));}+#[test]+fn file_open_dialog_opens_every_selected_path() {+ let dir = std::env::temp_dir().join(format!("photocraft-issue-595-open-{}", std::process::id()));+ std::fs::create_dir_all(&dir).unwrap();+ let paths = ["one.psd", "two.png"].map(|name| {+ let path = dir.join(name);+ std::fs::write(&path, b"x").unwrap();+ path.to_string_lossy().into_owned()+ });+ let (mut app, _) = app_with(None, None);+ let selected = paths.to_vec();+ app.services.pick_open_paths = Some(Box::new(move || Some(selected.clone())));++ menus::invoke(&mut app, &egui::Context::default(), "file.open", json!({})).unwrap();++ assert_eq!(app.session.documents().len(), 2);+ assert_eq!(+ app.session.documents().iter().map(|doc| doc.path.as_deref()).collect::<Vec<_>>(),+ paths.iter().map(|path| Some(path.as_str())).collect::<Vec<_>>()+ );+ assert_eq!(app.ui.recent_files, vec![paths[1].clone(), paths[0].clone()]);+ let _ = std::fs::remove_dir_all(&dir);+}++#[test]+fn cancelling_multi_file_open_does_not_fall_back_to_single_file_picker() {+ let (mut app, _) = app_with(Some(("/pics/unexpected.psd".into(), b"x".to_vec())), None);+ app.services.pick_open_paths = Some(Box::new(|| None));++ menus::invoke(&mut app, &egui::Context::default(), "file.open", json!({})).unwrap();++ assert!(app.session.documents().is_empty());+}+#[test]fn pcraft_documents_save_in_place_but_flat_files_ask() {let (mut app, written) = app_with(None, None);diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 260618b47..a4ef36612 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -154,6 +154,8 @@ pub type ExportFn = Box<dyn Fn(&Document, &str, &ExportSettings) -> Result<(Vec</// The picked file's name and its bytes, or why it could not be read (shown like any other open/// failure); `None` when the dialog was cancelled.pub type PickOpenFn = Box<dyn FnMut() -> Option<(String, Result<Vec<u8>, String>)>>;+/// File › Open's multi-file picker: the selected paths, `None` when cancelled.+pub type PickOpenPathsFn = Box<dyn FnMut() -> Option<Vec<String>>>;pub type PickSaveFn = Box<dyn FnMut(&str) -> Option<String>>;pub type WriteFn = Box<dyn FnMut(&str, &[u8]) -> Result<(), String>>;/// Read bytes through the desktop control session's authorized read root.@@ -207,8 +209,10 @@ pub struct Services {pub import: Option<ImportFn>,/// Encode a document for a file name (format chosen by extension).pub export: Option<ExportFn>,- /// Show an "open file" dialog; returns (name, bytes).+ /// Show a single-file picker for commands that import one file (Open As, presets, scripts).pub pick_open: Option<PickOpenFn>,+ /// Show File › Open's multi-file picker; returns the selected paths.+ pub pick_open_paths: Option<PickOpenPathsFn>,/// Show a "save file" dialog; returns a path/name to write.pub pick_save: Option<PickSaveFn>,/// Write bytes to a path (native) or trigger a download (web).@@ -705,9 +709,15 @@ impl PhotocraftApp {Ok(warnings)}- /// File › Open: the platform dialog returns the chosen file's path (native; the web delivers- /// picks through the inbox instead).+ /// File › Open: native platforms return all selected paths; the web delivers its pick through+ /// the single-file service/inbox instead.pub fn open_dialog_file(&mut self) {+ if let Some(pick_paths) = self.services.pick_open_paths.as_mut() {+ if let Some(paths) = pick_paths() {+ self.open_paths(&paths);+ }+ return;+ }let Some((path, bytes)) = self.services.pick_open.as_mut().and_then(|f| f()) else { return };if let Err(e) = bytes.and_then(|bytes| self.open_file(&path, &bytes)) {self.open_failed(&file_open::display_name(&path), &e);