From e8d751bc2ad147da019f510384b6ae10c8ab7c25 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Thu, 16 May 2024 18:32:13 -0700 Subject: [PATCH] first draft of synced lyrics integration, missing seek support --- ui/browsing/nowplayingpage.go | 3 ++- ui/widgets/lyricsviewer.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ui/browsing/nowplayingpage.go b/ui/browsing/nowplayingpage.go index 3cc6d28..c3a9ea5 100644 --- a/ui/browsing/nowplayingpage.go +++ b/ui/browsing/nowplayingpage.go @@ -241,8 +241,9 @@ func (s *nowPlayingPageState) Restore() Page { var _ CanShowPlayTime = (*NowPlayingPage)(nil) -func (a *NowPlayingPage) OnPlayTimeUpdate(_, _ float64) { +func (a *NowPlayingPage) OnPlayTimeUpdate(curTime, _ float64) { a.formatStatusLine() + a.lyricsViewer.UpdatePlayPos(curTime) } var _ CanSelectAll = (*NowPlayingPage)(nil) diff --git a/ui/widgets/lyricsviewer.go b/ui/widgets/lyricsviewer.go index 1456dc8..21d277a 100644 --- a/ui/widgets/lyricsviewer.go +++ b/ui/widgets/lyricsviewer.go @@ -13,6 +13,8 @@ type LyricsViewer struct { noLyricsLabel widget.Label viewer *fynelyrics.LyricsViewer + lyrics *mediaprovider.Lyrics + nextLyricLine int container *fyne.Container isEmpty bool @@ -28,6 +30,8 @@ func NewLyricsViewer() *LyricsViewer { } func (l *LyricsViewer) SetLyrics(lyrics *mediaprovider.Lyrics) { + l.lyrics = lyrics + l.nextLyricLine = 0 if lyrics == nil || len(lyrics.Lines) == 0 { if !l.isEmpty { l.container.Objects[0] = &l.noLyricsLabel @@ -53,6 +57,33 @@ func (l *LyricsViewer) SetLyrics(lyrics *mediaprovider.Lyrics) { } } +func (l *LyricsViewer) UpdatePlayPos(timeSecs float64) { + if l.lyrics == nil || !l.lyrics.Synced { + return + } + // advance if needed + if l.lyrics.Lines[l.nextLyricLine].Start <= timeSecs { + l.viewer.NextLine() + if l.nextLyricLine < len(l.lyrics.Lines) { + l.nextLyricLine++ + } + } +} + +func (l *LyricsViewer) OnSeeked(timeSecs float64) { + if l.lyrics == nil || !l.lyrics.Synced { + return + } + curLine := 0 + for i, l := range l.lyrics.Lines { + if l.Start < timeSecs { + curLine = i + 1 + break + } + } + l.viewer.SetCurrentLine(curLine) +} + func (l *LyricsViewer) CreateRenderer() fyne.WidgetRenderer { return widget.NewSimpleRenderer(l.container) }