Skip to content

Commit

Permalink
add debouncing for gridview loading
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Feb 21, 2025
1 parent 2d209a2 commit cbbdad3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
47 changes: 42 additions & 5 deletions ui/widgets/gridview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"sync"
"time"

"fyne.io/fyne/v2/lang"

Expand All @@ -19,6 +20,17 @@ import (
"fyne.io/fyne/v2/widget"
)

var (
gridViewUpdateCounter = util.NewEventCounter(70)

emptyItem = GridViewItemModel{
ID: "dummy",
Name: "—",
Secondary: []string{"—"},
Suffix: "—",
}
)

const batchFetchSize = 6

type BatchingIterator[M any] struct {
Expand Down Expand Up @@ -340,16 +352,41 @@ func (g *GridView) doUpdateItemCard(itemIdx int, card *GridViewItem) {
}
card.ItemIndex = itemIdx
g.itemForIndex[itemIdx] = card
g.stateMutex.Unlock()

card.ShowSuffix = g.ShowSuffix
card.Cover.Im.PlaceholderIcon = g.Placeholder
if !card.NeedsUpdate(item) {
// nothing to do
g.stateMutex.Unlock()
return
}
card.Cover.Im.PlaceholderIcon = g.Placeholder
g.stateMutex.Unlock()
card.Update(&item)
card.ImgLoader.Load(item.CoverArtID)

if gridViewUpdateCounter.NumEventsSince(time.Now().Add(-150*time.Millisecond)) > 64 {
if card.itemID != emptyItem.ID {
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 itemIdx > g.lenItems()-10 {
Expand Down
7 changes: 4 additions & 3 deletions ui/widgets/gridviewitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,10 @@ type GridViewItem struct {
focusRect *canvas.Rectangle

// updated by GridView
Cover *coverImage
ImgLoader util.ThumbnailLoader
ItemIndex int
Cover *coverImage
ImgLoader util.ThumbnailLoader
ItemIndex int
NextUpdateModel *GridViewItemModel

OnPlay func()
OnFavorite func(bool)
Expand Down

0 comments on commit cbbdad3

Please sign in to comment.