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 {