From eab78965008cc3ab007769ca6802d1a259d9bc6f Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Fri, 17 Nov 2023 01:08:11 +0000 Subject: [PATCH] Ensure that scroll offset is in bounds after layout --- src/widget/scroll_view.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/widget/scroll_view.rs b/src/widget/scroll_view.rs index 0364c594e..c368b75f8 100644 --- a/src/widget/scroll_view.rs +++ b/src/widget/scroll_view.rs @@ -117,11 +117,24 @@ impl Widget for ScrollView { height: f64::INFINITY, }, ); - let size = self.child.layout(cx, &cbc); - Size { - width: size.width.min(bc.max().width), - height: size.height.min(bc.max().height), + let child_size = self.child.layout(cx, &cbc); + let size = Size { + width: child_size.width.min(bc.max().width), + height: child_size.height.min(bc.max().height), + }; + + // Ensure that scroll offset is within bounds + let max_offset = (child_size.height - size.height).max(0.0); + if max_offset < self.offset { + self.offset = max_offset; + let new_origin = Point { + x: 0.0, + y: -self.offset, + }; + self.child.set_origin(cx.widget_state, new_origin); } + + size } fn paint(&mut self, cx: &mut PaintCx, builder: &mut SceneBuilder) {