Skip to content

Commit

Permalink
Update ScrollArea drag velocity when drag stopped
Browse files Browse the repository at this point in the history
Fixes #5174. The drag velocity was not being updated unless the cursor
counted as "dragging", which only happens when it's in motion. This
effectively guarantees that the drag velocity will never be zero, even
if the cursor is not moving, and results in spurious scroll velocity
being applied when the cursor is released. Instead, we update the
velocity only when the drag is stopped, which is when the kinetic
scrolling actually needs to begin.
  • Loading branch information
valadaptive committed Sep 27, 2024
1 parent 59d7183 commit 55a3ed3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,20 +621,35 @@ impl ScrollArea {
.interact_rect
.map(|rect| ui.interact(rect, id.with("area"), Sense::drag()));

if content_response_option.map(|response| response.dragged()) == Some(true) {
if content_response_option
.as_ref()
.is_some_and(|response| response.dragged())
{
for d in 0..2 {
if scroll_enabled[d] {
ui.input(|input| {
state.offset[d] -= input.pointer.delta()[d];
state.vel[d] = input.pointer.velocity()[d];
});
state.scroll_stuck_to_end[d] = false;
state.offset_target[d] = None;
} else {
state.vel[d] = 0.0;
}
}
} else {
// Apply the cursor velocity to the scroll area when the user releases the drag.
if content_response_option
.as_ref()
.is_some_and(|response| response.drag_stopped())
{
for d in 0..2 {
if scroll_enabled[d] {
ui.input(|input| {
state.vel[d] = input.pointer.velocity()[d];
});
} else {
state.vel[d] = 0.0;
}
}
}
for d in 0..2 {
// Kinetic scrolling
let stop_speed = 20.0; // Pixels per second.
Expand Down

0 comments on commit 55a3ed3

Please sign in to comment.