PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Pen: a close-path pointer over the first anchor (#4596) (#4612)
F4LCONf2c52a92c9fd
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 9cf6da73e..1cb3a14cb 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -3670,6 +3670,15 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:paint_bucket_cursor(ui.ctx(), p)}}+ // Over the first anchor, the close-path pointer (#4596), before the pens' own cursors.+ Tool::Pen | Tool::CurvaturePen+ if {+ let [x, y] = xf.to_doc(p);+ crate::vector_ui::closes_path_at(app, tool, x, y)+ } =>+ {+ crate::tool_cursor::close_path(&painter, p)+ }// The pen tools: their own icon with its nib on the point, or the crosshair for// precise Other Cursors (#4639).Tool::Pen | Tool::FreeformPen | Tool::CurvaturePen | Tool::AddAnchorPoint | Tool::DeleteAnchorPoint | Tool::ConvertPoint => {diff --git a/crates/ui-egui/src/input_tests.rs b/crates/ui-egui/src/input_tests.rsindex 52544488a..3754e821f 100644--- a/crates/ui-egui/src/input_tests.rs+++ b/crates/ui-egui/src/input_tests.rs@@ -910,6 +910,35 @@ fn esc_ends_a_pen_path_and_keeps_it() {assert_eq!(h.state().ui.selected_path.as_deref(), Some("work"));}+/// Over the first anchor of a Pen path the canvas shows the close-path pointer (#4596), a bitmap+/// of its own; anywhere else the Pen keeps its crosshair.+#[test]+#[cfg(not(target_arch = "wasm32"))]+fn pen_shows_the_close_path_pointer_over_the_first_anchor() {+ let mut h = harness();+ h.state_mut().ui.tool = crate::state::Tool::Pen;+ h.state_mut().ui.pen = Some(crate::vector_ui::PenPath { knots: vec![[[50.0, 50.0]; 3], [[150.0, 50.0]; 3]], ..Default::default() });+ let screen = |h: &Harness<'static, PhotocraftApp>, x: f32, y: f32| {+ let v = &h.state().ui.views[0];+ let c = h.state().last_canvas_rect.center();+ pos2(c.x + (x - v.center[0]) * v.zoom, c.y + (y - v.center[1]) * v.zoom)+ };+ let pointer = |h: &mut Harness<'static, PhotocraftApp>, x: f32, y: f32| {+ let p = screen(h, x, y);+ h.hover_at(p);+ h.run_steps(2);+ let out = &h.output().platform_output;+ (out.cursor_icon, out.cursor_image.as_ref().map(|i| i.rgba.clone()))+ };+ let close = pointer(&mut h, 50.0, 50.0);+ assert_eq!(close.0, egui::CursorIcon::Crosshair, "the fallback under the bitmap");+ assert!(close.1.is_some(), "a bitmap");+ let elsewhere = pointer(&mut h, 200.0, 200.0);+ assert_ne!(elsewhere.1, close.1, "empty canvas: the plain Pen pointer");+ assert_ne!(pointer(&mut h, 150.0, 50.0).1, close.1, "the last anchor doesn't close");+ assert_eq!(pointer(&mut h, 50.0, 50.0), close, "back on the first anchor");+}+/// Esc with no active operation deselects (#2674), like ⌘D.#[test]fn escape_deselects_when_nothing_else_needs_cancelling() {diff --git a/crates/ui-egui/src/tool_cursor.rs b/crates/ui-egui/src/tool_cursor.rsindex acf037480..20ce1b839 100644--- a/crates/ui-egui/src/tool_cursor.rs+++ b/crates/ui-egui/src/tool_cursor.rs@@ -77,6 +77,16 @@ pub(crate) fn outline(painter: &Painter, at: Pos2, rings: Vec<Vec<Vec2>>, centreshow(painter, at, Shape::Outline { rings: rings.into(), centre })}+/// The Pen's pointer over the anchor a click would close the path on (#4596): the centre cross+/// with a small ring below right, as Photoshop's pen shows a small circle there.+pub(crate) fn close_path(painter: &Painter, at: Pos2) -> CursorIcon {+ outline(painter, at, vec![close_ring()], true)+}++fn close_ring() -> Vec<Vec2> {+ (0..16).map(|i| (i as f32 / 16.0 * std::f32::consts::TAU, vec2(9.0, 9.0))).map(|(a, c)| c + vec2(a.cos(), a.sin()) * 3.5).collect()+}+/// Can this pass show an OS cursor bitmap? eframe's immediate viewports and its web integration/// don't upload cursor images; every desktop root viewport does (winit custom cursors).fn native(ctx: &egui::Context) -> bool {diff --git a/crates/ui-egui/src/tool_cursor/tests.rs b/crates/ui-egui/src/tool_cursor/tests.rsindex 54168f537..5d287901c 100644--- a/crates/ui-egui/src/tool_cursor/tests.rs+++ b/crates/ui-egui/src/tool_cursor/tests.rs@@ -344,6 +344,20 @@ fn painting_preference_and_zoom_change_the_native_cursor_without_changing_pointeassert!(h.output().platform_output.cursor_image.as_ref().unwrap().size[0] > cross_size);}+/// #4596: the close-path pointer is the centre cross with a ring below right.+#[test]+fn close_path_bitmap_has_the_centre_cross_and_a_ring_beside_it() {+ for scale in [1.0, 2.0] {+ let image = rasterize(&Shape::Outline { rings: vec![close_ring()].into(), centre: true }, scale).unwrap();+ let [x, y] = image.hotspot;+ let at = |dx: f32, dy: f32| alpha(&image, x + (dx * scale) as u16, y + (dy * scale) as u16);+ assert!(at(0.0, 0.0) > 230 && at(2.0, 0.0) > 0, "the centre cross");+ assert!(at(12.5, 9.0) > 0 && at(9.0, 12.5) > 0, "the ring around (9, 9)");+ assert_eq!(at(9.0, 9.0), 0, "open in the middle");+ assert_eq!(at(0.0, 9.0), 0, "and apart from the cross");+ }+}+/// A big tip's outline is a thin ring in a large square: only the ring (and the centre mark) is/// evaluated, and the bitmap is exactly what evaluating every pixel gives.#[test]diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rsindex 63d12fb19..5e694ff98 100644--- a/crates/ui-egui/src/vector_ui.rs+++ b/crates/ui-egui/src/vector_ui.rs@@ -187,14 +187,31 @@ fn pen_to_json(pen: &PenPath, closed: bool) -> Value {json!({"subpaths": [{"closed": closed, "knots": pen.knots.iter().enumerate().map(|(i, k)| json!({"anchor": k[0], "in": k[1], "out": k[2], "smooth": !pen.unlinked.contains(&i) && (k[1] != k[0] || k[2] != k[0])})).collect::<Vec<_>>()}]})}+/// Is `(x, y)` on the first anchor of a Pen path that a click there would close (two anchors or+/// more)? `tol` is the anchors' hit radius in document pixels.+fn on_closing_anchor(pen: &PenPath, x: f64, y: f64, tol: f64) -> bool {+ pen.knots.len() >= 2 && pen.knots.first().is_some_and(|k| (k[0][0] - x).hypot(k[0][1] - y) < tol)+}++/// Would a click at `(x, y)` with `tool` close the path being drawn: the Pen over the first+/// anchor, or the Curvature Pen over the first of three anchors or more? The canvas shows the+/// close-path pointer there, as Photoshop does (#4596).+pub fn closes_path_at(app: &PhotocraftApp, tool: Tool, x: f64, y: f64) -> bool {+ let Some(pen) = app.ui.pen.as_ref().filter(|p| !p.dragging && p.moving.is_none()) else { return false };+ match tool {+ Tool::Pen => !pen.curvature && on_closing_anchor(pen, x, y, 6.0 / app.point_zoom().max(0.01) as f64),+ Tool::CurvaturePen => pen.curvature && pen.knots.len() >= 3 && curvature_hit(app, x, y) == Some(0),+ _ => false,+ }+}+/// Pen press: close on the first anchor, reshape the last anchor's outgoing handle,/// or add a new anchor (dragging a new anchor pulls symmetrical handles).pub fn pen_down(app: &mut PhotocraftApp, x: f64, y: f64) {let tol = 6.0 / app.point_zoom().max(0.01) as f64;let pen = app.ui.pen.get_or_insert_with(PenPath::default);if let Some(first) = pen.knots.first().map(|k| k[0])- && pen.knots.len() >= 2- && (first[0] - x).hypot(first[1] - y) < tol+ && on_closing_anchor(pen, x, y, tol){// Pressing the first anchor closes the path on release. Until then a stand-in for it// takes the drag, which pulls out that anchor's handles (close-drag, #3306).@@ -2028,6 +2045,39 @@ mod tests {assert_eq!((wp.subpaths[0].knots[0].anchor.x, wp.subpaths[0].knots[0].anchor.y), (30.0, 25.0));}+ /// #4596: the close-path pointer shows where a click would close the path, and only there:+ /// over the first anchor once the Pen has two anchors (the Curvature Pen three), not while an+ /// anchor is being dragged.+ #[test]+ fn close_path_pointer_shows_where_a_click_closes() {+ let mut app = app();+ app.ui.tool = Tool::Pen;+ assert!(!closes_path_at(&app, Tool::Pen, 20.0, 20.0), "no path yet");+ pen_down(&mut app, 20.0, 20.0);+ pen_up(&mut app);+ assert!(!closes_path_at(&app, Tool::Pen, 20.0, 20.0), "one anchor: a click there doesn't close");+ pen_down(&mut app, 120.0, 20.0);+ assert!(!closes_path_at(&app, Tool::Pen, 20.0, 20.0), "not while dragging the new anchor");+ pen_up(&mut app);+ assert!(closes_path_at(&app, Tool::Pen, 21.0, 22.0), "over the first anchor");+ assert!(!closes_path_at(&app, Tool::Pen, 120.0, 20.0) && !closes_path_at(&app, Tool::Pen, 60.0, 60.0), "the last anchor and empty canvas");+ assert!(!closes_path_at(&app, Tool::CurvaturePen, 20.0, 20.0), "a Pen path isn't the Curvature Pen's");+ pen_down(&mut app, 21.0, 22.0);+ pen_up(&mut app);+ assert!(app.ui.pen.is_none() && !closes_path_at(&app, Tool::Pen, 20.0, 20.0), "the click closed it");++ app.ui.tool = Tool::CurvaturePen;+ for (x, y) in [(20.0, 20.0), (120.0, 20.0)] {+ curvature_down(&mut app, x, y);+ curvature_up(&mut app);+ }+ assert!(!closes_path_at(&app, Tool::CurvaturePen, 20.0, 20.0), "two anchors: the Curvature Pen moves the first");+ curvature_down(&mut app, 120.0, 120.0);+ curvature_up(&mut app);+ assert!(closes_path_at(&app, Tool::CurvaturePen, 20.0, 20.0));+ assert!(!closes_path_at(&app, Tool::CurvaturePen, 120.0, 120.0));+ }+#[test]fn pen_point_undo_preserves_earlier_knots_and_document_history() {let mut app = app();