From 5ced8f6c71cb2e59cfca10cb81707cfc7fc63170 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Tue, 5 Mar 2024 22:41:08 +0800 Subject: [PATCH 1/4] impl #1434 --- .../Snap.Hutao/Core/Windowing/Theme.cs | 17 ++++++++ .../Core/Windowing/WindowController.cs | 17 ++++++++ .../Model/Entity/SettingEntry.Constant.cs | 1 + .../Resource/Localization/SH.en.resx | 41 +++++++++++-------- .../Snap.Hutao/Resource/Localization/SH.resx | 15 +++++++ .../Snap.Hutao/Service/AppOptions.cs | 9 ++++ .../Snap.Hutao/View/Page/SettingPage.xaml | 11 +++++ .../ViewModel/Setting/SettingViewModel.cs | 13 ++++++ 8 files changed, 108 insertions(+), 16 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs new file mode 100644 index 00000000..2bf0a655 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs @@ -0,0 +1,17 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +namespace Snap.Hutao.Core.Windowing; + +[Localization] +internal enum Theme +{ + [LocalizationKey(nameof(SH.CoreWindowThemeLight))] + Light, + + [LocalizationKey(nameof(SH.CoreWindowThemeDark))] + Dark, + + [LocalizationKey(nameof(SH.CoreWindowThemeSystem))] + System, +} diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs index 5b4bada8..2ae9afd4 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs @@ -129,6 +129,13 @@ internal sealed class WindowController UpdateSystemBackdrop(options.BackdropType); } } + else if (e.PropertyName is nameof(AppOptions.Theme)) + { + if (sender is AppOptions options) + { + UpdateTheme(options.Theme); + } + } } private void OnWindowClosed(object sender, WindowEventArgs args) @@ -170,6 +177,16 @@ internal sealed class WindowController }; } + private void UpdateTheme(Theme theme) + { + ((FrameworkElement)window.Content).RequestedTheme = theme switch + { + Theme.Light => ElementTheme.Light, + Theme.Dark => ElementTheme.Dark, + _ => ElementTheme.Default, + }; + } + private void UpdateTitleButtonColor() { AppWindowTitleBar appTitleBar = window.AppWindow.TitleBar; diff --git a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs index 9b70a6db..7cc988e7 100644 --- a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs +++ b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs @@ -13,6 +13,7 @@ internal sealed partial class SettingEntry public const string Culture = "Culture"; public const string SystemBackdropType = "SystemBackdropType"; + public const string Theme = "Theme"; public const string BackgroundImageType = "BackgroundImageType"; public const string AnnouncementRegion = "AnnouncementRegion"; diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.en.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.en.resx index 06220390..4c03eb10 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.en.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.en.resx @@ -60,45 +60,45 @@ : and then encoded with base64 encoding. --> - + - + - - - - + + + + - - + + - - + + - - - - + + + + - + - + @@ -186,6 +186,15 @@ No WebView2 Runtime detected + + Dark + + + Light + + + System + Export diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 2450ca89..eead6b9d 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -189,6 +189,15 @@ [{0}] 热键 [{1}] 注册失败 + + 深色 + + + 浅色 + + + 跟随系统 + 导出 @@ -2627,6 +2636,12 @@ 评价软件 + + 更改窗体的颜色主题 + + + 颜色主题 + 贡献翻译 diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs index 34a082be..839e5488 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs @@ -16,6 +16,7 @@ internal sealed partial class AppOptions : DbStoreOptions { private bool? isEmptyHistoryWishVisible; private BackdropType? backdropType; + private Theme? theme; private BackgroundImageType? backgroundImageType; private Region? region; private string? geetestCustomCompositeUrl; @@ -34,6 +35,14 @@ internal sealed partial class AppOptions : DbStoreOptions set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, EnumToStringOrEmpty); } + public List> Themes { get; } = CollectionsNameValue.FromEnum(theme => theme.GetLocalizedDescription()); + + public Theme Theme + { + get => GetOption(ref theme, SettingEntry.Theme, EnumParse, Theme.System).Value; + set => SetOption(ref theme, SettingEntry.Theme, value, EnumToStringOrEmpty); + } + public List> BackgroundImageTypes { get; } = CollectionsNameValue.FromEnum(type => type.GetLocalizedDescription()); public BackgroundImageType BackgroundImageType diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml index ff1dd372..77115d0d 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml @@ -314,6 +314,17 @@ SelectedItem="{Binding SelectedBackdropType, Mode=TwoWay}"/> + + + + + ? selectedBackdropType; + private NameValue? selectedTheme; private NameValue? selectedBackgroundImageType; private NameValue? selectedCulture; private NameValue? selectedRegion; @@ -94,6 +95,18 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel } } + public NameValue? SelectedTheme + { + get => selectedTheme ??= AppOptions.Themes.Single(t => t.Value == AppOptions.Theme); + set + { + if (SetProperty(ref selectedTheme, value) && value is not null) + { + AppOptions.Theme = value.Value; + } + } + } + public NameValue? SelectedBackgroundImageType { get => selectedBackgroundImageType ??= AppOptions.BackgroundImageTypes.Single(t => t.Value == AppOptions.BackgroundImageType); From 91361ddab5c625741dda6d761039b814d3e642df Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Wed, 6 Mar 2024 11:13:37 +0800 Subject: [PATCH 2/4] resolve review --- .../Core/Windowing/KnownElementThemes.cs | 20 +++++++++++++++++++ .../Snap.Hutao/Core/Windowing/Theme.cs | 17 ---------------- .../Core/Windowing/WindowController.cs | 13 ++++-------- .../Model/Entity/SettingEntry.Constant.cs | 2 +- .../Snap.Hutao/Service/AppOptions.cs | 11 +++++----- .../Snap.Hutao/View/Page/SettingPage.xaml | 4 ++-- .../ViewModel/Setting/SettingViewModel.cs | 11 +++++----- 7 files changed, 39 insertions(+), 39 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs delete mode 100644 src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs new file mode 100644 index 00000000..90d60082 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs @@ -0,0 +1,20 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Microsoft.UI.Xaml; +using Snap.Hutao.Model; + +namespace Snap.Hutao.Core.Windowing; + +internal static class KnownElementThemes +{ + public static List> Get() + { + return + [ + new(SH.CoreWindowThemeLight, ElementTheme.Light), + new(SH.CoreWindowThemeDark,ElementTheme.Dark), + new(SH.CoreWindowThemeSystem, ElementTheme.Default), + ]; + } +} diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs deleted file mode 100644 index 2bf0a655..00000000 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/Theme.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) DGP Studio. All rights reserved. -// Licensed under the MIT license. - -namespace Snap.Hutao.Core.Windowing; - -[Localization] -internal enum Theme -{ - [LocalizationKey(nameof(SH.CoreWindowThemeLight))] - Light, - - [LocalizationKey(nameof(SH.CoreWindowThemeDark))] - Dark, - - [LocalizationKey(nameof(SH.CoreWindowThemeSystem))] - System, -} diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs index 2ae9afd4..46adc3f5 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs @@ -129,11 +129,11 @@ internal sealed class WindowController UpdateSystemBackdrop(options.BackdropType); } } - else if (e.PropertyName is nameof(AppOptions.Theme)) + else if (e.PropertyName is nameof(AppOptions.ElementTheme)) { if (sender is AppOptions options) { - UpdateTheme(options.Theme); + UpdateElementTheme(options.ElementTheme); } } } @@ -177,14 +177,9 @@ internal sealed class WindowController }; } - private void UpdateTheme(Theme theme) + private void UpdateElementTheme(ElementTheme theme) { - ((FrameworkElement)window.Content).RequestedTheme = theme switch - { - Theme.Light => ElementTheme.Light, - Theme.Dark => ElementTheme.Dark, - _ => ElementTheme.Default, - }; + ((FrameworkElement)window.Content).RequestedTheme = theme; } private void UpdateTitleButtonColor() diff --git a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs index 7cc988e7..1263d84f 100644 --- a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs +++ b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs @@ -13,7 +13,7 @@ internal sealed partial class SettingEntry public const string Culture = "Culture"; public const string SystemBackdropType = "SystemBackdropType"; - public const string Theme = "Theme"; + public const string ElementTheme = "ElementTheme"; public const string BackgroundImageType = "BackgroundImageType"; public const string AnnouncementRegion = "AnnouncementRegion"; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs index 839e5488..e9c31b03 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs @@ -1,6 +1,7 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. +using Microsoft.UI.Xaml; using Snap.Hutao.Core.Windowing; using Snap.Hutao.Model; using Snap.Hutao.Model.Entity; @@ -16,7 +17,7 @@ internal sealed partial class AppOptions : DbStoreOptions { private bool? isEmptyHistoryWishVisible; private BackdropType? backdropType; - private Theme? theme; + private ElementTheme? elementTheme; private BackgroundImageType? backgroundImageType; private Region? region; private string? geetestCustomCompositeUrl; @@ -35,12 +36,12 @@ internal sealed partial class AppOptions : DbStoreOptions set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, EnumToStringOrEmpty); } - public List> Themes { get; } = CollectionsNameValue.FromEnum(theme => theme.GetLocalizedDescription()); + public Lazy>> LazyElementThemes { get; } = new(KnownElementThemes.Get); - public Theme Theme + public ElementTheme ElementTheme { - get => GetOption(ref theme, SettingEntry.Theme, EnumParse, Theme.System).Value; - set => SetOption(ref theme, SettingEntry.Theme, value, EnumToStringOrEmpty); + get => GetOption(ref elementTheme, SettingEntry.ElementTheme, EnumParse, ElementTheme.Default).Value; + set => SetOption(ref elementTheme, SettingEntry.ElementTheme, value, EnumToStringOrEmpty); } public List> BackgroundImageTypes { get; } = CollectionsNameValue.FromEnum(type => type.GetLocalizedDescription()); diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml index 77115d0d..ce6b9618 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml @@ -321,8 +321,8 @@ + ItemsSource="{Binding AppOptions.LazyElementThemes.Value}" + SelectedItem="{Binding SelectedElementTheme, Mode=TwoWay}"/> ? selectedBackdropType; - private NameValue? selectedTheme; + private NameValue? selectedElementTheme; private NameValue? selectedBackgroundImageType; private NameValue? selectedCulture; private NameValue? selectedRegion; @@ -95,14 +96,14 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel } } - public NameValue? SelectedTheme + public NameValue? SelectedElementTheme { - get => selectedTheme ??= AppOptions.Themes.Single(t => t.Value == AppOptions.Theme); + get => selectedElementTheme ??= AppOptions.LazyElementThemes.Value.Single(t => t.Value == AppOptions.ElementTheme); set { - if (SetProperty(ref selectedTheme, value) && value is not null) + if (SetProperty(ref selectedElementTheme, value) && value is not null) { - AppOptions.Theme = value.Value; + AppOptions.ElementTheme = value.Value; } } } From 38577f9813ecf7988cd9b7d414a98b6f7f4b00e8 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Thu, 7 Mar 2024 13:37:22 +0800 Subject: [PATCH 3/4] apply suggestion --- .../Core/Windowing/WindowController.cs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs index 46adc3f5..67949e07 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/WindowController.cs @@ -122,20 +122,17 @@ internal sealed class WindowController private void OnOptionsPropertyChanged(object? sender, PropertyChangedEventArgs e) { - if (e.PropertyName is nameof(AppOptions.BackdropType)) + if (sender is not AppOptions options) { - if (sender is AppOptions options) - { - UpdateSystemBackdrop(options.BackdropType); - } + return; } - else if (e.PropertyName is nameof(AppOptions.ElementTheme)) + + _ = e.PropertyName switch { - if (sender is AppOptions options) - { - UpdateElementTheme(options.ElementTheme); - } - } + nameof(AppOptions.BackdropType) => UpdateSystemBackdrop(options.BackdropType), + nameof(AppOptions.ElementTheme) => UpdateElementTheme(options.ElementTheme), + _ => false, + }; } private void OnWindowClosed(object sender, WindowEventArgs args) @@ -165,7 +162,7 @@ internal sealed class WindowController } } - private void UpdateSystemBackdrop(BackdropType backdropType) + private bool UpdateSystemBackdrop(BackdropType backdropType) { window.SystemBackdrop = backdropType switch { @@ -175,11 +172,15 @@ internal sealed class WindowController BackdropType.Acrylic => new DesktopAcrylicBackdrop(), _ => null, }; + + return true; } - private void UpdateElementTheme(ElementTheme theme) + private bool UpdateElementTheme(ElementTheme theme) { ((FrameworkElement)window.Content).RequestedTheme = theme; + + return true; } private void UpdateTitleButtonColor() From 0c4c509fd661f72189e6c7c731288c7bbb298cf9 Mon Sep 17 00:00:00 2001 From: Lightczx <1686188646@qq.com> Date: Thu, 7 Mar 2024 16:27:01 +0800 Subject: [PATCH 4/4] code style --- .../Core/Windowing/KnownElementThemes.cs | 20 ------------------- .../Snap.Hutao/Service/AppOptions.cs | 7 ++++++- 2 files changed, 6 insertions(+), 21 deletions(-) delete mode 100644 src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs b/src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs deleted file mode 100644 index 90d60082..00000000 --- a/src/Snap.Hutao/Snap.Hutao/Core/Windowing/KnownElementThemes.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) DGP Studio. All rights reserved. -// Licensed under the MIT license. - -using Microsoft.UI.Xaml; -using Snap.Hutao.Model; - -namespace Snap.Hutao.Core.Windowing; - -internal static class KnownElementThemes -{ - public static List> Get() - { - return - [ - new(SH.CoreWindowThemeLight, ElementTheme.Light), - new(SH.CoreWindowThemeDark,ElementTheme.Dark), - new(SH.CoreWindowThemeSystem, ElementTheme.Default), - ]; - } -} diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs index e9c31b03..b09b066b 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs @@ -36,7 +36,12 @@ internal sealed partial class AppOptions : DbStoreOptions set => SetOption(ref backdropType, SettingEntry.SystemBackdropType, value, EnumToStringOrEmpty); } - public Lazy>> LazyElementThemes { get; } = new(KnownElementThemes.Get); + public Lazy>> LazyElementThemes { get; } = new(() => + [ + new(SH.CoreWindowThemeLight, ElementTheme.Light), + new(SH.CoreWindowThemeDark, ElementTheme.Dark), + new(SH.CoreWindowThemeSystem, ElementTheme.Default), + ]); public ElementTheme ElementTheme {