fix goroutine handling for pop-up queue

This commit is contained in:
Drew Weymouth
2025-04-10 08:23:56 -07:00
parent 08d477a438
commit 765e21305a
7 changed files with 44 additions and 57 deletions
+3 -2
View File
@@ -12,6 +12,7 @@ import (
"github.com/dweymouth/supersonic/backend" "github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/res" "github.com/dweymouth/supersonic/res"
"github.com/dweymouth/supersonic/ui" "github.com/dweymouth/supersonic/ui"
"github.com/dweymouth/supersonic/ui/util"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/app" "fyne.io/fyne/v2/app"
@@ -79,8 +80,8 @@ func main() {
mainWindow := ui.NewMainWindow(fyneApp, res.AppName, res.DisplayName, res.AppVersion, myApp) mainWindow := ui.NewMainWindow(fyneApp, res.AppName, res.DisplayName, res.AppVersion, myApp)
mainWindow.Window.SetMaster() mainWindow.Window.SetMaster()
myApp.OnReactivate = mainWindow.Show myApp.OnReactivate = util.FyneDoFunc(mainWindow.Show)
myApp.OnExit = func() { fyne.Do(mainWindow.Quit) } myApp.OnExit = util.FyneDoFunc(mainWindow.Quit)
windowStartupTasks := sync.OnceFunc(func() { windowStartupTasks := sync.OnceFunc(func() {
defaultServer := myApp.ServerManager.GetDefaultServer() defaultServer := myApp.ServerManager.GetDefaultServer()
+3 -9
View File
@@ -41,15 +41,9 @@ func NewBottomPanel(pm *backend.PlaybackManager, im *backend.ImageManager, contr
}) })
}) })
pm.OnPaused(func() { pm.OnPaused(util.FyneDoFunc(func() { bp.Controls.SetPlaying(false) }))
fyne.Do(func() { bp.Controls.SetPlaying(false) }) pm.OnPlaying(util.FyneDoFunc(func() { bp.Controls.SetPlaying(true) }))
}) pm.OnStopped(util.FyneDoFunc(func() { bp.Controls.SetPlaying(false) }))
pm.OnPlaying(func() {
fyne.Do(func() { bp.Controls.SetPlaying(true) })
})
pm.OnStopped(func() {
fyne.Do(func() { bp.Controls.SetPlaying(false) })
})
bp.NowPlaying = widgets.NewNowPlayingCard() bp.NowPlaying = widgets.NewNowPlayingCard()
bp.NowPlaying.OnCoverTapped = func() { bp.NowPlaying.OnCoverTapped = func() {
+1 -1
View File
@@ -102,7 +102,7 @@ func NewNowPlayingPage(
a := &NowPlayingPage{nowPlayingPageState: state} a := &NowPlayingPage{nowPlayingPageState: state}
a.ExtendBaseWidget(a) a.ExtendBaseWidget(a)
doFmtStatus := func() { fyne.Do(a.formatStatusLine) } doFmtStatus := util.FyneDoFunc(a.formatStatusLine)
pm.OnPaused(doFmtStatus) pm.OnPaused(doFmtStatus)
pm.OnPlaying(doFmtStatus) pm.OnPlaying(doFmtStatus)
pm.OnStopped(doFmtStatus) pm.OnStopped(doFmtStatus)
+23 -36
View File
@@ -10,7 +10,6 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"time" "time"
fynetooltip "github.com/dweymouth/fyne-tooltip" fynetooltip "github.com/dweymouth/fyne-tooltip"
@@ -56,7 +55,6 @@ type Controller struct {
UnselectAllPageFunc func() UnselectAllPageFunc func()
ToastProvider ToastProvider ToastProvider ToastProvider
popUpQueueMutex sync.Mutex
popUpQueue *widget.PopUp popUpQueue *widget.PopUp
popUpQueueList *widgets.PlayQueueList popUpQueueList *widgets.PlayQueueList
popUpQueueLastUsed int64 popUpQueueLastUsed int64
@@ -72,49 +70,41 @@ func New(app *backend.App, appVersion string, mainWindow fyne.Window) *Controlle
App: app, App: app,
} }
c.initVisualizations() c.initVisualizations()
c.App.PlaybackManager.OnQueueChange(func() { c.App.PlaybackManager.OnQueueChange(util.FyneDoFunc(func() {
c.popUpQueueMutex.Lock()
defer c.popUpQueueMutex.Unlock()
if c.popUpQueue != nil { if c.popUpQueue != nil {
c.popUpQueueList.SetItems(c.App.PlaybackManager.GetPlayQueue()) c.popUpQueueList.SetItems(c.App.PlaybackManager.GetPlayQueue())
} }
}) }))
c.App.PlaybackManager.OnSongChange(func(track mediaprovider.MediaItem, _ *mediaprovider.Track) { c.App.PlaybackManager.OnSongChange(func(track mediaprovider.MediaItem, _ *mediaprovider.Track) {
c.popUpQueueMutex.Lock() fyne.Do(func() {
defer c.popUpQueueMutex.Unlock() if c.popUpQueue == nil {
if c.popUpQueue == nil { return
return }
} if track == nil {
if track == nil { c.popUpQueueList.SetNowPlaying("")
c.popUpQueueList.SetNowPlaying("") } else {
} else { c.popUpQueueList.SetNowPlaying(track.Metadata().ID)
c.popUpQueueList.SetNowPlaying(track.Metadata().ID) }
} })
}) })
return c return c
} }
func (m *Controller) SelectAll() { func (m *Controller) SelectAll() {
m.popUpQueueMutex.Lock()
if m.popUpQueue != nil && m.popUpQueue.Visible() { if m.popUpQueue != nil && m.popUpQueue.Visible() {
m.popUpQueueList.SelectAll() m.popUpQueueList.SelectAll()
m.popUpQueueMutex.Unlock()
return return
} }
m.popUpQueueMutex.Unlock()
if m.SelectAllPageFunc != nil { if m.SelectAllPageFunc != nil {
m.SelectAllPageFunc() m.SelectAllPageFunc()
} }
} }
func (m *Controller) UnselectAll() { func (m *Controller) UnselectAll() {
m.popUpQueueMutex.Lock()
if m.popUpQueue != nil && m.popUpQueue.Visible() { if m.popUpQueue != nil && m.popUpQueue.Visible() {
m.popUpQueueList.UnselectAll() m.popUpQueueList.UnselectAll()
m.popUpQueueMutex.Unlock()
return return
} }
m.popUpQueueMutex.Unlock()
if m.SelectAllPageFunc != nil { if m.SelectAllPageFunc != nil {
m.UnselectAllPageFunc() m.UnselectAllPageFunc()
} }
@@ -196,7 +186,6 @@ func (m *Controller) ShowCastMenu(onPendingPlayerChange func()) {
} }
func (m *Controller) ShowPopUpPlayQueue() { func (m *Controller) ShowPopUpPlayQueue() {
m.popUpQueueMutex.Lock()
if m.popUpQueue == nil { if m.popUpQueue == nil {
m.popUpQueueList = widgets.NewPlayQueueList(m.App.ImageManager, false) m.popUpQueueList = widgets.NewPlayQueueList(m.App.ImageManager, false)
m.popUpQueueList.Reorderable = true m.popUpQueueList.Reorderable = true
@@ -229,25 +218,23 @@ func (m *Controller) ShowPopUpPlayQueue() {
go func() { go func() {
t := time.NewTicker(1 * time.Minute) t := time.NewTicker(1 * time.Minute)
for range t.C { for range t.C {
m.popUpQueueMutex.Lock() fyne.Do(func() {
now := time.Now().UnixMilli() now := time.Now().UnixMilli()
if m.popUpQueueLastUsed < now-120_000 /*2 min*/ { if m.popUpQueueLastUsed < now-120_000 /*2 min*/ {
fynetooltip.DestroyPopUpToolTipLayer(m.popUpQueue) fynetooltip.DestroyPopUpToolTipLayer(m.popUpQueue)
m.popUpQueue = nil m.popUpQueue = nil
m.popUpQueueList = nil m.popUpQueueList = nil
m.popUpQueueLastUsed = 0 m.popUpQueueLastUsed = 0
m.popUpQueueMutex.Unlock() t.Stop()
t.Stop() return
return }
} })
m.popUpQueueMutex.Unlock()
} }
}() }()
} }
m.popUpQueueLastUsed = time.Now().UnixMilli() m.popUpQueueLastUsed = time.Now().UnixMilli()
popUpQueueList := m.popUpQueueList popUpQueueList := m.popUpQueueList
pop := m.popUpQueue pop := m.popUpQueue
m.popUpQueueMutex.Unlock()
npID := "" npID := ""
if np := m.App.PlaybackManager.NowPlaying(); np != nil { if np := m.App.PlaybackManager.NowPlaying(); np != nil {
+6 -7
View File
@@ -12,6 +12,7 @@ import (
"github.com/dweymouth/supersonic/backend/mediaprovider" "github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/sharedutil" "github.com/dweymouth/supersonic/sharedutil"
"github.com/dweymouth/supersonic/ui/dialogs" "github.com/dweymouth/supersonic/ui/dialogs"
"github.com/dweymouth/supersonic/ui/util"
) )
// Show dialog to select playlist. // Show dialog to select playlist.
@@ -33,13 +34,11 @@ func (m *Controller) DoAddTracksToPlaylistWorkflow(trackIDs []string) {
m.ToastProvider.ShowSuccessToast(msg) m.ToastProvider.ShowSuccessToast(msg)
}) })
} }
notifyError := func() { notifyError := util.FyneDoFunc(func() {
fyne.Do(func() { m.ToastProvider.ShowErrorToast(
m.ToastProvider.ShowErrorToast( lang.L("An error occurred adding tracks to the playlist"),
lang.L("An error occurred adding tracks to the playlist"), )
) })
})
}
pop.Hide() pop.Hide()
m.App.Config.Application.AddToPlaylistSkipDuplicates = sp.SkipDuplicates m.App.Config.Application.AddToPlaylistSkipDuplicates = sp.SkipDuplicates
if id == "" /* creating new playlist */ { if id == "" /* creating new playlist */ {
+2 -2
View File
@@ -300,8 +300,8 @@ func (m *MainWindow) SetupSystemTrayMenu(appName string, fyneApp fyne.App) {
m.App.PlaybackManager.SetVolume(vol) m.App.PlaybackManager.SetVolume(vol)
}), }),
fyne.NewMenuItemSeparator(), fyne.NewMenuItemSeparator(),
fyne.NewMenuItem(lang.L("Show"), func() { fyne.Do(m.Window.Show) }), fyne.NewMenuItem(lang.L("Show"), m.Window.Show),
fyne.NewMenuItem(lang.L("Hide"), func() { fyne.Do(m.Window.Hide) }), fyne.NewMenuItem(lang.L("Hide"), m.Window.Hide),
) )
desk.SetSystemTrayMenu(menu) desk.SetSystemTrayMenu(menu)
desk.SetSystemTrayIcon(res.ResAppicon256Png) desk.SetSystemTrayIcon(res.ResAppicon256Png)
+6
View File
@@ -33,6 +33,12 @@ const (
DateFormatYMD DateFormatYMD
) )
func FyneDoFunc(f func()) func() {
return func() {
fyne.Do(f)
}
}
func dateFormatForLocale(locale string) DateFormat { func dateFormatForLocale(locale string) DateFormat {
var region string var region string
if i := strings.Index(locale, "-"); i > 0 && len(locale) >= 5 { if i := strings.Index(locale, "-"); i > 0 && len(locale) >= 5 {