diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 710e39816..1ef7a84f9 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -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 }; diff --git a/crates/ui-canvas/tests/it/canvas/scrollbars.rs b/crates/ui-canvas/tests/it/canvas/scrollbars.rs index bbecbcc7d..c601ecd18 100644 --- a/crates/ui-canvas/tests/it/canvas/scrollbars.rs +++ b/crates/ui-canvas/tests/it/canvas/scrollbars.rs @@ -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>) { + 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"); + } +}