From cb7880ad7f2f90b2414441d7253f2a5b93b06bb5 Mon Sep 17 00:00:00 2001 From: iFwu <16523864+iFwu@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:58:32 +0800 Subject: [PATCH] fix(core): land a List's jump past rows changed above the viewport A scroll to a row or to the end, asked in the same update as rows inserted, removed or measured above the viewport, landed shifted by those rows: updateList added what they moved into anchor, and the anchoring ItemScrollBy went out after the jump's ItemScrollTo, so the platform applied both. The jump sets the offset outright from the new sizes, so it now drops the anchor, as it already drops a pending retarget. Fixes #1019 --- CHANGELOG.md | 1 + core/list.go | 3 ++- core/list_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0c6bff52..703dd2c23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -121,6 +121,7 @@ - Keep what `ui.SidebarShell`'s sidebar, footer, headers and content hold and `ui.NavHeader`'s buttons clear of the left and right safe area, as on an iPhone in landscape, without padding a page inside the shell twice, and drop the gallery's own padding for it (#835 by @houko). - Have `nsgotest` count a press of another view as a new click on the macOS and Linux targets, as headless, so a press on a page that a press just pushed or popped reaches it instead of going to the page's previous view as a double click's second (#854 by @houko). - Free a removed view that was laid out on its own as a relayout boundary instead of the queue of boundaries keeping it, and through it the whole subtree it was in, until a later queue as long reused its slot (#1007 by @iFwu). +- Land a `List`'s scroll to a row, or to its end, where it asks when rows were inserted, removed or measured above the viewport in the same update, instead of shifted by the scroll that would have kept the visible rows in place (#1019 by @iFwu). ## [0.2.0] - 2026-11-19 diff --git a/core/list.go b/core/list.go index deb6f451c..d3804caca 100644 --- a/core/list.go +++ b/core/list.go @@ -694,7 +694,8 @@ func (t *Tree) updateList(v *node) bool { p.at, p.animating = offset, cmd.animated st.pin = &p } - retarget = nil + // The jump sets the offset outright: the anchoring scroll this update's changes asked for is already in it. + retarget, anchor = nil, 0 if !cmd.animated { st.jump(offset) } diff --git a/core/list_test.go b/core/list_test.go index 7a0565127..3cb589510 100644 --- a/core/list_test.go +++ b/core/list_test.go @@ -73,6 +73,35 @@ func TestKeyedListKeepsTheVisibleRowsInPlace(t *testing.T) { } } +// A jump to a row the same batch inserts above the viewport lands on that row, animated or not: the jump replaces the offset the inserted row would have kept. +func TestKeyedListJumpsToARowInsertedAboveTheViewport(t *testing.T) { + for _, animated := range []bool{false, true} { + var ids *core.Signal[[]int] + ref := &core.Ref{} + a := nsgotest.Start(t, func(cx *core.Scope) core.Node { + ids = core.NewSignalFunc(cx, makeIDs(100, 0), nil) + return core.List{TestID: "list", Ref: ref, Style: core.Style{Height: core.Pt(400)}, + Count: func() int { return len(ids.Get()) }, Key: func(i int) any { return ids.Get()[i] }, + EstimatedSize: 40, Row: func(cx *core.Scope, r core.ListRow) core.Node { + return core.Text{ContentFunc: func() string { return rowText(ids.Get()[r.Index()]) }, Style: core.Style{Height: core.Pt(40)}} + }} + }) + a.ScrollTo("id:list", core.Point{Y: 1000}) + a.Post(func() { + ids.Set(slices.Insert(slices.Clone(ids.Get()), 20, 1000)) + ref.ScrollToIndexAt(20, 0, 0, animated) + }) + a.Settle() + list := a.Find("id:list") + if got := onScreen(t, a, list, 1000); got != 0 { + t.Errorf("animated %v: the inserted row is %v from the viewport's start, want 0", animated, got) + } + if list.Offset.Y != 800 { + t.Errorf("animated %v: offset %v, want 800", animated, list.Offset.Y) + } + } +} + func makeIDs(n, from int) []int { out := make([]int, n) for i := range out {