fix crash when showing pop up play queue with nothing playing, also let it be freed after non-use

This commit is contained in:
Drew Weymouth
2024-07-25 17:28:24 -07:00
parent da2a175c5b
commit e9946c0511
+32 -3
View File
@@ -11,6 +11,7 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"time" "time"
"github.com/dweymouth/supersonic/backend" "github.com/dweymouth/supersonic/backend"
@@ -47,7 +48,9 @@ type Controller struct {
ReloadFunc func() ReloadFunc func()
RefreshPageFunc func() RefreshPageFunc func()
popUpQueueMutex sync.Mutex
popUpQueue *widgets.PlayQueueList popUpQueue *widgets.PlayQueueList
popUpQueueLastUsed int64
escapablePopUp *widget.PopUp escapablePopUp *widget.PopUp
haveModal bool haveModal bool
runOnModalClosed func() runOnModalClosed func()
@@ -110,15 +113,41 @@ func (m *Controller) HaveModal() bool {
} }
func (m *Controller) ShowPopUpPlayQueue() { func (m *Controller) ShowPopUpPlayQueue() {
m.popUpQueueMutex.Lock()
if m.popUpQueue == nil { if m.popUpQueue == nil {
m.popUpQueue = widgets.NewPlayQueueList(m.App.ImageManager, false) m.popUpQueue = widgets.NewPlayQueueList(m.App.ImageManager, false)
m.popUpQueue.Reorderable = true m.popUpQueue.Reorderable = true
m.popUpQueue.SetItems(m.App.PlaybackManager.GetPlayQueue()) m.popUpQueue.SetItems(m.App.PlaybackManager.GetPlayQueue())
m.ConnectPlayQueuelistActions(m.popUpQueue) m.ConnectPlayQueuelistActions(m.popUpQueue)
// free popUpQueue if it hasn't been used in awhile
go func() {
t := time.NewTicker(1 * time.Minute)
for range t.C {
m.popUpQueueMutex.Lock()
now := time.Now().UnixMilli()
if m.popUpQueueLastUsed < now-120_000 /*2 min*/ {
m.popUpQueue = nil
m.popUpQueueLastUsed = 0
m.popUpQueueMutex.Unlock()
t.Stop()
return
} }
m.popUpQueue.SetNowPlaying(m.App.PlaybackManager.NowPlaying().Metadata().ID) m.popUpQueueMutex.Unlock()
m.popUpQueue.UnselectAll() }
m.popUpQueue.ScrollToNowPlaying() }()
}
m.popUpQueueLastUsed = time.Now().UnixMilli()
popUpQueue := m.popUpQueue
m.popUpQueueMutex.Unlock()
npID := ""
if np := m.App.PlaybackManager.NowPlaying(); np != nil {
npID = np.Metadata().ID
}
popUpQueue.SetNowPlaying(npID)
popUpQueue.UnselectAll()
popUpQueue.ScrollToNowPlaying()
title := widget.NewRichTextWithText(lang.L("Play Queue")) title := widget.NewRichTextWithText(lang.L("Play Queue"))
title.Segments[0].(*widget.TextSegment).Style.Alignment = fyne.TextAlignCenter title.Segments[0].(*widget.TextSegment).Style.Alignment = fyne.TextAlignCenter