Skip to content

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

Web: File › Open opens every selected file (#2802) (#2807)

Valentin Kleinfaf2639fb83f
diff --git a/apps/photocraft-web/src/web.rs b/apps/photocraft-web/src/web.rs
index 21b70492a..21c55a8d8 100644
--- a/apps/photocraft-web/src/web.rs
+++ b/apps/photocraft-web/src/web.rs
@@ -353,17 +353,30 @@ fn services(inbox: Inbox) -> Services {
photocraft_io::export(doc, path, &opts).map(|r| (r.bytes, r.warnings)).map_err(|e| e.to_string())
})),
file_dialog: Some(Box::new(|request, _parent, reply| match request {
- // The browser's file picker hands over the file's contents, not a path.
- FileDialogRequest::Open { extensions, .. } => wasm_bindgen_futures::spawn_local(async move {
+ // The browser's file picker hands over the files' contents, not paths. File › Open
+ // asks for several files (`multiple`), each opened in its own tab.
+ FileDialogRequest::Open { multiple, extensions, .. } => wasm_bindgen_futures::spawn_local(async move {
let dialog = if let Some(exts) = extensions {
rfd::AsyncFileDialog::new().add_filter("Supported Files", &exts)
} else {
rfd::AsyncFileDialog::new().add_filter("All Formats", OPEN_EXTS).add_filter("SVG", SVG_EXTS)
};
- let picked = dialog.pick_file().await;
- let answer = match picked {
- Some(file) => Some(FileDialogAnswer::Contents(file.file_name(), file.read().await)),
- None => None,
+ let answer = if multiple {
+ match dialog.pick_files().await {
+ Some(picked) if !picked.is_empty() => {
+ let mut files = Vec::with_capacity(picked.len());
+ for file in picked {
+ files.push((file.file_name(), file.read().await));
+ }
+ Some(FileDialogAnswer::ContentsMany(files))
+ }
+ _ => None,
+ }
+ } else {
+ match dialog.pick_file().await {
+ Some(file) => Some(FileDialogAnswer::Contents(file.file_name(), file.read().await)),
+ None => None,
+ }
};
reply.send(answer);
}),
diff --git a/crates/ui-egui/src/file_dialog.rs b/crates/ui-egui/src/file_dialog.rs
index 389eb9f51..b314aba31 100644
--- a/crates/ui-egui/src/file_dialog.rs
+++ b/crates/ui-egui/src/file_dialog.rs
@@ -38,6 +38,8 @@ pub enum FileDialogAnswer {
Paths(Vec<String>),
/// A chosen file's name and contents (the web, where a page gets contents rather than paths).
Contents(String, Vec<u8>),
+ /// Several chosen files' names and contents, in order (the web's multi-select File › Open).
+ ContentsMany(Vec<(String, Vec<u8>)>),
/// The path to save to (desktop), or the download's name (web).
SaveTo(String),
}
@@ -182,17 +184,23 @@ impl PhotocraftApp {
FileDialogAnswer::Paths(paths) => {
app.open_paths(&paths);
}
- FileDialogAnswer::Contents(name, bytes) => {
- if let Err(e) = app.open_bytes(&name, &bytes) {
- app.open_failed(&name, &e);
- }
- }
+ FileDialogAnswer::Contents(name, bytes) => app.open_contents(vec![(name, bytes)]),
+ FileDialogAnswer::ContentsMany(files) => app.open_contents(files),
FileDialogAnswer::SaveTo(_) => return Err(UNEXPECTED.into()),
}
Ok(Value::Null)
})
}
+ /// Opens each picked file's contents as its own document, reporting each failure.
+ fn open_contents(&mut self, files: Vec<(String, Vec<u8>)>) {
+ for (name, bytes) in files {
+ if let Err(e) = self.open_bytes(&name, &bytes) {
+ self.open_failed(&name, &e);
+ }
+ }
+ }
+
/// Run `next` on the result of the open dialog's action once it is answered; its result
/// replaces the action's. False when no dialog is open.
pub(crate) fn after_file_dialog(&mut self, next: impl FnOnce(&mut Self, Result<Value, String>) -> Result<Value, String> + 'static) -> bool {
@@ -267,6 +275,7 @@ fn last_used_dir(recent: &[String]) -> Option<String> {
fn read_picked(answer: FileDialogAnswer) -> Result<(String, Vec<u8>), String> {
match answer {
FileDialogAnswer::Contents(name, bytes) => Ok((name, bytes)),
+ FileDialogAnswer::ContentsMany(files) => files.into_iter().next().ok_or_else(|| CANCELLED.into()),
FileDialogAnswer::Paths(paths) => {
let path = paths.into_iter().next().ok_or(CANCELLED)?;
let bytes = photocraft_format::read_file(std::path::Path::new(&path)).map_err(|e| format!("{}: {e}", display_name(&path)))?;
diff --git a/crates/ui-egui/src/file_open/tests.rs b/crates/ui-egui/src/file_open/tests.rs
index ad3651d6d..995eef15b 100644
--- a/crates/ui-egui/src/file_open/tests.rs
+++ b/crates/ui-egui/src/file_open/tests.rs
@@ -161,6 +161,18 @@ fn the_web_hands_over_contents_rather_than_paths() {
assert_eq!((st.doc.name.as_str(), st.path.as_deref()), ("cat.png", None));
}
+#[test]
+fn the_web_opens_every_selected_file() {
+ let files = vec![("one.png".into(), b"x".to_vec()), ("broken.png".into(), b"bad".to_vec()), ("two.jpg".into(), b"x".to_vec())];
+ let (mut app, _) = app_with(vec![Some(FileDialogAnswer::ContentsMany(files))]);
+ menus::invoke(&mut app, &egui::Context::default(), "file.open", json!({})).unwrap();
+ answer(&mut app);
+ // Each good file is its own tab; the broken one is reported without stopping the rest.
+ let names: Vec<_> = app.session.documents().iter().map(|st| st.doc.name.as_str()).collect();
+ assert_eq!(names, ["one.png", "two.jpg"]);
+ assert!(app.ui.notices.iter().any(|n| n.error && n.title.contains("broken.png")), "{:?}", app.ui.notices);
+}
+
#[test]
fn pcraft_documents_save_in_place_but_flat_files_ask() {
let (mut app, written) = app_with(vec![None]);