From bc52f3ef0cf97c5affbdce1ed2422b70e31d55bc Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 22 Nov 2026 02:20:50 +0100 Subject: [PATCH 1/2] Canvas scroll bars: a thumb's pressed extent ends with its press (photocraft#16328) The press slot that keeps the extent a thumb was pressed at is kept until the component clears it, and the bars never did. After a thumb drag the bars kept unioning the drag's old extent, so a later zoom, pan or Overscroll change showed the old range: page presses stopped short and PhotoCraft's Overscroll test read a scroll room of 0. The release and a cancel now clear it. Co-Authored-By: Claude Opus 5.5 --- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 17 ++++++++-- .../ui-canvas/tests/it/canvas/scrollbars.rs | 31 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 6b64e1624..b61253890 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -496,8 +496,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() { @@ -519,7 +520,13 @@ impl Component for CanvasScrollbars<'_> { // 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 +555,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}"); +} From 3485ca18add0b6ea7006b89b06cbcfc0c24799da Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 22 Nov 2026 03:18:35 +0100 Subject: [PATCH 2/2] Canvas scroll bars: a thumb's pressed extent belongs to its press (photocraft#16329) The extent is taken in the first pass that sees the press, keyed by the press's origin and time, and only from the view, instead of only in the pass that reports `Down`. In the running PhotoCraft a thumb drag moved the view the wrong way: it mapped the pointer onto an extent kept from an earlier press, which means the new press's `Down` pass did not leave its extent behind. With this and the release clearing the slot, real mouse drags keep the thumb under the pointer (right 40, left 40, past the end and back). Co-Authored-By: Claude Opus 5.5 --- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index b61253890..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. @@ -516,9 +530,19 @@ 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, .. } => { if !click {