Skip to content

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

Merge PR #884

Claude814e35b10367
diff --cc crates/ui-egui/src/tests_masking.rs
index da900a57c,832ec74e0..b707af476
--- a/crates/ui-egui/src/tests_masking.rs
+++ b/crates/ui-egui/src/tests_masking.rs
@@@ -1153,62 -1127,24 +1153,83 @@@ fn ai_masks_without_the_model_offer_the
assert!(t.elapsed() < SETTLE, "{:?}", t.elapsed());
}
+fn color_samples(h: &Headless) -> serde_json::Value {
+ let d = develop(h);
+ let m = d.masks.last().expect("a mask");
+ let Some(MaskShape::ColorRange { samples, .. }) = m.components.iter().map(|c| &c.shape).find(|s| matches!(s, MaskShape::ColorRange { .. }))
+ else {
+ panic!("colour range")
+ };
+ serde_json::to_value(samples).expect("samples")
+}
+
+/// Same samples, up to sub-pixel rounding and the preview level sampled.
+#[track_caller]
+fn assert_close(a: &serde_json::Value, b: &serde_json::Value) {
+ let flat = |v: &serde_json::Value| -> Vec<f64> {
+ v.as_array().unwrap().iter().flat_map(|s| s.as_array().unwrap().iter().map(|x| x.as_f64().unwrap())).collect()
+ };
+ let (a, b) = (flat(a), flat(b));
+ assert_eq!(a.len(), b.len());
+ assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() < 0.05), "{a:?} vs {b:?}");
+}
+
+/// Regression: the Colour Range "Pick" button armed the eyedropper, but with the Masking panel
+/// open the loupe's clicks went to the mask overlay, so no sample was ever taken.
+#[test]
+fn color_range_pick_button_samples_the_photo() {
+ let mut h = detail("panel.masking");
+ exec(&mut h, "mask.add", json!({"kind": "colorRange"}));
+ h.app.ui.tool.clear();
+ h.settle(SETTLE);
+ click(&mut h, "button:colorPick0");
+ assert_eq!(h.app.ui.tool, "colorRange", "Pick arms the eyedropper");
+ brush_at(&mut h, 0.25, 0.5);
+ let ui_sample = color_samples(&h);
+ assert_eq!(ui_sample.as_array().map(Vec::len), Some(1), "a click samples a colour");
+ // the sample is the colour at that normalized point
+ exec(&mut h, "mask.sampleColor", json!({"x": 0.25, "y": 0.5}));
+ assert_close(&color_samples(&h), &ui_sample);
+ // ⇧-click adds
+ let r = h.request("ui.pointer", json!({"shift": true, "events": [{"kind": "down", "x": 0.7, "y": 0.2}, {"kind": "up", "x": 0.7, "y": 0.2}]}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ assert_eq!(color_samples(&h).as_array().map(Vec::len), Some(2), "⇧-click adds a sample");
+
+ // cropped and zoomed: the click samples the photo point under it
+ exec(&mut h, "crop.set", json!({"rect": [0.2, 0.1, 0.9, 0.8]}));
+ h.app.ui.zoom = crate::state::Zoom::Percent(200.0);
+ h.app.ui.pan = (0.5, 0.5);
+ h.settle(SETTLE);
+ brush_at(&mut h, 0.5, 0.5);
+ let ui_sample = color_samples(&h);
+ assert_eq!(ui_sample.as_array().map(Vec::len), Some(1));
+ // (the centre of the crop box, in normalized source coordinates)
+ let c = develop(&h).crop.geometry.rect;
+ exec(&mut h, "mask.sampleColor", json!({"x": (c.x0 + c.x1) / 2.0, "y": (c.y0 + c.y1) / 2.0}));
+ assert_close(&color_samples(&h), &ui_sample); // a cropped, zoomed pick maps to the same frame point
+ // Pick again ends picking
+ click(&mut h, "button:colorPick0");
+ assert!(h.app.ui.tool.is_empty());
+ h.settle(SETTLE);
++
+ #[test]
+ fn a_selected_develop_slider_accepts_wheel_nudges() {
+ let mut h = detail("panel.edit");
+ h.settle(SETTLE);
+ let name = "slider:light.exposure";
+ let panel = h.app.widgets.iter().find(|(id, _)| id == "panel:right_panel").unwrap().1;
+ let track = h.app.widgets.iter().find(|(id, _)| id == name).unwrap().1;
+ assert!(panel.contains(track.center()), "slider must be visible before interaction");
+
+ let result = h.request("ui.clickWidget", json!({"id": name, "fx": 0.5}), T);
+ assert_eq!(result["ok"], true, "{result}");
+ let before = lightcraft_develop::controls::get(&develop(&h), "light.exposure");
+
+ // A wheel event over the selected control adjusts exposure instead of
+ // scrolling the right sidebar (an unselected slider leaves scrolling alone).
+ let result = h.request("ui.scroll", json!({"dy": 120.0}), T);
+ assert_eq!(result["ok"], true, "{result}");
+ h.step();
+ let after = lightcraft_develop::controls::get(&develop(&h), "light.exposure");
+ assert!(after > before, "selected exposure did not increase: {before} -> {after}");
}