Skip to content

Commit 471c4c1

Browse files
authored
Clamp scroll offset to prevent scrollbar thumb from going out of bounds (#21378)
# Objective Fixes a bug where the scrollbar thumb could go outside the scrollbar area when scrolling very quickly on trackpads. ## Solution Clamp the scroll offset to the maximum valid range before calculating the thumb’s position. This ensures the thumb never renders outside the scrollbar area bounds. ## Testing Manually verified that the scrollbar thumb stays within the scrollbar area when scrolling rapidly on a trackpad. Tested on MacOS (MacBook Pro M1)
1 parent c2e8d92 commit 471c4c1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/bevy_ui_widgets/src/scrollbar.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn update_scrollbar_thumb(
263263
visible_size: f32,
264264
track_length: f32,
265265
min_size: f32,
266-
offset: f32,
266+
mut offset: f32,
267267
) -> (f32, f32) {
268268
let thumb_size = if content_size > visible_size {
269269
(track_length * visible_size / content_size)
@@ -273,6 +273,15 @@ fn update_scrollbar_thumb(
273273
track_length
274274
};
275275

276+
if content_size > visible_size {
277+
let max_offset = content_size - visible_size;
278+
279+
// Clamp offset to prevent thumb from going out of bounds during inertial scroll
280+
offset = offset.clamp(0.0, max_offset);
281+
} else {
282+
offset = 0.0;
283+
}
284+
276285
let thumb_pos = if content_size > visible_size {
277286
offset * (track_length - thumb_size) / (content_size - visible_size)
278287
} else {

0 commit comments

Comments
 (0)