diff --git a/base/list.go b/base/list.go index 6c40cde31..f27fea8fe 100644 --- a/base/list.go +++ b/base/list.go @@ -218,7 +218,9 @@ func (s *ListState) HasFocus() bool { // list has not laid out is placed at the average height of the rows it has, so the // request stays for the paints that show the rows around the target and measure // them, and goes once the offset no longer moves; the list asks for those paints -// itself, and lets the request go if anything else moves the list first. +// itself, and lets the request go if anything else moves the list first. On a list +// that follows its end, a row away from the end leaves the end, as a scroll by the +// user does; ScrollToEnd follows it again. func (s *ListState) ScrollTo(row int, align ListAlign) { kind := listScrollStart switch align { @@ -910,7 +912,9 @@ func (p *Page) ListArea(c C, state *ListState, n int, horizontal bool, row func( id := p.ID(at) // Follow is declared on the area the List returns, after its rows: whether the area // keeps to its end is what the paint before left it, as ScrollState.Pinned says. - pinned := c.f.Scrolls.get(id).pinned() + // ScrollArea forgets whether it follows until that declaration, so it is kept here. + prev := c.f.Scrolls.get(id) + pinned, followed := prev.pinned(), prev.follow area := c.f.ScrollArea(id) p.SetNode(area.i, at) in := &state.list @@ -918,11 +922,17 @@ func (p *Page) ListArea(c C, state *ListState, n int, horizontal bool, row func( state.resize(n) scroll := c.f.Exts[c.f.Nodes[area.i].Ext].scrollState in.Scroll = scroll + // The request settles against the follow the paint before declared: a ScrollTo + // away from the end of a following list leaves the end, as a scroll by the user + // does, instead of the Follow(true) after the rows pulling the list back to it. + scroll.follow = followed if state.applyScrollRequest(c.f) { // The area was declared at the offset it had; the layout reads the node, so it // takes the requested one too, which the rows are placed for below (as ScrollAt). c.f.Nodes[area.i].Scroll = scroll.Offset + pinned = scroll.pinned() } + scroll.follow = false viewport := scroll.Viewport if viewport <= 0 { viewport = c.f.bounds.Height diff --git a/ui/list_follow_test.go b/ui/list_follow_test.go index 8afb14d56..4545dd4a5 100644 --- a/ui/list_follow_test.go +++ b/ui/list_follow_test.go @@ -100,3 +100,57 @@ func TestListFollowReachesTheEndOfALongListItStartsWith(t *testing.T) { t.Fatalf("the last row painted %v (Visible %v, at the end %v)", d.has("row 4999"), visibleOf(&m.list), m.list.AtEnd()) } } + +// ScrollTo on a List that follows its end leaves the end, as a scroll by the user +// does: the request is applied when the list is declared, before the Box it returns +// says Follow(true) again, and the rows that arrive after it do not pull the list +// back to the end. ScrollToEnd follows the end again. +func TestListScrollToLeavesTheEndOfAFollowingList(t *testing.T) { + m := newLogRows(400) + d := openPage(t, followingRows(m)) + settle := func(step string) { + t.Helper() + for frames := 0; d.driver.Transitioning(); frames++ { + if frames == 10 { + t.Fatalf("%s: the list asked for ten paints", step) + } + if err := d.driver.Advance(16 * time.Millisecond); err != nil { + t.Fatal(err) + } + } + d.repaint() + } + d.repaint() + settle("start") + if !m.list.AtEnd() { + t.Fatalf("the following list did not start at its end (Visible %v)", visibleOf(&m.list)) + } + const target = 20 + label := "row " + strconv.Itoa(target) + m.list.ScrollTo(target, ui.ListStart) + d.repaint() + settle("ScrollTo") + if !d.has(label) || m.list.AtEnd() { + t.Fatalf("after ScrollTo(%d) %s painted %v, at the end %v (Visible %v)", target, label, d.has(label), m.list.AtEnd(), visibleOf(&m.list)) + } + area := d.ListArea("Rows") + if top := d.hit(label).Bounds.Y - area.Y; top < -2 || top > 2 { + t.Fatalf("row %d is %v px from the top of the list, want it at the top", target, top) + } + m.n += 5 + d.repaint() + settle("rows arrived") + if !d.has(label) || m.list.AtEnd() { + t.Fatalf("rows that arrived after ScrollTo moved the list: %s painted %v, at the end %v (Visible %v)", label, d.has(label), m.list.AtEnd(), visibleOf(&m.list)) + } + m.list.ScrollToEnd() + d.repaint() + settle("ScrollToEnd") + m.n += 5 + d.repaint() + settle("rows arrived at the end") + last := "row " + strconv.Itoa(m.n-1) + if !d.has(last) || !m.list.AtEnd() { + t.Fatalf("after ScrollToEnd the list did not follow its end: %s painted %v, at the end %v (Visible %v)", last, d.has(last), m.list.AtEnd(), visibleOf(&m.list)) + } +}