From 4e1f51f3a196c996d0eb7a56c0a9a02e7b6b8fb8 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Thu, 30 Jan 2025 18:31:58 -0300 Subject: [PATCH] more fyne.Dos --- ui/browsing/browsingpane.go | 42 +++++++++++++++++++--------------- ui/browsing/nowplayingpage.go | 43 +++++++++++++---------------------- ui/browsing/playlistspage.go | 16 ++++++++----- 3 files changed, 50 insertions(+), 51 deletions(-) diff --git a/ui/browsing/browsingpane.go b/ui/browsing/browsingpane.go index e92b753b..c004e1bf 100644 --- a/ui/browsing/browsingpane.go +++ b/ui/browsing/browsingpane.go @@ -255,30 +255,36 @@ func (b *BrowsingPane) doSetPage(p Page) bool { } func (b *BrowsingPane) onSongChange(song mediaprovider.MediaItem, lastScrobbledIfAny *mediaprovider.Track) { - if b.curPage == nil { - return - } - if p, ok := b.curPage.(CanShowNowPlaying); ok { - fyne.Do(func() { p.OnSongChange(song, lastScrobbledIfAny) }) - } + fyne.Do(func() { + if b.curPage == nil { + return + } + if p, ok := b.curPage.(CanShowNowPlaying); ok { + p.OnSongChange(song, lastScrobbledIfAny) + } + }) } func (b *BrowsingPane) onPlayTimeUpdate(cur, total float64, seeked bool) { - if b.curPage == nil { - return - } - if p, ok := b.curPage.(CanShowPlayTime); ok { - p.OnPlayTimeUpdate(cur, total, seeked) - } + fyne.Do(func() { + if b.curPage == nil { + return + } + if p, ok := b.curPage.(CanShowPlayTime); ok { + p.OnPlayTimeUpdate(cur, total, seeked) + } + }) } func (b *BrowsingPane) onQueueChange() { - if b.curPage == nil { - return - } - if p, ok := b.curPage.(CanShowPlayQueue); ok { - p.OnPlayQueueChange() - } + fyne.Do(func() { + if b.curPage == nil { + return + } + if p, ok := b.curPage.(CanShowPlayQueue); ok { + p.OnPlayQueueChange() + } + }) } func (b *BrowsingPane) addPageToHistory(p Page, truncate bool) { diff --git a/ui/browsing/nowplayingpage.go b/ui/browsing/nowplayingpage.go index 4645f9fe..0a65869c 100644 --- a/ui/browsing/nowplayingpage.go +++ b/ui/browsing/nowplayingpage.go @@ -9,7 +9,6 @@ import ( "net/url" "slices" "strings" - "sync" "time" "github.com/cenkalti/dominantcolor" @@ -49,9 +48,6 @@ type NowPlayingPage struct { related []*mediaprovider.Track alreadyLoaded bool - lyricLock sync.Mutex - relatedLock sync.Mutex - // widgets for render background *canvas.LinearGradient queueList *widgets.PlayQueueList @@ -106,9 +102,10 @@ func NewNowPlayingPage( a := &NowPlayingPage{nowPlayingPageState: state} a.ExtendBaseWidget(a) - pm.OnPaused(a.formatStatusLine) - pm.OnPlaying(a.formatStatusLine) - pm.OnStopped(a.formatStatusLine) + doFmtStatus := func() { fyne.Do(a.formatStatusLine) } + pm.OnPaused(doFmtStatus) + pm.OnPlaying(doFmtStatus) + pm.OnStopped(doFmtStatus) a.card = widgets.NewLargeNowPlayingCard() a.card.OnAlbumNameTapped = func() { @@ -320,9 +317,6 @@ func (a *NowPlayingPage) onImageLoaded(img image.Image, err error) { } func (a *NowPlayingPage) updateLyrics() { - a.lyricLock.Lock() - defer a.lyricLock.Unlock() - if a.lyricFetchCancel != nil { a.lyricFetchCancel() } @@ -369,20 +363,17 @@ func (a *NowPlayingPage) fetchLyrics(ctx context.Context, song *mediaprovider.Tr case <-ctx.Done(): return default: - a.lyricLock.Lock() - a.lyricsLoading.Stop() - a.lyricsViewer.SetLyrics(lyrics) - if lyrics != nil { - a.lyricsViewer.OnSeeked(a.lastPlayPos) - } - a.lyricLock.Unlock() + fyne.Do(func() { + a.lyricsLoading.Stop() + a.lyricsViewer.SetLyrics(lyrics) + if lyrics != nil { + a.lyricsViewer.OnSeeked(a.lastPlayPos) + } + }) } } func (a *NowPlayingPage) updateRelatedList() { - a.relatedLock.Lock() - defer a.relatedLock.Unlock() - if a.relatedFetchCancel != nil { a.relatedFetchCancel() } @@ -407,11 +398,11 @@ func (a *NowPlayingPage) updateRelatedList() { case <-ctx.Done(): return default: - a.relatedLock.Lock() - a.related = related - a.relatedList.SetTracks(a.related) - a.relatedLoading.Stop() - a.relatedLock.Unlock() + fyne.Do(func() { + a.related = related + a.relatedList.SetTracks(a.related) + a.relatedLoading.Stop() + }) } }(ctx) } @@ -459,8 +450,6 @@ func (a *NowPlayingPage) OnPlayTimeUpdate(curTime, _ float64, seeked bool) { if a.tabs == nil || a.tabs.SelectedIndex() != 1 /*lyrics*/ { return } - a.lyricLock.Lock() - defer a.lyricLock.Unlock() if seeked { a.lyricsViewer.OnSeeked(curTime) } else { diff --git a/ui/browsing/playlistspage.go b/ui/browsing/playlistspage.go index 52c02883..f96f83b3 100644 --- a/ui/browsing/playlistspage.go +++ b/ui/browsing/playlistspage.go @@ -108,17 +108,21 @@ func (p *PlaylistsPage) Scroll(scrollAmt float32) { } } +// should be called asynchronously func (a *PlaylistsPage) load(searchOnLoad bool) { playlists, err := a.mp.GetPlaylists() if err != nil { log.Printf("error loading playlists: %v", err.Error()) } - a.playlists = playlists - if searchOnLoad { - a.onSearched(a.searcher.Entry.Text) - } else { - a.refreshView(playlists) - } + + fyne.Do(func() { + a.playlists = playlists + if searchOnLoad { + a.onSearched(a.searcher.Entry.Text) + } else { + a.refreshView(playlists) + } + }) } func (a *PlaylistsPage) createListView() {