diff --git a/crates/engine/src/cmd/addons/pathtools/tests.rs b/crates/engine/src/cmd/addons/pathtools/tests.rs index 7d11eedd1a..8cd83bacfd 100644 --- a/crates/engine/src/cmd/addons/pathtools/tests.rs +++ b/crates/engine/src/cmd/addons/pathtools/tests.rs @@ -7,7 +7,7 @@ use serde_json::{Value, json}; use vectorcraft_doc::{LiveCorners, LiveShape, NodeId}; use vectorcraft_geom::shapes::CornerKind; use vectorcraft_geom::{AnchorKind, PathData, Point, SubPath}; -use vectorcraft_tools::{PointerEvent, PointerKind}; +use vectorcraft_tools::{Action, Overlay, PointerEvent, PointerKind}; use crate::{Session, ViewInfo}; @@ -1055,6 +1055,37 @@ fn the_slide_tool_previews_and_commits_one_step() { assert_eq!(at(&s), Point::new(200.0, 100.0)); } +/// The Tangent Circle tool's release draws the circle and then tells the tool, which forgets the +/// first segment. When the command fails the session does not tell the tool, so the first segment +/// stays picked: the circle shows again over the second segment. The second click draws it in one +/// undo step. +#[test] +fn the_tangent_circle_tool_keeps_its_first_segment_when_drawing_fails() { + let mut s = session(); + line(&mut s, &[(100.0, 100.0), (300.0, 100.0)], false); + line(&mut s, &[(100.0, 140.0), (300.0, 140.0)], false); + let view = ViewInfo { smart_guides: false, ..ViewInfo::default() }; + s.select_tool("tangentCircle", view).unwrap(); + let at = |s: &mut Session, kind, x, y| s.pointer(&PointerEvent::new(kind, x, y), view); + let shown = |s: &mut Session| { + at(s, PointerKind::Move, 200.0, 139.0).unwrap(); + s.overlays(view).iter().any(|o| matches!(o, Overlay::Measure { text, .. } if text.starts_with("R: 20.00"))) + }; + at(&mut s, PointerKind::Down, 150.0, 101.0).unwrap(); + at(&mut s, PointerKind::Up, 150.0, 101.0).unwrap(); + assert!(shown(&mut s), "the first segment is picked"); + // The tool's release with a command that fails: three points on one line. + let n = steps(&s); + let failing = Action::Exec("shape.circleThroughPoints".into(), json!({"points": [[0, 0], [10, 0], [20, 0]]})); + assert!(s.apply_actions(vec![failing, Action::Notify("tangentCircle.drawn".into())]).is_err()); + assert_eq!(steps(&s), n, "nothing drawn"); + assert!(shown(&mut s), "the first segment is still picked"); + at(&mut s, PointerKind::Down, 200.0, 139.0).unwrap(); + at(&mut s, PointerKind::Up, 200.0, 139.0).unwrap(); + assert_eq!(steps(&s), n + 1, "one undo step"); + assert!(!shown(&mut s), "the first segment is forgotten once the circle is drawn"); +} + fn ids(v: &Value) -> Vec { v["ids"].as_array().unwrap().iter().map(|i| NodeId(i.as_u64().unwrap())).collect() } diff --git a/crates/pathops/src/addons/pathtools.rs b/crates/pathops/src/addons/pathtools.rs index 2de83cda42..2b2db31871 100644 --- a/crates/pathops/src/addons/pathtools.rs +++ b/crates/pathops/src/addons/pathtools.rs @@ -36,7 +36,6 @@ pub use tangent_circle::{Circle, MAX_TANGENT_CIRCLE_SEGMENTS, MAX_TANGENT_CIRCLE pub use tangents::{Coincide, MAX_TANGENT_SEGMENTS, Outline, OutlineError, Tangent, TangentKind, common_tangents}; pub use trim::{Span, slice, span_at, spans, without_spans}; -// The Tangent Circle tool's circles on two picked segments. pub use tangent_circle::{SegmentCircle, circles_of_radius, circles_through}; use crate::fit::{end_tangent, start_tangent}; diff --git a/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs b/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs index 979337554d..c130fb5be3 100644 --- a/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs +++ b/crates/pathops/src/addons/pathtools/tangent_circle/tests.rs @@ -608,7 +608,9 @@ fn lines_twice_the_radius_apart_give_circles_all_along() { assert!(xs.iter().any(|x| *x < 1.0) && xs.iter().any(|x| *x > 99.0), "from end to end: {xs:?}"); // At a radius of 1,000,000 pt the places tried along the line, 0.2 pt apart, stay apart. let found = circles_of_radius(line((0.0, 0.0), (100.0, 0.0)), &[line((0.0, 2e6), (100.0, 2e6))], 1e6); - assert_eq!(found.len(), 513, "{}", found.len()); + let xs: Vec = found.iter().map(|c| c.circle.center.x).collect(); + let widest = xs.windows(2).map(|w| w[1] - w[0]).fold(0.0, f64::max); + assert!(xs.iter().any(|x| *x < 1.0) && xs.iter().any(|x| *x > 99.0) && widest < 1.0, "from end to end, none merged: {xs:?}"); } #[test] diff --git a/crates/tools/src/addons/tangent_circle.rs b/crates/tools/src/addons/tangent_circle.rs index f5d3d547e5..fb04f3529b 100644 --- a/crates/tools/src/addons/tangent_circle.rs +++ b/crates/tools/src/addons/tangent_circle.rs @@ -7,12 +7,13 @@ //! pressed during the drag moves it by a tenth of the pointer's movement (held at the press, //! Cmd/Ctrl lends the selection tool as it does for every tool). The segments on either side of the //! second one count with it (the first one excepted), and of the circles found the one touching -//! them nearest the pointer is drawn. While the pointer is over the second segment the circle shows -//! with its center and radii; where there is none, or none that its three points can draw, a note -//! says so. The circle is a new ellipse with the current paint, drawn with -//! `shape.circleThroughPoints` in one undo step, so Align New Objects to Pixel Grid leaves it in -//! place; the first segment stays picked until the circle is drawn. Escape, an edit or Undo outside -//! the tool, or another document forgets the first segment. +//! them nearest the pointer (in a drag, nearest the press) is drawn. While the pointer is over the +//! second segment the circle shows with its center and radii; where there is none, or none that its +//! three points can draw, a note says so. The circle is a new ellipse with the current paint, drawn +//! with `shape.circleThroughPoints` in one undo step, so Align New Objects to Pixel Grid leaves it +//! in place; the first segment stays picked until the circle is drawn, also when drawing fails or the +//! press on the second segment is dropped. Escape, an edit or Undo outside the tool, or another +//! document forgets the first segment. use kurbo::{CubicBez, Shape}; use serde_json::{Value, json}; @@ -58,6 +59,8 @@ struct Press { second: Vec, /// Where the press was. at: Point, + /// Whether Shift was held when the click's circle was last looked for. + shift: bool, /// How far the pointer was from the press last time, and the radius the drag set (none until /// the pointer moves far enough). reach: f64, @@ -69,6 +72,8 @@ pub struct TangentCircleTool { first: Option, hover: Option, press: Option, + /// Whether the press in progress picked the first segment. + picking: bool, /// The circle the second segment gives, and whether one was looked for. found: Option, looked: bool, @@ -82,7 +87,7 @@ type CacheKey = (NodeId, usize, usize, Option); impl Default for TangentCircleTool { fn default() -> Self { - Self { radius: RADIUS, first: None, hover: None, press: None, found: None, looked: false, pointer: Point::ZERO, cache: None } + Self { radius: RADIUS, first: None, hover: None, press: None, picking: false, found: None, looked: false, pointer: Point::ZERO, cache: None } } } @@ -214,11 +219,21 @@ impl TangentCircleTool { /// The pointer at `ev` during a press on the second segment: once it is far enough from the /// press, the radius follows it (Cmd/Ctrl: a tenth of its movement; Shift: round steps). fn drag_to(&mut self, cx: &ToolContext, ev: &PointerEvent) { + let radius = self.radius; let (Some(first), Some(press)) = (&self.first, &mut self.press) else { return }; let reach = ev.pos.distance(press.at); let gear = if ev.mods.cmd { SLOW } else { 1.0 }; let r = match press.radius { - None if reach <= cx.tol(DRAG_PX) => return, + None if reach <= cx.tol(DRAG_PX) => { + // Shift pressed or let go before the drag starts: the click's circle, with or + // without the tool's radius. + if ev.mods.shift != press.shift { + press.shift = ev.mods.shift; + let found = circles(first, &press.second, press.shift.then_some(radius)); + self.found = nearest_to(found, press.at, cx.tol(DRAW_PX)); + } + return; + } None => reach * gear, Some(r) => (r + (reach - press.reach) * gear).max(0.0), }; @@ -232,6 +247,7 @@ impl TangentCircleTool { fn forget(&mut self) { self.first = None; self.press = None; + self.picking = false; self.found = None; self.looked = false; self.cache = None; @@ -259,6 +275,7 @@ impl Tool for TangentCircleTool { let Some(pick) = pick_at(cx, ev.pos) else { return vec![] }; let Some(first) = self.first.clone() else { self.first = Some(pick); + self.picking = true; self.hover = None; self.found = None; self.looked = false; @@ -269,7 +286,7 @@ impl Tool for TangentCircleTool { self.found = nearest_to(found, ev.pos, cx.tol(DRAW_PX)); self.looked = true; self.hover = None; - self.press = Some(Press { second, at: ev.pos, reach: 0.0, radius: None }); + self.press = Some(Press { second, at: ev.pos, shift: ev.mods.shift, reach: 0.0, radius: None }); vec![] } PointerKind::Drag => { @@ -279,6 +296,7 @@ impl Tool for TangentCircleTool { PointerKind::Up => { // The release's own position and keys set the radius of a drag. self.drag_to(cx, ev); + self.picking = false; if self.press.take().is_none() { return vec![]; } @@ -301,6 +319,19 @@ impl Tool for TangentCircleTool { vec![] } + /// A dropped press (a canceled touch, a second finger) undoes only what that press did: a press + /// on the second segment draws nothing and the first segment stays picked, and a press that + /// picked the first segment leaves none picked. + fn drop_press(&mut self, _cx: &ToolContext) -> Vec { + if self.press.take().is_some() { + self.found = None; + self.looked = false; + } else if std::mem::take(&mut self.picking) { + self.forget(); + } + vec![] + } + fn claims_key(&self, _cx: &ToolContext, key: ToolKey) -> bool { key == ToolKey::Escape && self.first.is_some() } @@ -485,12 +516,30 @@ mod tests { let mut t = TangentCircleTool::default(); t.set_option("radius", &json!(10)); assert_eq!(t.options()["radius"], json!(10.0)); + // A value outside 0.01 to 1000000, or not a number, leaves the radius as it is. + for bad in [json!(0), json!(0.005), json!(2e6), json!(-5), json!("12"), Value::Null] { + t.set_option("radius", &bad); + assert_eq!(t.options()["radius"], json!(10.0), "{bad}"); + } // The L's two segments: the circle of radius 10 in its corner. t.pointer(&cx, &ev(PointerKind::Down, 150.0, 300.0)); t.pointer(&cx, &ev(PointerKind::Up, 150.0, 300.0)); t.pointer(&cx, &shifted(PointerKind::Down, 300.0, 350.0)); let released = up(&mut t, &cx, &shifted(PointerKind::Up, 300.0, 350.0)); assert!(close(drawn(&released), (280.0, 300.0, 20.0, 20.0)), "{released:?}"); + // Shift pressed or let go during a click counts at the release: with a radius of 5 no + // circle reaches the line 40 pt away, and without Shift the click's circle is 20 pt. + let mut t = TangentCircleTool::default(); + t.set_option("radius", &json!(5)); + t.pointer(&cx, &ev(PointerKind::Down, 150.0, 101.0)); + t.pointer(&cx, &ev(PointerKind::Up, 150.0, 101.0)); + t.pointer(&cx, &ev(PointerKind::Down, 200.0, 139.0)); + t.pointer(&cx, &shifted(PointerKind::Drag, 200.0, 139.0)); + assert!(t.pointer(&cx, &shifted(PointerKind::Up, 200.0, 139.0)).is_empty(), "nothing drawn"); + assert!(t.first.is_some()); + t.pointer(&cx, &shifted(PointerKind::Down, 200.0, 139.0)); + let released = t.pointer(&cx, &ev(PointerKind::Up, 200.0, 139.0)); + assert!(close(drawn(&released), (130.0, 100.0, 40.0, 40.0)), "{released:?}"); } #[test] @@ -580,6 +629,39 @@ mod tests { assert!(t.first.is_none() && t.overlays(&cx).is_empty()); } + #[test] + fn a_dropped_press_undoes_only_what_it_did() { + let d = doc(); + let (sel, p) = (Selection::default(), paint()); + let cx = cx(&d, &sel, &p); + let mut t = TangentCircleTool::default(); + // A press on the second segment, dropped: nothing drawn, and the first segment stays. + t.pointer(&cx, &ev(PointerKind::Down, 150.0, 101.0)); + t.pointer(&cx, &ev(PointerKind::Up, 150.0, 101.0)); + t.pointer(&cx, &ev(PointerKind::Down, 200.0, 139.0)); + assert!(t.busy()); + assert!(t.drop_press(&cx).is_empty()); + assert!(t.pointer(&cx, &ev(PointerKind::Up, 200.0, 139.0)).is_empty(), "the release that follows draws nothing"); + assert!(t.first.is_some() && !t.busy()); + // The next click there draws the circle. + t.pointer(&cx, &ev(PointerKind::Down, 200.0, 139.0)); + let released = t.pointer(&cx, &ev(PointerKind::Up, 200.0, 139.0)); + assert!(close(drawn(&released), (130.0, 100.0, 40.0, 40.0)), "{released:?}"); + t.notify(&cx, DRAWN); + // A press that picked the first segment, dropped: no segment stays picked. + t.pointer(&cx, &ev(PointerKind::Down, 150.0, 101.0)); + assert!(t.drop_press(&cx).is_empty()); + t.pointer(&cx, &ev(PointerKind::Up, 150.0, 101.0)); + assert!(t.first.is_none()); + // A dropped press that picked nothing leaves the first segment picked. + t.pointer(&cx, &ev(PointerKind::Down, 150.0, 101.0)); + t.pointer(&cx, &ev(PointerKind::Up, 150.0, 101.0)); + t.pointer(&cx, &ev(PointerKind::Down, 50.0, 50.0)); + assert!(t.drop_press(&cx).is_empty()); + t.pointer(&cx, &ev(PointerKind::Up, 50.0, 50.0)); + assert!(t.first.is_some()); + } + #[test] fn a_circle_the_three_points_cannot_draw_keeps_the_first_segment() { // A circle the geometry found, moved to (x, 0) and given radius `r`. diff --git a/docs/mcp.md b/docs/mcp.md index 4ff9770026..391b42a689 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -12931,14 +12931,17 @@ Tools: movement (held at the press, Cmd/Ctrl lends the selection tool, as for every tool). The segments on either side of the second one, on its subpath, count with it (the first segment excepted; the two may be on the same path, and the first segment picked again gives no circle), and of the circles found the one - touching them nearest the pointer is drawn. A circle touches them when they lie outside it with a point on it, and - it never crosses the first segment. The circle is a new ellipse with the current paint, drawn through three of its - points with `shape.circleThroughPoints` so that Align New Objects to Pixel Grid leaves it in place, one undo step. - A circle too small or too large for its three points to give it back in floating point counts as no circle. With - no circle the note shows, a click draws nothing and the first segment stays picked; it also stays when drawing the - circle fails. Escape, an edit, Undo or Redo outside the tool, or another document forgets the first segment. - Option (`tool.setOption`, or a double-click on the tool): `radius` (pt, 0.01 to 1000000, 36; a value outside that - range, or not a number, leaves the radius as it is). + touching them nearest the pointer is drawn (in a drag, nearest the press). A circle touches them when they lie + outside it with a point on it (within a millionth of its radius); it never crosses the first segment, and it + touches the two segments at different points. The circle is a new ellipse with the current paint, drawn through + three of its points with `shape.circleThroughPoints` so that Align New Objects to Pixel Grid leaves it in place, one + undo step. A circle too small or too large for its three points to give it back in floating point counts as no + circle. Shift pressed or let go during a click counts at the release. With no circle the note shows, a click draws + nothing and the first segment stays picked; it also stays when drawing the circle fails or the press on the second + segment is canceled (a canceled touch). Escape, an edit, Undo or Redo outside the tool, or another document forgets + the first segment. + Option (`tool.setOption`, or a double-click on the tool): `radius` (pt, 0.01 to 1000000, 36). The value is not + checked yet: one outside that range, or not a number, leaves the radius as it is. - **Trim at Crossings Tool** (`trimAtCrossings`): removes stretches of paths between the places where they are crossed, on any path that can be edited, selected or not. A click runs `addon.pathtools.trim` with `at`, for the path within 4 screen pixels of the pointer. A drag draws a rectangle and runs it with `rect` on release; once the pointer has diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index d997853399..949c9f7c88 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2700,7 +2700,7 @@ A VectorCraft addition, not a feature of the reference app, so no area score mov ## Path Precision Tools add-on: Tangent Circle Tool (2026-11-17) -A VectorCraft addition, so no area score moves. The Tangent Circle Tool (`tangentCircle`; Object › Plug-ins › Path Precision Tools and the All Tools drawer) draws a circle on two clicked path segments: tangent to the first at the point clicked (its center on the normal there) and touching the second segment or one beside it on its subpath, the touch nearest the pointer winning. Shift gives the circle the tool's radius, and a drag from the second segment sets the radius to the drag's length (Shift: steps of 1, 2 or 5 times a power of 10 pt, at least 4 screen pixels; Cmd/Ctrl: a tenth of the movement); with a radius the circle may touch the first segment anywhere along it. Before the click the circle shows with its center, radii and radius, or a note says there is none. The circles are found by sampling the distance to the second segments along the first segment's normal (or along the first segment, for a radius) and taking the places where it equals the radius (samples, bisection where it crosses, and a search where it only touches). Left out are circles whose nearest point of the second segments is not on the circle (within a millionth of the radius: where the first segment turns back on itself the distance jumps past the radius without a touch), circles that cross the first segment, circles that touch both segments at one point, and circles too small or too large for three of their points to give them back in floating point. The circle is a new ellipse with the current paint, one undo step; the first segment stays picked until it is drawn. Tests: `tangent_circle::tests` in `vectorcraft-pathops` (parallel and crossing lines, a curved second segment, a corner's fillet, lines twice the radius apart, a touch between the places tried, the ends of a line with its handles on its anchors, a circle that crosses the first segment, a first segment that turns back, no circle), `addons::tangent_circle::tests` in `vectorcraft-tools` (two clicks, Shift, a drag with Shift and Cmd/Ctrl, no circle and Escape, a circle its three points cannot draw) and the Path Precision Tools menu, options, drawer and catalog tests in `vectorcraft-ui-egui`. Commands: `docs/mcp.md` › Path Precision Tools add-on. +A VectorCraft addition, so no area score moves. The Tangent Circle Tool (`tangentCircle`; Object › Plug-ins › Path Precision Tools and the All Tools drawer) draws a circle on two clicked path segments: tangent to the first at the point clicked (its center on the normal there) and touching the second segment or one beside it on its subpath, the touch nearest the pointer winning. Shift gives the circle the tool's radius, and a drag from the second segment sets the radius to the drag's length (Shift: steps of 1, 2 or 5 times a power of 10 pt, at least 4 screen pixels; Cmd/Ctrl: a tenth of the movement); with a radius the circle may touch the first segment anywhere along it. Before the click the circle shows with its center, radii and radius, or a note says there is none. The circles are found by sampling the distance to the second segments along the first segment's normal (or along the first segment, for a radius) and taking the places where it equals the radius (samples, bisection where it crosses, and a search where it only touches). Left out are circles whose nearest point of the second segments is not on the circle (within a millionth of the radius: where the first segment turns back on itself the distance jumps past the radius without a touch), circles that cross the first segment, circles that touch both segments at one point, circles without a finite radius above 0, and circles too small or too large for three of their points to give them back in floating point; a circle found twice (the same center, touching the first segment at the same point) is kept once. The circle is a new ellipse with the current paint, one undo step; the first segment stays picked until it is drawn, also when drawing fails or the press is canceled. Tests: `tangent_circle::tests` in `vectorcraft-pathops` (parallel and crossing lines, a curved second segment, a corner's fillet, lines twice the radius apart, a touch between the places tried, the ends of a line with its handles on its anchors, a circle that crosses the first segment, a first segment that turns back, no circle), `addons::tangent_circle::tests` in `vectorcraft-tools` (two clicks, Shift and the radius option, a drag with Shift and Cmd/Ctrl, no circle and Escape, a canceled press, a circle its three points cannot draw), the session test for a failed draw in `vectorcraft-engine` and the Path Precision Tools menu, options, drawer and catalog tests in `vectorcraft-ui-egui`. Commands: `docs/mcp.md` › Path Precision Tools add-on. ## Path Precision Tools add-on: Trim at Crossings (2026-11-13)