diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 6b64e1624..9a88ec726 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -34,8 +34,8 @@ use crate::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, Stateful, Style use craft_behavior::kurbo::{Point, Rect as DocRect}; use craft_behavior::overlay::{Align, Side}; use craft_behavior::scroll::ScrollSpan; -use craft_behavior::{Axis, CanvasView, Metrics, PartKey, PartRef, PressPhase, Sense}; -use egui::{CornerRadius, Rect, Response, Vec2, pos2, vec2}; +use craft_behavior::{Axis, CanvasView, Metrics, PartKey, PartRef, PressPhase, PressStart, Sense}; +use egui::{CornerRadius, Pos2, Rect, Response, Vec2, pos2, vec2}; use std::ops::{Deref, Range}; const HORIZONTAL: PartKey = PartKey::new("horizontal"); @@ -160,18 +160,32 @@ impl Bar { #[derive(Clone, Debug, Default, PartialEq)] struct HeldThumb { horizontal: bool, + /// The press this extent belongs to: where and when it went down. + origin: Pos2, + time: f64, content: Range, visible: Range, } impl HeldThumb { - fn of(axis: Axis, span: &ScrollSpan) -> Self { - HeldThumb { horizontal: axis == Axis::X, content: span.content.clone(), visible: span.visible.clone() } + fn of(axis: Axis, press: PressStart, span: &ScrollSpan) -> Self { + HeldThumb { + horizontal: axis == Axis::X, + origin: press.origin, + time: press.time, + content: span.content.clone(), + visible: span.visible.clone(), + } } fn on(&self, axis: Axis) -> bool { self.horizontal == (axis == Axis::X) } + + /// The extent of `press` on `axis`'s thumb. + fn of_press(&self, axis: Axis, press: &PressStart) -> bool { + self.on(axis) && self.origin == press.origin && self.time == press.time + } } /// The bars of a view over a document, in the order horizontal, vertical. @@ -496,8 +510,9 @@ impl Component for CanvasScrollbars<'_> { tip.close(cx); } let Some(view) = cx.value().copied() else { return }; - let held = cx.press_slot::().clone(); + let mut held = cx.press_slot::().clone(); let bars = self.bars(&view, cx.rect, cx.style, held.as_ref()); + let mut ended = false; let rtl = cx.flow.is_rtl(); let min_thumb = cx.style.look.min_thumb; for (key, axis, bar) in bars.each() { @@ -515,11 +530,27 @@ impl Component for CanvasScrollbars<'_> { } } let phase = cx.part(thumb_of(key)).press.phase; + // The press keeps the extent the thumb was pressed at for its drag. It is taken in the first pass that sees + // the press, which need not be its `Down` (a pass that runs again, a press that starts a drag in its first + // pass), and only from the view: an extent kept from another press never stands for this one. + if let Some(start) = phase.start().filter(|_| !matches!(phase, PressPhase::Release { .. } | PressPhase::Cancel { .. })) + && !held.as_ref().is_some_and(|held| held.of_press(axis, &start)) + { + let fresh = Bars::of(&view, self.document, self.overscroll, !self.vertical_only, cx.rect, cx.style, None); + let span = if axis == Axis::X { fresh.horizontal } else { fresh.vertical }.map_or_else(|| bar.span.clone(), |bar| bar.span); + let taken = HeldThumb::of(axis, start, &span); + *cx.press_slot::() = Some(taken.clone()); + held = Some(taken); + } match phase { - // The press keeps the extent the thumb was pressed at for its drag. - PressPhase::Down { .. } => *cx.press_slot::() = Some(HeldThumb::of(axis, &bar.span)), PressPhase::DragStart { .. } => cx.begin(), - PressPhase::Release { click: false, .. } => cx.commit(), + PressPhase::Release { click, .. } => { + if !click { + cx.commit(); + } + ended = true; + } + PressPhase::Cancel { .. } => ended = true, _ => {} } let range = match phase { @@ -548,6 +579,12 @@ impl Component for CanvasScrollbars<'_> { cx.set(next); } } + if ended { + // The pressed extent lasts as long as its press: the press slot is kept until it is cleared, and a stale + // extent would hold the bars' range after the drag (a later zoom, pan or Overscroll change). + *cx.press_slot::() = None; + held = None; + } self.held = held; } diff --git a/crates/ui-canvas/tests/it/canvas/scrollbars.rs b/crates/ui-canvas/tests/it/canvas/scrollbars.rs index 2aff72690..add8a164e 100644 --- a/crates/ui-canvas/tests/it/canvas/scrollbars.rs +++ b/crates/ui-canvas/tests/it/canvas/scrollbars.rs @@ -444,3 +444,34 @@ fn a_thumb_dragged_past_the_end_and_back_follows_the_pointer_in_the_same_drag() assert!(back.iter().any(|&x| x < at_end - 1.0), "the drag moves back: past the end {seen:?}, back {back:?}"); assert!((view.get().center.x - straight).abs() < 1.0, "back where a straight drag puts it: {} vs {straight}", view.get().center.x); } + +/// A thumb's pressed extent lasts only as long as its press: after the release the bars follow the view again. Here +/// the view zooms out after a thumb drag, and page presses on the track reach the new end (100 screen points of the +/// document left in view), as they do when no drag came first. The kept extent used to hold the range at the drag's +/// zoom (photocraft#16328). +#[trial] +fn after_a_thumb_drag_the_bars_follow_a_new_zoom() { + let large = Size::new(4000.0, 3000.0); + let zoomed_out = |view: CanvasView| CanvasView::new(view.rect, Point::new(large.width / 2.0, large.height / 2.0), 0.25); + let page_to_the_end = |fixture: &mut Fixture| { + let track = fixture.by_label("Horizontal scroll bar").expect("the track").rect; + for _ in 0..40 { + fixture.click_at(pos2(track.right() - 3.0, track.center().y)); + fixture.settle(); + } + }; + let (mut fixture, view) = centred_scrollbars(large, 1.0, true); + view.set(zoomed_out(view.get())); + fixture.settle(); + page_to_the_end(&mut fixture); + let fresh = shown_range(&view.get(), Axis::X).0; + assert!((fresh - (large.width - 400.0)).abs() < 1.0, "paging reaches the end: the last 400 units stay in view, shows from {fresh}"); + + let (mut fixture, view) = centred_scrollbars(large, 1.0, true); + drag_thumb(&mut fixture, "Horizontal scroll bar", vec2(40.0, 0.0)); + view.set(zoomed_out(view.get())); + fixture.settle(); + page_to_the_end(&mut fixture); + let first = shown_range(&view.get(), Axis::X).0; + assert!((first - fresh).abs() < 1.0, "after a thumb drag paging reaches the same end: shows from {first}, without the drag {fresh}"); +}