LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
feat(guided): guides start on the press and their endpoints can be dragged
mahmatiasa22c496d038b
diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rsindex 2dae69630..6a6c74e5b 100644--- a/crates/ui-egui/src/panels/detail.rs+++ b/crates/ui-egui/src/panels/detail.rs@@ -60,6 +60,11 @@ pub enum Gesture {Guide {a: Point,},+ /// Guided Upright: dragging the endpoint `end` (0 = `guide.0`, 1 = `guide.1`) of guide `i`.+ GuideEndpoint {+ i: usize,+ end: usize,+ },/// Red Eye: an ellipse being dragged from corner `a` (normalized).Eye {a: Point,@@ -1865,39 +1870,86 @@ fn guided_overlay(draw(sa, sb, selected == Some(i));}ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);- // a click near a guide selects it (Delete / Backspace removes it alone); a click away clears- if resp.clicked()- && let Some(q) = resp.interact_pointer_pos()+ // A press starts what it means, right away (the pointer's press point, not where a drag+ // threshold would have been crossed): on an endpoint it drags that endpoint (the guide is+ // selected when it lands), on a line it selects the guide (a press on the selected one+ // deselects), anywhere else it starts a new guide from the press point.+ if ui.input(|i| i.pointer.any_pressed())+ && let Some(q) = ui.input(|i| i.pointer.press_origin()).or(resp.interact_pointer_pos())+ && resp.rect.contains(q){- let hit = d.geometry.guides.iter().position(|guide| {+ let endpoint = d.geometry.guides.iter().enumerate().find_map(|(i, guide)| {+ let (a, b) = on_screen(guide);+ if a.distance(q) <= 7.0 {+ Some((i, 0))+ } else if b.distance(q) <= 7.0 {+ Some((i, 1))+ } else {+ None+ }+ });+ let line = d.geometry.guides.iter().position(|guide| {let (a, b) = on_screen(guide);point_segment_dist(q, a, b) <= 6.0});- app.ui.selected_guide = match hit {- Some(i) if selected != Some(i) => Some((id.0, i)),- _ => None,- };- }- if resp.drag_started() {- // drawing a new guide leaves nothing selected- app.ui.selected_guide = None;- }- if resp.drag_started()- && let Some(q) = resp.interact_pointer_pos()- {- app.gesture = Some(Gesture::Guide { a: map.norm(q) });+ match (endpoint, line) {+ (Some((i, end)), _) => app.gesture = Some(Gesture::GuideEndpoint { i, end }),+ (None, Some(i)) => app.ui.selected_guide = if selected == Some(i) { None } else { Some((id.0, i)) },+ // drawing a new guide leaves nothing selected+ (None, None) => {+ app.ui.selected_guide = None;+ app.gesture = Some(Gesture::Guide { a: map.norm(q) });+ }+ }}- if let (Some(Gesture::Guide { a }), Some(q)) = (&app.gesture, resp.interact_pointer_pos()) {+ let q = resp.interact_pointer_pos().or_else(|| ui.input(|i| i.pointer.latest_pos()));+ if let (Some(Gesture::Guide { a }), Some(q)) = (&app.gesture, q) {draw(map.screen(*a), q, false);}- if resp.drag_stopped()- && let (Some(Gesture::Guide { a }), Some(q)) = (app.gesture.clone(), resp.interact_pointer_pos())+ // the endpoint being dragged: the guide follows the pointer, bright+ if let (Some(Gesture::GuideEndpoint { i, end }), Some(q)) = (&app.gesture, q)+ && let Some(guide) = d.geometry.guides.get(*i){- app.gesture = None;- let b = map.norm(q);- if a.dist(b) > 0.02 {- let (ca, cb) = (frame.transformed_to_corrected(a), frame.transformed_to_corrected(b));- let _ = app.run("geometry.guides", json!({"guides": [[ca.x, ca.y, cb.x, cb.y]], "add": true}));+ let fixed = if *end == 0 { guide.1 } else { guide.0 };+ draw(map.screen(frame.corrected_to_transformed(fixed)), q, true);+ }+ if ui.input(|i| i.pointer.any_released()) {+ match app.gesture.take() {+ // the endpoint lands: the guide stays (one undo step) and is selected. A press+ // without a real drag only selects — no no-op undo entry for a click.+ Some(Gesture::GuideEndpoint { i, end }) => {+ app.ui.selected_guide = Some((id.0, i));+ if let (Some(q), Some(guide)) = (q, d.geometry.guides.get(i)) {+ let (old_t, fixed_t) = if end == 0 { (guide.0, guide.1) } else { (guide.1, guide.0) };+ let new_t = frame.transformed_to_corrected(map.norm(q));+ let (a, b) = if end == 0 { (new_t, fixed_t) } else { (fixed_t, new_t) };+ let (na, nb) = (frame.corrected_to_transformed(a), frame.corrected_to_transformed(b));+ let moved = map.screen(frame.corrected_to_transformed(old_t)).distance(q) > 2.0;+ if moved && na.dist(nb) > 0.02 {+ let all: Vec<[f64; 4]> = d+ .geometry+ .guides+ .iter()+ .enumerate()+ .map(|(j, g)| {+ let (x, y) = if j == i { (a, b) } else { *g };+ [x.x, x.y, y.x, y.y]+ })+ .collect();+ let _ = app.run("geometry.guides", json!({"guides": all}));+ }+ }+ }+ Some(Gesture::Guide { a }) => {+ if let Some(q) = q+ && map.norm(q).dist(a) > 0.02+ {+ let b = map.norm(q);+ let (ca, cb) = (frame.transformed_to_corrected(a), frame.transformed_to_corrected(b));+ let _ = app.run("geometry.guides", json!({"guides": [[ca.x, ca.y, cb.x, cb.y]], "add": true}));+ }+ }+ other => app.gesture = other,}}}diff --git a/crates/ui-egui/src/tests_guided_guides.rs b/crates/ui-egui/src/tests_guided_guides.rsindex 65e7be6b9..6d6b8df0d 100644--- a/crates/ui-egui/src/tests_guided_guides.rs+++ b/crates/ui-egui/src/tests_guided_guides.rs@@ -34,7 +34,11 @@ fn guides_len(h: &Headless) -> usize {}fn guide_at(h: &Headless, i: usize) -> egui::Pos2 {- h.app.widgets.iter().rev().find(|(w, _)| *w == format!("uprightGuide:{i}")).map(|(_, r)| r.center()).expect("the guide is on screen")+ guide_rect(h, i).center()+}++fn guide_rect(h: &Headless, i: usize) -> egui::Rect {+ h.app.widgets.iter().rev().find(|(w, _)| *w == format!("uprightGuide:{i}")).map(|(_, r)| *r).expect("the guide is on screen")}/// A click near a guide selects it (Delete has a visible target); the guides stay put.@@ -69,7 +73,7 @@ fn delete_removes_the_selected_guide() {let (a, b) = d.develop.geometry.guides[0];assert!((a.x - 0.2).abs() < 1e-6 && (a.y - 0.3).abs() < 1e-6 && (b.x - 0.8).abs() < 1e-6 && (b.y - 0.3).abs() < 1e-6,- "the first guide survived: {a:?}–{b:?}"+ "the first guide survived: {a:?}–{b:?}");}@@ -123,3 +127,82 @@ fn drawing_a_new_guide_clears_the_selection() {assert_eq!(guides_len(&h), 3, "the drawn guide is added");assert_eq!(h.app.ui.selected_guide, None, "and the click's selection is gone");}++/// A press on an endpoint drags it: the guide follows the pointer — the other end and the other+/// guide untouched — the moved guide ends up selected, and nothing is added (one edit, on release).+#[test]+fn dragging_a_guide_endpoint_moves_it() {+ let mut h = guided();+ assert_eq!(guides_len(&h), 2);+ let id = h.app.session.active().expect("a photo");+ let img = h.app.image_rect.expect("the photo on screen");+ let before = guide_rect(&h, 0);+ let from = before.left_top();+ let to = egui::pos2(from.x + 24.0, from.y + 60.0);+ // ui.pointer speaks normalized image coordinates+ let norm = |p: egui::Pos2| (((p.x - img.left()) / img.width()) as f64, ((p.y - img.top()) / img.height()) as f64);+ let (fx, fy) = norm(from);+ let (tx, ty) = norm(to);+ let r = h.request(+ "ui.pointer",+ json!({"events": [{"kind": "down", "x": fx, "y": fy}, {"kind": "drag", "x": tx, "y": ty}, {"kind": "up", "x": tx, "y": ty}]}),+ T,+ );+ assert_eq!(r["ok"], true, "{r}");+ h.settle(SETTLE);+ assert_eq!(guides_len(&h), 2, "no guide is added");+ let d = h.app.session.catalog.photo(id).expect("the photo");+ let (a, b) = d.develop.geometry.guides[0];+ assert!((a.x - tx).abs() < 1e-3 && (a.y - ty).abs() < 1e-3, "the pressed end followed the pointer: {a:?} vs ({tx}, {ty})");+ assert!((b.x - 0.8).abs() < 1e-6 && (b.y - 0.3).abs() < 1e-6, "the far end stays: {b:?}");+ assert_eq!(h.app.ui.selected_guide, Some((id.0, 0)), "the moved guide is selected");+}++/// A press-release on an endpoint without moving it selects the guide and changes nothing: no+/// coordinate moves and no undo entry appears for a click.+#[test]+fn clicking_an_endpoint_selects_it_without_changing_it() {+ let mut h = guided();+ let id = h.app.session.active().expect("a photo");+ let undo = h.app.session.undo.len();+ let img = h.app.image_rect.expect("the photo on screen");+ let at = guide_rect(&h, 1).left_top();+ let x = ((at.x - img.left()) / img.width()) as f64;+ let y = ((at.y - img.top()) / img.height()) as f64;+ let r = h.request("ui.pointer", json!({"events": [{"kind": "down", "x": x, "y": y}, {"kind": "up", "x": x, "y": y}]}), T);+ assert_eq!(r["ok"], true, "{r}");+ h.settle(SETTLE);+ assert_eq!(h.app.ui.selected_guide, Some((id.0, 1)), "the endpoint's guide is selected");+ assert_eq!(guides_len(&h), 2);+ let d = h.app.session.catalog.photo(id).expect("the photo");+ let (a, b) = d.develop.geometry.guides[1];+ assert!(+ (a.x - 0.2).abs() < 1e-6 && (a.y - 0.7).abs() < 1e-6 && (b.x - 0.8).abs() < 1e-6 && (b.y - 0.7).abs() < 1e-6,+ "the guide is untouched: {a:?}–{b:?}"+ );+ assert_eq!(h.app.session.undo.len(), undo, "a click is not an edit");+}++/// A new guide starts at the press point itself: a press, one far jump, a release — the guide+/// spans exactly those two points (it used to start where the drag threshold was crossed, so the+/// anchor was already at the far end).+#[test]+fn a_new_guide_starts_at_the_press_point() {+ let mut h = guided();+ assert_eq!(guides_len(&h), 2);+ let img = h.app.image_rect.expect("the photo on screen");+ let p1 = egui::pos2(img.left() + 0.35 * img.width(), img.top() + 0.55 * img.height());+ let p2 = egui::pos2(img.left() + 0.6 * img.width(), img.top() + 0.55 * img.height());+ let r = h.request(+ "ui.pointer",+ json!({"events": [{"kind": "down", "x": 0.35, "y": 0.55}, {"kind": "drag", "x": 0.6, "y": 0.55}, {"kind": "up", "x": 0.6, "y": 0.55}]}),+ T,+ );+ assert_eq!(r["ok"], true, "{r}");+ h.settle(SETTLE);+ assert_eq!(guides_len(&h), 3, "the drawn guide is added");+ assert_eq!(h.app.ui.selected_guide, None, "drawing a new guide leaves nothing selected");+ let rect = guide_rect(&h, 2);+ assert!(rect.left_top().distance(p1) < 2.0, "the guide starts at the press point: {rect:?} vs {p1:?}");+ assert!(rect.right_top().distance(p2) < 2.0, "and ends at the release point: {rect:?} vs {p2:?}");+}