From 909139da6a7540008a56be8f93a0b62e37c3cbb7 Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Thu, 19 Nov 2026 23:43:32 +0800 Subject: [PATCH] ui: clamp List wheel offsets to the measured extent --- base/list.go | 5 +++++ ui/list_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/base/list.go b/base/list.go index a7dd98f5..0e381bff 100644 --- a/base/list.go +++ b/base/list.go @@ -923,6 +923,11 @@ func (p *Page) ListArea(c C, state *ListState, n int, horizontal bool, row func( // last paint moved: the rows are placed for that end, not for the offset the // area had, which would leave the paint that adds them blank. scroll.Offset = max(state.top(n)-viewport, 0) + } else { + // Measuring the previous paint can shorten the estimated content. Choose + // rows against that extent before layout clamps the old offset; otherwise + // the rows declared at the old end are outside the new viewport. + scroll.Offset = min(max(scroll.Offset, 0), max(state.top(n)-viewport, 0)) } // The rows are placed and hit-tested on whole logical pixels, as the layout // applies them, so a label's native texture survives a fractional scroll. diff --git a/ui/list_test.go b/ui/list_test.go index f7bdcd04..dd1e1c50 100644 --- a/ui/list_test.go +++ b/ui/list_test.go @@ -135,6 +135,33 @@ func TestListRowHeightAtReportsLaidOutRows(t *testing.T) { } } +// The first paint estimates unseen rows at 32 px. Once its 24 px rows are +// measured, a wheel to the old end must use the new extent before choosing rows. +func TestListWheelClampsToTheMeasuredExtent(t *testing.T) { + var state ui.ListState + state.Label = func(i int) string { return "row " + strconv.Itoa(i) } + d := openPage(t, func(c ui.C) { + ui.Column(c).Fill().Children(func() { + ui.List(c, &state, 400, func(row ui.ListRow) { + row.Element.Height(24).Children(func() { + ui.Text(row.C, "row "+strconv.Itoa(row.Index)) + }) + }).Label("Rows").Grow(1) + }) + }) + area := d.ListArea("Rows") + if err := d.driver.Scroll(gfx.ScrollEvent{X: area.X + 20, Y: area.Y + 20, DeltaY: 20000, Precise: true}); err != nil { + t.Fatal(err) + } + if !d.has("row 399") { + t.Fatalf("the end is blank after scrolling: Visible %v", visibleOf(&state)) + } + bottom := d.hit("row 399").Bounds + if gap := area.Y + area.Height - bottom.Y - bottom.Height; gap < -1 || gap > 1 { + t.Fatalf("last row is %v px from the end of the viewport", gap) + } +} + func fmtSscan(Value string, i *int) (int, error) { n, err := strconv.Atoi(strings.TrimPrefix(Value, "row ")) *i = n