diff --git a/crates/app/src/interact.rs b/crates/app/src/interact.rs index 2f22791..443720f 100644 --- a/crates/app/src/interact.rs +++ b/crates/app/src/interact.rs @@ -216,6 +216,16 @@ pub(crate) fn swallow(_: &MouseDownEvent, _: &mut Window, cx: &mut App) { /// takes must still be able to leave a gap of a few frames there. pub(crate) const SNAP_PX: f64 = 5.; +/// The scrollbar's thumb never narrows past this, however long the timeline: +/// a thumb a pixel wide is a thumb no hand can hold. A quarter of the ruler's +/// own seek strip, which is the idiom it sits under. +pub(crate) const SCROLL_THUMB_MIN: f32 = 24.; + +/// The scrollbar track's own hit height: the bar looks 6 px tall, and the +/// strip that takes the press is taller for the ruler strip's reason -- a +/// hand must be able to find it without aiming (WCAG 2.5.8). +pub(crate) const SCROLL_HIT_H: f32 = 14.; + /// Where a held key would land, which is the whole of what auto-repeat has to /// know. [`Player::repeat_scope`] answers it from the same state the handler /// walks below itself. diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 4c35375..2c0530e 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -103,6 +103,13 @@ struct Player { /// The ruler's own box, recorded at prepaint: a mouse listener is handed /// the window position and nothing else. ruler: Rc>>, + /// The scrollbar track's laid-out box, recorded at prepaint like the + /// ruler's: a thumb drag is followed on the root (the pointer leaves the + /// strip at once), and the root has only window coordinates to work with. + scroll_track: Rc>>, + /// A thumb in the hand: how far into the thumb the press landed, in + /// track pixels. `None` with no drag in flight. + scroll_drag: Option, /// How wide a second of timeline is drawn, and from which moment. Held here /// and nowhere else: every frame-to-pixel answer in the panel comes out of /// it, so the boxes, the playhead and the pointer cannot disagree. @@ -646,14 +653,15 @@ fn main() { // Only ever used with a timeline; 30 keeps the empty // timecode reading in frames rather than in NaN. fps: 30., - // The file being opened, from the first frame the window + ruler: Rc::default(), + scroll_track: Rc::default(), + scroll_drag: None, // draws: the title bar and the header name it while its // header is still being read. name: name.clone(), image: None, sub_image: None, held: None, - ruler: Rc::default(), notice_h: Rc::default(), // A second is [`PPS_DEFAULT`] pixels wide until someone // zooms or asks for the fit: a project opens at a scale, not diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index b772ca5..2d4d101 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -1298,6 +1298,20 @@ impl Player { window: &mut Window, cx: &mut Context, ) { + // The scrollbar's thumb first: the pointer leaves the 6 px strip on + // the first move, so the gesture is followed here for the ruler's own + // reason -- and it must outrank every other drag, because a hand + // holding the view is not holding anything on it. + if self.scroll_drag.is_some() { + match event.pressed_button { + Some(MouseButton::Left) => self.scroll_drag_to(event.position.x, cx), + // A release outside the window never reaches the handler + // below, so the first button-up move is when we learn the + // drag is over. + _ => self.scroll_drag = None, + } + return; + } // A divider is answered before every gesture below it: it is pressed on // a strip of its own that nothing else is under, so neither can swallow // the other -- a seam over the timeline never scrubs, and a scrub is @@ -1399,6 +1413,10 @@ impl Player { ) { // The panel is left exactly where the hand let go, for the reason every // other release here lands one last sample. + if self.scroll_drag.take().is_some() { + self.scroll_drag_to(event.position.x, cx); + return; + } if let Some(split) = self.split_drag.take() { self.drag_split(split, event.position, window, cx); return; diff --git a/crates/app/src/player/view.rs b/crates/app/src/player/view.rs index aad8834..5f9d86b 100644 --- a/crates/app/src/player/view.rs +++ b/crates/app/src/player/view.rs @@ -238,6 +238,67 @@ impl Player { cx.notify(); } + /// A press on the scrollbar's track: on the thumb, the drag begins from + /// wherever in it the hand landed; beside it, the view jumps so the + /// thumb's middle is at the press and the drag carries on from there -- + /// a click is a jump, and holding it is a jump that keeps going. + pub(crate) fn scroll_press(&mut self, x: Pixels, cx: &mut Context) { + let view = self.view(); + if view.bed <= 0. || view.duration <= 0. { + return; + } + let (thumb_x, thumb_w) = + scroll_thumb(view.bed, view.duration, view.scale.start.max(0.), view.span()); + let at = px_along(x, self.scroll_track.get()); + let grab = match (thumb_x..thumb_x + thumb_w).contains(&at) { + true => at - thumb_x, + // Beside the thumb: jump so its middle is under the pointer. The + // thumb is clamped inside the track by [`scroll_thumb`], so the + // grab it leaves is always a real place inside it. + false => { + let view = View { + scale: Scale { + start: (view.scale.start.max(0.) + + f64::from(at - thumb_x - thumb_w / 2.) / view.scale.pps) + .max(0.), + ..view.scale + }, + ..view + }; + self.scale = view.settled(); + thumb_w / 2. + } + }; + self.scroll_drag = Some(grab); + // The wheel's own claim on the follow: a hand on the scrollbar is + // looking away from the playhead on purpose. + self.panned = true; + cx.notify(); + } + + /// A sample of a thumb drag: the window starts where the thumb's grabbed + /// point sits under the pointer, clamped by the same `settled` every other + /// scroll answers to. No reseek, no worker -- the view is the only thing + /// moving. + pub(crate) fn scroll_drag_to(&mut self, x: Pixels, cx: &mut Context) { + let Some(grab) = self.scroll_drag else { + return; + }; + let view = self.view(); + let at = px_along(x, self.scroll_track.get()); + // The thumb's grabbed point under the pointer names the start, in the + // track's own proportion. `settled` owns the ends -- and on a timeline + // so long the thumb sits at its floor width, the last stretch of track + // is the clamp's to hold, exactly as every scrollbar's is. + let start = f64::from((at - grab).max(0.)) / f64::from(view.bed.max(1.)) * view.duration; + self.scale = View { + scale: Scale { start, ..view.scale }, + ..view + } + .settled(); + cx.notify(); + } + /// One notch of the wheel anywhere over the timeline -- the ruler or a /// lane's bed alike, since a hand aims at the clip it is working on and not /// at the strip above it. Ctrl zooms about the pointer, bare scrolls the diff --git a/crates/app/src/tests/layout.rs b/crates/app/src/tests/layout.rs index 6edd880..b5b0444 100644 --- a/crates/app/src/tests/layout.rs +++ b/crates/app/src/tests/layout.rs @@ -266,6 +266,29 @@ fn a_lane_row_is_a_fixed_header_and_a_bed_that_can_be_hit() { assert_eq!(lanes_h(2), 2. * LANE_H + 8.); } +/// The scrollbar's thumb: the visible share of the timeline at its own +/// place on the track, full width with nothing to scroll, never narrower than +/// a hand can hold, and never off the ends. +#[test] +fn the_scroll_thumb_is_the_visible_share_at_its_own_place() { + use crate::scroll_thumb; + use crate::SCROLL_THUMB_MIN; + + // Nothing to scroll: the strip fills and says so. + assert_eq!(scroll_thumb(400., 10., 0., 12.), (0., 400.)); + assert_eq!(scroll_thumb(400., 0., 0., 0.), (0., 400.)); + // Half the timeline on the bed: a half-width thumb, halfway along for a + // window starting halfway in. + assert_eq!(scroll_thumb(400., 20., 0., 10.), (0., 200.)); + assert_eq!(scroll_thumb(400., 20., 10., 10.), (200., 200.)); + // The floor width: however long the timeline, the thumb stays holdable -- + // and the clamp keeps it on the track, not the caller. + let (x, w) = scroll_thumb(400., 1_000_000., 999_000., 100.); + assert_eq!(w, SCROLL_THUMB_MIN); + assert_eq!(x, 400. - SCROLL_THUMB_MIN); + assert_eq!(scroll_thumb(400., 1_000_000., 0., 100.).0, 0.); +} + /// A caption's box wears the rate its window plays at, derived off the /// placement itself: unity placements -- including odd widths whose frames /// came off `frames_of_us` rounding -- do not badge, and a re-rate does, with diff --git a/crates/app/src/timeline_math.rs b/crates/app/src/timeline_math.rs index 811a071..9cad527 100644 --- a/crates/app/src/timeline_math.rs +++ b/crates/app/src/timeline_math.rs @@ -182,6 +182,28 @@ pub(crate) fn caption_rate(sub: SubClip, fps: f64) -> Option { )) } +/// The scrollbar's thumb over a track `w` px wide: `(x, width)` of the box +/// standing for the visible share of a timeline `duration` long whose window +/// starts at `start` and spans `span`. Full width when nothing is off-screen +/// (there is nothing to scroll and the strip says so by filling), never +/// narrower than [`SCROLL_THUMB_MIN`] when there is (a thumb a pixel wide is a +/// thumb no hand can hold), and never off the track either way -- the clamp, +/// not the caller, owns that, because a drag's samples arrive raw. +pub(crate) fn scroll_thumb( + w: f32, + duration: f64, + start: f64, + span: f64, +) -> (f32, f32) { + if w <= 0. || duration <= 0. || span >= duration { + return (0., w.max(0.)); + } + let share = f64::from(w); + let width = ((span / duration) * share).clamp(f64::from(SCROLL_THUMB_MIN), f64::from(w)); + let x = (start / duration * share).clamp(0., f64::from(w) - width); + (x as f32, width as f32) +} + impl Selection { pub(crate) fn len(&self) -> usize { self.picks.len() diff --git a/crates/app/src/ui/timeline.rs b/crates/app/src/ui/timeline.rs index e257e89..0c443a7 100644 --- a/crates/app/src/ui/timeline.rs +++ b/crates/app/src/ui/timeline.rs @@ -89,6 +89,13 @@ impl Player { true => (lanes_box - LABEL_H - 8.).max(LANE_H), false => lanes_box, }; + // The scrollbar's geometry, off the very view the wheel and the zoom + // write: `settled` is what those paths clamp to, so the thumb never + // disagrees with where the panel actually is. + let view = self.view(); + let scrollable = view.duration > view.span(); + let (thumb_x, thumb_w) = + scroll_thumb(view.bed, view.duration, view.scale.start.max(0.), view.span()); // Live, not a count of the lanes: the column reports where it has been // taken to, so the line empties itself as the last track comes up // instead of insisting there is still something below. @@ -230,6 +237,75 @@ impl Player { .track_scroll(&self.lanes_scroll) .children(rows), ) + // The timeline's own scrollbar: the time axis is the one dimension + // the panel scrolls, and until now only the wheel and the keys + // reached it. The thumb is the visible share of the drawn duration + // and sits where that share is -- read off the very `Scale` every + // box is drawn through, so a wheel notch, a zoom about the pointer + // and the follow of the playhead all move it, because they all move + // the one state. + .child( + div() + .flex_none() + .flex() + .gap(px(HEADER_GAP)) + // Under the lane headers like the ruler is above them, so + // the thumb and the bed it stands for line up. + .child(div().flex_none().w(px(HEADER_W))) + .child( + div() + .id("timeline-scroll") + .flex_1() + .min_w(px(0.)) + .h(px(SCROLL_HIT_H)) + .flex() + .flex_col() + .justify_center() + .rounded(px(3.)) + .cursor_pointer() + .hover(|s| s.bg(rgb(BG_HOVER_DIM()))) + .tooltip(|_, cx| { + cx.new(|_| { + Tip("Scroll the timeline — drag the thumb or click to jump; \ + wheel scrolls, ctrl+wheel zooms" + .into()) + }) + .into() + }) + .on_scroll_wheel(cx.listener( + |this, event: &ScrollWheelEvent, _, cx| { + this.timeline_wheel(event, cx); + }, + )) + .on_mouse_down( + MouseButton::Left, + cx.listener(|this, event: &MouseDownEvent, _, cx| { + this.scroll_press(event.position.x, cx); + }), + ) + .child( + div() + .w_full() + .h(px(6.)) + .rounded(px(3.)) + .bg(rgb(BG_RAISED())) + .child(bounds_probe(self.scroll_track.clone())) + .child( + div() + .h_full() + .rounded(px(3.)) + .left(px(thumb_x)) + .absolute() + .top_0() + .w(px(thumb_w)) + .bg(rgb(match scrollable { + true => ACCENT_PRIMARY(), + false => FG_SECONDARY(), + })), + ), + ), + ), + ) // "It scrolls" is not "it can be found": when the region is too // short for every track -- which is what the 640x360 floor does to a // three-track project -- the lanes below the fold say so, in the one