Skip to content

Commit

Permalink
debounce tracklist row rendering when fast scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jan 12, 2025
1 parent e163666 commit 021609a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
43 changes: 43 additions & 0 deletions ui/util/eventcounter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package util

import (
"time"
)

type EventCounter struct {
buf []time.Time
ptr int
}

func NewEventCounter(maxN int) *EventCounter {
buffer := make([]time.Time, maxN)
return &EventCounter{
buf: buffer,
}
}

func (e *EventCounter) Add() {
e.buf[e.ptr] = time.Now()
e.ptr = (e.ptr + 1) % len(e.buf)
}

func (e *EventCounter) NumEventsSince(t time.Time) int {
i := e.ptr - 1

count := 0
for {
if i < 0 {
i = len(e.buf) - 1
}
if !e.buf[i].IsZero() && e.buf[i].After(t) {
count++
} else {
break
}
if count == len(e.buf) {
break
}
i--
}
return count
}
45 changes: 45 additions & 0 deletions ui/widgets/tracklistrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"image"
"strconv"
"sync"
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
Expand All @@ -15,10 +16,31 @@ import (
"fyne.io/fyne/v2/widget"
ttwidget "github.com/dweymouth/fyne-tooltip/widget"
"github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/backend/mediaprovider"
myTheme "github.com/dweymouth/supersonic/ui/theme"
"github.com/dweymouth/supersonic/ui/util"
)

var (
tracklistUpdateCounter = util.NewEventCounter(30)

emptyTrack = util.TrackListModel{
Item: &mediaprovider.Track{
ID: "dummy",
Title: "—",
ArtistIDs: []string{"—"},
ArtistNames: []string{"—"},
Album: "—",
Genres: []string{"—"},
ComposerNames: []string{"—"},
ComposerIDs: []string{"—"},
Comment: "—",
FilePath: "—",
ContentType: "—",
},
}
)

const tracklistThumbnailSize = 48

type TracklistColumn struct {
Expand Down Expand Up @@ -151,6 +173,9 @@ type tracklistRowBase struct {
isFavorite bool
playCount int

nextUpdateModel *util.TrackListModel
nextUpdateRowNum int

num *widget.Label
name *ttwidget.RichText
artist *MultiHyperlink
Expand Down Expand Up @@ -318,6 +343,25 @@ func (t *tracklistRowBase) TrackID() string {
}

func (t *tracklistRowBase) Update(tm *util.TrackListModel, rowNum int) {
if tracklistUpdateCounter.NumEventsSince(time.Now().Add(-150*time.Millisecond)) > 20 {
t.doUpdate(&emptyTrack, 1)
if t.nextUpdateModel == nil {
go fyne.Do(func() {
if t.nextUpdateModel != nil {
t.doUpdate(t.nextUpdateModel, t.nextUpdateRowNum)
}
t.nextUpdateModel = nil
})
}
t.nextUpdateModel = tm
t.nextUpdateRowNum = rowNum
} else {
t.nextUpdateModel = nil
t.doUpdate(tm, rowNum)
}
}

func (t *tracklistRowBase) doUpdate(tm *util.TrackListModel, rowNum int) {
changed := false
if tm.Selected != t.Selected {
t.Selected = tm.Selected
Expand Down Expand Up @@ -415,6 +459,7 @@ func (t *tracklistRowBase) Update(tm *util.TrackListModel, rowNum int) {
}

if changed {
tracklistUpdateCounter.Add()
t.Refresh()
}
}
Expand Down

0 comments on commit 021609a

Please sign in to comment.