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

Merged
merged 2 commits into from
Oct 2, 2024

Conversation

valadaptive
Copy link
Contributor

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. Note that we immediately apply the scroll velocity on the same frame that we first set it, to avoid a 1-frame gap where the scroll area doesn't move.

I believe that not setting scroll_stuck_to_end and offset_target when the drag is released is the correct thing to do, as they should apply immediately once the user stops dragging. Should we maybe clear the drag velocity instead if scroll_stuck_to_end is true or offset_target exists?

Fixes emilk#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.
Copy link

Preview available at https://egui-pr-preview.github.io/pr/5175-fix-drag-velocity
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

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

Makes sense!

Comment on lines 643 to 651
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;
}
}
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)

@emilk emilk added bug Something is broken egui labels Sep 30, 2024
@emilk emilk merged commit ac2466d into emilk:master Oct 2, 2024
23 checks passed
hacknus pushed a commit to hacknus/egui that referenced this pull request Oct 30, 2024
Fixes emilk#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. Note that we
immediately *apply* the scroll velocity on the same frame that we first
set it, to avoid a 1-frame gap where the scroll area doesn't move.

I believe that *not* setting `scroll_stuck_to_end` and `offset_target`
when the drag is released is the correct thing to do, as they should
apply immediately once the user stops dragging. Should we maybe clear
the drag velocity instead if `scroll_stuck_to_end` is true or
`offset_target` exists?

* Closes emilk#5174
* [x] I have followed the instructions in the PR template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken egui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When dragging to scroll, spurious drag velocity is applied
2 participants