diff --git a/crates/engine/src/tests/curvature_circle.rs b/crates/engine/src/tests/curvature_circle.rs index e862a03aaf..028ffa6487 100644 --- a/crates/engine/src/tests/curvature_circle.rs +++ b/crates/engine/src/tests/curvature_circle.rs @@ -176,6 +176,13 @@ fn click(s: &mut Session, mods: Mods, x: f64, y: f64) { } } +/// The path the last `shape.curvatureCircle` drew on. +fn drawn_on(s: &Session) -> NodeId { + let (cmd, p) = s.journal.last().unwrap().clone(); + assert_eq!(cmd, C); + id_of(&p) +} + /// With the Curvature Circle tool, a click on a path draws the circle that matches it there, in /// one undo step; with Shift, on the other side. A click on a straight line or away from every /// path draws nothing. @@ -223,4 +230,27 @@ fn the_next_click_passes_over_the_circle_the_tool_drew() { assert_eq!(object_count(&s), count + 1, "the circle drawn first is within reach again"); let third = *s.doc().unwrap().selection.objects.first().unwrap(); assert!(bounds_are(&s, third, Rect::new(100.0, 200.0, 300.0, 400.0)), "the click drew the first circle's own circle"); + assert_eq!(drawn_on(&s), first, "the click hit the first circle"); +} + +/// After Deselect, the pointer reaches the circle the tool drew again, with no edit in between, +/// and still does when the circle is selected again before the pointer moves. +#[test] +fn after_deselect_the_pointer_reaches_the_circle_the_tool_drew() { + for reselect in [false, true] { + let mut s = session(); + let path = parabola(&mut s); + s.select_tool("curvatureCircle", view()).unwrap(); + click(&mut s, Mods::default(), 200.0, 200.0); + let first = *s.doc().unwrap().selection.objects.first().unwrap(); + assert_eq!(drawn_on(&s), path); + let undo = undo_len(&s); + s.execute("select.none", &json!({})).unwrap(); + if reselect { + s.execute("select.set", &json!({"ids": [first.0]})).unwrap(); + } + assert_eq!(undo_len(&s), undo, "Deselect is not an edit"); + click(&mut s, Mods::default(), 280.0, 236.0); + assert_eq!(drawn_on(&s), first, "the click hit the circle drawn first ({reselect})"); + } } diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs index 7c1af811ed..986899cfd9 100644 --- a/crates/engine/src/tooling.rs +++ b/crates/engine/src/tooling.rs @@ -1548,11 +1548,12 @@ impl Session { } /// Let the active tool finish work a command from outside it ended ([`vectorcraft_tools::Tool::after_command`]): - /// the Type tool and the Eyedropper clear a character range whose text is no longer selected. + /// the Type tool and the Eyedropper clear a character range whose text is no longer selected, + /// and the Curvature Circle tool lets the pointer reach its circles once none is selected. /// After `edit.undo` or `edit.redo` (`cmd`), the tool is told first, so the Type tool puts the /// caret where the step changed the text. pub(crate) fn after_command(&mut self, cmd: Option<&str>) { - if self.in_tool_actions || (!self.tool.wants_text() && self.tool.id() != "eyedropper") { + if self.in_tool_actions || (!self.tool.wants_text() && !matches!(self.tool.id(), "eyedropper" | vectorcraft_tools::curvature_circle::ID)) { return; } if let Some(cmd @ ("edit.undo" | "edit.redo")) = cmd { diff --git a/crates/tools/src/curvature_circle.rs b/crates/tools/src/curvature_circle.rs index b484651181..7124f55cda 100644 --- a/crates/tools/src/curvature_circle.rs +++ b/crates/tools/src/curvature_circle.rs @@ -11,8 +11,9 @@ //! the side the path bends toward; with Shift held at the press, on the other side. On a straight //! stretch, at a cusp, at a corner, or where the circle would reach past the canvas, the pointer //! shows "Straight segment", "Cusp", "Corner" or "Circle too large" instead, and a click draws -//! nothing. The pointer passes over the circles the tool drew until another tool is chosen or an -//! edit, Undo or Redo is made outside the tool. +//! nothing. The pointer passes over the circles the tool drew until the selection holds none of +//! them, another tool is chosen, another document becomes active, or an edit, Undo or Redo is +//! made outside the tool. use std::collections::HashSet; @@ -58,10 +59,24 @@ pub struct CurvatureCircleTool { /// The press in progress, where the pointer was pressed: what a release draws. press: Option<(Hit, bool)>, from: Point, - /// The circles the tool drew since the history last moved outside it. + /// The circles the tool drew since the history last moved outside it or the selection last + /// held none of them. drawn: HashSet, } +impl CurvatureCircleTool { + /// After Deselect or a selection made outside the tool, the selection holds none of the + /// circles the tool drew, and the pointer reaches them again. + /// Returns whether it forgot any. + fn forget_unselected(&mut self, cx: &ToolContext) -> bool { + if self.drawn.is_empty() || cx.selection.objects.iter().any(|id| self.drawn.contains(id)) { + return false; + } + self.drawn.clear(); + true + } +} + /// The nearest point within reach of `p` on the paths a click can pick, guides and the paths in /// `skip` left out; of paths at the same distance, a selected one, then the one on top. fn hit_at(cx: &ToolContext, p: Point, skip: &HashSet) -> Option { @@ -118,6 +133,7 @@ impl Tool for CurvatureCircleTool { if !ev.pos.is_finite() { return vec![]; } + self.forget_unselected(cx); match ev.kind { PointerKind::Move => { self.hover = hit_at(cx, ev.pos, &self.drawn).map(|h| (h, ev.mods.shift)); @@ -185,6 +201,16 @@ impl Tool for CurvatureCircleTool { } } + /// A command from outside the tool (Deselect, a pick in the Layers panel) may leave none of the + /// circles the tool drew selected. The circle shown under the pointer then waits for the next + /// hover. + fn after_command(&mut self, cx: &ToolContext) -> Vec { + if self.forget_unselected(cx) { + self.hover = None; + } + vec![] + } + fn deactivate(&mut self, _cx: &ToolContext) -> Vec { self.hover = None; self.press = None; diff --git a/crates/tools/src/curvature_circle/tests.rs b/crates/tools/src/curvature_circle/tests.rs index d417af58dc..1dccebecc6 100644 --- a/crates/tools/src/curvature_circle/tests.rs +++ b/crates/tools/src/curvature_circle/tests.rs @@ -198,6 +198,27 @@ fn the_next_click_passes_over_the_circle_just_drawn() { assert_eq!(params(&click(&mut t, &cx, (302.0, 200.0), Mods::default()))["id"], json!(drawn.0)); } +/// Once the selection holds none of the circles the tool drew (after Deselect, or a pick of another +/// object), the pointer reaches them again. +#[test] +fn a_selection_without_the_drawn_circles_makes_them_reachable() { + let line = PathData::from_bezpath(&vectorcraft_geom::BezPath::from_svg("M0 450 L400 450").unwrap()); + let d = doc_with(&[circle(), circle(), line]); + let ids: Vec = d.layers[0].children().unwrap().iter().rev().map(|n| n.id).collect(); + let (drawn, under, other) = (ids[0], ids[1], ids[2]); + let p = paint(); + let holding = Selection { objects: vec![drawn], ..Selection::default() }; + let both = Selection { objects: vec![drawn, other], ..Selection::default() }; + let none = Selection::default(); + let picked = Selection { objects: vec![other], ..Selection::default() }; + for (after, want) in [(&holding, under), (&both, under), (&none, drawn), (&picked, drawn)] { + let mut t = CurvatureCircleTool::default(); + t.notify(&ToolContext { smart_guides: false, ..cx(&d, &holding, &p) }, "created"); + let cx = ToolContext { smart_guides: false, ..cx(&d, after, &p) }; + assert_eq!(params(&click(&mut t, &cx, (302.0, 200.0), Mods::default()))["id"], json!(want.0), "{:?}", after.objects); + } +} + /// A press that a second finger or a canceled touch drops draws nothing on the release, and an /// edit made outside the tool drops what the pointer was over. #[test] diff --git a/crates/tools/src/draw2.rs b/crates/tools/src/draw2.rs index 3c663006e5..d5d53d9c65 100644 --- a/crates/tools/src/draw2.rs +++ b/crates/tools/src/draw2.rs @@ -375,6 +375,7 @@ pub(crate) fn candidate_paths(cx: &ToolContext) -> Vec { } }); rest.reverse(); + // The loop skips ids that are already listed. let mut seen: std::collections::HashSet = v.iter().copied().collect(); for id in rest { if seen.insert(id) { diff --git a/docs/mcp.md b/docs/mcp.md index b9c0df2bb0..8e9481583a 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7291,8 +7291,10 @@ it touches and, with Measurement Labels on, its radius by the pointer, followed radius. A click draws the circle, and a press dragged 2 screen points or more, or canceled with Escape, draws nothing; Shift held at the press puts the circle on the other side of the path. On a straight segment, at a cusp, at a corner anchor, or where the circle would reach past the canvas, the pointer shows "Straight segment", "Cusp", "Corner" or -"Circle too large", and a click draws nothing. The pointer passes over the circles the tool drew until another tool -is chosen or an edit, Undo or Redo is made outside the tool. +"Circle too large", and a click draws nothing. The pointer passes over the circles the tool drew until Deselect or a +selection made outside the tool leaves none of them selected, another tool is chosen, another document becomes +active, or an edit, Undo or Redo is made outside the tool. Selecting one of them again does not put it back out of +reach. ```json {"name":"run_command","arguments":{"command":"shape.curvatureCircle","params":{"id":12,"segment":0,"t":0.5}}}