diff --git a/crates/tools/src/mirror.rs b/crates/tools/src/mirror.rs index 5560fbc321..31253003d9 100644 --- a/crates/tools/src/mirror.rs +++ b/crates/tools/src/mirror.rs @@ -184,4 +184,29 @@ mod tests { spec.kind = RepeatKind::Mirror { angle: f64::NAN, center: Point::ZERO }; assert!(MirrorAxis::of(&doc, &selection, 1.0).is_none()); } + + #[test] + fn a_tool_switch_cancels_a_mirror_axis_drag() { + use crate::{PointerEvent, PointerKind, Tool}; + let (mut doc, id) = doc_with_rect(); + let mut source = doc.node(id).unwrap().clone(); + source.id = doc.alloc_id(); + doc.node_mut(id).unwrap().kind = NodeKind::Repeat(RepeatSpec::mirror(vec![Arc::new(source)], 90.0, 10.0)); + let mut selection = Selection::default(); + selection.set([id]); + let paint = paint(); + let c = cx(&doc, &selection, &paint); + let axis = MirrorAxis::of(&doc, &selection, c.zoom).unwrap(); + let mut t = crate::select::SelectionTool::default(); + assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, axis.center.x, axis.center.y)).is_empty()); + let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, axis.center.x + 20.0, axis.center.y)); + assert!(matches!(a.first(), Some(Action::Begin(l)) if l == "Repeat Options"), "{a:?}"); + assert_eq!(t.deactivate(&c), vec![Action::Cancel]); + assert!(!t.busy()); + // A press that has not moved the axis leaves nothing to cancel. + t.pointer(&c, &PointerEvent::new(PointerKind::Down, axis.center.x, axis.center.y)); + assert!(t.busy()); + assert!(t.deactivate(&c).is_empty()); + assert!(!t.busy()); + } } diff --git a/crates/tools/src/repeat_grid.rs b/crates/tools/src/repeat_grid.rs index 07187a6a5a..eecc6ba0fe 100644 --- a/crates/tools/src/repeat_grid.rs +++ b/crates/tools/src/repeat_grid.rs @@ -392,4 +392,24 @@ mod tests { assert_eq!(cursor(&c, g.rows_at), Some(Cursor::ResizeV)); assert_eq!(cursor(&c, g.corner_at), Some(Cursor::ResizeNwSe)); } + + #[test] + fn a_tool_switch_cancels_a_grid_handle_drag() { + use crate::{PointerEvent, PointerKind, Tool}; + let (doc, selection) = grid(); + let paint = paint(); + let c = cx(&doc, &selection, &paint); + let g = GridHandles::of(&doc, &selection, crate::select::selection_box(&c), c.zoom).unwrap(); + let mut t = crate::select::SelectionTool::default(); + assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, g.rows_at.x, g.rows_at.y)).is_empty()); + let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, g.rows_at.x, g.rows_at.y + 150.0)); + assert!(preview(&a).is_some(), "{a:?}"); + assert_eq!(t.deactivate(&c), vec![Action::Cancel]); + assert!(!t.busy()); + // A press that has not changed the grid leaves nothing to cancel. + t.pointer(&c, &PointerEvent::new(PointerKind::Down, g.rows_at.x, g.rows_at.y)); + assert!(t.busy()); + assert!(t.deactivate(&c).is_empty()); + assert!(!t.busy()); + } } diff --git a/crates/tools/src/repeat_radial.rs b/crates/tools/src/repeat_radial.rs index 42a9f4395f..7304570404 100644 --- a/crates/tools/src/repeat_radial.rs +++ b/crates/tools/src/repeat_radial.rs @@ -681,4 +681,26 @@ mod tests { assert_eq!(preview(&d.drag(h.instances_at + Vec2::new(0.0, 32.0))), Some(&json!({"instances": 9}))); assert!(matches!(d.finish().as_slice(), [Action::Commit])); } + + #[test] + fn a_tool_switch_cancels_a_radial_handle_drag() { + use crate::{PointerEvent, PointerKind, Tool}; + let (doc, selection) = radial(); + let paint = paint(); + let c = cx(&doc, &selection, &paint); + let h = RadialHandles::of(&doc, &selection, crate::select::selection_box(&c), c.zoom).unwrap(); + let out = (h.radius_at - h.center) / 100.0; + let to = h.radius_at + out * 25.0; + let mut t = crate::select::SelectionTool::default(); + assert!(t.pointer(&c, &PointerEvent::new(PointerKind::Down, h.radius_at.x, h.radius_at.y)).is_empty()); + let a = t.pointer(&c, &PointerEvent::new(PointerKind::Drag, to.x, to.y)); + assert_eq!(preview(&a), Some(&json!({"radius": 125.0})), "{a:?}"); + assert_eq!(t.deactivate(&c), vec![Action::Cancel]); + assert!(!t.busy()); + // A press that has not changed the repeat leaves nothing to cancel. + t.pointer(&c, &PointerEvent::new(PointerKind::Down, h.radius_at.x, h.radius_at.y)); + assert!(t.busy()); + assert!(t.deactivate(&c).is_empty()); + assert!(!t.busy()); + } } diff --git a/crates/tools/src/select.rs b/crates/tools/src/select.rs index 34b08bd2c0..b0802595a1 100644 --- a/crates/tools/src/select.rs +++ b/crates/tools/src/select.rs @@ -907,14 +907,18 @@ impl Tool for SelectionTool { out } - /// 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. + /// Switching tools during a drag of a blend widget, a corner widget, a pie widget, a + /// polygon's side widget, a mirror axis, a grid or radial repeat's handles or an Image Trace + /// divider 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::Corner(c) if c.adjusting() => vec![Action::Cancel], State::Pie(d) if d.adjusting() => vec![Action::Cancel], State::Sides(d) => d.cancel(), + State::Mirror(d) => d.cancel(), + State::GridRepeat(d) => d.cancel(), + State::RadialRepeat(d) => d.cancel(), State::TraceSplit(d) => d.cancel(), State::BlendWidget(_) | State::Corner(_) | State::Pie(_) => vec![], other => {