From 2bc8068d31b978295ca5e097722f2bc255cf3243 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 13:46:25 -0800 Subject: [PATCH] Shapes: a side widget drag ends at the release point and cancels on a tool switch Releasing the side widget, with the Selection tool or a shape tool, reads the release position before the count is kept, so a release back at the start leaves the polygon's count with no undo step even when no drag event came at that point. Switching tools during the drag puts the count back, as Escape does. --- crates/tools/src/select.rs | 10 +++++--- crates/tools/src/shape.rs | 17 ++++++++++++- crates/tools/src/shape/tests.rs | 39 +++++++++++++++++++++++++++++ crates/tools/src/sidewidget.rs | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 4 deletions(-) diff --git a/crates/tools/src/select.rs b/crates/tools/src/select.rs index bc86aaf4bc..dbeca76c63 100644 --- a/crates/tools/src/select.rs +++ b/crates/tools/src/select.rs @@ -900,11 +900,13 @@ impl Tool for SelectionTool { out } - /// Switching tools during a blend widget's drag cancels it, as Escape does: its value goes back. + /// Switching tools during a blend widget's or a polygon's side widget's drag cancels it, as + /// Escape does: its value goes back. fn deactivate(&mut self, _cx: &ToolContext) -> Vec { match std::mem::take(&mut self.state) { State::BlendWidget(d) if d.adjusting() => vec![Action::Cancel], State::BlendWidget(_) => vec![], + State::Sides(d) => d.cancel(), other => { self.state = other; vec![] @@ -1136,9 +1138,11 @@ impl Tool for SelectionTool { self.state = State::Sides(d); out } - (PointerKind::Up, State::Sides(d)) => { + (PointerKind::Up, State::Sides(mut d)) => { self.state = State::Idle; - d.finish() + let mut out = d.drag(cx, p); + out.extend(d.finish()); + out } (PointerKind::Drag, State::BlendWidget(mut d)) => { let out = d.drag(cx, p); diff --git a/crates/tools/src/shape.rs b/crates/tools/src/shape.rs index 7d049acc16..fa911bb7d3 100644 --- a/crates/tools/src/shape.rs +++ b/crates/tools/src/shape.rs @@ -492,7 +492,11 @@ impl ShapeTool { PointerKind::Up => match self.widget.take() { Some(WidgetDrag::Corner(c)) => c.finish(), Some(WidgetDrag::Pie(d)) => d.finish(), - Some(WidgetDrag::Sides(d)) => d.finish(), + Some(WidgetDrag::Sides(mut d)) => { + let mut out = d.drag(cx, ev.pos); + out.extend(d.finish()); + out + } None => vec![], }, _ => vec![], @@ -676,6 +680,17 @@ impl Tool for ShapeTool { fn busy(&self) -> bool { self.start.is_some() || self.widget.is_some() || self.selection.is_some() } + /// Switching tools during a polygon's side widget drag cancels it, as Escape does: the count + /// goes back. + fn deactivate(&mut self, _cx: &ToolContext) -> Vec { + match self.widget.take() { + Some(WidgetDrag::Sides(d)) => d.cancel(), + other => { + self.widget = other; + vec![] + } + } + } /// A widget or selection drag takes the pointer; otherwise a press draws, unless it lands on a /// handle of the selection's box, its centre point or the rotate zone just outside its box's /// corner. diff --git a/crates/tools/src/shape/tests.rs b/crates/tools/src/shape/tests.rs index cf47db0767..e910ffc709 100644 --- a/crates/tools/src/shape/tests.rs +++ b/crates/tools/src/shape/tests.rs @@ -1354,6 +1354,45 @@ fn a_shape_tool_drags_a_polygons_side_widget() { assert!(matches!(a.last(), Some(Action::Preview(cmd, _)) if cmd == "shape.line"), "{a:?}"); } +/// With a shape tool, the side widget's release is read where it happens, with or without a drag +/// event there first, and switching tools during the drag puts the count back, as Escape does. +#[test] +fn a_shape_tools_side_widget_drag_ends_at_the_release_and_cancels_on_a_tool_switch() { + use vectorcraft_doc::{Appearance, LiveShape, Node, NodeKind}; + let shape = + LiveShape::Polygon { radius: 80.0, sides: 5, xf: Affine::translate((250.0, 250.0)), radii: vec![], kinds: vec![], radii_relative: false }; + let mut d = vectorcraft_doc::Document::new(500.0, 500.0); + let (l, id) = (d.layers[0].id, d.alloc_id()); + let mut n = Node::path(id, shape.to_path(), Appearance::default_art()); + if let NodeKind::Path { live: slot, .. } = &mut n.kind { + *slot = Some(Box::new(shape)); + } + d.insert(Some(l), 0, n).unwrap(); + let mut s = Selection::default(); + s.add(id); + let p = paint(); + let c = cx(&d, &s, &p); + let (w, _) = crate::sidewidget::SideWidget::for_tool(&c).expect("a side widget"); + let at = |kind: PointerKind, px: f64| PointerEvent::new(kind, w.at.x, w.at.y + px); + let sides = |n: u32| Action::Preview("object.setLiveShape".into(), json!({ "id": id.0, "sides": n })); + let mut t = ShapeTool::new("polygon"); + t.pointer(&c, &at(PointerKind::Down, 0.0)); + assert_eq!(t.pointer(&c, &at(PointerKind::Drag, 12.0)), vec![Action::Begin("Live Shape".into()), sides(6)]); + assert_eq!(t.pointer(&c, &at(PointerKind::Up, 0.0)), vec![sides(5), Action::Cancel]); + assert!(!t.busy()); + t.pointer(&c, &at(PointerKind::Down, 0.0)); + t.pointer(&c, &at(PointerKind::Drag, 12.0)); + assert_eq!(t.pointer(&c, &at(PointerKind::Up, 24.0)), vec![sides(7), Action::Commit]); + t.pointer(&c, &at(PointerKind::Down, 0.0)); + t.pointer(&c, &at(PointerKind::Drag, 12.0)); + assert_eq!(t.deactivate(&c), vec![Action::Cancel]); + assert!(!t.busy() && !t.transforming()); + // A press that changed nothing sends nothing. + t.pointer(&c, &at(PointerKind::Down, 0.0)); + assert!(t.deactivate(&c).is_empty()); + assert!(!t.busy()); +} + /// With Show Shape Widgets off, the shape tools neither show nor take a selected live rectangle's /// corner widget, a live ellipse's pie widget or a live polygon's side widget: the pointer there is /// the tool's own, a drag from there draws a new shape and a double-click asks for nothing. With it diff --git a/crates/tools/src/sidewidget.rs b/crates/tools/src/sidewidget.rs index 581cf2f8d4..75b5504cff 100644 --- a/crates/tools/src/sidewidget.rs +++ b/crates/tools/src/sidewidget.rs @@ -357,4 +357,48 @@ mod tests { assert!(!t.overlays(&c).iter().any(|o| matches!(o, Overlay::Measure { .. }))); assert!(t.pointer(&c, &ev(PointerKind::Up, w.at)).is_empty()); } + + /// Switching tools during a drag puts the count back, as Escape does; a press that changed + /// nothing sends nothing. + #[test] + fn switching_tools_cancels_a_side_widget_drag() { + let (d, _, s) = polygon_doc(5, Affine::translate((300.0, 300.0))); + let p = paint(); + let c = cx(&d, &s, &p); + let (w, _) = SideWidget::for_tool(&c).unwrap(); + let mut t = SelectionTool::default(); + t.pointer(&c, &ev(PointerKind::Down, w.at)); + assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, w.at + vectorcraft_geom::Vec2::new(0.0, 30.0)))), [7]); + assert_eq!(t.deactivate(&c), vec![Action::Cancel]); + assert!(!t.busy() && !t.transforming()); + t.pointer(&c, &ev(PointerKind::Down, w.at)); + assert!(t.pointer(&c, &ev(PointerKind::Drag, w.at + vectorcraft_geom::Vec2::new(0.0, 6.0))).is_empty()); + assert!(t.deactivate(&c).is_empty()); + assert!(!t.busy() && !t.transforming()); + } + + /// The release is read where it happens, with or without a drag event there first: back at the + /// start the polygon keeps its count with no undo step, and further down it takes the new count. + #[test] + fn the_release_position_sets_the_sides() { + let (d, id, s) = polygon_doc(5, Affine::translate((300.0, 300.0))); + let p = paint(); + let c = cx(&d, &s, &p); + let (w, _) = SideWidget::for_tool(&c).unwrap(); + let at = |px: f64| w.at + vectorcraft_geom::Vec2::new(0.0, px); + let mut t = SelectionTool::default(); + t.pointer(&c, &ev(PointerKind::Down, w.at)); + assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, at(12.0)))), [6]); + assert_eq!( + t.pointer(&c, &ev(PointerKind::Up, w.at)), + vec![Action::Preview("object.setLiveShape".into(), json!({ "id": id.0, "sides": 5 })), Action::Cancel] + ); + assert!(!t.busy() && !t.transforming()); + t.pointer(&c, &ev(PointerKind::Down, w.at)); + assert_eq!(sides_of(&t.pointer(&c, &ev(PointerKind::Drag, at(12.0)))), [6]); + assert_eq!( + t.pointer(&c, &ev(PointerKind::Up, at(24.0))), + vec![Action::Preview("object.setLiveShape".into(), json!({ "id": id.0, "sides": 7 })), Action::Commit] + ); + } }