gpui: Round scroll_max to two decimal places (#34832)

Follow up to #31836 

After enabling rounding in the Taffy layout engine, we frequently run
into cases where the bounds produced by Taffy and ours slightly differ
after 5 or more decimal places. This leads to cases where containers
become scrollable for less than 0.0000x Pixels. In case this happens for
e.g. hover popovers, we render a scrollbar due to the container being
technically scrollable, even though the scroll amount here will in
practice never be visible.

This change fixes this by rounding the `scroll_max` by which we clamp
the current scroll position to two decimal places. We don't benefit from
the additional floating point precision here at all and it stops such
containers from becoming scrollable altogether. Furthermore, we now
store the `scroll_max` instead of the `padded_content_size` as the
former gives a much better idea on whether the corresponding container
is scrollable or not.

| `main` | After these changes |
| -- | -- |
| <img width="610" height="316" alt="main"
src="https://github.com/user-attachments/assets/ffcc0322-6d6e-4f79-a916-bd3c57fe4211"
/> | <img width="610" height="316" alt="scroll_max_rounded"
src="https://github.com/user-attachments/assets/5fe530f5-2e21-4aaa-81f4-e5c53ab73e4f"
/> |

Release Notes:

- Fixed an issue where scrollbars would appear in containers where no
scrolling was possible.
This commit is contained in:
Finn Evers 2025-07-21 22:24:33 +02:00 committed by GitHub
parent 8eca7f32e2
commit 5b3e371812
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 55 additions and 33 deletions

View file

@ -29,8 +29,8 @@ impl ThumbState {
}
impl ScrollableHandle for UniformListScrollHandle {
fn content_size(&self) -> Size<Pixels> {
self.0.borrow().base_handle.content_size()
fn max_offset(&self) -> Size<Pixels> {
self.0.borrow().base_handle.max_offset()
}
fn set_offset(&self, point: Point<Pixels>) {
@ -47,8 +47,8 @@ impl ScrollableHandle for UniformListScrollHandle {
}
impl ScrollableHandle for ListState {
fn content_size(&self) -> Size<Pixels> {
self.content_size_for_scrollbar()
fn max_offset(&self) -> Size<Pixels> {
self.max_offset_for_scrollbar()
}
fn set_offset(&self, point: Point<Pixels>) {
@ -73,8 +73,8 @@ impl ScrollableHandle for ListState {
}
impl ScrollableHandle for ScrollHandle {
fn content_size(&self) -> Size<Pixels> {
self.padded_content_size()
fn max_offset(&self) -> Size<Pixels> {
self.max_offset()
}
fn set_offset(&self, point: Point<Pixels>) {
@ -91,7 +91,10 @@ impl ScrollableHandle for ScrollHandle {
}
pub trait ScrollableHandle: Any + Debug {
fn content_size(&self) -> Size<Pixels>;
fn content_size(&self) -> Size<Pixels> {
self.viewport().size + self.max_offset()
}
fn max_offset(&self) -> Size<Pixels>;
fn set_offset(&self, point: Point<Pixels>);
fn offset(&self) -> Point<Pixels>;
fn viewport(&self) -> Bounds<Pixels>;
@ -149,17 +152,17 @@ impl ScrollbarState {
fn thumb_range(&self, axis: ScrollbarAxis) -> Option<Range<f32>> {
const MINIMUM_THUMB_SIZE: Pixels = px(25.);
let content_size = self.scroll_handle.content_size().along(axis);
let max_offset = self.scroll_handle.max_offset().along(axis);
let viewport_size = self.scroll_handle.viewport().size.along(axis);
if content_size.is_zero() || viewport_size.is_zero() || content_size <= viewport_size {
if max_offset.is_zero() || viewport_size.is_zero() {
return None;
}
let content_size = viewport_size + max_offset;
let visible_percentage = viewport_size / content_size;
let thumb_size = MINIMUM_THUMB_SIZE.max(viewport_size * visible_percentage);
if thumb_size > viewport_size {
return None;
}
let max_offset = content_size - viewport_size;
let current_offset = self
.scroll_handle
.offset()
@ -307,7 +310,7 @@ impl Element for Scrollbar {
let compute_click_offset =
move |event_position: Point<Pixels>,
item_size: Size<Pixels>,
max_offset: Size<Pixels>,
event_type: ScrollbarMouseEvent| {
let viewport_size = padded_bounds.size.along(axis);
@ -323,7 +326,7 @@ impl Element for Scrollbar {
- thumb_offset)
.clamp(px(0.), viewport_size - thumb_size);
let max_offset = (item_size.along(axis) - viewport_size).max(px(0.));
let max_offset = max_offset.along(axis);
let percentage = if viewport_size > thumb_size {
thumb_start / (viewport_size - thumb_size)
} else {
@ -347,7 +350,7 @@ impl Element for Scrollbar {
} else {
let click_offset = compute_click_offset(
event.position,
scroll.content_size(),
scroll.max_offset(),
ScrollbarMouseEvent::GutterClick,
);
scroll.set_offset(scroll.offset().apply_along(axis, |_| click_offset));
@ -373,7 +376,7 @@ impl Element for Scrollbar {
ThumbState::Dragging(drag_state) if event.dragging() => {
let drag_offset = compute_click_offset(
event.position,
scroll.content_size(),
scroll.max_offset(),
ScrollbarMouseEvent::ThumbDrag(drag_state),
);
scroll.set_offset(scroll.offset().apply_along(axis, |_| drag_offset));