Canvas scroll bars: a thumb drag follows every move

The canvas settles the view it is given to whole device pixels, so a
thumb drag's first proposal came back with its centre a fraction of a
pixel off. The value step read that as the app changing the view under
the gesture and cancelled it (HostChanged); the cancelled gesture refused
every later proposal until the release, so the view moved once and then
stayed (photocraft#16006). The bars now take a host view with the same
zoom, turn, mirroring and aspect and a centre within a point as their own
value (same_value), as the number fields do with rounded values.
This commit is contained in:
David Mann committed 2026-11-20 20:51:50 +01:00
1 parent 552ce4a803
commit 6d3d29331c
2 files changed
+57

No files matched your search

@@ -383,6 +383,20 @@ impl Component for CanvasScrollbars<'_> {
Some(self.view.reborrow())
}
/// The canvas settles a view it is given to whole device pixels and to its own rect, so a thumb drag's proposal
/// comes back with its centre moved by under a point (photocraft#16006): that is the drag's own value, not the app
/// changing the view under it, and the drag goes on. A different zoom, turn, mirroring or aspect, or a centre a
/// point or more away, is a change.
fn same_value(&self, proposed: &CanvasView, host: &CanvasView) -> bool {
let reach = 1.0 / proposed.zoom.max(f64::MIN_POSITIVE);
proposed.zoom == host.zoom
&& proposed.rotation == host.rotation
&& proposed.flip_x == host.flip_x
&& proposed.aspect == host.aspect
&& (proposed.center.x - host.center.x).abs() < reach
&& (proposed.center.y - host.center.y).abs() < reach
}
/// The bars: `PartsCx::scroll` runs the thumb drag and the page press in the pipeline.
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
let Some(view) = cx.value().copied() else { return };
@@ -291,3 +291,46 @@ fn the_document_scroll_arrows_take_no_keyboard_focus() {
assert!(!on_arrow, "Tab {step} stopped on a scroll bar part: {:?}", fixture.focused_stop());
}
}
/// Document scroll bars over a document larger than the window, at 100%, with or without the document chrome.
fn large_document_scrollbars(chrome: bool) -> (Fixture, Rc<Cell<CanvasView>>) {
let large = Size::new(4000.0, 3000.0);
let view = Rc::new(Cell::new(CanvasView::new(DOCUMENT_WINDOW, Point::new(2000.0, 1500.0), 1.0)));
let observed = Rc::clone(&view);
let mut fixture = Fixture::new(move |ui| {
let mut next = observed.get();
Canvas::new("Document").document(large).view(next).show_at(ui, DOCUMENT_WINDOW, |_, body| {
if let Some(space) = canvas_space(&body.env) {
next = space.view;
}
});
let doc = DocRect::from_origin_size(Point::ZERO, large);
let bars = CanvasScrollbars::new("Scroll bars", Bind::bound(&mut next), doc);
let bars = if chrome { bars.document_chrome() } else { bars };
let _ = bars.show_at(ui, DOCUMENT_WINDOW);
observed.set(next);
});
fixture.room(vec2(760.0, 560.0)).settle();
(fixture, view)
}
/// photocraft#16006: a drag on a thumb moves the view on every pointer move until the release, not only on the first.
#[trial]
fn a_thumb_drag_follows_every_pointer_move() {
for chrome in [false, true] {
let (mut fixture, view) = large_document_scrollbars(chrome);
let track = fixture.by_label("Horizontal scroll bar").expect("the horizontal track").rect;
let start = pos2(track.center().x, track.center().y);
let before = view.get().center;
fixture.frame(vec![pointer::moved(start)]).frame(vec![pointer::press(start)]);
let mut seen = vec![view.get().center.x];
for step in 1..=10 {
fixture.frame(vec![pointer::moved(start + vec2(20.0 * step as f32, 0.0))]);
seen.push(view.get().center.x);
}
fixture.frame(vec![pointer::release(start + vec2(200.0, 0.0))]).settle();
let moves = seen.windows(2).filter(|pair| pair[1] > pair[0]).count();
assert!(moves >= 9, "chrome {chrome}: the view follows each of the ten moves: {seen:?} from {before:?}");
assert_eq!(view.get().center.y, before.y, "chrome {chrome}: a horizontal drag keeps the vertical position");
}
}