diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs index f88026a..f9173be 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/App.xaml.cs @@ -69,11 +69,9 @@ namespace BetterLyrics.WinUI3 protected override void OnLaunched(LaunchActivatedEventArgs args) { - // 先获取一个实例,确保初始化 LyricsWindowStatus var settingsService = Ioc.Default.GetRequiredService(); - // 设置托盘 - WindowHook.OpenOrShowWindow(); + _ = new SystemTrayWindow(); if (settingsService.AppSettings.GeneralSettings.AutoStartLyricsWindow) { diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/AppSettingsControl.xaml b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/AppSettingsControl.xaml index 6b9071f..d3fe90e 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/AppSettingsControl.xaml +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/AppSettingsControl.xaml @@ -71,12 +71,8 @@ - - - - - - + + diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsCanvas.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsCanvas.xaml.cs index f2617bf..441550a 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsCanvas.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsCanvas.xaml.cs @@ -252,15 +252,7 @@ namespace BetterLyrics.WinUI3.Controls { InitializeComponent(); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); + WeakReferenceMessenger.Default.RegisterAll(this); UpdateRenderLyricsLines(); } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml index 79a593c..4c452d4 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml @@ -155,8 +155,15 @@ + + + + - + @@ -178,77 +185,31 @@ Style="{StaticResource AccentButtonStyle}" /> - + - - - - - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml.cs index 9908321..a03fb4b 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/LyricsWindowSettingsControl.xaml.cs @@ -122,17 +122,6 @@ namespace BetterLyrics.WinUI3.Controls } } - private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e) - { - if (sender is Pivot pivot) - { - if (pivot.SelectedItem is PivotItem pivotItem) - { - ViewModel?.ListViewSelectedItemTag = pivotItem.Tag; - } - } - } - private async void ImportButton_Click(object sender, RoutedEventArgs e) { string[] fileTypeFilter = [".json"]; @@ -164,14 +153,16 @@ namespace BetterLyrics.WinUI3.Controls private void ConfigButton_Click(object sender, RoutedEventArgs e) { - WindowPivotItem.Visibility = Visibility.Visible; + WindowSelectorBarItem.Visibility = LayoutSelectorBarItem.Visibility = Visibility.Visible; + ConfigSelectorBar.SelectedItem = WindowSelectorBarItem; LyricsWindowStatus = (LyricsWindowStatus)((Button)sender).DataContext; ViewModel.OpenConfigPanel(); } private void EmbeddedConfigButton_Click(object sender, RoutedEventArgs e) { - WindowPivotItem.Visibility = Visibility.Collapsed; + WindowSelectorBarItem.Visibility = LayoutSelectorBarItem.Visibility = Visibility.Collapsed; + ConfigSelectorBar.SelectedItem = AlbumArtStyleSelectorBarItem; LyricsWindowStatus = _settingsService.AppSettings.MusicGallerySettings.LyricsWindowStatus; ViewModel.OpenConfigPanel(); } @@ -196,5 +187,15 @@ namespace BetterLyrics.WinUI3.Controls } } + private void ConfigSelectorBar_SelectionChanged(SelectorBar sender, SelectorBarSelectionChangedEventArgs args) + { + if (sender is SelectorBar bar) + { + if (bar.SelectedItem is SelectorBarItem item) + { + ViewModel?.SelectorBarSelectedItemTag = item.Tag; + } + } + } } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/PlaybackSettingsControl.xaml b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/PlaybackSettingsControl.xaml index 2f344b8..6854464 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/PlaybackSettingsControl.xaml +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/PlaybackSettingsControl.xaml @@ -22,8 +22,9 @@ - + + @@ -42,51 +43,53 @@ - - - - - - - - - - - - - - - - - - - - - - + IsClosable="False" + IsOpen="{x:Bind ViewModel.AppSettings.MusicGallerySettings.LyricsWindowStatus.IsOpened, Mode=OneWay}" + Message="音乐库窗口已打开,将忽略对其他播放源的监听" + Severity="Informational" /> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -269,6 +272,7 @@ + + + @@ -518,6 +524,7 @@ + diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/ShadowImage.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/ShadowImage.xaml.cs index 44aa9a2..cffe49f 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/ShadowImage.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Controls/ShadowImage.xaml.cs @@ -76,13 +76,13 @@ namespace BetterLyrics.WinUI3.Controls private void UpdateShadowCastGridCornerRadius() { var minSize = Math.Min(ShadowCastGrid.ActualHeight, ShadowCastGrid.ActualWidth); - ShadowCastGrid.CornerRadius = new(CornerRadiusAmount / 100.0 * minSize); + ShadowCastGrid.CornerRadius = new(CornerRadiusAmount / 100.0 * (minSize / 2)); } private void UpdateShadowRectCornerRadius() { var minSize = Math.Min(ShadowRect.ActualHeight, ShadowRect.ActualWidth); - ShadowRect.CornerRadius = new(CornerRadiusAmount / 100.0 * minSize); + ShadowRect.CornerRadius = new(CornerRadiusAmount / 100.0 * (minSize / 2)); } private void ShadowCastGrid_SizeChanged(object sender, SizeChangedEventArgs e) diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/PlayerIDHelper.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/PlayerIDHelper.cs index fdbe30d..6614c12 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/PlayerIDHelper.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Helper/PlayerIDHelper.cs @@ -29,6 +29,8 @@ namespace BetterLyrics.WinUI3.Helper public static bool IsAppleMusic(string? id) => id is PlayerID.AppleMusic or PlayerID.AppleMusicAlternative; + public static bool IsBetterLyrics(string? id) => id is PlayerID.BetterLyrics or PlayerID.BetterLyricsDebug; + public static string? GetDisplayName(string? id) => id switch { PlayerID.Spotify => PlayerName.Spotify, diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Hooks/WindowHook.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Hooks/WindowHook.cs index 5e005a8..f971570 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Hooks/WindowHook.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Hooks/WindowHook.cs @@ -5,6 +5,7 @@ using BetterLyrics.WinUI3.Helper; using BetterLyrics.WinUI3.Models; using BetterLyrics.WinUI3.Services.MediaSessionsService; +using BetterLyrics.WinUI3.Services.SettingsService; using BetterLyrics.WinUI3.Views; using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.WinUI; @@ -30,8 +31,6 @@ namespace BetterLyrics.WinUI3.Hooks private static readonly Dictionary _defaultWindowStyle = []; private static readonly Dictionary _defaultExtendedWindowStyle = []; - private static readonly IMediaSessionsService _mediaSessionsService = Ioc.Default.GetRequiredService(); - public static void HideWindow(this Window window) { window.Hide(); @@ -39,21 +38,16 @@ namespace BetterLyrics.WinUI3.Hooks public static void CloseWindow(this Window window) { - if (window is Window w) + if (window is NowPlayingWindow nowPlayingWindow) { - w.Close(); - _activeWindows.Remove(w); + if (GetWindowHandle(window) is IntPtr hwnd) + { + UnregisterWorkArea(hwnd); + } + nowPlayingWindow.LyricsWindowStatus.IsOpened = false; } - } - - public static void CloseWindow(this NowPlayingWindow window) - { - if (GetWindowHandle(window) is IntPtr hwnd) - { - UnregisterWorkArea(hwnd); - } - window.LyricsWindowStatus.IsOpened = false; - ((Window)window).CloseWindow(); + window.Close(); + _activeWindows.Remove(window); } public static void MinimizeWindow(this Window window) @@ -140,13 +134,6 @@ namespace BetterLyrics.WinUI3.Hooks { window = new LyricsWindowSwitchWindow(); } - else if (typeof(T) == typeof(SystemTrayWindow)) - { - window = new SystemTrayWindow(); - var systemTrayWindow = (SystemTrayWindow)window; - systemTrayWindow.EnsureLyricsWindowStatus(); - systemTrayWindow.InitShortcuts(); - } else { throw new ArgumentException("Unsupported window type", nameof(T)); @@ -155,14 +142,8 @@ namespace BetterLyrics.WinUI3.Hooks TrackWindow(window); var castedWindow = (Window)window; - if (typeof(T) == typeof(SystemTrayWindow)) - { - } - else - { - castedWindow.Restore(); - castedWindow.Activate(); - } + castedWindow.Restore(); + castedWindow.Activate(); if (typeof(T) == typeof(NowPlayingWindow)) { @@ -438,7 +419,7 @@ namespace BetterLyrics.WinUI3.Hooks /// /// /// 请确保此参数指向同一个对象,建议传值 BaseViewModel._dispatcherQueue - public static void SetLyricsWindowVisibilityByPlayingStatus(this NowPlayingWindow window, DispatcherQueue dispatcherQueue) + public static void SetLyricsWindowVisibilityByPlayingStatus(this NowPlayingWindow window, bool isPlaying, DispatcherQueue dispatcherQueue) { var status = window.LyricsWindowStatus; @@ -449,7 +430,7 @@ namespace BetterLyrics.WinUI3.Hooks var window = GetWindow(); if (window == null) return; - if (status.AutoShowOrHideWindow && !_mediaSessionsService.CurrentIsPlaying) + if (status.AutoShowOrHideWindow && !isPlaying) { if (status.IsWorkArea) { @@ -457,7 +438,7 @@ namespace BetterLyrics.WinUI3.Hooks } window.HideWindow(); } - else if (window.LyricsWindowStatus.AutoShowOrHideWindow && _mediaSessionsService.CurrentIsPlaying) + else if (window.LyricsWindowStatus.AutoShowOrHideWindow && isPlaying) { if (window.LyricsWindowStatus.IsWorkArea) { diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/MediaSourceProviderInfo.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/MediaSourceProviderInfo.cs index 859b4cc..25e4ddb 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/MediaSourceProviderInfo.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/MediaSourceProviderInfo.cs @@ -6,6 +6,7 @@ using BetterLyrics.WinUI3.Helper; using CommunityToolkit.Mvvm.ComponentModel; using System; using System.Linq; +using System.Text.Json.Serialization; namespace BetterLyrics.WinUI3.Models { @@ -34,11 +35,11 @@ namespace BetterLyrics.WinUI3.Models [ObservableProperty][NotifyPropertyChangedRecipients] public partial LyricsSearchType LyricsSearchType { get; set; } = LyricsSearchType.Sequential; [ObservableProperty][NotifyPropertyChangedRecipients] public partial int MatchingThreshold { get; set; } = 0; - public string LogoPath => PlayerIDHelper.GetLogoPath(Provider); + [JsonIgnore] public string LogoPath => PlayerIDHelper.GetLogoPath(Provider); - public string? DisplayName => PlayerIDHelper.GetDisplayName(Provider); + [JsonIgnore] public string? DisplayName => PlayerIDHelper.GetDisplayName(Provider); - public bool IsLXMusic => PlayerIDHelper.IsLXMusic(Provider); + [JsonIgnore] public bool IsLXMusic => PlayerIDHelper.IsLXMusic(Provider); public MediaSourceProviderInfo() { diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/Settings/MusicGallerySettings.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/Settings/MusicGallerySettings.cs index 98dcbd8..4c9437d 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/Settings/MusicGallerySettings.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Models/Settings/MusicGallerySettings.cs @@ -12,6 +12,7 @@ namespace BetterLyrics.WinUI3.Models.Settings [ObservableProperty][NotifyPropertyChangedRecipients] public partial bool AutoOpen { get; set; } = false; [ObservableProperty][NotifyPropertyChangedRecipients] public partial bool AutoPlay { get; set; } = false; [ObservableProperty][NotifyPropertyChangedRecipients] public partial LyricsWindowStatus LyricsWindowStatus { get; set; } = new(); + [ObservableProperty][NotifyPropertyChangedRecipients] public partial bool ExitOnWindowClosed { get; set; } = false; public MusicGallerySettings() { } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs index 596cbee..7bfc39e 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/IMediaSessionsService.cs @@ -24,7 +24,6 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService Task ChangeLyricsLine(int index); void UpdateLyrics(); - void UpdateTranslations(); MediaSourceProviderInfo? CurrentMediaSourceProviderInfo { get; } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs index 32a714a..9412fd5 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.LyricsUpdater.cs @@ -3,6 +3,7 @@ using BetterLyrics.WinUI3.Events; using BetterLyrics.WinUI3.Extensions; using BetterLyrics.WinUI3.Helper; using BetterLyrics.WinUI3.Models; +using BetterLyrics.WinUI3.Models.Settings; using BetterLyrics.WinUI3.Parsers.LyricsParser; using CommunityToolkit.Mvvm.ComponentModel; using Lyricify.Lyrics.Helpers.General; @@ -19,7 +20,6 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService public partial class MediaSessionsService : IMediaSessionsService { private LatestOnlyTaskRunner _refreshLyricsRunner = new(); - private LatestOnlyTaskRunner _refreshTranslationRunner = new(); private int _langIndex = 0; private List _lyricsDataArr = []; @@ -36,34 +36,33 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService private void SetCurrentLyricsData() { - CurrentLyricsData = _lyricsDataArr.ElementAtOrDefault(_langIndex); + App.Current.Resources.DispatcherQueue.TryEnqueue(() => + { + CurrentLyricsData = _lyricsDataArr.ElementAtOrDefault(_langIndex); + }); } - private async Task RefreshTranslationAsync(CancellationToken token) + private async Task RefreshTranslationAsync(TranslationSettings settings, CancellationToken token) { TranslationSearchProvider = null; _lyricsDataArr.ElementAtOrDefault(0)?.ClearTranslatedText(); - App.Current.Resources.DispatcherQueue.TryEnqueue(SetCurrentLyricsData); - IsTranslating = true; - await SetPhoneticTextAsync(token); - await SetTranslatedTextAsync(token); + SetPhoneticText(); + + await SetTranslatedTextAsync(settings, token); if (token.IsCancellationRequested) return; IsTranslating = false; - - App.Current.Resources.DispatcherQueue.TryEnqueue(SetCurrentLyricsData); - } - private async Task SetTranslatedTextAsync(CancellationToken token) + private async Task SetTranslatedTextAsync(TranslationSettings settings, CancellationToken token) { - if (!_settingsService.AppSettings.TranslationSettings.IsTranslationEnabled) return; + if (!settings.IsTranslationEnabled) return; _logger.LogInformation("SetTranslatedTextAsync"); - string targetLangCode = _settingsService.AppSettings.TranslationSettings.SelectedTargetLanguageCode; + string targetLangCode = settings.SelectedTargetLanguageCode; _logger.LogInformation("Target language code: {TargetLangCode}", targetLangCode); string? originalText = _lyricsDataArr.FirstOrDefault()?.WrappedOriginalText; if (originalText == null) return; @@ -88,7 +87,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService _lyricsDataArr.FirstOrDefault()?.SetTranslatedText(_lyricsDataArr[found], 50); TranslationSearchProvider = CurrentLyricsSearchResult?.Provider.ToTranslationSearchProvider(); } - else if (_settingsService.AppSettings.TranslationSettings.IsLibreTranslateEnabled) + else if (settings.IsLibreTranslateEnabled) { _logger.LogInformation("LibreTranslate is enabled, trying to translate lyrics..."); string translated = string.Empty; @@ -110,7 +109,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService } } - private async Task SetPhoneticTextAsync(CancellationToken token) + private void SetPhoneticText() { _logger.LogInformation("Showing phonetic text for lyrics..."); string targetPhoneticCode = ""; @@ -145,14 +144,14 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService } - private async Task RefreshLyricsAsync(CancellationToken token) + private async Task RefreshLyricsAsync(TranslationSettings settings, CancellationToken token) { _logger.LogInformation("RefreshLyricsAsync"); CurrentLyricsSearchResult = null; _lyricsDataArr = [LyricsData.GetLoadingPlaceholder()]; - App.Current.Resources.DispatcherQueue.TryEnqueue(SetCurrentLyricsData); + SetCurrentLyricsData(); if (CurrentSongInfo != null) { @@ -175,9 +174,11 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService // Show original first while loading phonetic and translated ApplyChinesePreference(); - App.Current.Resources.DispatcherQueue.TryEnqueue(SetCurrentLyricsData); + SetCurrentLyricsData(); - UpdateTranslations(); + await RefreshTranslationAsync(settings, token); + + SetCurrentLyricsData(); } private void ApplyChinesePreference() @@ -193,14 +194,13 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService } } - public void UpdateLyrics() + public async void UpdateLyrics() { - _ = _refreshLyricsRunner.RunAsync(RefreshLyricsAsync); + await _refreshLyricsRunner.RunAsync(async (token) => + { + await RefreshLyricsAsync(_settingsService.AppSettings.TranslationSettings, token); + }); } - public void UpdateTranslations() - { - _ = _refreshTranslationRunner.RunAsync(RefreshTranslationAsync); - } } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs index 59f634f..b2d5bf9 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/MediaSessionsService/MediaSessionsService.cs @@ -12,7 +12,6 @@ using BetterLyrics.WinUI3.Models.Settings; using BetterLyrics.WinUI3.Services.AlbumArtSearchService; using BetterLyrics.WinUI3.Services.DiscordService; using BetterLyrics.WinUI3.Services.LibWatcherService; - using BetterLyrics.WinUI3.Services.LyricsSearchService; using BetterLyrics.WinUI3.Services.SettingsService; using BetterLyrics.WinUI3.Services.TranslateService; @@ -158,7 +157,22 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService private bool IsMediaSourceEnabled(string id) { - return _settingsService.AppSettings.MediaSourceProvidersInfo.FirstOrDefault(s => s.Provider == id)?.IsEnabled ?? true; + var found = _settingsService.AppSettings.MediaSourceProvidersInfo.FirstOrDefault(s => s.Provider == id); + if (_settingsService.AppSettings.MusicGallerySettings.LyricsWindowStatus.IsOpened) + { + if (PlayerIDHelper.IsBetterLyrics(found?.Provider)) + { + return true; + } + else + { + return false; + } + } + else + { + return found?.IsEnabled ?? true; + } } private bool IsMediaSourceTimelineSyncEnabled(string? id) @@ -453,14 +467,12 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService GlobalSystemMediaTransportControlsSessionMediaProperties? mediaProps = null; var desiredSession = GetCurrentSession(); - //if (desiredSession == null || desiredSession.ControlSession == null) return; try { mediaProps = await desiredSession?.ControlSession?.TryGetMediaPropertiesAsync(); } catch (Exception) { } - //if (desiredSession == null || desiredSession.ControlSession == null) return; MediaManager_OnAnyTimelinePropertyChanged(desiredSession, desiredSession?.ControlSession?.GetTimelineProperties()); MediaManager_OnAnyMediaPropertyChanged(desiredSession, mediaProps); @@ -642,25 +654,32 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService { if (message.PropertyName == nameof(TranslationSettings.IsLibreTranslateEnabled)) { - UpdateTranslations(); + UpdateLyrics(); } else if (message.PropertyName == nameof(TranslationSettings.IsTranslationEnabled)) { - UpdateTranslations(); + UpdateLyrics(); } else if (message.PropertyName == nameof(TranslationSettings.IsChineseRomanizationEnabled)) { - UpdateTranslations(); + UpdateLyrics(); } else if (message.PropertyName == nameof(TranslationSettings.IsJapaneseRomanizationEnabled)) { - UpdateTranslations(); + UpdateLyrics(); } else if (message.PropertyName == nameof(TranslationSettings.IsTraditionalChineseEnabled)) { UpdateLyrics(); } } + else if (message.Sender is LyricsWindowStatus) + { + if (message.PropertyName == nameof(MusicGallerySettings.LyricsWindowStatus.IsOpened)) + { + MediaManager_OnFocusedSessionChanged(null); + } + } } public void Receive(PropertyChangedMessage message) @@ -670,7 +689,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService if (message.PropertyName == nameof(TranslationSettings.SelectedTargetLanguageCode)) { _logger.LogInformation("Target LibreTranslate language code changed: {code}", _settingsService.AppSettings.TranslationSettings.SelectedTargetLanguageCode); - UpdateTranslations(); + UpdateLyrics(); } } @@ -682,7 +701,7 @@ namespace BetterLyrics.WinUI3.Services.MediaSessionsService { if (message.PropertyName == nameof(TranslationSettings.ChineseRomanization)) { - UpdateTranslations(); + UpdateLyrics(); } } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/ISettingsService.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/ISettingsService.cs index 0b88465..212d25d 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/ISettingsService.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/ISettingsService.cs @@ -1,6 +1,7 @@ // 2025/6/23 by Zhe Fang using BetterLyrics.WinUI3.Models.Settings; +using System.Threading.Tasks; namespace BetterLyrics.WinUI3.Services.SettingsService { diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/SettingsService.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/SettingsService.cs index 4122cad..de3809f 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/SettingsService.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Services/SettingsService/SettingsService.cs @@ -8,11 +8,13 @@ using BetterLyrics.WinUI3.Models; using BetterLyrics.WinUI3.Models.Settings; using BetterLyrics.WinUI3.Serialization; using BetterLyrics.WinUI3.ViewModels; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.WinUI; using Microsoft.UI.Dispatching; using System; using System.IO; using System.Linq; +using System.Threading.Tasks; using Windows.Globalization; namespace BetterLyrics.WinUI3.Services.SettingsService @@ -20,7 +22,7 @@ namespace BetterLyrics.WinUI3.Services.SettingsService // 新建一个 AppSettings 类 public partial class SettingsService : BaseViewModel, ISettingsService { - private DispatcherQueueTimer _writeAppSettingsTimer; + private readonly DispatcherQueueTimer _writeAppSettingsTimer; public AppSettings AppSettings { get; set; } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/en-US/Resources.resw b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/en-US/Resources.resw index faaa416..192f3bf 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/en-US/Resources.resw +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/en-US/Resources.resw @@ -849,6 +849,9 @@ Inside the window + + Exit the program when closing the music library window + Exit the program when you close the lyrics window diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ja-JP/Resources.resw b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ja-JP/Resources.resw index 5c90a42..cd8f89e 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ja-JP/Resources.resw +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ja-JP/Resources.resw @@ -849,6 +849,9 @@ 窓の中だ + + 音楽ライブラリウィンドウを閉じてプログラムを終了してください + 歌詞ウィンドウを閉じてプログラムを終了してください diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ko-KR/Resources.resw b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ko-KR/Resources.resw index e9cec0a..5f5a922 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ko-KR/Resources.resw +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/ko-KR/Resources.resw @@ -849,6 +849,9 @@ 창문 안쪽 + + 음악 라이브러리 창을 닫을 때 프로그램을 종료하십시오 + 가사 창을 닫으면 프로그램을 종료하세요 diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-CN/Resources.resw b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-CN/Resources.resw index e5f94d3..aff24fa 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-CN/Resources.resw +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-CN/Resources.resw @@ -849,6 +849,9 @@ 窗口内部 + + 关闭音乐库窗口时退出程序 + 关闭歌词窗口时退出程序 diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-TW/Resources.resw b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-TW/Resources.resw index d465e7a..e55761f 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-TW/Resources.resw +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Strings/zh-TW/Resources.resw @@ -849,6 +849,9 @@ 視窗內部 + + 關閉音樂庫視窗時退出程式 + 關閉歌詞視窗時退出程式 diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsWindowSettingsControlViewModel.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsWindowSettingsControlViewModel.cs index e27b4ba..070ed3c 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsWindowSettingsControlViewModel.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/ViewModels/LyricsWindowSettingsControlViewModel.cs @@ -23,7 +23,7 @@ namespace BetterLyrics.WinUI3.ViewModels public partial AppSettings AppSettings { get; set; } [ObservableProperty] - public partial object ListViewSelectedItemTag { get; set; } = "General"; + public partial object SelectorBarSelectedItemTag { get; set; } = "AlbumArtStyle"; [ObservableProperty] public partial bool IsConfigPanelOpened { get; set; } = false; diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml index 3134eac..d05a140 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml @@ -11,7 +11,7 @@ xmlns:ui="using:CommunityToolkit.WinUI" mc:Ignorable="d"> - + diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml.cs index eb3e8de..8326377 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/MusicGalleryWindow.xaml.cs @@ -38,8 +38,7 @@ namespace BetterLyrics.WinUI3.Views AppWindow.Closing += AppWindow_Closing; - WeakReferenceMessenger.Default.Register>(this); - WeakReferenceMessenger.Default.Register>(this); + WeakReferenceMessenger.Default.RegisterAll(this); _ = UpdateAlbumArtThemeColorsAsync(); } @@ -56,7 +55,14 @@ namespace BetterLyrics.WinUI3.Views private void AppWindow_Closing(AppWindow sender, AppWindowClosingEventArgs args) { - this.CloseWindow(); + if (ViewModel.AppSettings.MusicGallerySettings.ExitOnWindowClosed) + { + WindowHook.ExitApp(); + } + else + { + this.CloseWindow(); + } } public async void Receive(PropertyChangedMessage message) @@ -102,5 +108,15 @@ namespace BetterLyrics.WinUI3.Views await Task.Delay(Constants.Time.AnimationDuration); NowPlayingPage.Visibility = Visibility.Collapsed; } + + private void RootGrid_Loaded(object sender, RoutedEventArgs e) + { + ViewModel.AppSettings.MusicGallerySettings.LyricsWindowStatus.IsOpened = true; + } + + private void RootGrid_Unloaded(object sender, RoutedEventArgs e) + { + ViewModel.AppSettings.MusicGallerySettings.LyricsWindowStatus.IsOpened = false; + } } } diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingPage.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingPage.xaml.cs index c1f05c2..32d85db 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingPage.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingPage.xaml.cs @@ -58,7 +58,7 @@ namespace BetterLyrics.WinUI3.Views DataContext = Ioc.Default.GetRequiredService(); - WeakReferenceMessenger.Default.Register>(this); + WeakReferenceMessenger.Default.RegisterAll(this); } private static void OnDependencyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingWindow.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingWindow.xaml.cs index 1ef255b..7fcf206 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingWindow.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/NowPlayingWindow.xaml.cs @@ -187,7 +187,7 @@ namespace BetterLyrics.WinUI3.Views private void OnAutoShowOrHideWindowChanged() { - this.SetLyricsWindowVisibilityByPlayingStatus(DispatcherQueue); + this.SetLyricsWindowVisibilityByPlayingStatus(_mediaSessionsService.CurrentIsPlaying, DispatcherQueue); } private void OnWorkAreaChanged() @@ -378,7 +378,7 @@ namespace BetterLyrics.WinUI3.Views { if (message.PropertyName == nameof(IMediaSessionsService.CurrentIsPlaying)) { - this.SetLyricsWindowVisibilityByPlayingStatus(DispatcherQueue); + this.SetLyricsWindowVisibilityByPlayingStatus(_mediaSessionsService.CurrentIsPlaying, DispatcherQueue); } } else if (message.Sender == LyricsWindowStatus) diff --git a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/SystemTrayWindow.xaml.cs b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/SystemTrayWindow.xaml.cs index b1606b4..0e6083f 100644 --- a/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/SystemTrayWindow.xaml.cs +++ b/BetterLyrics.WinUI3/BetterLyrics.WinUI3/Views/SystemTrayWindow.xaml.cs @@ -21,6 +21,7 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading.Tasks; using Vanara.PInvoke; using Windows.Foundation; using Windows.Foundation.Collections; @@ -36,22 +37,27 @@ namespace BetterLyrics.WinUI3.Views; /// public sealed partial class SystemTrayWindow : Window, IRecipient>> { - private readonly ISettingsService _settingsService = Ioc.Default.GetRequiredService(); + private ISettingsService _settingsService = Ioc.Default.GetRequiredService(); private readonly IMediaSessionsService _mediaSessionsService = Ioc.Default.GetRequiredService(); - private readonly WindowMessageMonitor _wmm; + private WindowMessageMonitor _wmm; public SystemTrayWindow() { InitializeComponent(); - WeakReferenceMessenger.Default.Register>>(this); SystemBackdrop = SystemBackdropHelper.CreateSystemBackdrop(BackdropType.Transparent); _wmm = new WindowMessageMonitor(this); _wmm.WindowMessageReceived += Wmm_WindowMessageReceived; + + WeakReferenceMessenger.Default.RegisterAll(this); + + InitShortcuts(); + + EnsureLyricsWindowStatus(); } - public void InitShortcuts() + private void InitShortcuts() { UpdateLyricsWindowSwitchShortcut(); UpdatePlayOrPauseSongShortcut(); @@ -67,13 +73,9 @@ public sealed partial class SystemTrayWindow : Window, IRecipient