Skip to content

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

fix(macos): default droplets to a writable user directory (#4746)

Nigel Ihza Firdausyc0e62673f85d
diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs
index a2b7f06b3..2423d2de3 100644
--- a/crates/ui-egui/src/file_ui.rs
+++ b/crates/ui-egui/src/file_ui.rs
@@ -22,6 +22,22 @@ fn default_dir(app: &PhotocraftApp) -> String {
app.session.active().and_then(|d| d.path.as_deref()).and_then(|p| p.rfind(['/', '\\']).map(|i| p[..i].to_string())).unwrap_or_else(|| ".".into())
}
+/// A packaged macOS app does not start in a useful writable working directory. A saved
+/// document keeps its own folder; otherwise prefer the user's Documents folder.
+fn droplet_default_dir(app: &PhotocraftApp) -> String {
+ let dir = default_dir(app);
+ if dir != "." {
+ return dir;
+ }
+ #[cfg(target_os = "macos")]
+ if let Some(home) = std::env::var_os("HOME") {
+ let home = std::path::PathBuf::from(home);
+ let documents = home.join("Documents");
+ return if documents.is_dir() { documents } else { home }.to_string_lossy().into_owned();
+ }
+ dir
+}
+
fn doc_stem(app: &PhotocraftApp) -> String {
app.session.active().map(|d| d.doc.name.rsplit_once('.').map_or(d.doc.name.clone(), |(a, _)| a.to_string())).unwrap_or_else(|| tl!("Untitled").into())
}
@@ -71,6 +87,7 @@ pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: &V
json!({"units": ["inches", "cm", "pixels"], "mode": ["rgb", "gray", "cmyk", "lab"]}),
),
"file.automate.createDroplet" => {
+ let dir = droplet_default_dir(app);
let Some(action) = crate::actions::selected_action(app) else {
return Some(Err("record an action in the Actions panel first".into()));
};
@@ -1064,6 +1081,20 @@ mod tests {
(app, egui::Context::default())
}
+ #[test]
+ fn droplet_folder_follows_the_document_or_uses_a_safe_fallback() {
+ let (mut app, _) = app();
+ let fallback = droplet_default_dir(&app);
+ #[cfg(target_os = "macos")]
+ if std::env::var_os("HOME").is_some() {
+ assert!(std::path::Path::new(&fallback).is_absolute());
+ }
+ #[cfg(not(target_os = "macos"))]
+ assert_eq!(fallback, ".");
+ app.session.active_mut().unwrap().path = Some("/tmp/photocraft/drawing.pcraft".into());
+ assert_eq!(droplet_default_dir(&app), "/tmp/photocraft");
+ }
+
#[test]
fn save_for_web_dialog_runs_the_command() {
let (mut app, ctx) = app();