merge-f: F3: TransformBox follows from the first move after the press (#11103); PressPhase::Held carries the pointer

This commit is contained in:
aiongg
2026-11-16 12:49:40 +00:00
parent 802cf26cc6
commit f7df657338
3 changed files with 115 additions and 12 deletions
+14 -3
View File
@@ -176,6 +176,8 @@ pub enum PressPhase {
Held {
/// The press.
start: PressStart,
/// The pointer position this pass.
pos: Pos2,
},
/// The press moved past slop on a part that senses a drag.
#[non_exhaustive]
@@ -225,7 +227,7 @@ impl PressPhase {
pub const fn start(&self) -> Option<PressStart> {
match self {
PressPhase::Down { start }
| PressPhase::Held { start }
| PressPhase::Held { start, .. }
| PressPhase::DragStart { start, .. }
| PressPhase::Drag { start, .. }
| PressPhase::Release { start, .. }
@@ -259,7 +261,7 @@ impl PressPhase {
match self {
PressPhase::Idle | PressPhase::Hover => self,
PressPhase::Down { start } => PressPhase::Down { start: at(start) },
PressPhase::Held { start } => PressPhase::Held { start: at(start) },
PressPhase::Held { start, pos } => PressPhase::Held { start: at(start), pos: point(pos) },
PressPhase::DragStart { start, pos } => PressPhase::DragStart { start: at(start), pos: point(pos) },
PressPhase::Drag { start, pos, delta: d } => PressPhase::Drag { start: at(start), pos: point(pos), delta: delta(d) },
PressPhase::Release { start, pos, click } => PressPhase::Release { start: at(start), pos: point(pos), click },
@@ -642,7 +644,7 @@ impl<T: Copy> PressMachine<T> {
} else if open.dragging {
PressPhase::Drag { start: open.start, pos, delta: input.delta }
} else {
PressPhase::Held { start: open.start }
PressPhase::Held { start: open.start, pos }
};
open.reported = true;
PressStep { target: Some(open.target), phase: Some(phase), dragging: open.drag_reported, activated: None }
@@ -703,6 +705,15 @@ mod tests {
.join(" ")
}
#[test]
fn held_reports_the_pointer_of_its_pass_within_slop() {
let mut machine = PressMachine::new();
pass(&mut machine, &[press(0.0, 1.0)], Sense::DRAG);
let near = pos2(SLOP / 2.0, 0.0);
let step = pass(&mut machine, &[PointerEvent::Moved { at: near, sample: None }], Sense::DRAG);
assert!(matches!(step.phase, Some(PressPhase::Held { pos, .. }) if pos == near), "{:?}", step.phase);
}
#[test]
fn two_clicks_whose_passes_share_a_release_and_a_press_report_every_phase_in_order() {
let up = PointerEvent::released(pos2(0.0, 0.0));
+38 -9
View File
@@ -217,6 +217,13 @@ struct Grip {
last: BoxState,
}
impl Grip {
/// Whether the box follows the pointer inside the slop: every part whose click means nothing.
fn follows_in_slop(&self) -> bool {
self.part != BoxPart::Outside && !self.moves_pivot
}
}
impl Default for Grip {
fn default() -> Self {
let none = BoxState::from_rect(DocRect::ZERO);
@@ -535,27 +542,35 @@ impl<'a> TransformBox<'a> {
let (part, begin) = if moves_pivot { (BoxPart::Pivot, BoxState::new(shown.quad, from)) } else { (part, *shown) };
held = Some(Grip { part, start: begin, from, began: false, moves_pivot, last: *shown });
}
// Inside the slop the box follows every move of a part whose click means nothing, so it does not jump
// when the drag is recognised. Outside (a click applies) and an Alt pivot press (a click sets the
// pivot) start at the drag.
PressPhase::Held { start, pos, .. } => {
let Some(mut grip) = held else { continue };
if !self.rules.allows(grip.part) || !grip.follows_in_slop() || (!grip.began && pos == start.origin) {
continue;
}
if !grip.began {
cx.begin();
grip.began = true;
}
self.follow(cx, space, snap.as_ref(), &mut grip, pos, mods);
held = Some(grip);
}
PressPhase::DragStart { pos, .. } | PressPhase::Drag { pos, .. } => {
let Some(mut grip) = held else { continue };
if !self.rules.allows(grip.part) {
continue;
}
if matches!(press.phase, PressPhase::DragStart { .. }) {
if matches!(press.phase, PressPhase::DragStart { .. }) && !grip.began {
cx.begin();
grip.began = true;
}
if !grip.began {
continue;
}
let (next, lines) = self.proposal(space, snap.as_ref(), &grip, pos, mods);
self.report.snapped = lines;
grip.last = next;
self.follow(cx, space, snap.as_ref(), &mut grip, pos, mods);
held = Some(grip);
cx.propose(next);
let rows = self.readout_rows(cx, space, grip.part, &next, &grip.start);
readout(&*cx, pos, &rows);
let cursor = self.cursor(&CursorLook { space, screen: &on_screen(&space.view, &next), style }, grip.part, pos, mods, true);
cx.cursor(cursor, CursorPriority::Drag);
}
PressPhase::Release { start, click, .. } => {
// A double click inside applies, whether or not the first press of the release was seen.
@@ -578,6 +593,20 @@ impl<'a> TransformBox<'a> {
*cx.press_slot::<Grip>() = held;
}
/// One step of a begun grip with the pointer at `pos`: the proposal and its snap lines, the readout and the drag
/// cursor.
fn follow(&mut self, cx: &mut Cx<'_, Self>, space: &CanvasSpace, snap: Option<&Snap>, grip: &mut Grip, pos: Pos2, mods: Mods) {
let style = cx.style;
let (next, lines) = self.proposal(space, snap, grip, pos, mods);
self.report.snapped = lines;
grip.last = next;
cx.propose(next);
let rows = self.readout_rows(cx, space, grip.part, &next, &grip.start);
readout(&*cx, pos, &rows);
let cursor = self.cursor(&CursorLook { space, screen: &on_screen(&space.view, &next), style }, grip.part, pos, mods, true);
cx.cursor(cursor, CursorPriority::Drag);
}
/// The session keys: the arrows nudge the picked part (the whole box when no part has focus), Enter applies and
/// Escape closes the box. Untaken, a session's arrows go back to the canvas's tool.
fn run_keys(&mut self, cx: &mut Cx<'_, Self>, space: &CanvasSpace, shown: &BoxState, pressing: bool) {
+63
View File
@@ -672,3 +672,66 @@ fn a_level_live_lines_ends_stand_for_the_sides() {
fn kurbo_vec(x: f64, y: f64) -> craft_behavior::kurbo::Vec2 {
craft_behavior::kurbo::Vec2::new(x, y)
}
/// How far the box's first corner moved from where `RECT` puts it, in screen points (the view is upright at scale 1).
fn moved_by(h: &Harness) -> Vec2 {
let corner = h.state().quad[0];
by((corner.x - RECT.x0) as f32, (corner.y - RECT.y0) as f32)
}
#[trial]
fn the_box_moves_from_the_first_pointer_move_after_the_press_and_keeps_up_with_no_jump() {
let mut h = upright();
let slop = craft_ui::testing::input::slop(h.fixture.ctx());
let from = h.screen(Point::new(200.0, 150.0));
frame(&mut h, vec![pointer::moved(from)]);
frame(&mut h, vec![pointer::press(from)]);
frame(&mut h, Vec::new());
assert!(h.host.borrow().events.is_empty(), "a press alone moves nothing: {:?}", h.host.borrow().events);
// One step per frame, a third of the slop each, through the slop and past it.
for step in 1..=9 {
let offset = by(slop / 3.0 * step as f32, slop / 6.0 * step as f32);
frame(&mut h, vec![pointer::moved(from + offset)]);
assert!((moved_by(&h) - offset).length() < 1e-3, "step {step}: the box keeps the pointer's offset {offset:?}, moved {:?}", moved_by(&h));
}
let end = from + by(slop * 3.0, slop * 1.5);
frame(&mut h, vec![pointer::release(end)]);
frame(&mut h, Vec::new());
let events = h.host.borrow().events.clone();
assert_eq!(events.iter().filter(|seen| **seen == Seen::Begin).count(), 1, "one gesture: {events:?}");
assert_eq!(events.iter().filter(|seen| **seen == Seen::Commit).count(), 1, "one commit: {events:?}");
// A move inside the slop and a release there still commit the move the user saw.
let mut h = upright();
let from = h.screen(Point::new(200.0, 150.0));
frame(&mut h, vec![pointer::moved(from)]);
frame(&mut h, vec![pointer::press(from)]);
frame(&mut h, vec![pointer::moved(from + by(slop / 2.0, 0.0))]);
frame(&mut h, vec![pointer::release(from + by(slop / 2.0, 0.0))]);
frame(&mut h, Vec::new());
assert!((moved_by(&h) - by(slop / 2.0, 0.0)).length() < 1e-3, "the box stays where it followed to: {:?}", moved_by(&h));
assert_eq!(h.host.borrow().events.last(), Some(&Seen::Commit), "{:?}", h.host.borrow().events);
}
#[trial]
fn a_click_on_the_box_moves_nothing_and_a_jittery_click_outside_applies_the_box_as_it_was() {
let mut h = upright();
let slop = craft_ui::testing::input::slop(h.fixture.ctx());
let inside = h.screen(Point::new(200.0, 150.0));
frame(&mut h, vec![pointer::moved(inside)]);
frame(&mut h, vec![pointer::press(inside)]);
frame(&mut h, vec![pointer::release(inside)]);
frame(&mut h, Vec::new());
assert!(h.host.borrow().events.is_empty(), "a click on the box proposes nothing: {:?}", h.host.borrow().events);
assert_eq!(h.state(), BoxState::from_rect(RECT));
let outside = h.screen(Point::new(340.0, 260.0));
frame(&mut h, vec![pointer::moved(outside)]);
frame(&mut h, vec![pointer::press(outside)]);
frame(&mut h, vec![pointer::moved(outside + by(slop / 2.0, slop / 3.0))]);
frame(&mut h, vec![pointer::release(outside + by(slop / 2.0, slop / 3.0))]);
frame(&mut h, Vec::new());
assert!(h.host.borrow().events.is_empty(), "a jittery click outside turns nothing: {:?}", h.host.borrow().events);
assert_eq!(h.state(), BoxState::from_rect(RECT), "the box stays as it was");
assert_eq!(h.host.borrow().applied, 1, "the click outside applies");
}