Skip to content

Commit d87d491

Browse files
Fix bevy_ui_widgets scrollbar bug where scrollbar_size wasn't taken into account (#21835)
# Objective Bevy ui widget scrollbar implementation didn't correctly handle scrollbar width inserted by taffy. ## Solution Correctly handle scrollbar width similarly as it is done in bevy engine scroll position calculation: https://github.com/bevyengine/bevy/blob/0d46518eb21df54ba951f503930501bc2639e2a2/crates/bevy_ui/src/layout/mod.rs#L334-L336 ## Testing - In personal project added scrollareas with scrollbars that automatically attach to overflow: scroll elements and utilize space provided by taffy scrollbar-width. Co-authored-by: Alice Cecile <[email protected]>
1 parent a1c4943 commit d87d491

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

crates/bevy_ui_widgets/src/scrollbar.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ fn scrollbar_on_pointer_down(
130130
// Convert the click coordinates into a scroll position. If it's greater than the
131131
// current scroll position, scroll forward by one step (visible size) otherwise scroll
132132
// back.
133-
let visible_size = scroll_content.size() * scroll_content.inverse_scale_factor;
133+
let visible_size = (scroll_content.size() - scroll_content.scrollbar_size)
134+
* scroll_content.inverse_scale_factor;
134135
let content_size = scroll_content.content_size() * scroll_content.inverse_scale_factor;
135136
let max_range = (content_size - visible_size).max(Vec2::ZERO);
136137

@@ -193,8 +194,11 @@ fn scrollbar_on_drag(
193194

194195
if drag.dragging {
195196
let distance = ev.event().distance / ui_scale.0;
196-
let visible_size = scroll_content.size() * scroll_content.inverse_scale_factor;
197+
198+
let visible_size = (scroll_content.size() - scroll_content.scrollbar_size)
199+
* scroll_content.inverse_scale_factor;
197200
let content_size = scroll_content.content_size() * scroll_content.inverse_scale_factor;
201+
198202
let scrollbar_size = (node.size() * node.inverse_scale_factor).max(Vec2::ONE);
199203

200204
match scrollbar.orientation {
@@ -250,7 +254,8 @@ fn update_scrollbar_thumb(
250254
};
251255

252256
// Size of the visible scrolling area.
253-
let visible_size = scroll_area.1.size() * scroll_area.1.inverse_scale_factor;
257+
let visible_size = (scroll_area.1.size() - scroll_area.1.scrollbar_size)
258+
* scroll_area.1.inverse_scale_factor;
254259

255260
// Size of the scrolling content.
256261
let content_size = scroll_area.1.content_size() * scroll_area.1.inverse_scale_factor;

0 commit comments

Comments
 (0)