diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest b/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest index 15d17f4..fb6bef6 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3 (Package)/Package.appxmanifest @@ -12,7 +12,7 @@ + Version="1.0.56.0" /> diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Events/LyricsChangedEventArgs.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Events/LyricsChangedEventArgs.cs new file mode 100644 index 0000000..9b1960d --- /dev/null +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Events/LyricsChangedEventArgs.cs @@ -0,0 +1,14 @@ +using BetterLyrics.WinUI3.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BetterLyrics.WinUI3.Events +{ + public class LyricsChangedEventArgs(LyricsData? lyricsData) : EventArgs + { + public LyricsData? LyricsData { get; } = lyricsData; + } +} diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/LanguageHelper.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/LanguageHelper.cs index ec161db..f6243a7 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/LanguageHelper.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/LanguageHelper.cs @@ -58,6 +58,11 @@ namespace BetterLyrics.WinUI3.Services _identifier = _factory.Load(PathHelper.LanguageProfilePath); } + private static string SimplifiedChineseOrTraditionalChinese(string text) + { + return text == ChineseConverter.ConvertToSimplifiedChinese(text) ? "zh-Hans" : "zh-Hant"; + } + public static string? DetectLanguageCode(string? text) { if (text == null) return null; @@ -69,7 +74,7 @@ namespace BetterLyrics.WinUI3.Services "simple" => "en", "zh_classical" => "zh-Hant", "zh_yue" => "zh-Hant", - "zh" => text == ChineseConverter.ConvertToSimplifiedChinese(text) ? "zh-Hans" : "zh-Hant", + "zh" => "zh-Hans", _ => code }; return code; diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/LyricsChangedEventArgs.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/LyricsChangedEventArgs.cs deleted file mode 100644 index 13e5bba..0000000 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/LyricsChangedEventArgs.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BetterLyrics.WinUI3.Models -{ - public class LyricsChangedEventArgs : EventArgs - { - } -} diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs index 789bf33..8d5dcbc 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs @@ -1,10 +1,11 @@ // 2025/6/23 by Zhe Fang +using BetterLyrics.WinUI3.Enums; +using BetterLyrics.WinUI3.Events; +using BetterLyrics.WinUI3.Models; using System; using System.Collections.Generic; using System.Threading.Tasks; -using BetterLyrics.WinUI3.Events; -using BetterLyrics.WinUI3.Models; using TagLib.Riff; namespace BetterLyrics.WinUI3.Services.MediaSessionsService @@ -29,6 +30,8 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService bool IsPlaying { get; } SongInfo? SongInfo { get; } TimeSpan Position { get; } - LyricsData? CurrentLyricsData { get; } + + LyricsSearchProvider? LyricsSearchProvider { get; } + TranslationSearchProvider? TranslationSearchProvider { get; } } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.AlbumArtUpdater.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.AlbumArtUpdater.cs index f4c30eb..5c3d45b 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.AlbumArtUpdater.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.AlbumArtUpdater.cs @@ -15,6 +15,8 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService { public partial class MediaSessionsService : IMediaSessionsService { + private readonly LatestOnlyTaskRunner _albumArtRefreshRunner = new(); + public event EventHandler? AlbumArtChanged; private void UpdateAlbumArt() @@ -57,10 +59,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService var albumArtLightAccentColor = ImageHelper.GetAccentColorsFromByte(bytes, 1, false).FirstOrDefault(); var albumArtDarkAccentColor = ImageHelper.GetAccentColorsFromByte(bytes, 1, true).FirstOrDefault(); - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => - { - AlbumArtChanged?.Invoke(this, new AlbumArtChangedEventArgs(null, albumArtSwBitmap, albumArtLightAccentColor, albumArtDarkAccentColor)); - }); + AlbumArtChanged?.Invoke(this, new AlbumArtChangedEventArgs(null, albumArtSwBitmap, albumArtLightAccentColor, albumArtDarkAccentColor)); }); } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs index fa70a6e..0f22759 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs @@ -21,7 +21,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService private int _langIndex = 0; private List _lyricsDataArr = []; - public LyricsData? CurrentLyricsData => _lyricsDataArr.ElementAtOrDefault(_langIndex); + private LyricsData? CurrentLyricsData => _lyricsDataArr.ElementAtOrDefault(_langIndex); public event EventHandler? LyricsChanged; @@ -35,7 +35,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService { TranslationSearchProvider = null; _lyricsDataArr.ElementAtOrDefault(0)?.SetDisplayedTextInOriginalText(); - LyricsChanged?.Invoke(this, new LyricsChangedEventArgs()); + LyricsChanged?.Invoke(this, new LyricsChangedEventArgs(CurrentLyricsData)); IsTranslating = true; if (_settingsService.AppSettings.TranslationSettings.IsTranslationEnabled) @@ -43,22 +43,16 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService _showTranslationsRunner.RunAsync(async token => { await SetDisplayedAlongWithTranslationsAsync(token); - _dispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Low, () => - { - IsTranslating = false; - LyricsChanged?.Invoke(this, new LyricsChangedEventArgs()); - }); + IsTranslating = false; + LyricsChanged?.Invoke(this, new LyricsChangedEventArgs(CurrentLyricsData)); }); } else { - _dispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Low, () => - { - _lyricsDataArr.ElementAtOrDefault(0)?.SetDisplayedTextInOriginalText(); - _langIndex = 0; - IsTranslating = false; - LyricsChanged?.Invoke(this, new LyricsChangedEventArgs()); - }); + _lyricsDataArr.ElementAtOrDefault(0)?.SetDisplayedTextInOriginalText(); + _langIndex = 0; + IsTranslating = false; + LyricsChanged?.Invoke(this, new LyricsChangedEventArgs(CurrentLyricsData)); } } @@ -136,16 +130,17 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService LyricsSearchProvider = null; _lyricsDataArr = [LyricsData.GetLoadingPlaceholder()]; - LyricsChanged?.Invoke(this, new LyricsChangedEventArgs()); + LyricsChanged?.Invoke(this, new LyricsChangedEventArgs(CurrentLyricsData)); string? lyricsRaw = null; - LyricsSearchProvider = null; + LyricsSearchProvider? lyricsSearchProvider = null; + LyricsSearchProvider = lyricsSearchProvider; if (SongInfo != null) { _logger.LogInformation("Searching lyrics for: Title={Title}, Artist={Artist}, Album={Album}, DurationMs={DurationMs}", SongInfo.Title, SongInfo.Artist, SongInfo.Album, SongInfo.DurationMs); - (lyricsRaw, LyricsSearchProvider) = await _lyrcsSearchService.SearchAsync( + (lyricsRaw, lyricsSearchProvider) = await _lyrcsSearchService.SearchAsync( SongInfo.SourceAppUserModelId ?? "", SongInfo.Title, SongInfo.Artist, @@ -153,6 +148,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService SongInfo.DurationMs ?? 0, token ); + LyricsSearchProvider = lyricsSearchProvider; _logger.LogInformation("Lyrics was found? {Found}, Provider: {LyricsSearchProvider}", lyricsRaw != null, LyricsSearchProvider?.ToString() ?? "null"); token.ThrowIfCancellationRequested(); @@ -168,7 +164,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService // This ensures that original lyrics are always shown while waiting for translations _lyricsDataArr[0].SetDisplayedTextInOriginalText(); - LyricsChanged?.Invoke(this, new LyricsChangedEventArgs()); + LyricsChanged?.Invoke(this, new LyricsChangedEventArgs(CurrentLyricsData)); UpdateTranslations(); } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs index b42ff7b..68647bb 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs @@ -63,9 +63,6 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService private readonly MediaManager _mediaManager = new(); - private readonly LatestOnlyTaskRunner _albumArtRefreshRunner = new(); - private readonly LatestOnlyTaskRunner _onAnyMediaPropertyChangedRunner = new(); - private SongInfo? _cachedSongInfo; private byte[]? _SMTCAlbumArtBytes = null; @@ -171,6 +168,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService private void LibWatcherService_MusicLibraryFilesChanged(object? sender, LibChangedEventArgs e) { + UpdateAlbumArt(); UpdateLyrics(); } @@ -189,94 +187,89 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService _mediaManager.OnAnyTimelinePropertyChanged += MediaManager_OnAnyTimelinePropertyChanged; _mediaManager.Start(); - Task.Run(() => - { - MediaManager_OnFocusedSessionChanged(null); - _mediaManager.CurrentMediaSessions.ToList().ForEach(x => RecordMediaSourceProviderInfo(x.Value)); - }); + + MediaManager_OnFocusedSessionChanged(null); + _mediaManager.CurrentMediaSessions.ToList().ForEach(x => RecordMediaSourceProviderInfo(x.Value)); } private void MediaManager_OnFocusedSessionChanged(MediaManager.MediaSession? mediaSession) { if (!_mediaManager.IsStarted) return; - SendFocusedMessagesAsync().ConfigureAwait(false); + SendFocusedMessagesAsync(); } private void MediaManager_OnAnyTimelinePropertyChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionTimelineProperties timelineProperties) { - if (!_mediaManager.IsStarted) return; - if (mediaSession == null) return; - - var focusedSession = _mediaManager.GetFocusedSession(); - - if (mediaSession != focusedSession) return; - - if (!IsMediaSourceEnabled(mediaSession.Id)) + _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => { - _cachedPosition = TimeSpan.Zero; - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => + if (!_mediaManager.IsStarted) return; + if (mediaSession == null) return; + + var focusedSession = _mediaManager.GetFocusedSession(); + + if (mediaSession != focusedSession) return; + + if (!IsMediaSourceEnabled(mediaSession.Id)) { + _cachedPosition = TimeSpan.Zero; TimelineChanged?.Invoke(this, new TimelineChangedEventArgs(_cachedPosition, TimeSpan.Zero)); - }); - } - else - { - _cachedPosition = timelineProperties.Position; - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => + } + else { + _cachedPosition = timelineProperties.Position; TimelineChanged?.Invoke(this, new TimelineChangedEventArgs(_cachedPosition, timelineProperties.EndTime)); - }); - } + } + }); } private void MediaManager_OnAnyPlaybackStateChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo) { - if (!_mediaManager.IsStarted) return; - if (mediaSession == null) return; - - var focusedSession = _mediaManager.GetFocusedSession(); - - //RecordMediaSourceProviderInfo(mediaSession); - if (mediaSession != focusedSession) return; - - if (!IsMediaSourceEnabled(mediaSession.Id)) - { - _cachedIsPlaying = false; - } - else - { - _cachedIsPlaying = playbackInfo.PlaybackStatus switch - { - GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing => true, - _ => false, - }; - } - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => { + if (!_mediaManager.IsStarted) return; + if (mediaSession == null) return; + + var focusedSession = _mediaManager.GetFocusedSession(); + + //RecordMediaSourceProviderInfo(mediaSession); + if (mediaSession != focusedSession) return; + + if (!IsMediaSourceEnabled(mediaSession.Id)) + { + _cachedIsPlaying = false; + } + else + { + _cachedIsPlaying = playbackInfo.PlaybackStatus switch + { + GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing => true, + _ => false, + }; + } + IsPlayingChanged?.Invoke(this, new IsPlayingChangedEventArgs(_cachedIsPlaying)); }); } private void MediaManager_OnAnyMediaPropertyChanged(MediaManager.MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties) { - if (!_mediaManager.IsStarted) return; - if (mediaSession == null) return; - - string id = mediaSession.Id; - - var focusedSession = _mediaManager.GetFocusedSession(); - - //RecordMediaSourceProviderInfo(mediaSession); - if (mediaSession != focusedSession) return; - - if (!IsMediaSourceEnabled(id)) + _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, async () => { - _cachedSongInfo = null; + if (!_mediaManager.IsStarted) return; + if (mediaSession == null) return; - _onAnyMediaPropertyChangedRunner.RunAsync(async token => + string id = mediaSession.Id; + + var focusedSession = _mediaManager.GetFocusedSession(); + + //RecordMediaSourceProviderInfo(mediaSession); + if (mediaSession != focusedSession) return; + + if (!IsMediaSourceEnabled(id)) { + _cachedSongInfo = null; + _logger.LogInformation("Media properties changed: Title: {Title}, Artist: {Artist}, Album: {Album}", mediaProperties.Title, mediaProperties.Artist, mediaProperties.AlbumTitle); @@ -286,42 +279,26 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService } _SMTCAlbumArtBytes = null; - - UpdateAlbumArt(); - - if (!token.IsCancellationRequested) - { - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => - { - SongInfoChanged?.Invoke(this, new SongInfoChangedEventArgs(_cachedSongInfo)); - }); - } - }); - } - else - { - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => + } + else { var currentMediaSourceProviderInfo = GetCurrentMediaSourceProviderInfo(); if (currentMediaSourceProviderInfo?.ResetPositionOffsetOnSongChanged == true) { currentMediaSourceProviderInfo?.PositionOffset = 0; } - }); - _cachedSongInfo = new SongInfo - { - Title = mediaProperties.Title, - Artist = mediaProperties.Artist, - Album = mediaProperties.AlbumTitle, - DurationMs = mediaSession.ControlSession.GetTimelineProperties().EndTime.TotalMilliseconds, - SourceAppUserModelId = id, - }; + _cachedSongInfo = new SongInfo + { + Title = mediaProperties.Title, + Artist = mediaProperties.Artist, + Album = mediaProperties.AlbumTitle, + DurationMs = mediaSession.ControlSession.GetTimelineProperties().EndTime.TotalMilliseconds, + SourceAppUserModelId = id, + }; - _cachedSongInfo.Duration = (int)(_cachedSongInfo.DurationMs / 1000f); + _cachedSongInfo.Duration = (int)(_cachedSongInfo.DurationMs / 1000f); - _onAnyMediaPropertyChangedRunner.RunAsync(async token => - { _logger.LogInformation("Media properties changed: Title: {Title}, Artist: {Artist}, Album: {Album}", mediaProperties.Title, mediaProperties.Artist, mediaProperties.AlbumTitle); @@ -342,19 +319,12 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService { _SMTCAlbumArtBytes = null; } + } - UpdateAlbumArt(); - UpdateLyrics(); - - if (!token.IsCancellationRequested) - { - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => - { - SongInfoChanged?.Invoke(this, new SongInfoChangedEventArgs(_cachedSongInfo)); - }); - } - }); - } + SongInfoChanged?.Invoke(this, new SongInfoChangedEventArgs(_cachedSongInfo)); + UpdateAlbumArt(); + UpdateLyrics(); + }); } private void MediaManager_OnAnySessionClosed(MediaManager.MediaSession mediaSession) @@ -450,7 +420,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService private void Sse_Disconnected(object sender, DisconnectEventArgs e) { - Task.Run(async () => + _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, async () => { await Task.Delay(e.ReconnectDelay); if (_sse != null && !_sse.IsDisposed) _sse.Start(); @@ -459,25 +429,26 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService private void Sse_MessageReceived(object sender, EventSourceMessageEventArgs e) { - if (_cachedSongInfo?.SourceAppUserModelId == Constants.PlayerID.LXMusic) + _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => { - var data = JsonSerializer.Deserialize(e.Message, Serialization.SourceGenerationContext.Default.JsonElement); - if (data.ValueKind == JsonValueKind.Number) + if (_cachedSongInfo?.SourceAppUserModelId == Constants.PlayerID.LXMusic) { - if (e.Event == "progress") - { - _lxMusicPositionSeconds = data.GetDouble(); - } - else if (e.Event == "duration") - { - _lxMusicDurationSeconds = data.GetDouble(); - } - _dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => + var data = JsonSerializer.Deserialize(e.Message, Serialization.SourceGenerationContext.Default.JsonElement); + if (data.ValueKind == JsonValueKind.Number) { + if (e.Event == "progress") + { + _lxMusicPositionSeconds = data.GetDouble(); + } + else if (e.Event == "duration") + { + _lxMusicDurationSeconds = data.GetDouble(); + } + TimelineChanged?.Invoke(this, new TimelineChangedEventArgs(TimeSpan.FromSeconds(_lxMusicPositionSeconds), TimeSpan.FromSeconds(_lxMusicDurationSeconds))); - }); + } } - } + }); } public async Task PlayAsync() @@ -547,7 +518,6 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService } else if (message.PropertyName == nameof(TranslationSettings.IsTranslationEnabled)) { - _logger.LogInformation("Translation enabled state changed: {IsEnabled}", _settingsService.AppSettings.TranslationSettings.IsTranslationEnabled); UpdateTranslations(); } else if (message.PropertyName == nameof(TranslationSettings.ShowTranslationOnly)) diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Draw.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Draw.cs index 848a22f..79ee768 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Draw.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Draw.cs @@ -63,7 +63,7 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel { _drawFrameCount++; - var currentPlayingLine = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); + var currentPlayingLine = _currentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); if (currentPlayingLine != null) { @@ -203,7 +203,7 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel private void DrawBlurredLyrics(ICanvasAnimatedControl control, CanvasDrawingSession ds) { - var currentPlayingLine = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); + var currentPlayingLine = _currentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); if (currentPlayingLine == null) { @@ -212,7 +212,7 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel for (int i = _startVisibleLineIndex; i <= _endVisibleLineIndex; i++) { - var line = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(i); + var line = _currentLyricsData?.LyricsLines.ElementAtOrDefault(i); if (line == null) continue; var textLayout = line.CanvasTextLayout; diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Update.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Update.cs index c49d9c9..7fe9de8 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Update.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.Update.cs @@ -336,9 +336,9 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel double y = 0; // Init Positions - for (int i = 0; i < _mediaSessionsService.CurrentLyricsData?.LyricsLines.Count; i++) + for (int i = 0; i < _currentLyricsData?.LyricsLines.Count; i++) { - var line = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(i); + var line = _currentLyricsData?.LyricsLines.ElementAtOrDefault(i); if (line == null) { @@ -366,7 +366,7 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel // Set _scrollOffsetY - LyricsLine? currentPlayingLine = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); + LyricsLine? currentPlayingLine = _currentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); if (currentPlayingLine == null) return; @@ -374,7 +374,7 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel if (playingTextLayout == null) return; - double? targetYScrollOffset = -currentPlayingLine!.Position.Y + _mediaSessionsService.CurrentLyricsData?.LyricsLines[0].Position.Y - playingTextLayout.LayoutBounds.Height / 2.0; + double? targetYScrollOffset = -currentPlayingLine!.Position.Y + _currentLyricsData?.LyricsLines[0].Position.Y - playingTextLayout.LayoutBounds.Height / 2.0; if (!targetYScrollOffset.HasValue) return; @@ -385,7 +385,7 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel { var (startLineIndex, endLineIndex) = GetMaxLyricsLineIndexBoundaries(); - var lines = _mediaSessionsService.CurrentLyricsData?.LyricsLines; + var lines = _currentLyricsData?.LyricsLines; if (lines == null || lines.Count == 0) return; double offset = _canvasYScrollTransition.Value + _canvasHeight / 2; @@ -555,13 +555,13 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel private void UpdateVisibleLinesProps(ICanvasAnimatedControl control) { - var currentPlayingLine = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); + var currentPlayingLine = _currentLyricsData?.LyricsLines.ElementAtOrDefault(_playingLineIndex); if (currentPlayingLine == null) return; for (int i = _startVisibleLineIndex; i <= _endVisibleLineIndex + 1; i++) { - var line = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(i); + var line = _currentLyricsData?.LyricsLines.ElementAtOrDefault(i); if (line == null) continue; diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.cs index d899050..d10c8d0 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsRendererViewModel/LyricsRendererViewModel.cs @@ -56,8 +56,6 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel private int _drawFrameCount = 0; private int _displayedDrawFrameCount = 0; - private Queue _cachedAlbumArtSwBitmaps = []; - private SoftwareBitmap? _lastAlbumArtSwBitmap = null; private SoftwareBitmap? _albumArtSwBitmap = null; @@ -122,6 +120,8 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel private int _startVisibleLineIndex = -1; private int _endVisibleLineIndex = -1; + private LyricsData? _currentLyricsData; + private bool _isDebugOverlayEnabled = false; [ObservableProperty] @@ -206,19 +206,20 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel private void MediaSessionsService_LyricsChanged(object? sender, LyricsChangedEventArgs e) { + _currentLyricsData = e.LyricsData; _isLayoutChanged = true; } private int GetCurrentPlayingLineIndex() { var totalMs = TotalTime.TotalMilliseconds + _positionOffset.TotalMilliseconds; - if (totalMs < _mediaSessionsService.CurrentLyricsData?.LyricsLines.FirstOrDefault()?.StartMs) return 0; + if (totalMs < _currentLyricsData?.LyricsLines.FirstOrDefault()?.StartMs) return 0; - for (int i = 0; i < _mediaSessionsService.CurrentLyricsData?.LyricsLines.Count; i++) + for (int i = 0; i < _currentLyricsData?.LyricsLines.Count; i++) { - var line = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(i); + var line = _currentLyricsData?.LyricsLines.ElementAtOrDefault(i); if (line == null) continue; - var nextLine = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(i + 1); + var nextLine = _currentLyricsData?.LyricsLines.ElementAtOrDefault(i + 1); if (nextLine != null && line.StartMs <= totalMs && totalMs < nextLine.StartMs) { return i; @@ -238,9 +239,9 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel charLength = 0; charProgress = 0f; - var line = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(lineIndex); + var line = _currentLyricsData?.LyricsLines.ElementAtOrDefault(lineIndex); if (line == null) return; - var nextLine = _mediaSessionsService.CurrentLyricsData?.LyricsLines.ElementAtOrDefault(lineIndex + 1); + var nextLine = _currentLyricsData?.LyricsLines.ElementAtOrDefault(lineIndex + 1); int lineEndMs; if (line.EndMs != null) lineEndMs = line.EndMs.Value; @@ -325,14 +326,14 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel { if ( SongInfo == null - || _mediaSessionsService.CurrentLyricsData == null - || _mediaSessionsService.CurrentLyricsData.LyricsLines.Count == 0 + || _currentLyricsData == null + || _currentLyricsData.LyricsLines.Count == 0 ) { return new Tuple(-1, -1); } - return new Tuple(0, _mediaSessionsService.CurrentLyricsData.LyricsLines.Count - 1); + return new Tuple(0, _currentLyricsData.LyricsLines.Count - 1); } private void MediaSessionsService_IsPlayingChanged(object? sender, IsPlayingChangedEventArgs e) @@ -390,37 +391,15 @@ namespace BetterLyrics.WinUI3.ViewModels.LyricsRendererViewModel private void MediaSessionsService_AlbumArtChangedChanged(object? sender, AlbumArtChangedEventArgs e) { - if (e.AlbumArtSwBitmap != _albumArtSwBitmap) - { - _cachedAlbumArtSwBitmaps.Append(_albumArtSwBitmap); + _lastAlbumArtSwBitmap = _albumArtSwBitmap; + _albumArtSwBitmap = e.AlbumArtSwBitmap; - _lastAlbumArtSwBitmap = _albumArtSwBitmap; + _albumArtChanged = true; - if (_cachedAlbumArtSwBitmaps.Count > 2) - { - _cachedAlbumArtSwBitmaps.Dequeue()?.Dispose(); - } + _albumArtLightAccentColor = e.AlbumArtLightAccentColor ?? Colors.Transparent; + _albumArtDarkAccentColor = e.AlbumArtDarkAccentColor ?? Colors.Transparent; - _cachedAlbumArtSwBitmaps.Append(e.AlbumArtSwBitmap); - - _albumArtSwBitmap = e.AlbumArtSwBitmap; - - if (_cachedAlbumArtSwBitmaps.Count > 2) - { - _cachedAlbumArtSwBitmaps.Dequeue()?.Dispose(); - } - - _albumArtChanged = true; - - _albumArtLightAccentColor = e.AlbumArtLightAccentColor ?? Colors.Transparent; - _albumArtDarkAccentColor = e.AlbumArtDarkAccentColor ?? Colors.Transparent; - - UpdateColorConfig(); - } - else - { - e.AlbumArtSwBitmap?.Dispose(); - } + UpdateColorConfig(); } } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/PlaybackSettingsControlViewModel.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/PlaybackSettingsControlViewModel.cs index 0c9c5a1..30a907f 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/PlaybackSettingsControlViewModel.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/PlaybackSettingsControlViewModel.cs @@ -80,6 +80,9 @@ namespace BetterLyrics.WinUI3.ViewModels IsLastFMAuthenticated = _lastFMService.IsAuthenticated; LastFMUser = _lastFMService.User; + LyricsSearchProvider = _mediaSessionsService.LyricsSearchProvider; + TranslationSearchProvider = _mediaSessionsService.TranslationSearchProvider; + SelectedMediaSourceProvider = AppSettings.MediaSourceProvidersInfo.FirstOrDefault(); }