Skip to content

Commit

Permalink
#922 修复部分颜色与设置主题不匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
ywmoyue committed Nov 24, 2024
1 parent 98cd2a9 commit e9f739d
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 39 deletions.
19 changes: 12 additions & 7 deletions src/BiliLite.UWP/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ private async void Navigation(object arguments, bool prelaunch = false)

rootFrame.NavigationFailed += OnNavigationFailed;


//主题颜色
rootFrame.RequestedTheme = (ElementTheme)SettingService.GetValue<int>(SettingConstants.UI.THEME, 0);

Expand All @@ -182,7 +181,8 @@ private async void Navigation(object arguments, bool prelaunch = false)
}
// 确保当前窗口处于活动状态
Window.Current.Activate();
ExtendAcrylicIntoTitleBar();
var themeService = ServiceProvider.GetRequiredService<ThemeService>();
themeService.InitTitleBar();
}
}

Expand Down Expand Up @@ -222,6 +222,16 @@ private async void InitBili()
}
VideoPlayHistoryHelper.LoadABPlayHistories(true);

try
{
var themeService = ServiceProvider.GetRequiredService<ThemeService>();
await themeService.Init();
}
catch (Exception ex)
{
logger.Error("初始化主题错误", ex);
}

//var pluginService = ServiceProvider.GetRequiredService<PluginService>();
//await pluginService.Start();
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/BiliLite.UWP/BiliLite.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@
<Compile Include="Services\DownloadService.cs" />
<Compile Include="Services\PluginService.cs" />
<Compile Include="Services\SqlMigrateService.cs" />
<Compile Include="Services\ThemeService.cs" />
<Compile Include="ViewModels\Common\MainPageViewModel.cs" />
<Compile Include="ViewModels\Common\PieControlViewModel.cs" />
<Compile Include="ViewModels\Search\BaseSearchPivotViewModel.cs" />
Expand Down Expand Up @@ -1461,10 +1462,11 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Themes\Colors.xaml">
<Content Include="Themes\Colors.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Page Include="Themes\Converter.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
27 changes: 8 additions & 19 deletions src/BiliLite.UWP/Controls/Settings/UISettingsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<ThemeService>();
this.InitializeComponent();
LoadUI();
}
Expand All @@ -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);
});
});

Expand Down
10 changes: 7 additions & 3 deletions src/BiliLite.UWP/Converters/ColorSelectedConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ThemeService>();
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)
Expand Down
4 changes: 3 additions & 1 deletion src/BiliLite.UWP/Pages/SeasonDetailPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@
</StackPanel>
</Grid>
</Grid>
<Grid Grid.Column="1" VerticalAlignment="Top" Height="{x:Bind m_viewModel.RightInfoHeight,Mode=OneWay}">
<Grid Grid.Column="1" VerticalAlignment="Top"
Background="{x:Bind m_viewModel.RightInfoBackground,Mode=OneWay}"
Height="{x:Bind m_viewModel.RightInfoHeight,Mode=OneWay}">

<Pivot x:Name="pivot" SelectionChanged="pivot_SelectionChanged" >
<PivotItem Margin="0">
Expand Down
71 changes: 71 additions & 0 deletions src/BiliLite.UWP/Services/ThemeService.cs
Original file line number Diff line number Diff line change
@@ -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<int>(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();
}
}
}
1 change: 1 addition & 0 deletions src/BiliLite.UWP/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void ConfigureServices(HostBuilderContext context, IServiceCollection ser
services.AddSingleton<SearchService>();

services.AddSingleton<GrpcService>();
services.AddSingleton<ThemeService>();
}
}
}
24 changes: 20 additions & 4 deletions src/BiliLite.UWP/Themes/Colors.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@

<SolidColorBrush x:Key="AccentTextFillColorPrimaryBrush" Color="{ThemeResource SystemAccentColor}" />
<SolidColorBrush x:Key="AccentTextFillColorSecondaryBrush" Color="{ThemeResource SystemAccentColor}" />
<SolidColorBrush
x:Key="AccentTextFillColorTertiaryBrush"
Opacity="0.7"
Color="{ThemeResource SystemAccentColor}" />

<AcrylicBrush x:Key="PlayerControlAcrylicBrush"
AlwaysUseFallback="False"
BackgroundSource="Backdrop"
FallbackColor="#FFD4D4D4"
Opacity="1"
TintColor="#FFD4D4D4"
TintLuminosityOpacity="0.96"
TintOpacity="0.15"
TintTransitionDuration="0:0:0.5" />
</ResourceDictionary>
</muxc:XamlControlsResources.MergedDictionaries>
</muxc:XamlControlsResources>
Expand Down Expand Up @@ -101,6 +107,16 @@
x:Key="AccentTextFillColorTertiaryBrush"
Opacity="0.7"
Color="{ThemeResource SystemAccentColor}" />

<AcrylicBrush x:Key="PlayerControlAcrylicBrush"
AlwaysUseFallback="False"
BackgroundSource="Backdrop"
FallbackColor="#FF2C2C2C"
Opacity="1"
TintColor="#FF2C2C2C"
TintLuminosityOpacity="0.96"
TintOpacity="0.15"
TintTransitionDuration="0:0:0.5" />
</ResourceDictionary>
</muxc:XamlControlsResources.MergedDictionaries>
</muxc:XamlControlsResources>
Expand Down
19 changes: 18 additions & 1 deletion src/BiliLite.UWP/ViewModels/Season/SeasonDetailPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PropertyChanged;
using Windows.UI.Xaml.Media;

namespace BiliLite.ViewModels.Season
{
Expand All @@ -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<IMapper>();
m_mapper = App.ServiceProvider.GetRequiredService<IMapper>();
m_themeService = App.ServiceProvider.GetRequiredService<ThemeService>();
m_seasonApi = new SeasonApi();
m_playerApi = new PlayerAPI();
m_followApi = new FollowAPI();
Expand Down Expand Up @@ -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
{
Expand Down
6 changes: 4 additions & 2 deletions src/BiliLite.UWP/ViewModels/Video/VideoDetailPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ public class VideoDetailPageViewModel : BaseViewModel
readonly PlayerAPI PlayerAPI;
readonly FollowAPI followAPI;
private readonly IMapper m_mapper;
private readonly ThemeService m_themeService;

#endregion

#region Constructors

public VideoDetailPageViewModel()
{
m_mapper = App.ServiceProvider.GetService<IMapper>();
m_mapper = App.ServiceProvider.GetRequiredService<IMapper>();
m_themeService = App.ServiceProvider.GetRequiredService<ThemeService>();
videoAPI = new VideoAPI();
favoriteAPI = new FavoriteApi();
PlayerAPI = new PlayerAPI();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e9f739d

Please sign in to comment.