diff --git a/crates/draw/src/ui/pen/bezier/mod.rs b/crates/draw/src/ui/pen/bezier/mod.rs index 1866cb9ba..e80b884d2 100644 --- a/crates/draw/src/ui/pen/bezier/mod.rs +++ b/crates/draw/src/ui/pen/bezier/mod.rs @@ -14,8 +14,8 @@ mod words; use craft_behavior::geometry::{SelectionOperation, axis_or_diagonal}; use craft_behavior::kurbo::{Point, Rect as DocRect, Vec2}; use craft_behavior::{ - Axis, CanvasView, CursorPriority, EditCommand, GesturePhase, Granularity, HitLevel, HitPriority, HitShape, Intent, MoveMods, NavMove, NavShape, - PartKey, PartRef, PointerButton, PressPhase, PressStart, Roving, Sense, + Axis, CancelReason, CanvasView, CursorPriority, EditCommand, GesturePhase, Granularity, HitLevel, HitPriority, HitShape, Intent, MoveMods, NavMove, NavShape, + PartKey, PartRef, PointerButton, PressCancel, PressPhase, PressStart, Roving, Sense, }; use craft_keymap::{Chord, Key, Mods}; use craft_theme::Srgba; @@ -257,6 +257,8 @@ struct Held { start: Vec, /// Past the click slop: the gesture began. began: bool, + /// The tool the press began under: the press stays that tool's until it ends, whatever the host's tool becomes. + tool: BezierTool, } /// Where a press is in a pass: the view and snap it maps through, the press, the pointer and the modifiers held. @@ -398,6 +400,10 @@ pub struct BezierOverlay<'a> { /// thickness replaces the line width (and the selected segment grows by the same amount). #[prop] path_options: Option<&'a PathOptionsSettings>, + /// The host ended what the open press was editing (its target changed): the press is cancelled in this pass and + /// reports nothing more. + #[prop] + cancelled: bool, report: Report, marquee: Option, hover_mark: Option, @@ -415,6 +421,7 @@ impl<'a> BezierOverlay<'a> { anchors: AnchorsShown::default(), segment_mark: SegmentMark::default(), path_options: None, + cancelled: false, report: Report::default(), marquee: None, hover_mark: None, @@ -776,7 +783,7 @@ impl<'a> BezierOverlay<'a> { tools::anchor_click(self.tool, hit, press.mods, self.selection, &mut self.report); *cx.press_slot::() = None; } else { - *cx.press_slot::() = Some(Held { origin: start.origin, target, start: paths.to_vec(), began: false }); + *cx.press_slot::() = Some(Held { origin: start.origin, target, start: paths.to_vec(), began: false, tool: self.tool }); } } PressPhase::DragStart { start, pos, .. } | PressPhase::Drag { start, pos, .. } if start.button == PointerButton::Primary => { @@ -796,7 +803,7 @@ impl<'a> BezierOverlay<'a> { match press.phase { PressPhase::Down { start, .. } if start.button == PointerButton::Primary => { self.report.pressed_empty = true; - *cx.press_slot::() = Some(Held { origin: start.origin, target: Target::Marquee, start: Vec::new(), began: false }); + *cx.press_slot::() = Some(Held { origin: start.origin, target: Target::Marquee, start: Vec::new(), began: false, tool: self.tool }); } PressPhase::DragStart { start, pos, .. } | PressPhase::Drag { start, pos, .. } if start.button == PointerButton::Primary => { let first = matches!(press.phase, PressPhase::DragStart { .. }); @@ -825,9 +832,9 @@ impl<'a> BezierOverlay<'a> { (Some(held), _) => held, (None, Some(item)) => { let target = self.press_target(item.hit(paths, view, start.origin), start, mods, paths); - Held { origin: start.origin, target, start: paths.to_vec(), began: false } + Held { origin: start.origin, target, start: paths.to_vec(), began: false, tool: self.tool } } - (None, None) => Held { origin: start.origin, target: Target::Marquee, start: Vec::new(), began: false }, + (None, None) => Held { origin: start.origin, target: Target::Marquee, start: Vec::new(), began: false, tool: self.tool }, }; let moves = !matches!(held.target, Target::Nothing | Target::Toggle { .. } | Target::Pick { .. }); if first && moves { @@ -1187,8 +1194,16 @@ impl Component for BezierOverlay<'_> { fn update(&mut self, cx: &mut Cx<'_, Self>) { let Some(space) = canvas_space(&cx.env) else { return }; let Some(paths) = cx.value().cloned() else { return }; + if let Some(held) = cx.press_slot::().as_ref() { + self.tool = held.tool; + } let scene = self.scene(&paths, &space.view, cx.rect, cx.style.reach); - self.run_pointer(cx, &space, &scene, &paths); + if self.cancelled && cx.press_slot::().take().is_some() { + cx.cancel(CancelReason::HostChanged); + cx.cancel_press(PressCancel::Disabled); + } else { + self.run_pointer(cx, &space, &scene, &paths); + } self.run_keys(cx, &space, &scene, &paths); } diff --git a/crates/draw/tests/it/bezier.rs b/crates/draw/tests/it/bezier.rs index 944b54d39..327f7204b 100644 --- a/crates/draw/tests/it/bezier.rs +++ b/crates/draw/tests/it/bezier.rs @@ -174,6 +174,8 @@ struct State { tool_clicks: usize, /// The anchor tool the canvas runs under the overlay, if any. canvas_tool: Option, + /// The host ended what the open press was editing. + cancelled: bool, /// Passes in which that tool ran a gesture. tool_gestures: usize, } @@ -239,6 +241,7 @@ fn harness_with(paths: Vec, pans: bool) -> Harness { lone_selects: 0, tool_clicks: 0, canvas_tool: None, + cancelled: false, tool_gestures: 0, })); let shown = Rc::clone(&state); @@ -270,6 +273,7 @@ fn harness_with(paths: Vec, pans: bool) -> Harness { .handles(st.handles) .anchors(st.anchors) .segment_mark(st.segment_mark) + .cancelled(st.cancelled) .show_at(ui, body.rect) }); if canvas.output.click_at().is_some() { @@ -871,3 +875,37 @@ fn arrow_keys_nudge_the_selected_anchors_and_shift_nudges_farther() { assert!(far.x > small.x, "{small:?} {far:?}"); assert_eq!(h.state.borrow().changes.len(), 2, "one edit per press"); } + +// A press belongs to the tool it began under: switching the host's tool in the middle of a drag leaves the drag its +// own, and it commits as that tool's edit. +#[trial] +fn a_press_keeps_the_tool_it_began_under_across_a_tool_switch() { + let mut h = two_paths(); + let anchor = h.screen(p(160.0, 60.0)); + h.press_at(anchor); + h.move_to(anchor + vec2(FAR * 0.5, FAR * 0.5)); + h.update(|st| st.tool = BezierTool::Anchors); + h.move_to(anchor + vec2(FAR, FAR)); + h.release_at(anchor + vec2(FAR, FAR)); + let st = h.state.borrow(); + assert_eq!(st.seen.last(), Some(&Seen::Commit), "{:?}", st.seen); + assert!(matches!(st.changes.last(), Some(PathEdit::MoveAnchors { path: 1, .. })), "{:?}", st.changes); +} + +// A press the host cancels reports nothing more: no commit, no one-shot edit from its release, and the paths stay. +#[trial] +fn a_cancelled_press_reports_nothing_on_release() { + let mut h = two_paths(); + let before = h.state.borrow().paths.clone(); + let anchor = h.screen(p(160.0, 60.0)); + h.press_at(anchor); + h.move_to(anchor + vec2(FAR * 0.5, FAR * 0.5)); + h.update(|st| st.cancelled = true); + h.update(|st| st.cancelled = false); + h.move_to(anchor + vec2(FAR, FAR)); + h.release_at(anchor + vec2(FAR, FAR)); + let st = h.state.borrow(); + assert!(st.seen.contains(&Seen::Cancel), "{:?}", st.seen); + assert!(!st.seen.contains(&Seen::Commit) && !st.seen.contains(&Seen::Set), "{:?}", st.seen); + assert_eq!(st.paths, before); +}