Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(selection): constrain Polygonal Lasso segments with Shift (#2352) (#2396)

Nigel Ihza Firdausy41d54036fcdf
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 5e5390845..6c31678d8 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3352,7 +3352,17 @@ fn draw_tool_state(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform,
if !app.ui.polygon.is_empty() {
let mut pts: Vec<Pos2> = app.ui.polygon.iter().map(|p| xf.to_screen(p[0] as f32, p[1] as f32)).collect();
if let Some(h) = hover {
- pts.push(h);
+ // The rubber-band preview must use the same constrained point as the click,
+ // including when the canvas is zoomed, rotated or flipped.
+ let mods = painter.ctx().input(|i| i.modifiers);
+ if app.ui.tool == Tool::PolygonLasso && mods.shift && !mods.alt {
+ let raw = xf.to_doc(h);
+ let last = app.ui.polygon.last().copied().unwrap_or(raw);
+ let snapped = constrained_polygon_point(last, raw);
+ pts.push(xf.to_screen(snapped[0] as f32, snapped[1] as f32));
+ } else {
+ pts.push(h);
+ }
}
// Just the outline and its rubber band: the vertices aren't handles to grab.
crate::tool_feedback::draw_ants(painter, &pts, false);
@@ -4216,6 +4226,22 @@ pub(crate) fn selection_mode(app: &PhotocraftApp, m: egui::Modifiers) -> &'stati
crate::tool_feedback::document_selection_mode(app, Tool::Lasso, m)
}
+/// Shift constrains a Polygonal Lasso segment to Photoshop's 45° directions.
+/// Work in document coordinates so zoom, view rotation and mirroring cannot skew it.
+pub(crate) fn constrained_polygon_point(from: [f64; 2], point: [f64; 2]) -> [f64; 2] {
+ let dx = point[0] - from[0];
+ let dy = point[1] - from[1];
+ if !dx.is_finite() || !dy.is_finite() {
+ return point;
+ }
+ let length = dx.hypot(dy);
+ if length == 0.0 {
+ return point;
+ }
+ let angle = (dy.atan2(dx) / std::f64::consts::FRAC_PI_4).round() * std::f64::consts::FRAC_PI_4;
+ [from[0] + length * angle.cos(), from[1] + length * angle.sin()]
+}
+
/// A polygonal lasso click adds a vertex; clicking near the first vertex closes the polygon. The
/// first click fixes the selection mode; a new-selection polygon hides the old outline while it is
/// drawn, and replaces it in one history step when it closes.
@@ -4235,7 +4261,10 @@ fn polygon_click(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers)
let intent = if nothing_selected { egui::Modifiers { alt: false, ..mods } } else { mods };
app.ui.polygon_mode = selection_mode(app, intent).into();
}
- app.ui.polygon.push([x, y]);
+ // Only constrain segments after the first vertex; the first click still sets selection intent.
+ // Alt-drag is freehand and must not snap to an angle.
+ let point = if mods.shift && !mods.alt { app.ui.polygon.last().copied().map_or([x, y], |last| constrained_polygon_point(last, [x, y])) } else { [x, y] };
+ app.ui.polygon.push(point);
}
/// Remove the Polygonal Lasso's last vertex (⌫, Delete or a right-click while drawing, #1229);
diff --git a/crates/ui-egui/src/polygon_lasso_tests.rs b/crates/ui-egui/src/polygon_lasso_tests.rs
index 2dbfd0605..f370073c6 100644
--- a/crates/ui-egui/src/polygon_lasso_tests.rs
+++ b/crates/ui-egui/src/polygon_lasso_tests.rs
@@ -344,3 +344,37 @@ fn alt_drags_draw_freehand_and_releasing_alt_keeps_the_polygon_open() {
crate::canvas::commit_polygon(&mut app);
assert!(app.session.active().unwrap().doc.selection.as_ref().unwrap().sample_channel(110, 100, 0) > 0.5);
}
+
+#[test]
+fn shift_constrains_polygonal_lasso_segments_but_unmodified_clicks_remain_free() {
+ let mut app = polygon_app();
+ ev(&mut app, "down", 100.0, 100.0, Modifiers::NONE);
+ ev(&mut app, "up", 100.0, 100.0, Modifiers::NONE);
+ assert_eq!(app.ui.polygon, vec![[100.0, 100.0]]);
+
+ // Near a diagonal: Shift constrains to exactly 45° from the previous fixed point.
+ ev(&mut app, "down", 140.0, 120.0, Modifiers::SHIFT);
+ ev(&mut app, "up", 140.0, 120.0, Modifiers::SHIFT);
+ let diagonal = app.ui.polygon[1];
+ assert!((diagonal[0] - diagonal[1]).abs() < 1e-8, "45-degree direction: {diagonal:?}");
+ assert!((diagonal[0] - 100.0 - 40.0_f64.hypot(20.0) / 2.0_f64.sqrt()).abs() < 1e-8);
+ // Releasing Shift restores free placement rather than constraining subsequent points.
+ ev(&mut app, "down", 155.0, 175.0, Modifiers::NONE);
+ ev(&mut app, "up", 155.0, 175.0, Modifiers::NONE);
+ assert_eq!(app.ui.polygon[2], [155.0, 175.0]);
+}
+
+#[test]
+fn shift_polygonal_lasso_snap_has_all_eight_directions_and_handles_zero_length() {
+ let origin = [200.0, 150.0];
+ let directions = [[40.0, 1.0], [40.0, 25.0], [1.0, 40.0], [-40.0, 25.0], [-40.0, -1.0], [-40.0, -25.0], [-1.0, -40.0], [40.0, -25.0]];
+ for delta in directions {
+ let end = crate::canvas::constrained_polygon_point(origin, [origin[0] + delta[0], origin[1] + delta[1]]);
+ let dx = end[0] - origin[0];
+ let dy = end[1] - origin[1];
+ let angle = dy.atan2(dx) / std::f64::consts::FRAC_PI_4;
+ assert!((angle - angle.round()).abs() < 1e-9, "{delta:?} → {end:?}");
+ assert!((dx.hypot(dy) - delta[0].hypot(delta[1])).abs() < 1e-8);
+ }
+ assert_eq!(crate::canvas::constrained_polygon_point(origin, origin), origin);
+}