Repository: bevyengine/bevy
File: crates/bevy_ui/src/ui_node.rs (Rust)

Code before:
        clip_rect.max.y -= clip_inset.bottom;

        if overflow.x == OverflowAxis::Visible {
            clip_rect.min.x = -f32::INFINITY;
            clip_rect.max.x = f32::INFINITY;
        }
        if overflow.y == OverflowAxis::Visible {
            clip_rect.min.y = -f32::INFINITY;
            clip_rect.max.y = f32::INFINITY;
        }

        clip_rect
    }

    const fn compute_thumb(
        gutter_min: f32,
        content_length: f32,
        gutter_length: f32,
        scroll_position: f32,
    ) -> [f32; 2] {
        if content_length <= gutter_length {
            return [gutter_min, gutter_min + gutter_length];
        }
        let thumb_len = gutter_length * gutter_length / content_length;
        let thumb_min = gutter_min
            + scroll_position / (content_length - gutter_length) * (gutter_length - thumb_len);
        [thumb_min, thumb_min + thumb_len]
    }

    /// Compute the bounds of the horizontal scrollbar and the thumb
    /// in object-centered coordinates.
    pub fn horizontal_scrollbar(&self) -> Option<(Rect, [f32; 2])> {
        if self.scrollbar_size.y <= 0. {
            return None;
        }
        let content_inset = self.content_inset();
        let half_size = 0.5 * self.size;
        let min_x = -half_size.x + content_inset.left;
        let max_x = half_size.x - content_inset.right - self.scrollbar_size.x;
        let max_y = half_size.y - content_inset.bottom;
        let min_y = max_y - self.scrollbar_size.y;
        let gutter = Rect {
            min: Vec2::new(min_x, min_y),
            max: Vec2::new(max_x, max_y),
        };
        Some((
            gutter,
            Self::compute_thumb(
                gutter.min.x,
                self.content_size.x,
                gutter.size().x,

Review comment (refactor):
You have the option to simplify this to: 
```suggestion
            + scroll_position * gutter_length / content_length;
```
If you find it too verbose
<img width="355" height="226" alt="Screenshot 2025-11-28 at 6 25 32 PM" src="https://github.com/user-attachments/assets/4460218c-aafb-4ae1-9d00-7e8452ab0063" />
I understand if you want to keep it written the way you have it since they’re equivalent / you may find it clearer the way you wrote it.

At the very least, I think it’s easier to read the line was changed to have the multiplicative term first i.e.
```suggestion
            + scroll_position * (gutter_length - thumb_len) / (content_length - gutter_length);
```
so it can be seen as multiplying by the ratio of “scrollable length in scrollbar” to “original hidden content length"

The new tests you wrote pass for me when I replace with the first suggested line.

Code after:
        clip_rect.max.y -= clip_inset.bottom;

        if overflow.x == OverflowAxis::Visible {
            clip_rect.min.x = -f32::INFINITY;
            clip_rect.max.x = f32::INFINITY;
        }
        if overflow.y == OverflowAxis::Visible {
            clip_rect.min.y = -f32::INFINITY;
            clip_rect.max.y = f32::INFINITY;
        }

        clip_rect
    }

    const fn compute_thumb(
        gutter_min: f32,
        content_length: f32,
        gutter_length: f32,
        scroll_position: f32,
    ) -> [f32; 2] {
        if content_length <= gutter_length {
            return [gutter_min, gutter_min + gutter_length];
        }
        let thumb_len = gutter_length * gutter_length / content_length;
        let thumb_min = gutter_min + scroll_position * gutter_length / content_length;
        [thumb_min, thumb_min + thumb_len]
    }

    /// Compute the bounds of the horizontal scrollbar and the thumb
    /// in object-centered coordinates.
    pub fn horizontal_scrollbar(&self) -> Option<(Rect, [f32; 2])> {
        if self.scrollbar_size.y <= 0. {
            return None;
        }
        let content_inset = self.content_inset();
        let half_size = 0.5 * self.size;
        let min_x = -half_size.x + content_inset.left;
        let max_x = half_size.x - content_inset.right - self.scrollbar_size.x;
        let max_y = half_size.y - content_inset.bottom;
        let min_y = max_y - self.scrollbar_size.y;
        let gutter = Rect {
            min: Vec2::new(min_x, min_y),
            max: Vec2::new(max_x, max_y),
        };
        Some((
            gutter,
            Self::compute_thumb(
                gutter.min.x,
                self.content_size.x,
                gutter.size().x,
                self.scroll_position.x,