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

@ -1664,6 +1664,11 @@ impl Interactivity {
window: &mut Window,
_cx: &mut App,
) -> Point<Pixels> {
fn round_to_two_decimals(pixels: Pixels) -> Pixels {
const ROUNDING_FACTOR: f32 = 100.0;
(pixels * ROUNDING_FACTOR).round() / ROUNDING_FACTOR
}
if let Some(scroll_offset) = self.scroll_offset.as_ref() {
let mut scroll_to_bottom = false;
let mut tracked_scroll_handle = self
@ -1678,8 +1683,16 @@ impl Interactivity {
let rem_size = window.rem_size();
let padding = style.padding.to_pixels(bounds.size.into(), rem_size);
let padding_size = size(padding.left + padding.right, padding.top + padding.bottom);
// The floating point values produced by Taffy and ours often vary
// slightly after ~5 decimal places. This can lead to cases where after
// subtracting these, the container becomes scrollable for less than
// 0.00000x pixels. As we generally don't benefit from a precision that
// high for the maximum scroll, we round the scroll max to 2 decimal
// places here.
let padded_content_size = self.content_size + padding_size;
let scroll_max = (padded_content_size - bounds.size).max(&Size::default());
let scroll_max = (padded_content_size - bounds.size)
.map(round_to_two_decimals)
.max(&Default::default());
// Clamp scroll offset in case scroll max is smaller now (e.g., if children
// were removed or the bounds became larger).
let mut scroll_offset = scroll_offset.borrow_mut();
@ -1692,7 +1705,7 @@ impl Interactivity {
}
if let Some(mut scroll_handle_state) = tracked_scroll_handle {
scroll_handle_state.padded_content_size = padded_content_size;
scroll_handle_state.max_offset = scroll_max;
}
*scroll_offset
@ -2936,7 +2949,7 @@ impl ScrollAnchor {
struct ScrollHandleState {
offset: Rc<RefCell<Point<Pixels>>>,
bounds: Bounds<Pixels>,
padded_content_size: Size<Pixels>,
max_offset: Size<Pixels>,
child_bounds: Vec<Bounds<Pixels>>,
scroll_to_bottom: bool,
overflow: Point<Overflow>,
@ -2965,6 +2978,11 @@ impl ScrollHandle {
*self.0.borrow().offset.borrow()
}
/// Get the maximum scroll offset.
pub fn max_offset(&self) -> Size<Pixels> {
self.0.borrow().max_offset
}
/// Get the top child that's scrolled into view.
pub fn top_item(&self) -> usize {
let state = self.0.borrow();
@ -2999,11 +3017,6 @@ impl ScrollHandle {
self.0.borrow().child_bounds.get(ix).cloned()
}
/// Get the size of the content with padding of the container.
pub fn padded_content_size(&self) -> Size<Pixels> {
self.0.borrow().padded_content_size
}
/// scroll_to_item scrolls the minimal amount to ensure that the child is
/// fully visible
pub fn scroll_to_item(&self, ix: usize) {

View file

@ -411,9 +411,9 @@ impl ListState {
self.0.borrow_mut().set_offset_from_scrollbar(point);
}
/// Returns the size of items we have measured.
/// Returns the maximum scroll offset according to the items we have measured.
/// This value remains constant while dragging to prevent the scrollbar from moving away unexpectedly.
pub fn content_size_for_scrollbar(&self) -> Size<Pixels> {
pub fn max_offset_for_scrollbar(&self) -> Size<Pixels> {
let state = self.0.borrow();
let bounds = state.last_layout_bounds.unwrap_or_default();
@ -421,7 +421,7 @@ impl ListState {
.scrollbar_drag_start_height
.unwrap_or_else(|| state.items.summary().height);
Size::new(bounds.size.width, height)
Size::new(Pixels::ZERO, Pixels::ZERO.max(height - bounds.size.height))
}
/// Returns the current scroll offset adjusted for the scrollbar