Skip to content

Commit

Permalink
Merge pull request #1933 from fpabl0/fix/1864
Browse files Browse the repository at this point in the history
Fix widget.List layout when the items length is lower than the current visible ones, fixes #1864
  • Loading branch information
andydotxyz committed Feb 12, 2021
2 parents e685b54 + 3f5094f commit c8a14e0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions widget/list.go
Expand Up @@ -207,6 +207,11 @@ func (l *listRenderer) Layout(size fyne.Size) {
return
}
min := int(fyne.Min(float32(length), float32(l.visibleItemCount)))
if length < len(l.children) {
l.firstItemIndex = 0
l.list.offsetY = 0
l.visibleItemCount = min
}
if len(l.children) > min {
for i := len(l.children); i >= min; i-- {
l.itemPool.Release(l.children[i-1])
Expand Down
40 changes: 40 additions & 0 deletions widget/list_test.go
Expand Up @@ -308,6 +308,46 @@ func TestList_RemoveItem(t *testing.T) {
test.AssertRendersToMarkup(t, "list/item_removed.xml", w.Canvas())
}

func TestList_ScrollThenShrink(t *testing.T) {
test.NewApp()
defer test.NewApp()

data := make([]string, 0, 20)
for i := 0; i < 20; i++ {
data = append(data, fmt.Sprintf("Data %d", i))
}

list := NewList(
func() int {
return len(data)
},
func() fyne.CanvasObject {
return NewLabel("TEMPLATE")
},
func(id ListItemID, item fyne.CanvasObject) {
item.(*Label).SetText(data[id])
},
)
w := test.NewWindow(list)
w.Resize(fyne.NewSize(300, 300))

visibles := test.WidgetRenderer(list).(*listRenderer).children
visibleCount := len(visibles)
assert.Equal(t, visibleCount, 8)

list.scroller.ScrollToBottom()
visibles = test.WidgetRenderer(list).(*listRenderer).children
assert.Equal(t, "Data 19", visibles[len(visibles)-1].(*listItem).child.(*Label).Text)

data = data[:1]
assert.NotPanics(t, func() { list.Refresh() })

visibles = test.WidgetRenderer(list).(*listRenderer).children
visibleCount = len(visibles)
assert.Equal(t, visibleCount, 1)
assert.Equal(t, "Data 0", visibles[0].(*listItem).child.(*Label).Text)
}

func TestList_NoFunctionsSet(t *testing.T) {
list := &List{}
w := test.NewWindow(list)
Expand Down

0 comments on commit c8a14e0

Please sign in to comment.