Skip to content

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

Lasso: Alt clicks add straight segments, releasing Alt closes the outline (#888)

jason4short01752be81adb
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index cd869e033..630d9f2c7 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2552,11 +2552,8 @@ fn draw_tool_state(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform,
if let Some(h) = hover {
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);
- for p in pts.iter().take(app.ui.polygon.len()) {
- painter.rect_filled(Rect::from_center_size(*p, vec2(5.0, 5.0)), 0.0, Color32::WHITE);
- painter.rect_stroke(Rect::from_center_size(*p, vec2(5.0, 5.0)), 0.0, Stroke::new(1.0, Color32::BLACK), egui::StrokeKind::Outside);
- }
}
if let Some(c) = app.ui.crop_rect {
let r = Rect::from_two_pos(xf.to_screen(c[0] as f32, c[1] as f32), xf.to_screen(c[2] as f32, c[3] as f32));
@@ -3011,6 +3008,14 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
app.live_stroke = if strokes_live(tool) { begin_live_stroke(app) } else { None };
}
ToolEvent::Move { x, y, pressure } => {
+ // Polygonal Lasso: ⌥ held while the button is down draws freehand into the polygon;
+ // releasing ⌥ goes back to straight segments (the polygon stays open).
+ if tool == Tool::PolygonLasso && mods.alt && app.drag.is_none() && !app.ui.polygon.is_empty() {
+ if app.ui.polygon.last().is_none_or(|l| (l[0] - x).hypot(l[1] - y) >= 1.0) {
+ app.ui.polygon.push([x, y]);
+ }
+ return;
+ }
tool_move(app, x, y, pressure, mods);
feed_live_stroke(app);
}
@@ -3268,7 +3273,10 @@ fn polygon_click(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers)
return;
}
if app.ui.polygon.is_empty() {
- app.ui.polygon_mode = selection_mode(app, mods).into();
+ // ⌥ with nothing selected draws freehand; there is nothing to subtract from.
+ let nothing_selected = app.session.active().is_none_or(|st| st.doc.selection.is_none());
+ 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]);
}
diff --git a/crates/ui-egui/src/lasso_ui.rs b/crates/ui-egui/src/lasso_ui.rs
index 2328f748b..369267a97 100644
--- a/crates/ui-egui/src/lasso_ui.rs
+++ b/crates/ui-egui/src/lasso_ui.rs
@@ -1,5 +1,9 @@
-//! One lasso outline can alternate between freehand strokes and Alt-held straight segments.
-//! Selection intent is latched at the first press; Alt pressed later changes drawing mode.
+//! One lasso outline can alternate between freehand strokes and Alt-held straight segments:
+//! with Alt held, each click adds a straight segment and a drag draws freehand. Releasing Alt
+//! while the button is up closes the outline and makes the selection.
+//! Selection intent is latched at the first press: Alt held then subtracts from an existing
+//! selection; with no selection there is nothing to subtract, so it starts straight segments.
+//! Alt pressed later changes drawing mode.
use crate::{
PhotocraftApp,
@@ -71,15 +75,18 @@ fn modifiers(app: &mut PhotocraftApp, mods: Modifiers) {
d.track(mods);
// Alt held before the initial press still means subtract, until released and pressed again.
let polygonal = mods.alt && (!d.modifiers.alt || d.released.alt);
- if let Some(lasso) = &d.lasso {
- let anchor = (lasso.polygonal && !polygonal && lasso.down).then_some(lasso.cursor);
- if let Some(p) = anchor {
- append(d, p);
- }
+ // Alt released between straight segments (button up): the outline is done.
+ let close = d.lasso.as_ref().is_some_and(|l| l.polygonal && !polygonal && !l.down);
+ let anchor = d.lasso.as_ref().and_then(|l| (l.polygonal && !polygonal && l.down).then_some(l.cursor));
+ if let Some(p) = anchor {
+ append(d, p);
}
if let Some(lasso) = &mut d.lasso {
lasso.polygonal = polygonal;
}
+ if close {
+ commit(app);
+ }
}
pub fn commit(app: &mut PhotocraftApp) {
@@ -114,10 +121,10 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
match ev {
ToolEvent::Down { .. } => {
if active(app) {
- let close = app
- .drag
- .as_ref()
- .is_some_and(|d| d.points.len() >= 3 && (d.start[0] - p[0]).hypot(d.start[1] - p[1]) < 8.0 / app.current_zoom().max(0.01) as f64);
+ // A click on the first point, or on the last one again, closes the outline.
+ let reach = 8.0 / app.current_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]]))));
if close {
commit(app);
return true;
@@ -130,8 +137,13 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
}
}
} else {
- let mut d = Drag::new(Tool::Lasso, p, vec![[p[0], p[1], 1.0]], mods, false);
- d.lasso = Some(Lasso { cursor: p, down: true, polygonal: false, document: app.session.active().map(|st| st.doc.id) });
+ // Alt with no selection has nothing to subtract from: the outline starts with
+ // straight segments, as a new selection.
+ let nothing_selected = app.session.active().is_none_or(|st| st.doc.selection.is_none());
+ let polygonal = mods.alt && nothing_selected;
+ let intent = if polygonal { Modifiers { alt: false, ..mods } } else { mods };
+ let mut d = Drag::new(Tool::Lasso, p, vec![[p[0], p[1], 1.0]], intent, false);
+ d.lasso = Some(Lasso { cursor: p, down: true, polygonal, document: app.session.active().map(|st| st.doc.id) });
app.drag = Some(d);
}
}
@@ -144,7 +156,9 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
// Move relative to the rubber-band endpoint, not the last fixed vertex.
let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]);
d.shift_to([last[0] + p[0] - previous[0], last[1] + p[1] - previous[1]]);
- } else if down && (!polygonal || up) {
+ } else if down {
+ // While the button is down the outline follows the pointer (freehand), Alt or
+ // not; between Alt clicks only the rubber band moves.
append(d, p);
}
if let Some(lasso) = &mut d.lasso {
diff --git a/crates/ui-egui/src/lasso_ui/tests.rs b/crates/ui-egui/src/lasso_ui/tests.rs
index 97480f6b3..0e2d03f4a 100644
--- a/crates/ui-egui/src/lasso_ui/tests.rs
+++ b/crates/ui-egui/src/lasso_ui/tests.rs
@@ -26,24 +26,25 @@ fn begin(app: &mut PhotocraftApp, mods: Modifiers) {
}
#[test]
-fn alt_clicks_preserve_outline_and_hover_does_not_draw() {
+fn alt_clicks_add_straight_segments_and_releasing_alt_closes_the_outline() {
let mut app = app();
begin(&mut app, Modifiers::NONE);
event(&mut app, "up", 120.0, 50.0, Modifiers::ALT);
- assert!(app.session.active().unwrap().doc.selection.is_none());
+ assert!(app.session.active().unwrap().doc.selection.is_none(), "Alt held at the release keeps it open");
+ // Hover with Alt only moves the rubber band.
event(&mut app, "move", 170.0, 10.0, Modifiers::ALT);
event(&mut app, "move", 180.0, 100.0, Modifiers::ALT);
assert_eq!(app.drag.as_ref().unwrap().points.len(), 3);
+ // An Alt click adds a straight segment to it.
event(&mut app, "down", 180.0, 100.0, Modifiers::ALT);
event(&mut app, "up", 180.0, 100.0, Modifiers::ALT);
- assert_eq!(app.drag.as_ref().unwrap().points.len(), 4);
+ event(&mut app, "move", 60.0, 140.0, Modifiers::ALT);
+ event(&mut app, "down", 60.0, 140.0, Modifiers::ALT);
+ event(&mut app, "up", 60.0, 140.0, Modifiers::ALT);
+ assert_eq!(app.drag.as_ref().unwrap().points.len(), 5);
assert_eq!(crate::tool_feedback::badge(&app, Tool::Lasso, Modifiers::ALT), None);
- // Release Alt between clicks, then continue a freehand stroke in the same outline.
- event(&mut app, "move", 140.0, 150.0, Modifiers::NONE);
- assert_eq!(app.drag.as_ref().unwrap().points.len(), 4);
- event(&mut app, "down", 130.0, 140.0, Modifiers::NONE);
- event(&mut app, "move", 90.0, 130.0, Modifiers::NONE);
- event(&mut app, "up", 50.0, 120.0, Modifiers::NONE);
+ // Releasing Alt with the button up closes the outline and makes the selection.
+ event(&mut app, "move", 70.0, 140.0, Modifiers::NONE);
assert!(!active(&app));
let sel = app.session.active().unwrap().doc.selection.as_ref().unwrap();
assert!(sel.sample_channel(100, 80, 0) > 0.9);
@@ -53,18 +54,44 @@ fn alt_clicks_preserve_outline_and_hover_does_not_draw() {
}
#[test]
-fn switching_back_while_pressed_fixes_the_straight_endpoint() {
+fn a_drag_with_alt_held_draws_freehand() {
let mut app = app();
begin(&mut app, Modifiers::NONE);
+ // Alt pressed during the drag: still freehand while the button is down.
event(&mut app, "move", 180.0, 100.0, Modifiers::ALT);
- assert_eq!(app.drag.as_ref().unwrap().points.len(), 3);
- event(&mut app, "move", 170.0, 130.0, Modifiers::NONE);
+ event(&mut app, "move", 170.0, 130.0, Modifiers::ALT);
let pts = &app.drag.as_ref().unwrap().points;
assert_eq!(&pts[3..], &[[180.0, 100.0, 1.0], [170.0, 130.0, 1.0]]);
- event(&mut app, "up", 50.0, 120.0, Modifiers::NONE);
+ // Released with Alt held: the outline stays open for straight segments...
+ event(&mut app, "up", 170.0, 130.0, Modifiers::ALT);
+ assert!(active(&app));
+ // ...and an Alt drag adds a freehand stretch to it.
+ event(&mut app, "down", 120.0, 150.0, Modifiers::ALT);
+ event(&mut app, "move", 90.0, 155.0, Modifiers::ALT);
+ event(&mut app, "move", 60.0, 140.0, Modifiers::ALT);
+ event(&mut app, "up", 60.0, 140.0, Modifiers::ALT);
+ assert_eq!(app.drag.as_ref().unwrap().points.len(), 8);
+ event(&mut app, "move", 60.0, 140.0, Modifiers::NONE);
assert!(app.session.active().unwrap().doc.selection.is_some());
}
+#[test]
+fn alt_at_the_first_press_without_a_selection_starts_straight_segments() {
+ let mut app = app();
+ assert_eq!(crate::tool_feedback::badge(&app, Tool::Lasso, Modifiers::ALT), None, "no − badge: nothing to subtract");
+ // Alt-click three corners of a triangle, then let go of Alt.
+ for [x, y] in [[50.0, 50.0], [250.0, 50.0], [150.0, 200.0]] {
+ event(&mut app, "down", x, y, Modifiers::ALT);
+ event(&mut app, "up", x, y, Modifiers::ALT);
+ assert!(active(&app), "Alt held: the outline stays open");
+ }
+ event(&mut app, "move", 150.0, 200.0, Modifiers::NONE);
+ assert!(!active(&app));
+ let sel = app.session.active().unwrap().doc.selection.as_ref().unwrap();
+ assert!(sel.sample_channel(150, 100, 0) > 0.9, "a new selection, nothing subtracted");
+ assert!(sel.sample_channel(60, 180, 0) < 0.1);
+}
+
#[test]
fn initial_alt_still_subtracts_and_initial_shift_still_adds() {
for initial in [Modifiers::ALT, Modifiers::SHIFT, Modifiers::SHIFT | Modifiers::ALT] {
@@ -250,3 +277,17 @@ fn real_canvas_backspace_and_right_click_retract_polygonal_points() {
assert!(h.state().session.active().unwrap().doc.layer(layer).is_some());
assert!(h.state().session.active().unwrap().doc.selection.is_none());
}
+
+#[test]
+fn clicking_the_last_point_again_closes_the_outline() {
+ let mut app = app();
+ for [x, y] in [[50.0, 50.0], [250.0, 50.0], [150.0, 200.0]] {
+ event(&mut app, "down", x, y, Modifiers::ALT);
+ event(&mut app, "up", x, y, Modifiers::ALT);
+ }
+ assert!(active(&app));
+ // Alt still held: a second click on the last point (however long after) closes it.
+ event(&mut app, "down", 151.0, 199.0, Modifiers::ALT);
+ assert!(!active(&app));
+ assert!(app.session.active().unwrap().doc.selection.as_ref().unwrap().sample_channel(150, 100, 0) > 0.9);
+}
diff --git a/crates/ui-egui/src/polygon_lasso_tests.rs b/crates/ui-egui/src/polygon_lasso_tests.rs
index 519c098c2..aac2df9ff 100644
--- a/crates/ui-egui/src/polygon_lasso_tests.rs
+++ b/crates/ui-egui/src/polygon_lasso_tests.rs
@@ -285,3 +285,24 @@ fn cmd_drag_inside_the_selection_floats_it_with_the_polygonal_lasso() {
ev(&mut app, "up", 35.0, 20.0, Modifiers::NONE);
assert_eq!(app.ui.polygon.len(), 1);
}
+
+/// ⌥ held: a drag draws freehand into the polygon; releasing ⌥ goes back to straight segments,
+/// and the polygon stays open (a new selection, even though ⌥ was held at the first click).
+#[test]
+fn alt_drags_draw_freehand_and_releasing_alt_keeps_the_polygon_open() {
+ let mut app = polygon_app();
+ let alt = Modifiers::ALT;
+ ev(&mut app, "down", 50.0, 50.0, alt);
+ for x in [70.0, 90.0, 110.0, 130.0, 150.0] {
+ ev(&mut app, "move", x, 50.0 + (x - 50.0) / 4.0, alt);
+ }
+ ev(&mut app, "up", 150.0, 75.0, alt);
+ assert!(app.ui.polygon.len() >= 6, "freehand points: {}", app.ui.polygon.len());
+ assert_eq!(app.ui.polygon_mode, "replace", "⌥ with nothing selected doesn't subtract");
+ // ⌥ released: clicks add straight segments, nothing closes.
+ ev(&mut app, "down", 150.0, 200.0, Modifiers::NONE);
+ ev(&mut app, "up", 150.0, 200.0, Modifiers::NONE);
+ assert!(!app.ui.polygon.is_empty() && app.session.active().unwrap().doc.selection.is_none());
+ crate::canvas::commit_polygon(&mut app);
+ assert!(app.session.active().unwrap().doc.selection.as_ref().unwrap().sample_channel(110, 100, 0) > 0.5);
+}
diff --git a/crates/ui-egui/src/tool_feedback.rs b/crates/ui-egui/src/tool_feedback.rs
index b25c60d6e..e1a5be3ec 100644
--- a/crates/ui-egui/src/tool_feedback.rs
+++ b/crates/ui-egui/src/tool_feedback.rs
@@ -91,7 +91,11 @@ pub fn badge(app: &PhotocraftApp, tool: Tool, m: egui::Modifiers) -> Option<Badg
if m.command && app.drag.is_none() {
return None;
}
- let m = app.drag.as_ref().filter(|d| d.tool == Tool::Lasso && tool == Tool::Lasso).map_or(m, |d| d.modifiers);
+ let mut m = app.drag.as_ref().filter(|d| d.tool == Tool::Lasso && tool == Tool::Lasso).map_or(m, |d| d.modifiers);
+ // The Lasso's Alt with nothing selected draws straight segments; it subtracts nothing.
+ if matches!(tool, Tool::Lasso | Tool::PolygonLasso) && app.drag.is_none() && app.session.active().is_none_or(|st| st.doc.selection.is_none()) {
+ m.alt = false;
+ }
Badge::from_mode(selection_mode(tool, app.ui.selection_mode, m))
}
@@ -212,12 +216,18 @@ mod tests {
let none = egui::Modifiers::NONE;
let (shift, alt) = (egui::Modifiers::SHIFT, egui::Modifiers::ALT);
let both = egui::Modifiers { shift: true, alt: true, ..Default::default() };
+ app.run("file.new", serde_json::json!({"width": 40, "height": 30})).unwrap();
+ app.run("select.rect", serde_json::json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap();
for t in [Tool::RectMarquee, Tool::EllipseMarquee, Tool::Lasso, Tool::PolygonLasso, Tool::MagicWand] {
assert_eq!(badge(&app, t, none), None, "{t:?}: New selection has no badge");
assert_eq!(badge(&app, t, shift), Some(Badge::Add));
assert_eq!(badge(&app, t, alt), Some(Badge::Subtract));
assert_eq!(badge(&app, t, both), Some(Badge::Intersect));
}
+ // With nothing selected the Lasso's Alt draws straight segments: no − badge.
+ app.run("select.deselect", serde_json::json!({})).unwrap();
+ assert_eq!(badge(&app, Tool::Lasso, alt), None);
+ assert_eq!(badge(&app, Tool::RectMarquee, alt), Some(Badge::Subtract));
// The options bar's mode shows without any key held; keys still override it.
app.ui.selection_mode = 2;
assert_eq!(badge(&app, Tool::Lasso, none), Some(Badge::Subtract));