-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(tui): momentum-based scrolling #10420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
161b8c6
wip
MendyLanda 8a1d3c3
trackpad wip
MendyLanda ea36a76
wip
MendyLanda a08b236
wip
MendyLanda 048b757
wip
MendyLanda 280d66e
wip
MendyLanda a8d54b5
config that works on both touchpad and mouse wheel
MendyLanda def38e6
chore(tui): avoid throttling when using mouse
chris-olszewski 262cff7
Merge pull request #1 from chris-olszewski/olszewski/jam_on_scroll_vals
MendyLanda 2e9812d
Update crates/turborepo-ui/src/tui/scroll.rs
chris-olszewski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ mod input; | |
| mod pane; | ||
| mod popup; | ||
| mod preferences; | ||
| pub mod scroll; | ||
| mod search; | ||
| mod size; | ||
| mod spinner; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| use crate::tui::event::Direction; | ||
|
|
||
| /// The maximum number of lines that can be scrolled per event. | ||
| /// Increase for a higher top speed; decrease for a lower top speed. | ||
| const MAX_VELOCITY: f32 = 12.0; // max lines per event | ||
|
|
||
| /// The minimum number of lines to scroll per event (when not accelerating). | ||
| /// Usually leave at 1.0 for single-line scrolls. | ||
| const MIN_VELOCITY: f32 = 1.0; | ||
|
|
||
| /// How much the scroll velocity increases per qualifying event. | ||
| /// Increase for faster acceleration (reaches top speed quicker, feels | ||
| /// snappier). Decrease for slower, smoother acceleration (takes longer to reach | ||
| /// top speed). | ||
| const ACCELERATION: f32 = 0.3; | ||
|
|
||
| /// How long (in ms) between scrolls before momentum resets. | ||
| /// Increase to allow longer pauses between scrolls while keeping momentum. | ||
| /// Decrease to require faster, more continuous scrolling to maintain momentum. | ||
| const DECAY_TIME: Duration = Duration::from_millis(350); | ||
|
|
||
| /// How long (in ms) between scrolls before events are ignored | ||
| /// Increase to allow longer pauses between scrolls to trigger throttling | ||
| /// Decrease to require faster, more continuous scrolling to trigger throttling | ||
| const THROTTLE_TIME: Duration = Duration::from_millis(50); | ||
|
|
||
| /// Only process 1 out of every N scroll events (throttling). | ||
| /// Increase to make scrolling less sensitive to high-frequency mouse wheels | ||
| /// (e.g. trackpads). Decrease to process more events (smoother, but may be | ||
| /// too fast on some input devices). | ||
| const THROTTLE_FACTOR: u8 = 3; | ||
|
|
||
| /// Tracks and computes momentum-based scrolling. | ||
| pub struct ScrollMomentum { | ||
| velocity: f32, | ||
| last_event: Option<Instant>, | ||
| last_direction: Option<Direction>, | ||
| throttle_counter: u8, | ||
| } | ||
|
|
||
| impl Default for ScrollMomentum { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl ScrollMomentum { | ||
| /// Create a new ScrollMomentum tracker. | ||
| pub fn new() -> Self { | ||
| Self { | ||
| velocity: 0.0, | ||
| last_event: None, | ||
| last_direction: None, | ||
| throttle_counter: 0, | ||
| } | ||
| } | ||
|
|
||
| /// Call this on every scroll event (mouse wheel, key, etc). | ||
| /// Returns the number of lines to scroll for this event. | ||
| pub fn on_scroll_event(&mut self, direction: Direction) -> usize { | ||
| let now = Instant::now(); | ||
| let last_event = self.last_event.replace(now); | ||
| let last_direction = self.last_direction.replace(direction); | ||
|
|
||
| let has_direction_changed = last_direction.is_some_and(|last| last != direction); | ||
| let is_scrolling_quickly = | ||
| last_event.is_some_and(|last| now.duration_since(last) < DECAY_TIME); | ||
| let is_throttling = last_event.is_some_and(|last| now.duration_since(last) < THROTTLE_TIME); | ||
|
|
||
| if is_throttling { | ||
| self.throttle_counter = (self.throttle_counter + 1) % THROTTLE_FACTOR; | ||
| let should_throttle = self.throttle_counter != 0; | ||
| if should_throttle { | ||
| return 0; | ||
| } | ||
| } else { | ||
| self.throttle_counter = 0; | ||
| } | ||
|
|
||
| if has_direction_changed { | ||
| self.velocity = MIN_VELOCITY; | ||
| } else if is_scrolling_quickly { | ||
| self.velocity = (self.velocity + ACCELERATION).min(MAX_VELOCITY); | ||
| } else { | ||
| self.velocity = MIN_VELOCITY; | ||
| } | ||
|
|
||
| self.velocity.round().clamp(MIN_VELOCITY, MAX_VELOCITY) as usize | ||
| } | ||
|
|
||
| /// Reset the momentum (e.g. on focus loss or scroll stop) | ||
| pub fn reset(&mut self) { | ||
| self.velocity = 0.0; | ||
| self.last_event = None; | ||
| self.last_direction = None; | ||
| self.throttle_counter = 0; | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn first_event_scrolls() { | ||
| let mut scroll = ScrollMomentum::new(); | ||
| assert_eq!(scroll.on_scroll_event(Direction::Up), 1); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.