Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

M10.9: Export with Previous (⌥⇧⌘E), export dialog remembers the last options (prefs.json); Show in Finder (⌘R); New Folder… (⇧⌘N)

Brandon Thomasb718d9248a65
diff --git a/apps/lightcraft-web/src/web.rs b/apps/lightcraft-web/src/web.rs
index f669ec268..00ef74ad9 100644
--- a/apps/lightcraft-web/src/web.rs
+++ b/apps/lightcraft-web/src/web.rs
@@ -158,6 +158,7 @@ fn services(originals: Originals, backend: Option<Backend>, ctx: egui::Context)
png: Some(Box::new(|img: &lightcraft_raster::Rgba8| {
lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(img), &lightcraft_codecs::EncodeMeta::default()).unwrap_or_default()
})),
+ reveal: None,
}
}
diff --git a/apps/lightcraft/src/main.rs b/apps/lightcraft/src/main.rs
index baac940f1..4c534d955 100644
--- a/apps/lightcraft/src/main.rs
+++ b/apps/lightcraft/src/main.rs
@@ -76,6 +76,19 @@ fn save_prefs(app: &LightcraftApp) {
fn services() -> Services {
Services {
+ reveal: Some(Box::new(|path: &str| {
+ let status = if cfg!(target_os = "macos") {
+ std::process::Command::new("open").args(["-R", path]).status()
+ } else if cfg!(target_os = "windows") {
+ std::process::Command::new("explorer").arg(format!("/select,{path}")).status()
+ } else {
+ let dir = std::path::Path::new(path).parent().map(|d| d.to_string_lossy().to_string()).unwrap_or_else(|| ".".into());
+ std::process::Command::new("xdg-open").arg(dir).status()
+ };
+ status
+ .map_err(|e| e.to_string())
+ .and_then(|s| if s.success() || cfg!(target_os = "windows") { Ok(()) } else { Err(format!("reveal failed: {s}")) })
+ })),
pick_files: Some(Box::new(|| {
rfd::FileDialog::new()
.add_filter(
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 2f971c97e..633555dc0 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -94,6 +94,8 @@ pub struct Session {
pub library: Option<library::Library>,
/// XMP sidecar preferences (persisted with the library).
pub xmp: sidecar::XmpPrefs,
+ /// Parameters of the last export (`app.export` params, minus targets), persisted in prefs.json.
+ pub last_export: Option<serde_json::Value>,
}
impl Default for Session {
@@ -126,6 +128,7 @@ impl Session {
active_mask: None,
library: None,
xmp: sidecar::XmpPrefs::default(),
+ last_export: None,
}
}
diff --git a/crates/engine/src/library.rs b/crates/engine/src/library.rs
index d0fd0ac3b..5225a00a5 100644
--- a/crates/engine/src/library.rs
+++ b/crates/engine/src/library.rs
@@ -99,6 +99,8 @@ impl Library {
#[serde(default)]
struct PrefsFile {
xmp: crate::sidecar::XmpPrefs,
+ /// Parameters of the last export (for Export with Previous).
+ last_export: Option<serde_json::Value>,
}
fn presets_json(presets: &[Preset]) -> String {
@@ -151,7 +153,9 @@ impl Session {
}
}
// preferences
- self.xmp = read_json::<PrefsFile>(files.as_mut(), "prefs.json").map(|p| p.xmp).unwrap_or_default();
+ let prefs = read_json::<PrefsFile>(files.as_mut(), "prefs.json").unwrap_or_default();
+ self.xmp = prefs.xmp;
+ self.last_export = prefs.last_export;
// view state
if let Some(v) = read_json::<ViewFile>(files.as_mut(), "view.json") {
self.source = v.source;
@@ -248,7 +252,7 @@ impl Session {
/// Save the library preferences (no-op for in-memory sessions).
pub fn save_prefs(&mut self) -> Result<()> {
- let v = serde_json::to_vec_pretty(&PrefsFile { xmp: self.xmp }).unwrap_or_default();
+ let v = serde_json::to_vec_pretty(&PrefsFile { xmp: self.xmp, last_export: self.last_export.clone() }).unwrap_or_default();
let Some(lib) = self.library.as_mut() else { return Ok(()) };
lib.files.write_atomic("prefs.json", &v).map_err(|e| EngineError::Other(format!("prefs: {e}")))
}
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index c92203028..4630ccc2a 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -330,6 +330,14 @@ pub fn export_active(app: &mut LightcraftApp, p: &Value) -> Result<Value, String
w(&path, &e.bytes)?;
out.push(json!({"path": path, "width": e.width, "height": e.height, "bytes": e.bytes.len()}));
}
+ // remember for Export with Previous (and to prefill the dialog)
+ let mut last = p.clone();
+ if let Some(o) = last.as_object_mut() {
+ o.remove("ids");
+ o.remove("path");
+ }
+ app.session.last_export = Some(last);
+ let _ = app.session.save_prefs();
Ok(json!({"files": out}))
}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 757301ea0..ded4d51bb 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -30,6 +30,8 @@ pub type PickFiles = Box<dyn FnMut() -> Vec<String>>;
pub type SaveFile = Box<dyn FnMut(&str) -> Option<String>>;
pub type WriteFn = Box<dyn FnMut(&str, &[u8]) -> Result<(), String>>;
pub type PngEncode = Box<dyn Fn(&lightcraft_raster::Rgba8) -> Vec<u8>>;
+/// Reveal a file in the system file manager (Finder / Explorer / the folder on Linux).
+pub type RevealFn = Box<dyn FnMut(&str) -> Result<(), String>>;
/// Platform services injected by the host app (desktop or web).
#[derive(Default)]
@@ -43,6 +45,8 @@ pub struct Services {
pub write: Option<WriteFn>,
/// PNG encoder (the host links an image encoder; the UI crate stays codec-free).
pub png: Option<PngEncode>,
+ /// Show a file in the system file manager (desktop only).
+ pub reveal: Option<RevealFn>,
}
#[derive(Clone, Copy, Debug, Default)]
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 4c1b22a70..c1bfd0b53 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -52,6 +52,7 @@ pub const UI_COMMANDS: &[UiCommand] = &[
("tool.wbPicker", "White Balance Selector", Some("W"), "View"),
("tool.none", "No Tool", None, "View"),
("dialog.newAlbum", "New Album…", Some("Cmd+N"), "File"),
+ ("dialog.newFolder", "New Folder…", Some("Cmd+Shift+N"), "File"),
("dialog.createPreset", "Create Preset…", Some("Cmd+Shift+P"), "Photo"),
("dialog.copySettings", "Choose Edit Settings to Copy…", Some("Cmd+Shift+C"), "Photo"),
("dialog.export", "Export…", Some("Cmd+Shift+E"), "File"),
@@ -61,6 +62,8 @@ pub const UI_COMMANDS: &[UiCommand] = &[
("app.about", "About LightCraft", None, "LightCraft"),
("app.shortcuts", "Keyboard Shortcuts", Some("Cmd+/"), "Help"),
("app.export", "Export Now", None, ""),
+ ("app.showInFinder", "Show in Finder", Some("Cmd+R"), "Photo"),
+ ("app.exportPrevious", "Export with Previous", Some("Cmd+Alt+Shift+E"), "File"),
];
fn panel(app: &mut LightcraftApp, ctx: &egui::Context, p: RightPanel, name: &str) {
@@ -262,6 +265,10 @@ pub fn run_ui_command(app: &mut LightcraftApp, id: &str, p: &Value) -> Option<Re
}
Ok(Value::Null)
}
+ "dialog.newFolder" => {
+ app.ui.dialog = Some(Dialog::NewAlbum { name: p.get("name").and_then(Value::as_str).unwrap_or("").into(), folder: true });
+ Ok(Value::Null)
+ }
"dialog.newAlbum" => {
app.ui.dialog = Some(Dialog::NewAlbum { name: p.get("name").and_then(Value::as_str).unwrap_or("").into(), folder: false });
Ok(Value::Null)
@@ -277,8 +284,15 @@ pub fn run_ui_command(app: &mut LightcraftApp, id: &str, p: &Value) -> Option<Re
Ok(Value::Null)
}
"dialog.export" => {
- app.ui.dialog =
- Some(Dialog::Export { opts: Default::default(), long_edge: 2048, limit_kb: 0, dir: crate::control::default_export_dir() });
+ let prev = app.session.last_export.clone().unwrap_or_default();
+ let u = |k: &str, d: u64| prev.get(k).and_then(Value::as_u64).unwrap_or(d);
+ let dir = prev.get("dir").and_then(Value::as_str).map(str::to_string).unwrap_or_else(crate::control::default_export_dir);
+ app.ui.dialog = Some(Dialog::Export {
+ opts: lightcraft_engine::export::ExportOptions::from_json(&prev),
+ long_edge: u("longEdge", 2048) as u32,
+ limit_kb: u("limitKb", 0) as u32,
+ dir,
+ });
Ok(Value::Null)
}
"app.about" => {
@@ -353,6 +367,11 @@ pub fn run_ui_command(app: &mut LightcraftApp, id: &str, p: &Value) -> Option<Re
return Some(r);
}
"app.export" => crate::control::export_active(app, p),
+ "app.showInFinder" => show_in_finder(app),
+ "app.exportPrevious" => match app.session.last_export.clone() {
+ Some(prev) => crate::control::export_active(app, &prev),
+ None => Err("nothing exported yet — use Export…".into()),
+ },
_ => return None,
};
Some(r)
@@ -362,6 +381,15 @@ pub fn ui_enabled(app: &LightcraftApp, id: &str) -> bool {
match id {
s if s.starts_with("panel.") || s.starts_with("tool.") || s.starts_with("section.") => app.session.active().is_some() || s == "panel.close",
"app.export" | "dialog.export" | "dialog.createPreset" | "dialog.copySettings" => app.session.active().is_some(),
+ "app.exportPrevious" => app.session.active().is_some() && app.session.last_export.is_some(),
+ "app.showInFinder" => {
+ app.services.reveal.is_some()
+ && app
+ .session
+ .active()
+ .and_then(|id| app.session.catalog.photo(id))
+ .is_some_and(|p| matches!(p.source, lightcraft_engine::catalog::Source::File { .. }))
+ }
"file.exportPresets" => app.session.presets.iter().any(|p| !p.builtin),
_ => true,
}
@@ -402,3 +430,15 @@ pub fn menu_entries(app: &LightcraftApp) -> Vec<MenuEntry> {
}
v
}
+
+/// Reveal the active photo's original in the system file manager.
+fn show_in_finder(app: &mut LightcraftApp) -> Result<Value, String> {
+ let id = app.session.active().ok_or("no photo selected")?;
+ let path = match app.session.catalog.photo(id).map(|p| p.source.clone()) {
+ Some(lightcraft_engine::catalog::Source::File { path }) => path,
+ _ => return Err("this photo has no file (demo scene)".into()),
+ };
+ let reveal = app.services.reveal.as_mut().ok_or("not available here")?;
+ reveal(&path)?;
+ Ok(json!({"path": path}))
+}