From bbc1f4b732f2631d23806cdefcb5cf440e62861e Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Tue, 1 Dec 2026 20:46:44 +0800 Subject: [PATCH] fix(base): keep the rows on screen when rows above them are measured A List places rows it has not measured by the average height of those it has. Measuring a row above the viewport, or the estimate changing with it, moved where every row on screen was computed to start, so reading a long list of uneven rows upward from its end (or after a fling skipped rows) made the rows jump, sometimes by more than the viewport. The list now keeps the row at the top of the viewport where the offset was chosen for it: the next paint moves the offset by as much as that row moved in content space, and a paint whose overscan rows measured differently is painted again with the offset kept. Requests to scroll, following the end and scrollbar drags are left as they were. --- base/list.go | 54 ++++++++++++++++++++++++++-- ui/list_anchor_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 ui/list_anchor_test.go diff --git a/base/list.go b/base/list.go index 069fe9fc6..91a69c0f6 100644 --- a/base/list.go +++ b/base/list.go @@ -121,6 +121,12 @@ type listInternals struct { scrollX float32 // how far the rows are scrolled sideways, as the user left it View Rect // the area as painted, for the drag and the width of the rows drag listDrag + // anchorRow is 1 + the row at the top of the viewport in the last paint, or 0, and + // anchorAt where that paint chose the offset for it to start. Measuring rows above it + // changes where the list computes it to start; the next paint moves the offset by + // as much, so what the user is reading stays where it was (see keepAnchor). + anchorRow int32 + anchorAt float32 } // listDeclRow is a row a List or a Tree declared in the last paint: its element @@ -610,6 +616,23 @@ func (s *ListState) afterLayout() { } } +// keepAnchor moves the offset by as much as the row at the top of the viewport in the +// last paint moved in content space since: rows above it measured at other heights +// than the list estimated, or the estimate of the rows still unmeasured changing with +// them. Without it, a wheel that brings rows in from above (a log opened at its end +// and read upward) makes the rows on screen jump by the difference, sometimes by +// more than the viewport. A scroll by the wheel or the keys between the paints moved +// the offset itself and stays in it. A request to scroll (ScrollTo) places the offset +// where it asks instead, and a scrollbar drag puts it under the pointer. +func (s *ListState) keepAnchor(scroll *ScrollState) { + in := &s.list + row := int(in.anchorRow) - 1 + if row < 0 || row >= in.n || in.pending != listScrollNone || scroll.Dragging() { + return + } + scroll.Offset += s.top(row) - in.anchorAt +} + // scrollTarget is where the request kind puts row as the list knows the heights now: // the offset to scroll to, and whether the end of the content is what it scrolls to, // which only a layout can place. @@ -784,6 +807,9 @@ type listDecl struct { rows, until int32 // the List's rows in Page.listRows pinned int32 // the element of the pinned header, or -1 pinnedRow int + anchor int // the row at the top of the viewport, or -1 when the list does not keep one + anchorAt float32 // where the offset was chosen for the anchor to start, in content space + spacer float32 // the height above the first row declared: where the list placed it } // listOverscan is how many rows a List declares beyond what is visible each way, so @@ -939,24 +965,30 @@ func (p *Page) ListArea(c C, state *ListState, n int, horizontal bool, row func( if viewport <= 0 { viewport = c.f.bounds.Height } - if pinned || scroll.pinned() { + atEnd := pinned || scroll.pinned() + if atEnd { // The layout puts the area at its end, which the rows that arrived since the // 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 { + state.keepAnchor(scroll) // 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)) } + in.anchorRow = 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. offset := snapScroll(scroll.Offset) c.f.Nodes[area.i].Scroll = offset first := max(state.rowAt(offset)-listOverscan, 0) + anchor := -1 if in.n == 0 { first = 0 + } else if !atEnd { + anchor = min(state.rowAt(offset), in.n-1) } bottom := offset + viewport last := first @@ -1034,7 +1066,8 @@ func (p *Page) ListArea(c C, state *ListState, n int, horizontal bool, row func( } }) in.pinned, in.pinnedRow = pinnedNode, pin - p.lists = append(p.lists, listDecl{state: state, First: first, rows: int32(start), until: int32(len(p.listRows)), pinned: pinnedNode, pinnedRow: pin}) + p.lists = append(p.lists, listDecl{state: state, First: first, rows: int32(start), until: int32(len(p.listRows)), pinned: pinnedNode, pinnedRow: pin, + anchor: anchor, anchorAt: state.top(max(anchor, 0)), spacer: state.top(first)}) return area } @@ -1326,8 +1359,23 @@ func rowSlotHeight(node *Node) float32 { // measureLists records the heights the layout gave the rows the Lists declared. func (p *Page) measureLists() { for _, list := range p.lists { + in := &list.state.list + at := list.spacer // where the layout placed each row, from the first declared for k, node := range p.listRows[list.rows:list.until] { - list.state.measured(list.First+k, rowSlotHeight(&p.ctx.f.Nodes[node])) + h := rowSlotHeight(&p.ctx.f.Nodes[node]) + if list.First+k == list.anchor { + // The paint after this one keeps the anchor where the offset was chosen + // for it, not where the rows above it measured in this paint put it. + in.anchorRow, in.anchorAt = int32(list.anchor+1), list.anchorAt + if moved := at - list.anchorAt; moved > 0.5 || moved < -0.5 { + // Rows declared above it to fill the overscan measured at other + // heights than estimated, so this paint shows it moved: paint + // again with the offset kept rather than show that for a frame. + p.retry = true + } + } + at += h + list.state.measured(list.First+k, h) } if list.pinned >= 0 { list.state.measured(list.pinnedRow, rowSlotHeight(&p.ctx.f.Nodes[list.pinned])) diff --git a/ui/list_anchor_test.go b/ui/list_anchor_test.go new file mode 100644 index 000000000..a6e6f74bd --- /dev/null +++ b/ui/list_anchor_test.go @@ -0,0 +1,81 @@ +package ui_test + +import ( + "strconv" + "testing" + + "github.com/vivaui/viva/ui" +) + +// talkRows is a conversation: two short lines, then a long reply, over and over, so +// the rows a List has not measured are far from the average of those it has. +type talkRows struct { + list ui.ListState + n int +} + +func (m *talkRows) height(i int) float32 { + if i%3 == 2 { + return 300 + } + return 30 +} + +func (m *talkRows) page(c ui.C) { + ui.Column(c).Fill().Children(func() { + ui.List(c, &m.list, m.n, func(row ui.ListRow) { + row.Element.Height(m.height(row.Index)).Children(func() { + ui.Text(row.C, "row "+strconv.Itoa(row.Index)) + }) + }).Label("Talk").Grow(1) + }) +} + +// rowTop is where the row named label starts on screen. +func (d *pageDriver) rowTop(label string) (float32, bool) { + for _, hit := range d.driver.Snapshot().Hits { + if hit.Label == label && !hit.IsLabel && !hit.Backdrop { + return hit.Bounds.Y, true + } + } + return 0, false +} + +// Read upward from the end, a List brings in rows it has never measured. Their heights, +// and the average it estimates the rows above them by, change where the list computes +// every row above to start; the rows on screen must still move by what the wheel moved +// them, as on a page, not jump by the difference (which here is more than the viewport). +func TestListKeepsTheRowsOnScreenAsRowsAboveAreMeasured(t *testing.T) { + m := &talkRows{n: 600} + m.list.Label = func(i int) string { return "row " + strconv.Itoa(i) } + d := openPage(t, m.page) + d.repaint() + m.list.ScrollToEnd() + for range 12 { + d.repaint() + } + area := d.ListArea("Talk") + for step := range 300 { + // A short row well inside the viewport, so its bounds are not clipped. + label := "" + var before float32 + first, last := m.list.Visible() + for i := first; i <= last && label == ""; i++ { + if y, ok := d.rowTop("row " + strconv.Itoa(i)); ok && i%3 != 2 && y > 100 && y < 300 { + label, before = "row "+strconv.Itoa(i), y + } + } + if label == "" { + d.wheel(area, 0, -24, 0) + continue + } + d.wheel(area, 0, -24, 0) + after, ok := d.rowTop(label) + if !ok { + t.Fatalf("step %d: a wheel of 24 took %s at %v off the screen", step, label, before) + } + if moved := after - before; moved < 23 || moved > 25 { + t.Fatalf("step %d: a wheel of 24 moved %s by %v", step, label, moved) + } + } +}