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 <noreply@anthropic.com>
This commit is contained in:
David Mann
2026-11-22 03:18:35 +01:00
co-authored by Claude Opus 5.5
parent bc52f3ef0c
commit 3485ca18ad
+30 -6
View File
@@ -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<f64>,
visible: Range<f64>,
}
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::<HeldThumb>() = 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::<HeldThumb>() = Some(HeldThumb::of(axis, &bar.span)),
PressPhase::DragStart { .. } => cx.begin(),
PressPhase::Release { click, .. } => {
if !click {