-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
gridview.go
477 lines (424 loc) · 11.7 KB
/
gridview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package widgets
import (
"context"
"fmt"
"sync"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/sharedutil"
myTheme "github.com/dweymouth/supersonic/ui/theme"
"github.com/dweymouth/supersonic/ui/util"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
const batchFetchSize = 6
type BatchingIterator[M any] struct {
iter mediaprovider.MediaIterator[M]
}
func NewBatchingIterator[M any](iter mediaprovider.MediaIterator[M]) BatchingIterator[M] {
return BatchingIterator[M]{iter}
}
func (b *BatchingIterator[M]) NextN(n int) []*M {
results := make([]*M, 0, n)
i := 0
for i < n {
value := b.iter.Next()
if value == nil {
break
}
results = append(results, value)
i++
}
return results
}
type GridViewIterator interface {
NextN(int) []GridViewItemModel
}
type gridViewAlbumIterator struct {
iter BatchingIterator[mediaprovider.Album]
}
func (g gridViewAlbumIterator) NextN(n int) []GridViewItemModel {
albums := g.iter.NextN(n)
return sharedutil.MapSlice(albums, func(al *mediaprovider.Album) GridViewItemModel {
return GridViewItemModel{
Name: al.Name,
ID: al.ID,
CoverArtID: al.CoverArtID,
Secondary: al.ArtistNames,
SecondaryIDs: al.ArtistIDs,
}
})
}
func NewGridViewAlbumIterator(iter mediaprovider.AlbumIterator) GridViewIterator {
return gridViewAlbumIterator{iter: NewBatchingIterator(iter)}
}
type gridViewArtistIterator struct {
iter BatchingIterator[mediaprovider.Artist]
}
func (g gridViewArtistIterator) NextN(n int) []GridViewItemModel {
artists := g.iter.NextN(n)
return sharedutil.MapSlice(artists, func(ar *mediaprovider.Artist) GridViewItemModel {
albumsLabel := "albums"
if ar.AlbumCount == 1 {
albumsLabel = "album"
}
return GridViewItemModel{
Name: ar.Name,
ID: ar.ID,
CoverArtID: ar.CoverArtID,
Secondary: []string{fmt.Sprintf("%d %s", ar.AlbumCount, albumsLabel)},
}
})
}
func NewGridViewArtistIterator(iter mediaprovider.ArtistIterator) GridViewIterator {
return gridViewArtistIterator{iter: NewBatchingIterator(iter)}
}
type GridView struct {
widget.BaseWidget
stateMutex sync.RWMutex
fetchCancel context.CancelFunc
GridViewState
grid *disabledGridWrap
loadingDots *LoadingDots
menu *widget.PopUpMenu
menuGridViewItemId string
itemForIndex map[int]*GridViewItem
itemWidth float32
numColsCached int
shareMenuItem *fyne.MenuItem
}
type GridViewState struct {
items []GridViewItemModel
iter GridViewIterator
imageFetcher util.ImageFetcher
Placeholder fyne.Resource
highestShown int
done bool
DisableSharing bool
OnPlay func(id string, shuffle bool)
OnPlayNext func(id string)
OnAddToQueue func(id string)
OnAddToPlaylist func(id string)
OnDownload func(id string)
OnShare func(id string)
OnShowItemPage func(id string)
OnShowSecondaryPage func(id string)
scrollPos float32
}
var _ fyne.Widget = (*GridView)(nil)
func newGridView() *GridView {
g := &GridView{
loadingDots: NewLoadingDots(),
itemWidth: NewGridViewItem(nil).MinSize().Width,
itemForIndex: make(map[int]*GridViewItem),
}
return g
}
func NewFixedGridView(items []GridViewItemModel, fetch util.ImageFetcher, placeholder fyne.Resource) *GridView {
g := newGridView()
g.GridViewState = GridViewState{
items: items,
done: true,
imageFetcher: fetch,
Placeholder: placeholder,
}
g.ExtendBaseWidget(g)
g.createGridWrap()
return g
}
func NewGridView(iter GridViewIterator, fetch util.ImageFetcher, placeholder fyne.Resource) *GridView {
g := newGridView()
g.GridViewState = GridViewState{
iter: iter,
imageFetcher: fetch,
Placeholder: placeholder,
}
g.ExtendBaseWidget(g)
g.createGridWrap()
g.loadingDots.Start()
// fetch initial items
g.checkFetchMoreItems(36)
return g
}
func (g *GridView) SaveToState() *GridViewState {
g.stateMutex.RLock()
s := g.GridViewState
g.stateMutex.RUnlock()
s.scrollPos = g.grid.GetScrollOffset()
return &s
}
func NewGridViewFromState(state *GridViewState) *GridView {
g := newGridView()
g.GridViewState = *state
g.ExtendBaseWidget(g)
g.createGridWrap()
g.Refresh() // needed to initialize the widget
g.grid.ScrollToOffset(state.scrollPos)
return g
}
func (g *GridView) Clear() {
g.stateMutex.Lock()
defer g.stateMutex.Unlock()
g.cancelFetch()
g.items = nil
g.done = true
}
func (g *GridView) Reset(iter GridViewIterator) {
g.stateMutex.Lock()
g.cancelFetch()
g.items = nil
g.itemForIndex = make(map[int]*GridViewItem)
g.done = false
g.highestShown = 0
g.iter = iter
g.stateMutex.Unlock()
g.checkFetchMoreItems(36)
g.loadingDots.Start()
g.Refresh()
}
func (g *GridView) ResetFromState(state *GridViewState) {
g.stateMutex.Lock()
g.cancelFetch()
g.GridViewState = *state
g.itemForIndex = make(map[int]*GridViewItem)
g.stateMutex.Unlock()
g.grid.Refresh()
g.grid.ScrollToOffset(state.scrollPos)
}
func (g *GridView) ResetFixed(items []GridViewItemModel) {
g.stateMutex.Lock()
g.cancelFetch()
g.items = items
g.itemForIndex = make(map[int]*GridViewItem)
g.done = true
g.highestShown = 0
g.iter = nil
g.stateMutex.Unlock()
g.Refresh()
}
func (g *GridView) GetScrollOffset() float32 {
return g.grid.GetScrollOffset()
}
func (g *GridView) ScrollToOffset(offs float32) {
g.grid.ScrollToOffset(offs)
}
func (g *GridView) Resize(size fyne.Size) {
g.numColsCached = -1
g.BaseWidget.Resize(size)
}
var _ fyne.Tappable = (*GridView)(nil)
func (g *GridView) Tapped(*fyne.PointEvent) {
fyne.CurrentApp().Driver().CanvasForObject(g).Unfocus()
}
func (g *GridView) createGridWrap() {
g.grid = NewDisabledGridWrap(
g.lenItems,
g.createNewItemCard,
// update func
func(itemID widget.GridWrapItemID, obj fyne.CanvasObject) {
ac := obj.(*GridViewItem)
g.doUpdateItemCard(int(itemID), ac)
},
)
}
func (g *GridView) createNewItemCard() fyne.CanvasObject {
card := NewGridViewItem(g.Placeholder)
card.ItemIndex = -1
card.ImgLoader = util.NewThumbnailLoader(g.imageFetcher, card.Cover.SetImage)
card.ImgLoader.OnBeforeLoad = func() { card.Cover.SetImage(nil) }
card.OnPlay = func() { g.onPlay(card.ItemID(), false) }
card.OnShowSecondaryPage = func(id string) {
if g.OnShowSecondaryPage != nil {
g.OnShowSecondaryPage(id)
}
}
card.OnShowItemPage = func() {
if g.OnShowItemPage != nil {
g.OnShowItemPage(card.ItemID())
}
}
card.OnShowContextMenu = func(p fyne.Position) {
g.showContextMenu(card, p)
}
card.OnFocusNeighbor = func(neighbor int) {
focusIndex := -1
switch neighbor {
case 0: // left
focusIndex = card.ItemIndex - 1
case 1: // right
focusIndex = card.ItemIndex + 1
case 2: // up
focusIndex = card.ItemIndex - g.grid.ColumnCount()
case 3: // down
focusIndex = card.ItemIndex + g.grid.ColumnCount()
}
if focusIndex >= 0 && focusIndex < g.lenItems() {
g.grid.ScrollTo(focusIndex)
g.stateMutex.RLock()
if item, ok := g.itemForIndex[focusIndex]; ok {
fyne.CurrentApp().Driver().CanvasForObject(g).Focus(item)
}
g.stateMutex.RUnlock()
}
}
return card
}
func (g *GridView) doUpdateItemCard(itemIdx int, card *GridViewItem) {
if itemIdx > g.highestShown {
g.highestShown = itemIdx
}
var item GridViewItemModel
g.stateMutex.Lock()
// 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]
}
// update itemForIndex map
if c, ok := g.itemForIndex[card.ItemIndex]; ok && c == card {
delete(g.itemForIndex, card.ItemIndex)
}
card.ItemIndex = itemIdx
g.itemForIndex[itemIdx] = card
card.Cover.Im.CenterIcon = g.Placeholder
if !card.NeedsUpdate(item) && card.ItemIndex == itemIdx {
// nothing to do
g.stateMutex.Unlock()
return
}
g.stateMutex.Unlock()
card.Update(item)
card.ImgLoader.Load(item.CoverArtID)
// if user has scrolled near the bottom, fetch more
if itemIdx > g.lenItems()-10 {
g.checkFetchMoreItems(20)
}
}
func (g *GridView) lenItems() int {
g.stateMutex.RLock()
defer g.stateMutex.RUnlock()
return len(g.items)
}
// fetches at least count more items if fetch not in progress and not done
// acquires stateMutex for atomicity
func (g *GridView) checkFetchMoreItems(count int) {
g.stateMutex.Lock()
defer g.stateMutex.Unlock()
if g.done || g.fetchCancel != nil {
return // done, or fetch already in progress
}
if g.iter == nil {
g.done = true
return
}
ctx, cancel := context.WithCancel(context.Background())
g.fetchCancel = cancel
go func() {
// keep repeating the fetch task as long as the user
// has scrolled near the bottom
for !g.done && g.highestShown >= g.lenItems()-10 {
n := 0
for !g.done && n < count {
items := g.iter.NextN(batchFetchSize)
select {
case <-ctx.Done():
return
default:
g.stateMutex.Lock()
g.items = append(g.items, items...)
g.stateMutex.Unlock()
g.loadingDots.Stop()
if len(items) < batchFetchSize {
g.done = true
}
n += len(items)
if len(items) > 0 {
g.grid.Refresh()
}
}
}
}
// call cancelfunc to release Context resources
g.stateMutex.Lock()
g.cancelFetch()
g.stateMutex.Unlock()
}()
}
// must be called with stateMutex locked for writing
func (g *GridView) cancelFetch() {
if g.fetchCancel != nil {
g.fetchCancel()
g.fetchCancel = nil
}
}
func (g *GridView) showContextMenu(card *GridViewItem, pos fyne.Position) {
g.menuGridViewItemId = card.ItemID()
if g.menu == nil {
play := fyne.NewMenuItem("Play", func() { g.onPlay(g.menuGridViewItemId, false) })
play.Icon = theme.MediaPlayIcon()
shuffle := fyne.NewMenuItem("Shuffle", func() { g.onPlay(g.menuGridViewItemId, true) })
shuffle.Icon = myTheme.ShuffleIcon
queueNext := fyne.NewMenuItem("Play next", func() {
if g.OnPlayNext != nil {
g.OnPlayNext(g.menuGridViewItemId)
}
})
queueNext.Icon = myTheme.PlayNextIcon
queue := fyne.NewMenuItem("Add to queue", func() {
if g.OnAddToQueue != nil {
g.OnAddToQueue(g.menuGridViewItemId)
}
})
queue.Icon = theme.ContentAddIcon()
playlist := fyne.NewMenuItem("Add to playlist...", func() {
if g.OnAddToPlaylist != nil {
g.OnAddToPlaylist(g.menuGridViewItemId)
}
})
playlist.Icon = myTheme.PlaylistIcon
download := fyne.NewMenuItem("Download...", func() {
if g.OnDownload != nil {
g.OnDownload(g.menuGridViewItemId)
}
})
download.Icon = theme.DownloadIcon()
g.shareMenuItem = fyne.NewMenuItem("Share...", func() {
g.OnShare(g.menuGridViewItemId)
})
g.shareMenuItem.Icon = myTheme.ShareIcon
g.menu = widget.NewPopUpMenu(fyne.NewMenu("", play, shuffle, queueNext, queue, playlist, download, g.shareMenuItem),
fyne.CurrentApp().Driver().CanvasForObject(g))
}
g.shareMenuItem.Disabled = g.DisableSharing
g.menu.ShowAtPosition(pos)
}
func (g *GridView) onPlay(itemID string, shuffle bool) {
if g.OnPlay != nil {
g.OnPlay(itemID, shuffle)
}
}
func (g *GridView) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(container.NewStack(
g.grid, container.NewCenter(g.loadingDots),
))
}
// a disabled widget is not considered focusable by the focus manager
type disabledGridWrap struct {
widget.GridWrap
}
func NewDisabledGridWrap(len func() int, create func() fyne.CanvasObject, update func(widget.GridWrapItemID, fyne.CanvasObject)) *disabledGridWrap {
g := &disabledGridWrap{
GridWrap: widget.GridWrap{
Length: len,
CreateItem: create,
UpdateItem: update,
},
}
g.ExtendBaseWidget(g)
return g
}
var _ fyne.Disableable = (*disabledGridWrap)(nil)
func (g *disabledGridWrap) Disabled() bool { return true }
func (g *disabledGridWrap) Disable() {}
func (g *disabledGridWrap) Enable() {}