Skip to content

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

Save As and export dialogs start beside the document (#1866)

Jason Xiang941b84a74882
diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs
index 1bb7c1746..ce137be19 100644
--- a/apps/photocraft/src/services.rs
+++ b/apps/photocraft/src/services.rs
@@ -96,6 +96,9 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>,
if let Some(name) = Path::new(&suggested).file_name() {
dialog = dialog.set_file_name(name.to_string_lossy());
}
+ if let Some(dir) = Path::new(&suggested).parent().filter(|p| !p.as_os_str().is_empty()) {
+ dialog = dialog.set_directory(dir);
+ }
let picked = dialog.save_file();
Box::pin(async move { picked.await.map(|file| FileDialogAnswer::SaveTo(path_of(&file))) })
}
diff --git a/crates/ui-egui/src/file_dialog.rs b/crates/ui-egui/src/file_dialog.rs
index 92479e01a..dd6600ff8 100644
--- a/crates/ui-egui/src/file_dialog.rs
+++ b/crates/ui-egui/src/file_dialog.rs
@@ -106,7 +106,15 @@ impl PhotocraftApp {
/// Ask where to save: `then` gets the chosen path.
pub(crate) fn pick_save(&mut self, suggested: &str, then: impl FnOnce(&mut Self, String) -> Result<Value, String> + 'static) -> Result<Value, String> {
- self.ask_file(FileDialogRequest::Save { suggested: suggested.to_string() }, move |app, answer| match answer {
+ let mut suggested = std::path::PathBuf::from(suggested);
+ // Export dialogs usually provide only a file name. Start beside the source document,
+ // while preserving a caller's explicit directory and the untitled-document fallback.
+ if suggested.parent().is_some_and(|p| p.as_os_str().is_empty())
+ && let Some(dir) = self.session.active().and_then(|d| d.path.as_deref()).and_then(|p| std::path::Path::new(p).parent())
+ {
+ suggested = dir.join(suggested);
+ }
+ self.ask_file(FileDialogRequest::Save { suggested: suggested.to_string_lossy().into_owned() }, move |app, answer| match answer {
FileDialogAnswer::SaveTo(path) => then(app, path),
_ => Err(UNEXPECTED.into()),
})
diff --git a/crates/ui-egui/src/file_dialog/tests.rs b/crates/ui-egui/src/file_dialog/tests.rs
index b8c4d9003..701f6ff2d 100644
--- a/crates/ui-egui/src/file_dialog/tests.rs
+++ b/crates/ui-egui/src/file_dialog/tests.rs
@@ -88,6 +88,48 @@ fn the_app_keeps_running_while_a_dialog_is_open() {
assert!(!app.file_dialog_open());
}
+#[test]
+fn save_and_export_dialogs_start_beside_the_document() {
+ let (mut app, open, _) = app();
+ let ctx = egui::Context::default();
+ let dir = std::env::temp_dir().join("PhotoCraft projects").join("图像");
+ let source = dir.join("original.psd");
+ app.session.active_mut().unwrap().path = Some(source.to_string_lossy().into_owned());
+ for command in ["file.saveAs", "file.export.exportAs", "file.export.saveForWebLegacy"] {
+ let r = menus::invoke(&mut app, &ctx, command, json!({})).unwrap();
+ if let Some(id) = r["dialog"].as_u64() {
+ crate::dialogs::confirm(&mut app, id).unwrap();
+ }
+ app.poll_file_dialog(&ctx, None);
+ {
+ let held = open.borrow();
+ let (FileDialogRequest::Save { suggested }, _) = &held[0] else { panic!("save dialog expected") };
+ assert_eq!(std::path::Path::new(suggested).parent(), Some(dir.as_path()), "{command}");
+ if command == "file.saveAs" {
+ assert_eq!(std::path::Path::new(suggested), source);
+ }
+ }
+ answer(&open, None);
+ app.poll_file_dialog(&ctx, None);
+ }
+}
+
+#[test]
+fn save_suggestions_preserve_explicit_directories_and_untitled_names() {
+ let (mut app, open, _) = app();
+ let ctx = egui::Context::default();
+ for (document_path, suggested) in
+ [(None, "Untitled.png"), (Some("project/source.psd"), "exports/result.png"), (Some("project/source.psd"), "/other/result.png")]
+ {
+ app.session.active_mut().unwrap().path = document_path.map(str::to_string);
+ app.pick_save(suggested, |_, _| Ok(Value::Null)).unwrap();
+ app.poll_file_dialog(&ctx, None);
+ assert!(matches!(&open.borrow()[0].0, FileDialogRequest::Save { suggested: actual } if actual == suggested));
+ answer(&open, None);
+ app.poll_file_dialog(&ctx, None);
+ }
+}
+
#[test]
fn the_answer_goes_to_the_document_it_was_asked_for() {
let (mut app, open, written) = app();