Skip to content

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

LR-VIEW-BEFOREAFTER: before = import state, set from history / version / current, copy and swap

Brandon Thomasa418bf163ea3
diff --git a/crates/engine/src/cmd/before.rs b/crates/engine/src/cmd/before.rs
new file mode 100644
index 000000000..a6de32c46
--- /dev/null
+++ b/crates/engine/src/cmd/before.rs
@@ -0,0 +1,108 @@
+//! Before / After: what the "before" side shows. By default a photo's import state (camera
+//! defaults plus the import preset, see [`Photo::import_defaults`]); it can be set from the
+//! current settings, a history step or a version, and copied or swapped with the current
+//! settings. The before side always uses the current crop and orientation so the two line up.
+
+use std::sync::Arc;
+
+use lightcraft_catalog::Photo;
+use lightcraft_develop::DevelopSettings;
+use serde_json::{Value, json};
+
+use super::{CommandSpec, bad, cmd, has_active};
+use crate::{Result, Session};
+
+impl Session {
+ /// The settings the "before" side of photo `p` renders with.
+ pub fn before_settings(&self, p: &Photo) -> DevelopSettings {
+ let base = self.before.get(&p.id).map(|b| (**b).clone()).unwrap_or_else(|| p.import_defaults());
+ DevelopSettings { crop: p.develop.crop, orientation: p.develop.orientation, ..base }
+ }
+}
+
+fn active(s: &Session, c: &str) -> Result<lightcraft_catalog::PhotoId> {
+ s.active().ok_or_else(|| bad(c, "no active photo"))
+}
+
+fn state(s: &Session) -> Result<Value> {
+ let custom = s.active().is_some_and(|id| s.before.contains_key(&id));
+ Ok(json!({"before": if custom { "custom" } else { "import" }}))
+}
+
+/// `{source: import | current | history | version, index?, name?}`
+fn set_before(s: &mut Session, p: &Value) -> Result<Value> {
+ const C: &str = "beforeAfter.setBefore";
+ let id = active(s, C)?;
+ let photo = s.catalog.photo(id).ok_or_else(|| bad(C, "no photo"))?;
+ let settings: Option<Arc<DevelopSettings>> = match p.get("source").and_then(Value::as_str).unwrap_or("current") {
+ "import" | "original" => None,
+ "current" => Some(photo.develop.clone()),
+ "history" => {
+ let i = p.get("index").and_then(Value::as_u64).ok_or_else(|| bad(C, "history needs `index`"))? as usize;
+ Some(photo.history.get(i).ok_or_else(|| bad(C, format!("no history step {i}")))?.settings.clone())
+ }
+ "version" => {
+ let name = p.get("name").and_then(Value::as_str).ok_or_else(|| bad(C, "version needs `name`"))?;
+ Some(photo.versions.iter().find(|v| v.name == name).ok_or_else(|| bad(C, format!("no version `{name}`")))?.settings.clone())
+ }
+ other => return Err(bad(C, format!("unknown source `{other}` (import, current, history, version)"))),
+ };
+ match settings {
+ Some(b) => s.before.insert(id, b),
+ None => s.before.remove(&id),
+ };
+ state(s)
+}
+
+/// Copy the before settings to the photo (an edit, undoable); `swap` also makes the old
+/// current settings the new before.
+fn before_to_after(s: &mut Session, swap: bool) -> Result<Value> {
+ let c = if swap { "beforeAfter.swap" } else { "beforeAfter.copyBeforeToAfter" };
+ let id = active(s, c)?;
+ let photo = s.catalog.photo(id).ok_or_else(|| bad(c, "no photo"))?;
+ let after = photo.develop.clone();
+ let before = s.before.get(&id).map(|b| (**b).clone()).unwrap_or_else(|| photo.import_defaults());
+ s.set_develop(id, before, if swap { "Swap Before and After" } else { "Copy Before to After" })?;
+ if swap {
+ s.before.insert(id, after);
+ }
+ state(s)
+}
+
+pub fn specs() -> Vec<CommandSpec> {
+ vec![
+ cmd!(
+ "beforeAfter.setBefore",
+ "Set Before",
+ [],
+ None,
+ "{source: import|current|history|version, index? (history step), name? (version)} — what the before side shows (this session)",
+ has_active,
+ set_before
+ ),
+ cmd!(
+ "beforeAfter.copyAfterToBefore",
+ "Copy After's Settings to Before",
+ ["View", "Before/After Settings"],
+ None,
+ "{}",
+ has_active,
+ |s, _| { set_before(s, &json!({"source": "current"})) }
+ ),
+ cmd!(
+ "beforeAfter.copyBeforeToAfter",
+ "Copy Before's Settings to After",
+ ["View", "Before/After Settings"],
+ None,
+ "{}",
+ has_active,
+ |s, _| { before_to_after(s, false) }
+ ),
+ cmd!("beforeAfter.swap", "Swap Before and After Settings", ["View", "Before/After Settings"], None, "{}", has_active, |s, _| {
+ before_to_after(s, true)
+ }),
+ cmd!("beforeAfter.resetBefore", "Reset Before to Import State", ["View", "Before/After Settings"], None, "{}", has_active, |s, _| {
+ set_before(s, &json!({"source": "import"}))
+ }),
+ ]
+}
diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs
index 1c82dd4ff..06e3e6f43 100644
--- a/crates/engine/src/cmd/mod.rs
+++ b/crates/engine/src/cmd/mod.rs
@@ -4,6 +4,7 @@
//! delete), `album.*`, `develop.*` (settings), `crop.*`, `mask.*`, `preset.*`, `version.*`,
//! `edit.*` (undo/redo), and queries (`catalog.query`, `photo.inspect`, `develop.get`…).
+mod before;
mod color;
mod develop;
mod edit;
@@ -119,6 +120,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(preset_files::specs());
v.extend(prefs::specs());
v.extend(export::specs());
+ v.extend(before::specs());
v
})
}
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 69d149869..868607bfe 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -107,6 +107,9 @@ pub struct Session {
pub last_export: Option<serde_json::Value>,
/// The user's export presets (built-ins: [`export::builtin_presets`]), persisted in prefs.json.
pub export_presets: Vec<export::ExportPreset>,
+ /// Before/After: the "before" settings chosen per photo (this session; default: the photo's
+ /// import state). See `cmd/before.rs`.
+ pub before: std::collections::HashMap<PhotoId, Arc<DevelopSettings>>,
/// File probes from the last import review (`library.importPreview`), reused by the import.
pub import_probes: std::collections::HashMap<String, media::ProbeInfo>,
/// Develop defaults applied on import (persisted in prefs.json).
@@ -150,6 +153,7 @@ impl Session {
xmp: sidecar::XmpPrefs::default(),
last_export: None,
export_presets: Vec::new(),
+ before: Default::default(),
import_probes: Default::default(),
import_defaults: import::ImportDefaults::default(),
cache_mb: 0,
diff --git a/crates/engine/src/media.rs b/crates/engine/src/media.rs
index d85f23ec1..bd4b060da 100644
--- a/crates/engine/src/media.rs
+++ b/crates/engine/src/media.rs
@@ -507,7 +507,7 @@ impl crate::Session {
let p = self.catalog.photo(id)?.clone();
let level = SourceLevel::for_size(max_w.max(max_h));
let source = self.media.source_ref(&p, level);
- let settings = if before { Arc::new(lightcraft_pipeline::before_settings(&p.develop)) } else { p.develop.clone() };
+ let settings = if before { Arc::new(self.before_settings(&p)) } else { p.develop.clone() };
let request = RenderRequest { apply_crop, ..RenderRequest::fit(max_w, max_h) };
// the photo id is part of the key: two photos with the same settings and size must not
// share a result (a view slot showing photo A would otherwise look current for photo B)
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index ede8defc4..98ddfe802 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -257,3 +257,41 @@ fn masks_reorder_and_duplicate_and_invert() {
assert_eq!(d.masks[pos].components, d.masks[pos - 1].components);
assert_eq!(s.active_mask, Some(copy));
}
+
+#[test]
+fn before_is_the_import_state_and_can_be_set_copied_and_swapped() {
+ let mut s = demo();
+ let id = s.active().unwrap();
+ let import = s.catalog.photo(id).unwrap().import_defaults();
+ s.execute("develop.set", &json!({"values": {"light.exposure": 1.5}})).unwrap();
+ s.execute("crop.set", &json!({"rect": [0.1, 0.1, 0.9, 0.9]})).unwrap();
+ let before = |s: &Session| s.before_settings(s.catalog.photo(s.active().unwrap()).unwrap());
+ // default: the import state, with the current crop so both sides line up
+ let b = before(&s);
+ assert_eq!(b.light.exposure, import.light.exposure);
+ assert_eq!(b.crop, active_dev(&s).crop);
+ // from the current settings, then a history step
+ s.execute("beforeAfter.copyAfterToBefore", &json!({})).unwrap();
+ assert_eq!(before(&s).light.exposure, 1.5);
+ s.execute("develop.set", &json!({"values": {"light.exposure": -1.0}})).unwrap();
+ let steps = s.catalog.photo(id).unwrap().history.len();
+ let r = s.execute("beforeAfter.setBefore", &json!({"source": "history", "index": steps - 1})).unwrap();
+ assert_eq!(r["before"], "custom");
+ assert_eq!(before(&s).light.exposure, -1.0);
+ assert!(s.execute("beforeAfter.setBefore", &json!({"source": "history", "index": 999})).is_err());
+ // swap: the photo gets the before settings, the before side the old current ones
+ s.execute("beforeAfter.setBefore", &json!({"source": "import"})).unwrap();
+ s.execute("beforeAfter.swap", &json!({})).unwrap();
+ assert_eq!(active_dev(&s).light.exposure, import.light.exposure);
+ assert_eq!(before(&s).light.exposure, -1.0);
+ // copy before → after is one undoable edit
+ s.execute("beforeAfter.copyBeforeToAfter", &json!({})).unwrap();
+ assert_eq!(active_dev(&s).light.exposure, -1.0);
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert_eq!(active_dev(&s).light.exposure, import.light.exposure);
+ // the before render follows (the render key changes with the before settings)
+ let k1 = s.render_job(id, 64, 64, true, true).unwrap().key;
+ s.execute("beforeAfter.resetBefore", &json!({})).unwrap();
+ let k2 = s.render_job(id, 64, 64, true, true).unwrap().key;
+ assert_ne!(k1, k2);
+}
diff --git a/crates/ui-egui/src/menubar.rs b/crates/ui-egui/src/menubar.rs
index ff50418b1..a48d04b00 100644
--- a/crates/ui-egui/src/menubar.rs
+++ b/crates/ui-egui/src/menubar.rs
@@ -113,6 +113,7 @@ const LAYOUT: &[(&str, &[&str])] = &[
"view.showOriginal",
"view.beforeAfter",
"view.beforeAfterSplit",
+ "@Before/After Settings",
"---",
"view.zoomIn",
"view.zoomOut",
diff --git a/crates/ui-egui/src/panels/right.rs b/crates/ui-egui/src/panels/right.rs
index 13fe44b7a..ee5d2e646 100644
--- a/crates/ui-egui/src/panels/right.rs
+++ b/crates/ui-egui/src/panels/right.rs
@@ -519,6 +519,11 @@ fn activity(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) {
if resp.clicked() {
let _ = app.run("history.restore", json!({"index": i}));
}
+ resp.context_menu(|ui| {
+ if ui.button("Copy History Step Settings to Before").clicked() {
+ let _ = app.run("beforeAfter.setBefore", json!({"source": "history", "index": i}));
+ }
+ });
}
});
}
diff --git a/docs/parity.md b/docs/parity.md
index e314ea34a..a56267be9 100644
--- a/docs/parity.md
+++ b/docs/parity.md
@@ -23,7 +23,7 @@ and prints the summary; `cargo xtask parity --write` refreshes the summary table
|---|---:|---:|---:|---:|---:|---:|
| A. Import (IMP) | 5 | 3 | 5 | 1 | 2/4 (50%) | 2/4 (50%) |
| B. Library management (LIB) | 17 | 2 | 6 | 2 | 9/9 (100%) | 7/9 (78%) |
-| C. Views & navigation (VIEW) | 13 | 1 | 3 | 0 | 8/9 (89%) | 4/4 (100%) |
+| C. Views & navigation (VIEW) | 14 | 0 | 3 | 0 | 9/9 (100%) | 4/4 (100%) |
| D. Search & filter (FILT) | 7 | 2 | 4 | 0 | 4/4 (100%) | 3/4 (75%) |
| E. Metadata (META) | 2 | 2 | 2 | 0 | 2/2 (100%) | 0/2 (0%) |
| F. Edit panel — global adjustments (EDIT) | 36 | 2 | 10 | 1 | 28/28 (100%) | 8/14 (57%) |
@@ -48,7 +48,7 @@ and prints the summary; `cargo xtask parity --write` refreshes the summary table
| Y. Menus | 61 | 11 | 13 | 8 | 46/47 (98%) | 13/23 (57%) |
| Z. Keyboard shortcuts (desktop) | 58 | 11 | 11 | 1 | 49/52 (94%) | 9/23 (39%) |
| Lightroom Classic extras | 4 | 20 | 55 | 9 | — | 3/21 (14%) |
-| **Total** | 273 | 71 | 156 | 37 | 191/198 (96%) | 72/144 (50%) |
+| **Total** | 274 | 70 | 156 | 37 | 192/198 (97%) | 72/144 (50%) |
<!-- /parity:summary -->
## Top gaps
@@ -155,7 +155,7 @@ under the UI command that wraps it (a no-op error); the UI command now wins.
| LR-VIEW-FILMSTRIP | Filmstrip | P0 | ✅ | `cmd:view.filmstrip` | |
| LR-VIEW-ZOOM | Zoom & pan | P0 | ✅ | `cmd:view.zoomFit`, `cmd:view.zoom100`, `cmd:view.zoomIn`, `cmd:view.zoomOut`, `cmd:view.zoomToggle` | steps 25–800 % (not 6–1600 %); Fill only in the bottom bar |
| LR-VIEW-NAVIGATOR | Navigator mini map | P1 | ✅ | `cmd:view.navigator`, `crates/ui-egui/src/panels/detail.rs` | shown while zoomed (bottom right); click/drag pans |
-| LR-VIEW-BEFOREAFTER | Before / after | P0 | 🟡 | `cmd:view.showOriginal`, `cmd:view.beforeAfter`, `cmd:view.beforeAfterSplit`, `cmd:view.beforeAfterTopBottom`, `cmd:view.beforeAfterSplitTopBottom` | all four layouts; "before" is always the original (no before-state from history) |
+| LR-VIEW-BEFOREAFTER | Before / after | P0 | ✅ | `cmd:view.showOriginal`, `cmd:view.beforeAfter`, `cmd:view.beforeAfterSplit`, `cmd:view.beforeAfterTopBottom`, `cmd:view.beforeAfterSplitTopBottom`, `cmd:beforeAfter.setBefore`, `cmd:beforeAfter.copyAfterToBefore`, `cmd:beforeAfter.copyBeforeToAfter`, `cmd:beforeAfter.swap`, `cmd:beforeAfter.resetBefore` | all four layouts; before = the import state (defaults + import preset) or a chosen history step / version / the current settings; copy and swap (View → Before/After Settings, History row menu). The chosen before lasts for the session |
| LR-VIEW-COMPARE | Compare two photos | P1 | ✅ | `cmd:view.compare`, `cmd:compare.swap`, `cmd:compare.makeSelect`, `crates/ui-egui/src/panels/compare.rs` | select / candidate, synced zoom + pan, arrows move the candidate; no zoom-link toggle |
| LR-VIEW-SURVEY | Survey view [Classic] | P2 | ✅ | `cmd:view.survey`, `crates/ui-egui/src/panels/compare.rs` | selection tiled (≤ 48), keys act on the active photo, hover × removes |
| LR-VIEW-INFOOVERLAY | Info overlay on the photo | P1 | ✅ | `cmd:view.infoOverlay` | off / file + date + size / exposure + camera; ⌘I cycles (I in full screen; elsewhere I stays the Info panel) |