ui-canvas: the rulers' corner drag proposes where it lands at its start and its release, so a drag of one move sets the zero point

This commit is contained in:
bflatastic
2026-11-23 13:37:04 +07:00
parent f536089107
commit a29bfe866b
2 changed files with 31 additions and 4 deletions
+11 -4
View File
@@ -438,17 +438,24 @@ impl Component for Rulers<'_> {
let press = cx.part(CORNER).press;
let started = press.phase.start().filter(|start| start.button == PointerButton::Primary);
if started.is_some() && !self.origin_locked {
// Every step of the drag proposes where it lands, its start and its release too: a drag of one move lands.
let land =
|pos: Pos2, start: Point| if corner.contains(pos) { start } else { Self::landing(&space, self.snap.as_ref(), pos, press.mods, zero) };
match press.phase {
PressPhase::DragStart { .. } => {
PressPhase::DragStart { pos, .. } => {
*cx.press_slot::<Point>() = Some(zero);
cx.begin();
cx.propose(land(pos, zero));
}
PressPhase::Drag { pos, .. } => {
let start = cx.press_slot::<Point>().unwrap_or(zero);
let at = if corner.contains(pos) { start } else { Self::landing(&space, self.snap.as_ref(), pos, press.mods, zero) };
cx.propose(at);
cx.propose(land(pos, start));
}
PressPhase::Release { pos, click: false, .. } => {
let start = cx.press_slot::<Point>().unwrap_or(zero);
cx.propose(land(pos, start));
cx.commit();
}
PressPhase::Release { click: false, .. } => cx.commit(),
_ => {}
}
if press.double_pressed() {
@@ -170,3 +170,23 @@ fn with_rulers_on_the_tool_and_a_canvas_control_paint_inside_the_view_and_the_ru
});
assert!(strips_keep_the_canvas, "the rulers paint inside the top strip under the canvas's rect: {strip:?} in {shapes:?}");
}
/// A drag of the rulers' corner with one move past the drag threshold, released there, moves the zero point to where it
/// was released: the drag proposes its landing at its start and at its release, not only on the moves between.
#[trial]
fn a_corner_drag_of_one_move_lands_the_zero_point() {
let zero = Rc::new(RefCell::new(Point::ZERO));
let view = |room: Rect| CanvasView::new(room, Point::new(f64::from(room.center().x), f64::from(room.center().y)), 1.0);
let snap = Snap::new(std::sync::Arc::new(SnapTargets::new()), SnapOptions::default());
let mut fixture = ToolFixture::with_rulers("Document", Overpaint, view, snap, zero.clone(), |_, _| {});
fixture.fixture().room(Vec2::new(500.0, 400.0)).settle();
let canvas = fixture.canvas().rect;
let (from, to) = (canvas.min + Vec2::splat(4.0), canvas.center());
let fixture = fixture.fixture();
fixture.frame(vec![craft_ui::testing::pointer::moved(from)]);
fixture.frame(vec![craft_ui::testing::pointer::press(from)]);
fixture.frame(vec![craft_ui::testing::pointer::moved(to)]);
fixture.frame(vec![craft_ui::testing::pointer::release(to)]);
fixture.settle();
assert_ne!(*zero.borrow(), Point::ZERO, "the zero point lands where the one-move drag released");
}