Shapes: switching tools during a widget drag puts the shape back

Switching away from the Selection tool or a shape tool during a drag of
a corner, pie or side widget left the drag's interaction open with its
preview in the document. Both tools now cancel the drag on deactivate,
as Escape does, as the Selection tool already did for a blend widget.
This commit is contained in:
dotCooCoo
2026-11-18 01:21:57 -08:00
parent bec193c6bc
commit 255cdcb6ca
3 changed files with 75 additions and 2 deletions
+6 -2
View File
@@ -900,11 +900,15 @@ 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 drag of a blend widget, a corner widget, a pie widget or a
/// polygon's side widget cancels it, as Escape does: its value goes back.
fn deactivate(&mut self, _cx: &ToolContext) -> Vec<Action> {
match std::mem::take(&mut self.state) {
State::BlendWidget(d) if d.adjusting() => vec![Action::Cancel],
State::BlendWidget(_) => vec![],
State::Corner(c) if c.adjusting() => vec![Action::Cancel],
State::Pie(d) if d.adjusting() => vec![Action::Cancel],
State::Sides(d) => d.cancel(),
State::BlendWidget(_) | State::Corner(_) | State::Pie(_) => vec![],
other => {
self.state = other;
vec![]
+6
View File
@@ -831,6 +831,12 @@ impl Tool for ShapeTool {
_ => vec![],
}
}
/// Switching tools during a drag of a corner, pie or side widget cancels it, as Escape does:
/// the shape goes back.
fn deactivate(&mut self, cx: &ToolContext) -> Vec<Action> {
if self.widget.is_some() { self.key(cx, ToolKey::Escape, Mods::default()) } else { vec![] }
}
/// ↑/↓ change a polygon's sides, a star's points or the rounded rectangle's radius while it is
/// drawn; Escape turns a widget or selection drag back.
fn key(&mut self, cx: &ToolContext, key: ToolKey, _mods: Mods) -> Vec<Action> {
+63
View File
@@ -1354,6 +1354,69 @@ fn a_shape_tool_drags_a_polygons_side_widget() {
assert!(matches!(a.last(), Some(Action::Preview(cmd, _)) if cmd == "shape.line"), "{a:?}");
}
/// Switching away from a shape tool or the Selection tool during a drag of a corner, pie or side
/// widget cancels it, as Escape does: the shape goes back. After a press that has not moved there
/// is nothing to put back.
#[test]
fn a_tool_switch_cancels_a_widget_drag() {
use vectorcraft_doc::{Appearance, LiveShape, Node, NodeKind};
let live = |shape: LiveShape| {
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);
(d, s)
};
let xf = Affine::translate((100.0, 100.0));
let rect = live(LiveShape::Rectangle { w: 100.0, h: 100.0, radii: [0.0; 4], kinds: Default::default(), radii_relative: false, xf });
let ellipse = live(LiveShape::Ellipse { w: 200.0, h: 100.0, pie: (0.0, 360.0), xf });
let polygon = live(LiveShape::Polygon {
radius: 80.0,
sides: 5,
xf: Affine::translate((250.0, 250.0)),
radii: vec![],
kinds: vec![],
radii_relative: false,
});
let p = paint();
let side = crate::sidewidget::SideWidget::for_tool(&cx(&polygon.0, &polygon.1, &p)).expect("a side widget").0.at;
// The corner widget at (110, 110), the pie widget 12 px right of (300, 150), the side widget.
let drags = [
(&rect, Point::new(110.0, 110.0), Point::new(130.0, 130.0)),
(&ellipse, Point::new(312.0, 150.0), Point::new(200.0, 20.0)),
(&polygon, side, side + Vec2::new(0.0, 25.0)),
];
for ((d, s), at, to) in drags {
let c = cx(d, s, &p);
let tools: [Box<dyn Tool>; 6] = [
Box::new(crate::select::SelectionTool::default()),
Box::new(ShapeTool::new("rectangle")),
Box::new(ShapeTool::new("roundedRectangle")),
Box::new(ShapeTool::new("ellipse")),
Box::new(ShapeTool::new("polygon")),
Box::new(ShapeTool::new("star")),
];
for mut t in tools {
let id = t.id();
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, at.x, at.y)).is_empty(), "{id} at {at:?}");
let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, to.x, to.y));
assert!(matches!(a.first(), Some(Action::Begin(_))), "{id} at {at:?}: {a:?}");
assert_eq!(t.deactivate(&c), vec![Action::Cancel], "{id} at {at:?}");
assert!(!t.busy() && !t.transforming(), "{id} at {at:?}");
assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Up, to.x, to.y)).is_empty(), "{id} at {at:?}");
t.pointer(&c, &PointerEvent::new(PointerKind::Down, at.x, at.y));
assert!(t.busy(), "{id} at {at:?}");
assert!(t.deactivate(&c).is_empty(), "{id} at {at:?}");
assert!(!t.busy(), "{id} at {at:?}");
}
}
}
/// A double-click on a corner widget of a selected live shape, with a shape tool that shows the
/// widgets, asks for the Transform panel and nothing else: neither click draws, edits or begins an
/// undo step, and the tool stays idle. Line Segment, a plain path, a pie widget, a hidden or