From e9f739de6cf7fa5a09b0790baf40dfa10ca3d7fb Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Sun, 24 Nov 2024 14:42:39 +0800 Subject: [PATCH] =?UTF-8?q?#922=20=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E4=B8=8E=E8=AE=BE=E7=BD=AE=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E4=B8=8D=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/App.xaml.cs | 19 +++-- src/BiliLite.UWP/BiliLite.UWP.csproj | 6 +- .../Settings/UISettingsControl.xaml.cs | 27 +++---- .../Converters/ColorSelectedConvert.cs | 10 ++- src/BiliLite.UWP/Pages/SeasonDetailPage.xaml | 4 +- src/BiliLite.UWP/Services/ThemeService.cs | 71 +++++++++++++++++++ src/BiliLite.UWP/Startup.cs | 1 + src/BiliLite.UWP/Themes/Colors.xaml | 24 +++++-- .../Season/SeasonDetailPageViewModel.cs | 19 ++++- .../Video/VideoDetailPageViewModel.cs | 6 +- 10 files changed, 148 insertions(+), 39 deletions(-) create mode 100644 src/BiliLite.UWP/Services/ThemeService.cs diff --git a/src/BiliLite.UWP/App.xaml.cs b/src/BiliLite.UWP/App.xaml.cs index 187139b9..ce6e7b5f 100644 --- a/src/BiliLite.UWP/App.xaml.cs +++ b/src/BiliLite.UWP/App.xaml.cs @@ -161,7 +161,6 @@ private async void Navigation(object arguments, bool prelaunch = false) rootFrame.NavigationFailed += OnNavigationFailed; - //主题颜色 rootFrame.RequestedTheme = (ElementTheme)SettingService.GetValue(SettingConstants.UI.THEME, 0); @@ -182,7 +181,8 @@ private async void Navigation(object arguments, bool prelaunch = false) } // 确保当前窗口处于活动状态 Window.Current.Activate(); - ExtendAcrylicIntoTitleBar(); + var themeService = ServiceProvider.GetRequiredService(); + themeService.InitTitleBar(); } } @@ -222,6 +222,16 @@ private async void InitBili() } VideoPlayHistoryHelper.LoadABPlayHistories(true); + try + { + var themeService = ServiceProvider.GetRequiredService(); + await themeService.Init(); + } + catch (Exception ex) + { + logger.Error("初始化主题错误", ex); + } + //var pluginService = ServiceProvider.GetRequiredService(); //await pluginService.Start(); } @@ -255,11 +265,6 @@ private void OnSuspending(object sender, SuspendingEventArgs e) deferral.Complete(); } - public static void ExtendAcrylicIntoTitleBar() - { - AppExtensions.HandleTitleTheme(); - } - protected override void OnActivated(IActivatedEventArgs args) { base.OnActivated(args); diff --git a/src/BiliLite.UWP/BiliLite.UWP.csproj b/src/BiliLite.UWP/BiliLite.UWP.csproj index 3bc9bf1a..84b3684f 100644 --- a/src/BiliLite.UWP/BiliLite.UWP.csproj +++ b/src/BiliLite.UWP/BiliLite.UWP.csproj @@ -281,6 +281,7 @@ + @@ -1461,10 +1462,11 @@ MSBuild:Compile Designer - + MSBuild:Compile Designer - + Always + MSBuild:Compile Designer diff --git a/src/BiliLite.UWP/Controls/Settings/UISettingsControl.xaml.cs b/src/BiliLite.UWP/Controls/Settings/UISettingsControl.xaml.cs index fcb763cb..2e85ee31 100644 --- a/src/BiliLite.UWP/Controls/Settings/UISettingsControl.xaml.cs +++ b/src/BiliLite.UWP/Controls/Settings/UISettingsControl.xaml.cs @@ -7,6 +7,7 @@ using BiliLite.Models.Common; using BiliLite.Models.Common.Home; using BiliLite.Services; +using Microsoft.Extensions.DependencyInjection; using Microsoft.UI.Xaml.Controls; using Microsoft.Toolkit.Uwp.UI; @@ -16,8 +17,11 @@ namespace BiliLite.Controls.Settings { public sealed partial class UISettingsControl : UserControl { + private readonly ThemeService m_themeService; + public UISettingsControl() { + m_themeService = App.ServiceProvider.GetRequiredService(); this.InitializeComponent(); LoadUI(); } @@ -29,25 +33,10 @@ private void LoadUI() { cbTheme.SelectionChanged += new SelectionChangedEventHandler((obj, args) => { - SettingService.SetValue(SettingConstants.UI.THEME, cbTheme.SelectedIndex); - Frame rootFrame = Window.Current.Content as Frame; - switch (cbTheme.SelectedIndex) - { - case 1: - rootFrame.RequestedTheme = ElementTheme.Light; - break; - case 2: - rootFrame.RequestedTheme = ElementTheme.Dark; - break; - //case 3: - // // TODO: 切换自定义主题 - // rootFrame.Resources = Application.Current.Resources.ThemeDictionaries["Pink"] as ResourceDictionary; - // break; - default: - rootFrame.RequestedTheme = ElementTheme.Default; - break; - } - App.ExtendAcrylicIntoTitleBar(); + var themeIndex = cbTheme.SelectedIndex; + if (themeIndex > 2) + m_themeService.SetTheme(ElementTheme.Default); + m_themeService.SetTheme((ElementTheme)themeIndex); }); }); diff --git a/src/BiliLite.UWP/Converters/ColorSelectedConvert.cs b/src/BiliLite.UWP/Converters/ColorSelectedConvert.cs index e29abf5e..59ea77bc 100644 --- a/src/BiliLite.UWP/Converters/ColorSelectedConvert.cs +++ b/src/BiliLite.UWP/Converters/ColorSelectedConvert.cs @@ -2,16 +2,20 @@ using Windows.UI; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Media; +using BiliLite.Services; +using Microsoft.Extensions.DependencyInjection; + namespace BiliLite.Converters { public class ColorSelectedConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { - if (value == null) return new SolidColorBrush((Color)App.Current.Resources["TextColor"]); + var themeService = App.ServiceProvider.GetRequiredService(); + if (value == null) return new SolidColorBrush((Color)themeService.ThemeResource["TextColor"]); return value.ToString() == parameter.ToString() - ? new SolidColorBrush((Color)App.Current.Resources["SystemAccentColor"]) - : new SolidColorBrush((Color)App.Current.Resources["TextColor"]); + ? new SolidColorBrush((Color)themeService.ThemeResource["SystemAccentColor"]) + : new SolidColorBrush((Color)themeService.ThemeResource["TextColor"]); } public object ConvertBack(object value, Type targetType, object parameter, string language) diff --git a/src/BiliLite.UWP/Pages/SeasonDetailPage.xaml b/src/BiliLite.UWP/Pages/SeasonDetailPage.xaml index 03461d03..491ddad2 100644 --- a/src/BiliLite.UWP/Pages/SeasonDetailPage.xaml +++ b/src/BiliLite.UWP/Pages/SeasonDetailPage.xaml @@ -159,7 +159,9 @@ - + diff --git a/src/BiliLite.UWP/Services/ThemeService.cs b/src/BiliLite.UWP/Services/ThemeService.cs new file mode 100644 index 00000000..dff3d850 --- /dev/null +++ b/src/BiliLite.UWP/Services/ThemeService.cs @@ -0,0 +1,71 @@ +using System; +using System.Threading.Tasks; +using Windows.Storage; +using Windows.UI.Xaml.Markup; +using Windows.UI.Xaml; +using BiliLite.Extensions; +using BiliLite.Models.Common; +using Windows.UI.Xaml.Controls; + +namespace BiliLite.Services +{ + public class ThemeService + { + private ResourceDictionary m_defaultColorsResource; + private ElementTheme m_theme; + + public ThemeService() + { + m_theme = (ElementTheme)SettingService.GetValue(SettingConstants.UI.THEME, 0); + if (m_theme == ElementTheme.Default) + { + m_theme = (ElementTheme)(App.Current.RequestedTheme - 1); + } + } + + public async Task Init() + { + var colorFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Themes/Colors.xaml")); + var colorsText = await FileIO.ReadTextAsync(colorFile); + m_defaultColorsResource = (ResourceDictionary)XamlReader.Load(colorsText); + } + + public ResourceDictionary ThemeResource + { + get + { + if(m_theme==ElementTheme.Light)return m_defaultColorsResource.ThemeDictionaries["Light"] as ResourceDictionary; + return m_defaultColorsResource.ThemeDictionaries["Dark"] as ResourceDictionary; + } + } + + public void InitTitleBar() + { + AppExtensions.HandleTitleTheme(); + } + + public void SetTheme(ElementTheme theme) + { + m_theme = theme; + SettingService.SetValue(SettingConstants.UI.THEME, (int)theme); + var rootFrame = Window.Current.Content as Frame; + switch (theme) + { + case ElementTheme.Light: + rootFrame.RequestedTheme = ElementTheme.Light; + break; + case ElementTheme.Dark: + rootFrame.RequestedTheme = ElementTheme.Dark; + break; + //case 3: + // // TODO: 切换自定义主题 + // rootFrame.Resources = Application.Current.Resources.ThemeDictionaries["Pink"] as ResourceDictionary; + // break; + default: + rootFrame.RequestedTheme = ElementTheme.Default; + break; + } + InitTitleBar(); + } + } +} diff --git a/src/BiliLite.UWP/Startup.cs b/src/BiliLite.UWP/Startup.cs index 1745f0fa..97dd28e4 100644 --- a/src/BiliLite.UWP/Startup.cs +++ b/src/BiliLite.UWP/Startup.cs @@ -32,6 +32,7 @@ public void ConfigureServices(HostBuilderContext context, IServiceCollection ser services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); } } } diff --git a/src/BiliLite.UWP/Themes/Colors.xaml b/src/BiliLite.UWP/Themes/Colors.xaml index bcc590b4..59f0e68e 100644 --- a/src/BiliLite.UWP/Themes/Colors.xaml +++ b/src/BiliLite.UWP/Themes/Colors.xaml @@ -35,10 +35,16 @@ - + + @@ -101,6 +107,16 @@ x:Key="AccentTextFillColorTertiaryBrush" Opacity="0.7" Color="{ThemeResource SystemAccentColor}" /> + + diff --git a/src/BiliLite.UWP/ViewModels/Season/SeasonDetailPageViewModel.cs b/src/BiliLite.UWP/ViewModels/Season/SeasonDetailPageViewModel.cs index 4bd0df62..b5be6711 100644 --- a/src/BiliLite.UWP/ViewModels/Season/SeasonDetailPageViewModel.cs +++ b/src/BiliLite.UWP/ViewModels/Season/SeasonDetailPageViewModel.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using System.Windows.Input; +using Windows.UI; using Windows.UI.Xaml; using AutoMapper; using BiliLite.Extensions; @@ -18,6 +19,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PropertyChanged; +using Windows.UI.Xaml.Media; namespace BiliLite.ViewModels.Season { @@ -30,13 +32,15 @@ public class SeasonDetailPageViewModel : BaseViewModel private readonly FollowAPI m_followApi; private readonly IMapper m_mapper; private static readonly ILogger _logger = GlobalLogger.FromCurrentType(); + private readonly ThemeService m_themeService; #endregion #region Constructors public SeasonDetailPageViewModel() { - m_mapper = App.ServiceProvider.GetService(); + m_mapper = App.ServiceProvider.GetRequiredService(); + m_themeService = App.ServiceProvider.GetRequiredService(); m_seasonApi = new SeasonApi(); m_playerApi = new PlayerAPI(); m_followApi = new FollowAPI(); @@ -116,6 +120,19 @@ public GridLength RightInfoWidth } } + [DependsOn(nameof(PageHeight), nameof(PageWidth))] + public Brush RightInfoBackground + { + get + { + if (PageWidth < 1000) + { + return (Brush)m_themeService.ThemeResource["PlayerControlAcrylicBrush"]; + } + return new SolidColorBrush(Colors.Transparent); + } + } + [DependsOn(nameof(PageHeight), nameof(PageWidth))] public double RightInfoHeight { diff --git a/src/BiliLite.UWP/ViewModels/Video/VideoDetailPageViewModel.cs b/src/BiliLite.UWP/ViewModels/Video/VideoDetailPageViewModel.cs index 6ad9810c..39b4e1ac 100644 --- a/src/BiliLite.UWP/ViewModels/Video/VideoDetailPageViewModel.cs +++ b/src/BiliLite.UWP/ViewModels/Video/VideoDetailPageViewModel.cs @@ -39,6 +39,7 @@ public class VideoDetailPageViewModel : BaseViewModel readonly PlayerAPI PlayerAPI; readonly FollowAPI followAPI; private readonly IMapper m_mapper; + private readonly ThemeService m_themeService; #endregion @@ -46,7 +47,8 @@ public class VideoDetailPageViewModel : BaseViewModel public VideoDetailPageViewModel() { - m_mapper = App.ServiceProvider.GetService(); + m_mapper = App.ServiceProvider.GetRequiredService(); + m_themeService = App.ServiceProvider.GetRequiredService(); videoAPI = new VideoAPI(); favoriteAPI = new FavoriteApi(); PlayerAPI = new PlayerAPI(); @@ -152,7 +154,7 @@ public Brush RightInfoBackground { if (PageWidth < 1000) { - return (Brush)Application.Current.Resources["AcrylicInAppFillColorDefaultBrush"]; + return (Brush)m_themeService.ThemeResource["PlayerControlAcrylicBrush"]; } return new SolidColorBrush(Colors.Transparent);