PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #7605: Lasso tools: the outline is solid until the selection is made (#7495)
aiongg77ad0f2d0e3f
diff --cc crates/ui-egui/src/canvas.rsindex 3a6fa065e,84356d9fc..1f2748bd8--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@@ -9065,6 -8893,70 +9072,74 @@@ mod rotation_preview_isolation_tests#[path = "canvas_display_memo_tests.rs"]mod display_memo_tests;+#[cfg(test)]+#[path = "canvas/blend_viewport_tests.rs"]+mod blend_viewport_regression;+++ #[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");+ }+ }