PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Lasso tools: the outline is solid until the selection is made (#7495)
Hector Villarreal308385b0bfa8
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex db11fc730..84356d9fc 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -4787,8 +4787,9 @@ fn draw_tool_state(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform,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);+ // Just the outline and its rubber band (the vertices aren't handles to grab), solid until+ // the selection is made (#7495).+ crate::tool_feedback::draw_solid_path(painter, &pts, false);}crate::crop_straighten::draw(app, painter, |p| xf.to_screen(p[0] as f32, p[1] as f32), hover);crate::blur_pins::draw(app, painter, xf);@@ -5059,7 +5060,13 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View// On pixel centres like the marquee's ants, so a 1 pt line stays crisp wherever the// canvas sits (a pixel edge would blur it to grey).let pts: Vec<Pos2> = pts.into_iter().map(|p| p.round() + vec2(0.5, 0.5)).collect();- crate::tool_feedback::draw_ants(painter, &pts, false);+ // The Lasso's outline is solid until the release makes the selection (#7495); a Patch+ // or Content-Aware Move area keeps its ants.+ if d.tool == Tool::Lasso {+ crate::tool_feedback::draw_solid_path(painter, &pts, false);+ } else {+ crate::tool_feedback::draw_ants(painter, &pts, false);+ }}Tool::Gradient => {let a = xf.to_screen(d.start[0] as f32, d.start[1] as f32);@@ -8885,3 +8892,71 @@ mod rotation_preview_isolation_tests {#[cfg(test)]#[path = "canvas_display_memo_tests.rs"]mod display_memo_tests;++#[cfg(test)]+mod lasso_preview_tests {+ use super::*;++ fn xf() -> ViewXform {+ ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }+ }++ /// What `draw` paints: (black dashes, black solid paths).+ fn painted(app: &mut PhotocraftApp, draw: impl Fn(&mut PhotocraftApp, &egui::Painter)) -> (usize, usize) {+ let ctx = egui::Context::default();+ let mut output = ctx.run_ui(egui::RawInput::default(), |ui| draw(app, ui.painter()));+ output.textures_delta.clear();+ let mut flat = Vec::new();+ for clipped in output.shapes {+ let mut stack = vec![clipped.shape];+ while let Some(shape) = stack.pop() {+ match shape {+ egui::Shape::Vec(v) => stack.extend(v),+ s => flat.push(s),+ }+ }+ }+ let dashes = flat.iter().filter(|s| matches!(s, egui::Shape::LineSegment { stroke, .. } if stroke.color == Color32::BLACK)).count();+ let solid = flat.iter().filter(|s| matches!(s, egui::Shape::Path(p) if p.stroke.color == egui::epaint::ColorMode::Solid(Color32::BLACK))).count();+ (dashes, solid)+ }++ fn document() -> PhotocraftApp {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap();+ app.sync_views();+ app+ }++ /// #7495: as in Photoshop, the Lasso's, Polygonal Lasso's and Magnetic Lasso's outline is a+ /// solid line until the selection is made; a Patch area dragged out keeps its marching ants.+ #[test]+ fn lasso_outlines_are_solid_until_the_selection_is_made() {+ let points = vec![[20.0, 20.0, 1.0], [120.0, 30.0, 1.0], [150.0, 140.0, 1.0], [40.0, 160.0, 1.0]];+ for tool in [Tool::Lasso, Tool::Patch] {+ let mut app = document();+ app.ui.tool = tool;+ app.drag = Some(Drag::new(tool, [20.0, 20.0], points.clone(), egui::Modifiers::NONE, false));+ let (dashes, solid) = painted(&mut app, |app, p| draw_drag_preview(app, p, &xf()));+ if tool == Tool::Lasso {+ assert_eq!((dashes, solid), (0, 1), "{tool:?}: one solid outline, no ants");+ } else {+ assert!(dashes > 10 && solid == 0, "{tool:?}: still marching ants ({dashes} dashes)");+ }+ }++ let mut app = document();+ app.ui.tool = Tool::PolygonLasso;+ app.ui.polygon = vec![[20.0, 20.0], [120.0, 30.0], [150.0, 140.0]];+ let (dashes, solid) = painted(&mut app, |app, p| draw_tool_state(app, p, &xf(), Some(pos2(60.0, 150.0))));+ assert_eq!((dashes, solid), (0, 1), "Polygonal Lasso: its sides and rubber band are one solid line");++ let mut app = document();+ app.ui.tool = Tool::MagneticLasso;+ app.ui.magnetic.path = vec![[20.0, 20.0], [80.0, 25.0], [120.0, 60.0]];+ app.ui.magnetic.anchors = vec![0, 2];+ app.ui.magnetic.live = vec![[120.0, 60.0], [130.0, 100.0]];+ let (dashes, solid) = painted(&mut app, |app, p| draw_tool_state(app, p, &xf(), None));+ assert_eq!((dashes, solid), (0, 1), "Magnetic Lasso: the traced border is solid");+ }+}diff --git a/crates/ui-egui/src/magnetic_lasso_ui.rs b/crates/ui-egui/src/magnetic_lasso_ui.rsindex 90655368f..28ef34321 100644--- a/crates/ui-egui/src/magnetic_lasso_ui.rs+++ b/crates/ui-egui/src/magnetic_lasso_ui.rs@@ -420,7 +420,8 @@ pub fn draw(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, hover:}let screen = |q: &[f64; 2]| xf.to_screen(q[0] as f32, q[1] as f32);let pts: Vec<Pos2> = m.path.iter().chain(m.live.iter().skip(1)).map(screen).collect();- crate::tool_feedback::draw_ants(painter, &pts, false);+ // Solid until the selection is made, as in Photoshop (#7495).+ crate::tool_feedback::draw_solid_path(painter, &pts, false);for p in m.anchors.iter().filter_map(|&i| m.path.get(i)).map(screen) {let r = Rect::from_center_size(p, vec2(5.0, 5.0));painter.rect_filled(r, 0.0, Color32::WHITE);diff --git a/crates/ui-egui/src/tool_feedback.rs b/crates/ui-egui/src/tool_feedback.rsindex 2e66ae07c..58bd14cce 100644--- a/crates/ui-egui/src/tool_feedback.rs+++ b/crates/ui-egui/src/tool_feedback.rs@@ -217,6 +217,20 @@ pub fn draw_ants(painter: &egui::Painter, points: &[Pos2], closed: bool) {painter.extend(ants(points, closed, phase));}+/// A selection outline still being drawn (Lasso, Polygonal Lasso, Magnetic Lasso): a solid line,+/// as Photoshop draws it until the selection is made (#7495). Marching ants are for selections+/// that exist. A thin dark line on a soft light edge: Photoshop's look on light pixels, and still+/// visible on dark ones.+pub fn solid_path(points: &[Pos2], closed: bool) -> Vec<Shape> {+ let make = |s: Stroke| if closed { Shape::closed_line(points.to_vec(), s) } else { Shape::line(points.to_vec(), s) };+ vec![make(Stroke::new(3.0, Color32::from_white_alpha(140))), make(Stroke::new(1.0, Color32::BLACK))]+}++/// Draw [`solid_path`].+pub fn draw_solid_path(painter: &egui::Painter, points: &[Pos2], closed: bool) {+ painter.extend(solid_path(points, closed));+}+/// Polygon approximating the ellipse inscribed in `r` (enough vertices to look smooth).pub fn ellipse_points(r: egui::Rect) -> Vec<Pos2> {let (c, a, b) = (r.center(), r.width() / 2.0, r.height() / 2.0);@@ -294,6 +308,18 @@ mod tests {assert!(badge_glyph(Badge::Intersect, c, 3.0).iter().all(|[a, b]| a.x != b.x && a.y != b.y));}+ /// #7495: an outline still being drawn is one unbroken line (no dashes, nothing marching),+ /// black over a light edge.+ #[test]+ fn solid_path_is_one_unbroken_dark_line_on_a_light_edge() {+ let pts = [pos2(0.0, 0.0), pos2(40.0, 0.0), pos2(40.0, 30.0)];+ for closed in [false, true] {+ let shapes = solid_path(&pts, closed);+ assert!(shapes.iter().all(|s| matches!(s, Shape::Path(p) if p.closed == closed && p.points.len() == 3)), "{shapes:?}");+ assert_eq!(strokes(&shapes), vec![Color32::from_white_alpha(140), Color32::BLACK], "the dark line on top");+ }+ }+#[test]fn ants_use_two_contrasting_colours_and_march() {let pts = [pos2(0.0, 0.0), pos2(40.0, 0.0), pos2(40.0, 30.0)];