add debouncing for gridview loading

This commit is contained in:
Drew Weymouth
2025-02-21 07:58:56 -08:00
parent 2d209a29f5
commit cbbdad3a10
2 changed files with 46 additions and 8 deletions
+42 -5
View File
@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"sync" "sync"
"time"
"fyne.io/fyne/v2/lang" "fyne.io/fyne/v2/lang"
@@ -19,6 +20,17 @@ import (
"fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/widget"
) )
var (
gridViewUpdateCounter = util.NewEventCounter(70)
emptyItem = GridViewItemModel{
ID: "dummy",
Name: "—",
Secondary: []string{"—"},
Suffix: "—",
}
)
const batchFetchSize = 6 const batchFetchSize = 6
type BatchingIterator[M any] struct { type BatchingIterator[M any] struct {
@@ -340,16 +352,41 @@ func (g *GridView) doUpdateItemCard(itemIdx int, card *GridViewItem) {
} }
card.ItemIndex = itemIdx card.ItemIndex = itemIdx
g.itemForIndex[itemIdx] = card g.itemForIndex[itemIdx] = card
g.stateMutex.Unlock()
card.ShowSuffix = g.ShowSuffix card.ShowSuffix = g.ShowSuffix
card.Cover.Im.PlaceholderIcon = g.Placeholder
if !card.NeedsUpdate(item) { if !card.NeedsUpdate(item) {
// nothing to do // nothing to do
g.stateMutex.Unlock()
return return
} }
card.Cover.Im.PlaceholderIcon = g.Placeholder
g.stateMutex.Unlock() if gridViewUpdateCounter.NumEventsSince(time.Now().Add(-150*time.Millisecond)) > 64 {
card.Update(&item) if card.itemID != emptyItem.ID {
card.ImgLoader.Load(item.CoverArtID) card.Update(&emptyItem)
card.ImgLoader.Load("")
}
if card.NextUpdateModel == nil {
// queue to run later
go func() {
<-time.After(10 * time.Millisecond)
fyne.Do(func() {
if card.NextUpdateModel != nil {
gridViewUpdateCounter.Add()
card.Update(card.NextUpdateModel)
card.ImgLoader.Load(card.NextUpdateModel.CoverArtID)
}
card.NextUpdateModel = nil
})
}()
}
card.NextUpdateModel = &item
} else {
card.NextUpdateModel = nil
gridViewUpdateCounter.Add()
card.Update(&item)
card.ImgLoader.Load(item.CoverArtID)
}
// if user has scrolled near the bottom, fetch more // if user has scrolled near the bottom, fetch more
if itemIdx > g.lenItems()-10 { if itemIdx > g.lenItems()-10 {
+4 -3
View File
@@ -308,9 +308,10 @@ type GridViewItem struct {
focusRect *canvas.Rectangle focusRect *canvas.Rectangle
// updated by GridView // updated by GridView
Cover *coverImage Cover *coverImage
ImgLoader util.ThumbnailLoader ImgLoader util.ThumbnailLoader
ItemIndex int ItemIndex int
NextUpdateModel *GridViewItemModel
OnPlay func() OnPlay func()
OnFavorite func(bool) OnFavorite func(bool)