diff --git a/crates/tools/src/circle.rs b/crates/tools/src/circle.rs index 9e467b35d6..72f959bad3 100644 --- a/crates/tools/src/circle.rs +++ b/crates/tools/src/circle.rs @@ -8,7 +8,8 @@ //! the release, the tool shows the circle as a dashed outline (solid once it is more than 50,000 //! screen points around) with its points, its center as a small cross, a line from the center to //! each point and, with Measurement Labels on, the diameter by the pointer. The circle is one undo -//! step. Escape, a press dropped part way, and an edit, Undo or Redo made outside the tool drop +//! step. A press let go within 2 screen points of where it was pressed is a click, also after a +//! drag. Escape, a press dropped part way, and an edit, Undo or Redo made outside the tool drop //! the first point. A second press within 3 screen points //! of the first point, or one that snaps to it, is a press on it: let go there (a double-click) it //! places nothing, and dragged out it draws the circle on the diameter it drags out. Where no @@ -137,10 +138,11 @@ impl CircleTool { vec![Action::Exec(c, v)] } - /// Is the press a drag at `ev`: dragged already, or `ev` is [`DRAG_PX`] or more from the press? + /// Is the press let go at `ev` a drag: is `ev` [`DRAG_PX`] or more from the press? A release + /// nearer the press is a click, also after a drag that came back to it. fn drags(&self, cx: &ToolContext, ev: &PointerEvent) -> bool { match self.press { - Some(Press::First { from, dragged, .. } | Press::Second { from, dragged, .. }) => dragged || ev.pos.distance(from) >= cx.tol(DRAG_PX), + Some(Press::First { from, .. } | Press::Second { from, .. }) => ev.pos.distance(from) >= cx.tol(DRAG_PX), None => false, } } @@ -156,9 +158,16 @@ impl Tool for CircleTool { return vec![]; } // A drag ends where it is let go: the release moves the points as a drag to it would. A - // click, or a release within DRAG_PX of the press, keeps the points of the press. - if ev.kind == PointerKind::Up && self.drags(cx, ev) { - self.pointer(cx, &PointerEvent { kind: PointerKind::Drag, ..*ev }); + // click, or a release within DRAG_PX of the press (also after a drag), keeps the points of + // the press. + if ev.kind == PointerKind::Up { + if self.drags(cx, ev) { + self.pointer(cx, &PointerEvent { kind: PointerKind::Drag, ..*ev }); + } else if let Some(Press::First { at, dragged, .. } | Press::Second { at, dragged, .. }) = &mut self.press { + *dragged = false; + self.pointer = Some(*at); + self.snap.clear(); + } } match ev.kind { PointerKind::Move => { diff --git a/crates/tools/src/circle/tests.rs b/crates/tools/src/circle/tests.rs index e262d242ef..5228403670 100644 --- a/crates/tools/src/circle/tests.rs +++ b/crates/tools/src/circle/tests.rs @@ -120,6 +120,52 @@ fn a_drag_ends_where_it_is_let_go() { assert_eq!(t.pointer(&cx, &ev(up, (381.0, 360.0), Mods::default())), circle(&[(300.0, 300.0), (380.0, 360.0)])); } +/// A release within 2 screen points of the press is a click also after a drag that came back to +/// it: a second press draws the circle on the diameter to the press, a press on the first point +/// places nothing, and a first press places the first point. +#[test] +fn a_release_near_the_press_is_a_click_after_a_drag() { + let (d, _) = doc_with_rect(); + let (s, p) = (Selection::default(), paint()); + let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) }; + let mut t = CircleTool::default(); + click(&mut t, &cx, (300.0, 300.0)); + assert_eq!(drag(&mut t, &cx, (380.0, 300.0), &[(300.0, 360.0), (381.0, 300.0)], Mods::default()), circle(&[(300.0, 300.0), (380.0, 300.0)])); + click(&mut t, &cx, (300.0, 300.0)); + assert_eq!(drag(&mut t, &cx, (300.0, 300.0), &[(300.0, 380.0), (301.0, 300.0)], Mods::default()), vec![]); + assert!(t.ends_on_escape(&cx), "the first point stays"); + assert_eq!(click(&mut t, &cx, (380.0, 360.0)), circle(&[(300.0, 300.0), (380.0, 360.0)])); + assert_eq!(drag(&mut t, &cx, (300.0, 300.0), &[(380.0, 300.0), (301.0, 300.0)], Mods::default()), vec![]); + assert!(t.ends_on_escape(&cx), "the press placed the first point"); + assert_eq!(click(&mut t, &cx, (380.0, 360.0)), circle(&[(300.0, 300.0), (380.0, 360.0)])); + // Let go near the press straight from far away, the tool shows no circle to the last drag. + t.pointer(&cx, &ev(PointerKind::Down, (300.0, 300.0), Mods::default())); + t.pointer(&cx, &ev(PointerKind::Drag, (380.0, 300.0), Mods::default())); + assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (301.0, 300.0), Mods::default())), vec![]); + assert_eq!(preview(&t.overlays(&cx)), None); + t.pointer(&cx, &ev(PointerKind::Down, (300.0, 300.0), Mods::default())); + t.pointer(&cx, &ev(PointerKind::Drag, (300.0, 380.0), Mods::default())); + assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (301.0, 300.0), Mods::default())), vec![]); + assert_eq!(preview(&t.overlays(&cx)), None); + assert!(t.ends_on_escape(&cx)); +} + +/// A press on the first point, dragged to snap to the rectangle's corner anchor and let go near +/// the press, leaves no guides of the drag. +#[test] +fn a_release_near_the_press_hides_the_guides_of_the_drag() { + let (d, _) = doc_with_rect(); + let (s, p) = (Selection::default(), paint()); + let cx = cx(&d, &s, &p); + let mut t = CircleTool::default(); + click(&mut t, &cx, (300.0, 300.0)); + t.pointer(&cx, &ev(PointerKind::Down, (300.0, 300.0), Mods::default())); + t.pointer(&cx, &ev(PointerKind::Drag, (201.0, 201.0), Mods::default())); + assert!(!t.snap.guides().is_empty(), "the drag snapped to the corner anchor"); + assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (301.0, 300.0), Mods::default())), vec![]); + assert!(t.snap.guides().is_empty(), "{:?}", t.snap.guides()); +} + /// Three points on one line show no circle, and the release draws nothing and starts afresh. A /// circle past the canvas is not shown either. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 5bc273289c..912b371def 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6706,6 +6706,7 @@ the release, the tool shows the circle as a dashed outline (solid once it is mor with its points, a small cross at its center, a line from the center to each point and, with Measurement Labels on, the diameter by the pointer. Three different points on one line, or a circle past the canvas, show "Circle too large" by the pointer instead, and the release draws nothing. +A press let go within 2 screen points of where it was pressed is a click, also after a drag that came back to it. Escape, a dropped press, and an edit, Undo or Redo made outside the tool drop the first point. A second press within 3 screen points of the first point, or one that snaps to it, is a press on it: let go there (a double-click), it places no second point, and dragged out, it draws the circle on the diameter it drags out.