Skip to content

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

Merge main (straighten, vignette style) into the polish branch

Brandon Thomas5f401d2eef07
diff --cc crates/gpu/src/ctx.rs
index b71471f60,b71471f60..f0b48da30
--- a/crates/gpu/src/ctx.rs
+++ b/crates/gpu/src/ctx.rs
@@@ -77,14 -77,14 +77,31 @@@ impl Buf
impl Drop for Buf {
fn drop(&mut self) {
if let Some(b) = self.buf.take() {
-- RETIRED.with(|r| r.borrow_mut().push(b));
++ // during thread teardown the list may already be gone: then the buffer leaks
++ let mut b = Some(b);
++ let _ = RETIRED.try_with(|r| r.0.borrow_mut().extend(b.take()));
++ if let Some(b) = b {
++ std::mem::forget(b);
++ }
++ }
++ }
++}
++
++/// Buffers dropped on a thread since its last submit.
++struct Retired(std::cell::RefCell<Vec<wgpu::Buffer>>);
++
++impl Drop for Retired {
++ /// At thread exit wgpu's own thread-locals may already be destroyed, and dropping a buffer
++ /// then aborts the process: leak the few buffers left instead (the device owns their memory).
++ fn drop(&mut self) {
++ for b in self.0.get_mut().drain(..) {
++ std::mem::forget(b);
}
}
}
thread_local! {
-- /// Buffers dropped on this thread since its last submit.
-- static RETIRED: std::cell::RefCell<Vec<wgpu::Buffer>> = const { std::cell::RefCell::new(Vec::new()) };
++ static RETIRED: Retired = const { Retired(std::cell::RefCell::new(Vec::new())) };
}
/// Most bytes kept in the free pool.
@@@ -233,7 -233,7 +250,7 @@@ impl Gpu
/// Submit command buffers, then make the buffers this thread dropped before reusable.
pub fn submit(&self, cmds: impl IntoIterator<Item = wgpu::CommandBuffer>) {
self.queue.submit(cmds);
-- let retired = RETIRED.with(|r| std::mem::take(&mut *r.borrow_mut()));
++ let retired = RETIRED.with(|r| std::mem::take(&mut *r.0.borrow_mut()));
let mut f = self.free.lock().unwrap_or_else(|e| e.into_inner());
f.extend(retired);
let mut total: u64 = f.iter().map(|b| b.size()).sum();
diff --cc crates/ui-egui/src/headless.rs
index 0ae29c29e,ccac06f21..b91cba02d
--- a/crates/ui-egui/src/headless.rs
+++ b/crates/ui-egui/src/headless.rs
@@@ -242,6 -242,6 +242,10 @@@ impl Headless
mod tests {
use super::*;
++ /// Generous: renders are slow when the machine is loaded (parallel builds), and a timed-out
++ /// settle would show a half-rendered UI.
++ const SETTLE: Duration = Duration::from_secs(120);
++
fn demo(size: [f32; 2]) -> Headless {
let services = crate::Services { png: None, ..Default::default() };
let mut app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), services);
@@@ -253,7 -253,7 +257,7 @@@
fn demo_grid_snapshot_has_ui_pixels() {
let t0 = Instant::now();
let mut h = demo([1200.0, 760.0]);
-- let img = h.snapshot(Duration::from_secs(20));
++ let img = h.snapshot(SETTLE);
eprintln!("headless snapshot: {:?} in {:?} ({} frames)", img.size, t0.elapsed(), h.frames());
assert_eq!(img.size, [1200, 760]);
// not blank: many distinct colours
@@@ -275,108 -275,24 +279,110 @@@
let mut h = demo([900.0, 600.0]);
let r = h.request("ui.set", json!({"view": "detail"}), Duration::from_secs(10));
assert_eq!(r["ok"], true, "{r}");
-- h.snapshot(Duration::from_secs(20))
++ h.snapshot(SETTLE)
};
let (a, b) = (shot(), shot());
assert_eq!(a.size, b.size);
-- let diff = a.pixels.iter().zip(&b.pixels).filter(|(x, y)| x != y).count();
++ // The photo may come from the GPU in one run and the CPU in the other (the GPU warms up in
++ // the background), which differ by ≤ 1–2 LSB; anything more is a real difference.
++ let diff = a.pixels.iter().zip(&b.pixels).filter(|(x, y)| x.to_array().iter().zip(y.to_array()).any(|(p, q)| p.abs_diff(q) > 2)).count();
assert_eq!(diff, 0, "{diff} pixels differ between two runs");
}
+ /// Keyboard culling: Compare (rating keys hit the candidate, arrows move it, auto-advance),
+ /// Survey (keys hit the active photo only), and auto-advance in Detail.
+ #[test]
+ fn compare_survey_and_auto_advance_by_keyboard() {
+ let mut h = demo([1000.0, 700.0]);
+ let t = Duration::from_secs(10);
+ let rating = |h: &Headless, id: u64| h.app.session.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().rating;
+ let flag = |h: &Headless, id: u64| h.app.session.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().flag;
+ let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
+ h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]]}}), t);
+ let r = h.request("engine.execute", json!({"command": "view.compare"}), t);
+ assert_eq!(r["result"], json!({"select": vis[0], "candidate": vis[1]}), "{r}");
+ assert_eq!(h.app.ui.view, crate::state::ViewMode::Compare);
+ // rating applies to the candidate (active) only
+ let before = rating(&h, vis[0]);
+ h.request("ui.key", json!({"key": "2"}), t);
+ assert_eq!((rating(&h, vis[0]), rating(&h, vis[1])), (before, 2));
+ // arrows move the candidate; the select stays
+ h.request("ui.key", json!({"key": "right"}), t);
+ assert_eq!(h.app.ui.compare, Some((vis[0], vis[2])));
+ h.request("ui.key", json!({"key": "left"}), t);
+ assert_eq!(h.app.ui.compare, Some((vis[0], vis[1])));
+ // Shift+X: reject and advance to the next candidate
+ h.request("ui.key", json!({"key": "x", "shift": true}), t);
+ assert_eq!(flag(&h, vis[1]), lightcraft_catalog::Flag::Reject);
+ assert_eq!(h.app.ui.compare, Some((vis[0], vis[2])));
+ h.request("engine.execute", json!({"command": "compare.swap"}), t);
+ assert_eq!(h.app.ui.compare, Some((vis[2], vis[0])));
+ h.request("engine.execute", json!({"command": "compare.makeSelect"}), t);
+ assert_eq!(h.app.ui.compare.map(|c| c.0), Some(vis[0]));
+ // Survey: the selection tiled; keys hit the active photo, auto-advance walks the survey
+ h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[3], vis[4], vis[5]], "active": vis[3]}}), t);
+ let r = h.request("engine.execute", json!({"command": "view.survey"}), t);
+ assert_eq!(r["result"]["photos"], 3);
+ h.request("engine.execute", json!({"command": "view.autoAdvance"}), t);
+ assert!(h.app.ui.auto_advance);
+ h.request("ui.key", json!({"key": "5"}), t);
+ h.request("ui.key", json!({"key": "p"}), t);
+ assert_eq!((rating(&h, vis[3]), flag(&h, vis[4])), (5, lightcraft_catalog::Flag::Pick));
+ assert_eq!(h.app.session.selection.active.map(|p| p.0), Some(vis[5]));
+ assert_eq!(h.app.session.selection.ids.len(), 3, "the survey keeps its selection");
- let img = h.snapshot(Duration::from_secs(20));
++ let img = h.snapshot(SETTLE);
+ assert_eq!(img.size, [1000, 700]);
+ // Detail: auto-advance moves to the next photo
+ h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[6]]}}), t);
+ h.request("ui.set", json!({"view": "detail"}), t);
+ h.request("ui.key", json!({"key": "u"}), t);
+ assert_eq!(h.app.session.selection.active.map(|p| p.0), Some(vis[7]));
+ // Escape leaves the culling views for Detail
+ h.request("ui.set", json!({"view": "survey"}), t);
+ h.request("ui.key", json!({"key": "escape"}), t);
+ assert_eq!(h.app.ui.view, crate::state::ViewMode::Detail);
+ // let in-flight renders finish: worker threads must not outlive the test process' TLS
- h.settle(Duration::from_secs(20));
++ h.settle(SETTLE);
+ }
+
+ /// The filter bar drives `library.filter` and saves the view as a smart album.
+ #[test]
+ fn filter_bar_filters_and_saves_a_smart_album() {
+ let mut h = demo([1400.0, 800.0]);
+ let t = Duration::from_secs(10);
+ let all = h.app.session.visible_cloned().len();
+ h.request("engine.execute", json!({"command": "view.filterBar"}), t);
+ assert!(h.app.ui.filter_bar);
+ for w in ["filter:star3", "filter:pick", "filter:label-red", "filter:label-red"] {
+ let r = h.request("ui.clickWidget", json!({"id": w}), t);
+ assert_eq!(r["ok"], true, "{w}: {r}");
+ }
+ let f = h.app.session.filter.clone();
+ assert_eq!((f.rating, f.flag, f.label), (3, Some(lightcraft_catalog::Flag::Pick), None), "a second click clears the label");
+ let n = h.app.session.visible_cloned().len();
+ assert!(n > 0 && n < all);
+ h.request("ui.clickWidget", json!({"id": "button:filterSave"}), t);
+ assert!(matches!(h.app.ui.dialog, Some(crate::state::Dialog::NewSmartAlbum { .. })));
+ let r = h.request("ui.dialog.confirm", json!({}), t);
+ assert_eq!(r["ok"], true, "{r}");
+ let smart = h.app.session.catalog.albums().find(|a| a.is_smart()).expect("smart album").id;
+ assert_eq!(h.app.session.catalog.album_count(smart), n);
+ h.request("ui.clickWidget", json!({"id": "button:filterClear"}), t);
+ assert_eq!(h.app.session.filter, Default::default());
+ assert_eq!(h.app.session.visible_cloned().len(), all);
- h.settle(Duration::from_secs(20));
++ h.settle(SETTLE);
+ }
+
#[test]
fn screenshot_request_is_answered_without_a_window() {
let mut h = demo([800.0, 500.0]);
-- let r = h.request("ui.screenshot", json!({}), Duration::from_secs(20));
++ let r = h.request("ui.screenshot", json!({}), SETTLE);
assert_eq!(r["ok"], true, "{r}");
assert_eq!(r["result"]["width"], 800);
assert_eq!(r["result"]["height"], 500);
let r = h.request("ui.resize", json!({"width": 640, "height": 400}), Duration::from_secs(5));
assert_eq!(r["ok"], true);
-- let r = h.request("ui.screenshot", json!({"headless": true}), Duration::from_secs(20));
++ let r = h.request("ui.screenshot", json!({"headless": true}), SETTLE);
assert_eq!(r["result"]["width"], 640, "{r}");
}
}
diff --cc docs/parity.md
index a522e01fb,aa57982b0..dc1a3ccdc
--- a/docs/parity.md
+++ b/docs/parity.md
@@@ -22,13 -22,13 +22,13 @@@ and prints the summary; `cargo xtask pa
| Section | ✅ | 🟡 | ⬜ | 🚫 | P0 done | P1 done |
|---|---:|---:|---:|---:|---:|---:|
| A. Import (IMP) | 2 | 4 | 7 | 1 | 1/4 (25%) | 1/4 (25%) |
-| B. Library management (LIB) | 9 | 4 | 12 | 2 | 7/9 (78%) | 1/9 (11%) |
-| C. Views & navigation (VIEW) | 7 | 2 | 8 | 0 | 7/9 (78%) | 0/4 (0%) |
-| D. Search & filter (FILT) | 2 | 7 | 4 | 0 | 2/4 (50%) | 0/4 (0%) |
+| B. Library management (LIB) | 12 | 4 | 9 | 2 | 7/9 (78%) | 4/9 (44%) |
+| C. Views & navigation (VIEW) | 9 | 2 | 6 | 0 | 7/9 (78%) | 1/4 (25%) |
+| 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) | 35 | 3 | 10 | 1 | 27/28 (96%) | 8/14 (57%) |
+ | F. Edit panel — global adjustments (EDIT) | 36 | 2 | 10 | 1 | 28/28 (100%) | 8/14 (57%) |
| G. Profiles (PROF) | 3 | 1 | 6 | 0 | 2/3 (67%) | 1/3 (33%) |
- | H. Crop & rotate (CROP) | 5 | 4 | 0 | 1 | 5/6 (83%) | 0/3 (0%) |
+ | H. Crop & rotate (CROP) | 7 | 2 | 0 | 1 | 6/6 (100%) | 1/3 (33%) |
| I. Remove / healing (REM) | 3 | 3 | 4 | 2 | 2/4 (50%) | 1/3 (33%) |
| J. Red eye (EYE) | 0 | 0 | 2 | 0 | — | 0/1 (0%) |
| K. Masking (MASK) | 6 | 10 | 7 | 0 | 4/8 (50%) | 2/5 (40%) |
@@@ -45,10 -45,10 +45,10 @@@
| V. Preferences (PREF) | 0 | 3 | 5 | 3 | 0/1 (0%) | 0/4 (0%) |
| W. Cloud & AI infrastructure (CLOUD / AI) | 0 | 1 | 1 | 2 | — | — |
| X. Cross-cutting behaviours (BEHAV) | 10 | 2 | 5 | 1 | 7/8 (88%) | 3/5 (60%) |
-| Y. Menus | 42 | 19 | 24 | 8 | 37/47 (79%) | 3/23 (13%) |
-| Z. Keyboard shortcuts (desktop) | 45 | 16 | 19 | 1 | 39/52 (75%) | 6/23 (26%) |
-| Lightroom Classic extras | 1 | 17 | 61 | 9 | — | 1/21 (5%) |
-| **Total** | 187 | 107 | 206 | 37 | 150/198 (76%) | 34/144 (24%) |
+| Y. Menus | 46 | 18 | 21 | 8 | 37/47 (79%) | 7/23 (30%) |
+| Z. Keyboard shortcuts (desktop) | 46 | 16 | 18 | 1 | 39/52 (75%) | 7/23 (30%) |
+| Lightroom Classic extras | 4 | 19 | 56 | 9 | — | 3/21 (14%) |
- | **Total** | 202 | 106 | 192 | 37 | 150/198 (76%) | 47/144 (33%) |
++| **Total** | 205 | 103 | 192 | 37 | 152/198 (77%) | 48/144 (33%) |
<!-- /parity:summary -->
## Top gaps