|
| 1 | +use std::time::{Duration, Instant}; |
| 2 | + |
| 3 | +use crate::tui::event::Direction; |
| 4 | + |
| 5 | +/// The maximum number of lines that can be scrolled per event. |
| 6 | +/// Increase for a higher top speed; decrease for a lower top speed. |
| 7 | +const MAX_VELOCITY: f32 = 12.0; // max lines per event |
| 8 | + |
| 9 | +/// The minimum number of lines to scroll per event (when not accelerating). |
| 10 | +/// Usually leave at 1.0 for single-line scrolls. |
| 11 | +const MIN_VELOCITY: f32 = 1.0; |
| 12 | + |
| 13 | +/// How much the scroll velocity increases per qualifying event. |
| 14 | +/// Increase for faster acceleration (reaches top speed quicker, feels |
| 15 | +/// snappier). Decrease for slower, smoother acceleration (takes longer to reach |
| 16 | +/// top speed). |
| 17 | +const ACCELERATION: f32 = 0.3; |
| 18 | + |
| 19 | +/// How long (in ms) between scrolls before momentum resets. |
| 20 | +/// Increase to allow longer pauses between scrolls while keeping momentum. |
| 21 | +/// Decrease to require faster, more continuous scrolling to maintain momentum. |
| 22 | +const DECAY_TIME: Duration = Duration::from_millis(350); |
| 23 | + |
| 24 | +/// How long (in ms) between scrolls before events are ignored |
| 25 | +/// Increase to allow longer pauses between scrolls to trigger throttling |
| 26 | +/// Decrease to require faster, more continuous scrolling to trigger throttling |
| 27 | +const THROTTLE_TIME: Duration = Duration::from_millis(50); |
| 28 | + |
| 29 | +/// Only process 1 out of every N scroll events (throttling). |
| 30 | +/// Increase to make scrolling less sensitive to high-frequency mouse wheels |
| 31 | +/// (e.g. trackpads). Decrease to process more events (smoother, but may be |
| 32 | +/// too fast on some input devices). |
| 33 | +const THROTTLE_FACTOR: u8 = 3; |
| 34 | + |
| 35 | +/// Tracks and computes momentum-based scrolling. |
| 36 | +pub struct ScrollMomentum { |
| 37 | + velocity: f32, |
| 38 | + last_event: Option<Instant>, |
| 39 | + last_direction: Option<Direction>, |
| 40 | + throttle_counter: u8, |
| 41 | +} |
| 42 | + |
| 43 | +impl Default for ScrollMomentum { |
| 44 | + fn default() -> Self { |
| 45 | + Self::new() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl ScrollMomentum { |
| 50 | + /// Create a new ScrollMomentum tracker. |
| 51 | + pub fn new() -> Self { |
| 52 | + Self { |
| 53 | + velocity: 0.0, |
| 54 | + last_event: None, |
| 55 | + last_direction: None, |
| 56 | + throttle_counter: 0, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Call this on every scroll event (mouse wheel, key, etc). |
| 61 | + /// Returns the number of lines to scroll for this event. |
| 62 | + pub fn on_scroll_event(&mut self, direction: Direction) -> usize { |
| 63 | + let now = Instant::now(); |
| 64 | + let last_event = self.last_event.replace(now); |
| 65 | + let last_direction = self.last_direction.replace(direction); |
| 66 | + |
| 67 | + let has_direction_changed = last_direction.is_some_and(|last| last != direction); |
| 68 | + let is_scrolling_quickly = |
| 69 | + last_event.is_some_and(|last| now.duration_since(last) < DECAY_TIME); |
| 70 | + let is_throttling = last_event.is_some_and(|last| now.duration_since(last) < THROTTLE_TIME); |
| 71 | + |
| 72 | + if is_throttling { |
| 73 | + self.throttle_counter = (self.throttle_counter + 1) % THROTTLE_FACTOR; |
| 74 | + let should_throttle = self.throttle_counter != 0; |
| 75 | + if should_throttle { |
| 76 | + return 0; |
| 77 | + } |
| 78 | + } else { |
| 79 | + self.throttle_counter = 0; |
| 80 | + } |
| 81 | + |
| 82 | + if has_direction_changed { |
| 83 | + self.velocity = MIN_VELOCITY; |
| 84 | + } else if is_scrolling_quickly { |
| 85 | + self.velocity = (self.velocity + ACCELERATION).min(MAX_VELOCITY); |
| 86 | + } else { |
| 87 | + self.velocity = MIN_VELOCITY; |
| 88 | + } |
| 89 | + |
| 90 | + self.velocity.round().clamp(MIN_VELOCITY, MAX_VELOCITY) as usize |
| 91 | + } |
| 92 | + |
| 93 | + /// Reset the momentum (e.g. on focus loss or scroll stop) |
| 94 | + pub fn reset(&mut self) { |
| 95 | + self.velocity = 0.0; |
| 96 | + self.last_event = None; |
| 97 | + self.last_direction = None; |
| 98 | + self.throttle_counter = 0; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod test { |
| 104 | + use super::*; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn first_event_scrolls() { |
| 108 | + let mut scroll = ScrollMomentum::new(); |
| 109 | + assert_eq!(scroll.on_scroll_event(Direction::Up), 1); |
| 110 | + } |
| 111 | +} |
0 commit comments