Skip to content
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

Update ScrollArea drag velocity when drag stopped #5175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Comment on lines +643 to +651
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we could write this as

Suggested change
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;
}
}
state.vel = scroll_enabled.to_vec2() * input.pointer.velocity();

(requires implementing adding a fn to_vec2(&self) to Vec2b)

}
for d in 0..2 {
// Kinetic scrolling
let stop_speed = 20.0; // Pixels per second.
Expand Down
Loading