PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Lasso: a short straight segment places a vertex instead of closing the outline
Hamza1f6dbf8415b3
diff --git a/crates/ui-egui/src/lasso_ui.rs b/crates/ui-egui/src/lasso_ui.rsindex 1f3f7adba..0fe904f74 100644--- a/crates/ui-egui/src/lasso_ui.rs+++ b/crates/ui-egui/src/lasso_ui.rs@@ -42,6 +42,11 @@ pub fn active(app: &PhotocraftApp) -> bool {app.drag.as_ref().is_some_and(|d| owns(d.tool) && d.lasso.is_some())}+/// How far from the last vertex a click still counts as that same vertex clicked again (points+/// on screen): a hand's jitter, not an aiming radius -- a vertex placed further away than this is+/// the next vertex of the outline, however short the segment (#7629).+const SAME_POINT: f64 = 3.0;+/// Between polygonal clicks, Backspace/right-click retracts the last fixed vertex./// Do not affect a freehand stroke whose primary button is still held.pub fn waiting_for_vertex(app: &PhotocraftApp) -> bool {@@ -154,13 +159,18 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> boolmatch ev {ToolEvent::Down { .. } => {if active(app) {- // A click on the first point, or on the last one again, closes the outline.- let reach = 8.0 / app.point_zoom().max(0.01) as f64;- let near = |q: [f64; 2]| (q[0] - p[0]).hypot(q[1] - p[1]) < reach;- let close = app.drag.as_ref().is_some_and(|d| d.points.len() >= 3 && (near(d.start) || d.points.last().is_some_and(|l| near([l[0], l[1]]))));+ // A click on the first point, or on the last one again, closes the outline. Aiming+ // at the first point is a grab; clicking the last one again is the same click+ // repeated, so it has only to allow for a pixel of hand jitter. Sharing the grab+ // radius made every segment shorter than it impossible to draw, because the click+ // meant to place the next vertex closed the outline instead (#7629).+ let zoom = app.point_zoom().max(0.01) as f64;+ let near = |q: [f64; 2], reach: f64| (q[0] - p[0]).hypot(q[1] - p[1]) < reach / zoom;+ let again = |d: &Drag| d.points.last().is_some_and(|l| near([l[0], l[1]], SAME_POINT));+ let close = app.drag.as_ref().is_some_and(|d| d.points.len() >= 3 && (near(d.start, 8.0) || again(d)));if close {// A path closes on its start point (a selection outline always closes).- if let Some(d) = app.drag.as_mut().filter(|d| d.tool == Tool::FreeformPen && near(d.start)) {+ if let Some(d) = app.drag.as_mut().filter(|d| d.tool == Tool::FreeformPen && near(d.start, 8.0)) {let (start, n) = (d.start, d.points.len());append(d, start);if let Some(lasso) = d.lasso.as_mut().filter(|_| d.points.len() > n) {diff --git a/crates/ui-egui/src/polygon_lasso_tests.rs b/crates/ui-egui/src/polygon_lasso_tests.rsindex 62975e2d7..6101436a1 100644--- a/crates/ui-egui/src/polygon_lasso_tests.rs+++ b/crates/ui-egui/src/polygon_lasso_tests.rs@@ -138,6 +138,45 @@ fn double_click_closes_the_polygon_after_quickly_placed_vertices() {assert!(selection(&h).is_some_and(|r| near(r, 50, 50, 150, 150)), "{:?}", selection(&h));}+/// The Lasso's ⌥ (straight-segment) mode could not draw a segment shorter than the radius for+/// grabbing the first vertex: the click that should have placed the next vertex counted as the+/// last one clicked again and closed the outline, so a small outline could not be traced at all+/// (#7629). Clicking the same point again still closes it, within a jitter of where it is.+#[test]+fn a_short_alt_lasso_segment_places_a_vertex_instead_of_closing() {+ let mut app = polygon_app();+ app.ui.tool = Tool::Lasso;+ let alt = Modifiers::ALT;+ let click = |app: &mut PhotocraftApp, x: f64, y: f64| {+ ev(app, "down", x, y, alt);+ ev(app, "up", x, y, alt);+ };+ for (x, y) in [(50.0, 50.0), (90.0, 50.0), (90.0, 90.0)] {+ click(&mut app, x, y);+ }+ // 5 document units from the last vertex, inside the 8-unit reach at 100 %.+ click(&mut app, 85.0, 91.0);+ let points = app.drag.as_ref().map(|d| d.points.len());+ assert_eq!(points, Some(4), "the short segment placed a vertex: {:?}", app.drag.as_ref().map(|d| d.points.clone()));+ assert!(app.session.active().unwrap().doc.selection.is_none(), "and the outline is still being drawn");++ // Clicking the last vertex again, within a jitter of it, still closes the outline.+ click(&mut app, 86.0, 92.0);+ assert!(app.drag.is_none(), "the same point clicked again closes it");+ let r = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds());+ assert!(r.is_some_and(|r| near(r, 50, 50, 90, 91)), "{r:?}");++ // And so does a click on the first vertex, which is a grab rather than a repeat.+ let mut app = polygon_app();+ app.ui.tool = Tool::Lasso;+ for (x, y) in [(50.0, 50.0), (90.0, 50.0), (90.0, 90.0), (85.0, 91.0)] {+ click(&mut app, x, y);+ }+ click(&mut app, 54.0, 53.0);+ assert!(app.drag.is_none(), "the first vertex closes it from a few points away");+ assert!(app.session.active().unwrap().doc.selection.is_some());+}+/// Double-clicking on the first vertex: the first click closes the polygon, and the second must/// not start a new one (which would deselect what was just made).#[test]