Merge pull request #217 from dweymouth/perf/reuse-widgets
Reuse complex widgets across pages to reduce memory spikes when browsing
This commit is contained in:
+46
-24
@@ -71,6 +71,8 @@ func NewGridViewAlbumIterator(iter mediaprovider.AlbumIterator) GridViewIterator
|
||||
type GridView struct {
|
||||
widget.BaseWidget
|
||||
|
||||
stateMutex sync.RWMutex
|
||||
fetchCancel context.CancelFunc
|
||||
GridViewState
|
||||
|
||||
grid *xwidget.GridWrap
|
||||
@@ -78,12 +80,10 @@ type GridView struct {
|
||||
|
||||
type GridViewState struct {
|
||||
items []GridViewItemModel
|
||||
itemsMutex sync.RWMutex
|
||||
iter GridViewIterator
|
||||
imageFetcher ImageFetcher
|
||||
placeholder fyne.Resource
|
||||
Placeholder fyne.Resource
|
||||
highestShown int
|
||||
fetchCancel context.CancelFunc
|
||||
done bool
|
||||
|
||||
OnPlay func(id string, shuffle bool)
|
||||
@@ -104,7 +104,7 @@ func NewFixedGridView(items []GridViewItemModel, fetch ImageFetcher, placeholder
|
||||
items: items,
|
||||
done: true,
|
||||
imageFetcher: fetch,
|
||||
placeholder: placeholder,
|
||||
Placeholder: placeholder,
|
||||
},
|
||||
}
|
||||
g.ExtendBaseWidget(g)
|
||||
@@ -117,7 +117,7 @@ func NewGridView(iter GridViewIterator, fetch ImageFetcher, placeholder fyne.Res
|
||||
GridViewState: GridViewState{
|
||||
iter: iter,
|
||||
imageFetcher: fetch,
|
||||
placeholder: placeholder,
|
||||
Placeholder: placeholder,
|
||||
},
|
||||
}
|
||||
g.ExtendBaseWidget(g)
|
||||
@@ -128,14 +128,16 @@ func NewGridView(iter GridViewIterator, fetch ImageFetcher, placeholder fyne.Res
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GridView) SaveToState() GridViewState {
|
||||
func (g *GridView) SaveToState() *GridViewState {
|
||||
g.stateMutex.RLock()
|
||||
s := g.GridViewState
|
||||
g.stateMutex.RUnlock()
|
||||
s.scrollPos = g.grid.GetScrollOffset()
|
||||
return s
|
||||
return &s
|
||||
}
|
||||
|
||||
func NewGridViewFromState(state GridViewState) *GridView {
|
||||
g := &GridView{GridViewState: state}
|
||||
func NewGridViewFromState(state *GridViewState) *GridView {
|
||||
g := &GridView{GridViewState: *state}
|
||||
g.ExtendBaseWidget(g)
|
||||
g.createGridWrap()
|
||||
g.Refresh() // needed to initialize the widget
|
||||
@@ -144,8 +146,12 @@ func NewGridViewFromState(state GridViewState) *GridView {
|
||||
}
|
||||
|
||||
func (g *GridView) Clear() {
|
||||
g.itemsMutex.Lock()
|
||||
defer g.itemsMutex.Unlock()
|
||||
if g.fetchCancel != nil {
|
||||
g.fetchCancel()
|
||||
g.fetchCancel = nil
|
||||
}
|
||||
g.stateMutex.Lock()
|
||||
defer g.stateMutex.Unlock()
|
||||
g.items = nil
|
||||
g.done = true
|
||||
}
|
||||
@@ -155,13 +161,27 @@ func (g *GridView) Reset(iter GridViewIterator) {
|
||||
g.fetchCancel()
|
||||
g.fetchCancel = nil
|
||||
}
|
||||
g.itemsMutex.Lock()
|
||||
g.stateMutex.Lock()
|
||||
g.items = nil
|
||||
g.done = false
|
||||
g.highestShown = 0
|
||||
g.iter = iter
|
||||
g.itemsMutex.Unlock()
|
||||
g.stateMutex.Unlock()
|
||||
g.fetchMoreItems(36)
|
||||
g.Refresh()
|
||||
}
|
||||
|
||||
func (g *GridView) ResetFromState(state *GridViewState) {
|
||||
if g.fetchCancel != nil {
|
||||
g.fetchCancel()
|
||||
g.fetchCancel = nil
|
||||
}
|
||||
g.stateMutex.Lock()
|
||||
g.GridViewState = *state
|
||||
g.stateMutex.Unlock()
|
||||
g.grid.Refresh()
|
||||
g.grid.ScrollToOffset(state.scrollPos)
|
||||
g.grid.Refresh()
|
||||
}
|
||||
|
||||
func (g *GridView) ResetFixed(items []GridViewItemModel) {
|
||||
@@ -169,12 +189,13 @@ func (g *GridView) ResetFixed(items []GridViewItemModel) {
|
||||
g.fetchCancel()
|
||||
g.fetchCancel = nil
|
||||
}
|
||||
g.itemsMutex.Lock()
|
||||
g.stateMutex.Lock()
|
||||
g.items = items
|
||||
g.itemsMutex.Unlock()
|
||||
g.done = true
|
||||
g.highestShown = 0
|
||||
g.iter = nil
|
||||
g.stateMutex.Unlock()
|
||||
g.Refresh()
|
||||
}
|
||||
|
||||
func (g *GridView) GetScrollOffset() float32 {
|
||||
@@ -192,7 +213,7 @@ func (g *GridView) createGridWrap() {
|
||||
},
|
||||
// create func
|
||||
func() fyne.CanvasObject {
|
||||
card := NewGridViewItem(g.placeholder)
|
||||
card := NewGridViewItem(g.Placeholder)
|
||||
card.OnPlay = func(shuffle bool) {
|
||||
if g.OnPlay != nil {
|
||||
g.OnPlay(card.ItemID(), shuffle)
|
||||
@@ -220,7 +241,7 @@ func (g *GridView) createGridWrap() {
|
||||
}
|
||||
card.OnDownload = func() {
|
||||
if g.OnDownload != nil {
|
||||
g.OnDownload(card.itemID)
|
||||
g.OnDownload(card.ItemID())
|
||||
}
|
||||
}
|
||||
return card
|
||||
@@ -238,13 +259,14 @@ func (g *GridView) doUpdateItemCard(itemIdx int, card *GridViewItem) {
|
||||
g.highestShown = itemIdx
|
||||
}
|
||||
var item GridViewItemModel
|
||||
g.itemsMutex.RLock()
|
||||
g.stateMutex.RLock()
|
||||
// itemIdx can rarely be out of range if the data is being updated
|
||||
// as the view is requested to refresh
|
||||
if itemIdx < len(g.items) {
|
||||
item = g.items[itemIdx]
|
||||
}
|
||||
g.itemsMutex.RUnlock()
|
||||
g.stateMutex.RUnlock()
|
||||
card.Cover.Im.CenterIcon = g.Placeholder
|
||||
if card.PrevID == item.ID {
|
||||
// nothing to do
|
||||
return
|
||||
@@ -290,8 +312,8 @@ func (g *GridView) doUpdateItemCard(itemIdx int, card *GridViewItem) {
|
||||
}
|
||||
|
||||
func (g *GridView) lenItems() int {
|
||||
g.itemsMutex.RLock()
|
||||
defer g.itemsMutex.RUnlock()
|
||||
g.stateMutex.RLock()
|
||||
defer g.stateMutex.RUnlock()
|
||||
return len(g.items)
|
||||
}
|
||||
|
||||
@@ -314,15 +336,15 @@ func (g *GridView) fetchMoreItems(count int) {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
g.itemsMutex.Lock()
|
||||
g.stateMutex.Lock()
|
||||
g.items = append(g.items, items...)
|
||||
g.itemsMutex.Unlock()
|
||||
g.stateMutex.Unlock()
|
||||
if len(items) < batchFetchSize {
|
||||
g.done = true
|
||||
}
|
||||
n += len(items)
|
||||
if len(items) > 0 {
|
||||
g.Refresh()
|
||||
g.grid.Refresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,6 +211,10 @@ func (g *GridViewItem) Update(model GridViewItemModel) {
|
||||
g.Cover.ResetPlayButton()
|
||||
}
|
||||
|
||||
func (g *GridViewItem) Refresh() {
|
||||
g.BaseWidget.Refresh()
|
||||
}
|
||||
|
||||
func (g *GridViewItem) ItemID() string {
|
||||
return g.itemID
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ import (
|
||||
// a placeholder with a rectangular border frame
|
||||
// and an icon positioned in the center of the frame.
|
||||
type ImagePlaceholder struct {
|
||||
ScaleMode canvas.ImageScale
|
||||
ScaleMode canvas.ImageScale
|
||||
CenterIcon fyne.Resource
|
||||
|
||||
widget.BaseWidget
|
||||
content *fyne.Container
|
||||
@@ -30,7 +31,7 @@ type ImagePlaceholder struct {
|
||||
}
|
||||
|
||||
func NewImagePlaceholder(centerIcon fyne.Resource, minSize float32) *ImagePlaceholder {
|
||||
i := &ImagePlaceholder{minSize: minSize}
|
||||
i := &ImagePlaceholder{minSize: minSize, CenterIcon: centerIcon}
|
||||
i.ExtendBaseWidget(i)
|
||||
i.iconImage = canvas.NewImageFromResource(centerIcon)
|
||||
i.iconImage.FillMode = canvas.ImageFillContain
|
||||
@@ -86,6 +87,7 @@ func (i *ImagePlaceholder) MinSize() fyne.Size {
|
||||
|
||||
func (i *ImagePlaceholder) Refresh() {
|
||||
i.border.Hidden = i.HaveImage()
|
||||
i.iconImage.Resource = i.CenterIcon
|
||||
i.iconImage.Hidden = i.HaveImage()
|
||||
i.imageDisp.Hidden = !i.HaveImage()
|
||||
i.imageDisp.ScaleMode = i.ScaleMode
|
||||
|
||||
+20
-9
@@ -47,9 +47,7 @@ type TracklistSort struct {
|
||||
ColumnName string
|
||||
}
|
||||
|
||||
type Tracklist struct {
|
||||
widget.BaseWidget
|
||||
|
||||
type TracklistOptions struct {
|
||||
// AutoNumber sets whether to auto-number the tracks 1..N in display order,
|
||||
// or to use the number from the track's metadata
|
||||
AutoNumber bool
|
||||
@@ -68,6 +66,12 @@ type Tracklist struct {
|
||||
|
||||
// Disables sorting the tracklist by clicking individual columns.
|
||||
DisableSorting bool
|
||||
}
|
||||
|
||||
type Tracklist struct {
|
||||
widget.BaseWidget
|
||||
|
||||
Options TracklistOptions
|
||||
|
||||
// user action callbacks
|
||||
OnPlayTrackAt func(int)
|
||||
@@ -141,7 +145,7 @@ func NewTracklist(tracks []*mediaprovider.Track) *Tracklist {
|
||||
tr := item.(*TrackRow)
|
||||
tr.trackIdx = itemID
|
||||
i := -1 // signal that we want to display the actual track num.
|
||||
if t.AutoNumber {
|
||||
if t.Options.AutoNumber {
|
||||
i = itemID + 1
|
||||
}
|
||||
tr.Update(t.trackModelAt(itemID), i)
|
||||
@@ -153,6 +157,13 @@ func NewTracklist(tracks []*mediaprovider.Track) *Tracklist {
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *Tracklist) Reset() {
|
||||
t.Clear()
|
||||
t.Options = TracklistOptions{}
|
||||
t.ctxMenu = nil
|
||||
t.SetSorting(TracklistSort{})
|
||||
}
|
||||
|
||||
func (t *Tracklist) buildHeader() {
|
||||
t.hdr = NewListHeader([]ListColumn{
|
||||
{Text: "#", Alignment: fyne.TextAlignTrailing, CanToggleVisible: false},
|
||||
@@ -331,7 +342,7 @@ func (t *Tracklist) CreateRenderer() fyne.WidgetRenderer {
|
||||
}
|
||||
|
||||
func (t *Tracklist) Refresh() {
|
||||
t.hdr.DisableSorting = t.DisableSorting
|
||||
t.hdr.DisableSorting = t.Options.DisableSorting
|
||||
t.BaseWidget.Refresh()
|
||||
}
|
||||
|
||||
@@ -487,7 +498,7 @@ func (t *Tracklist) onShowContextMenu(e *fyne.PointEvent, trackIdx int) {
|
||||
t.list.Refresh()
|
||||
if t.ctxMenu == nil {
|
||||
t.ctxMenu = fyne.NewMenu("")
|
||||
if !t.DisablePlaybackMenu {
|
||||
if !t.Options.DisablePlaybackMenu {
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items,
|
||||
fyne.NewMenuItem("Play", func() {
|
||||
if t.OnPlaySelection != nil {
|
||||
@@ -530,9 +541,9 @@ func (t *Tracklist) onShowContextMenu(e *fyne.PointEvent, trackIdx int) {
|
||||
t.onSetRatings(t.selectedTracks(), rating, true)
|
||||
})
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items, ratingMenu)
|
||||
if len(t.AuxiliaryMenuItems) > 0 {
|
||||
if len(t.Options.AuxiliaryMenuItems) > 0 {
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items, fyne.NewMenuItemSeparator())
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items, t.AuxiliaryMenuItems...)
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items, t.Options.AuxiliaryMenuItems...)
|
||||
}
|
||||
}
|
||||
widget.ShowPopUpMenuAtPosition(t.ctxMenu, fyne.CurrentApp().Driver().CanvasForObject(t), e.AbsolutePosition)
|
||||
@@ -754,7 +765,7 @@ func (t *TrackRow) Update(tm *trackModel, rowNum int) {
|
||||
var str string
|
||||
if rowNum < 0 {
|
||||
rowNum = tr.TrackNumber
|
||||
if t.tracklist.ShowDiscNumber {
|
||||
if t.tracklist.Options.ShowDiscNumber {
|
||||
discNum = tr.DiscNumber
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
// Component that manages lazily loading more tracks into a Tracklist
|
||||
// as the user scrolls near the bottom.
|
||||
type TracklistLoader struct {
|
||||
disposed bool
|
||||
|
||||
tracklist *Tracklist
|
||||
iter mediaprovider.TrackIterator
|
||||
|
||||
@@ -28,11 +30,17 @@ func NewTracklistLoader(tracklist *Tracklist, iter mediaprovider.TrackIterator)
|
||||
return t
|
||||
}
|
||||
|
||||
// Cancels all asynchronous loads so that they will no longer modify the tracklist.
|
||||
func (t *TracklistLoader) Dispose() {
|
||||
t.disposed = true
|
||||
t.tracklist.OnTrackShown = nil
|
||||
}
|
||||
|
||||
func (t *TracklistLoader) onTrackShown(tracknum int) {
|
||||
if tracknum > t.highestShown {
|
||||
t.highestShown = tracknum
|
||||
}
|
||||
if t.highestShown >= t.len-25 && !t.fetching && !t.done {
|
||||
if t.highestShown >= t.len-25 && !t.fetching && !t.done && !t.disposed {
|
||||
t.fetching = true
|
||||
go t.loadMoreTracks(25)
|
||||
}
|
||||
@@ -52,6 +60,12 @@ func (t *TracklistLoader) loadMoreTracks(num int) {
|
||||
break
|
||||
}
|
||||
t.trackBuffer = append(t.trackBuffer, tr)
|
||||
if t.disposed {
|
||||
break
|
||||
}
|
||||
}
|
||||
if t.disposed {
|
||||
return
|
||||
}
|
||||
t.tracklist.AppendTracks(t.trackBuffer)
|
||||
t.tracklist.Refresh()
|
||||
|
||||
Reference in New Issue
Block a user