From 781749e9634b7fd4468d1c456c0059f80c06a404 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 23 Jan 2021 21:24:37 +0000 Subject: WinGui: Extend the dark theme option to be able to honour OS "AppsUseLightTheme" setting. #3369 --- win/CS/HandBrakeWPF/App.xaml.cs | 24 ++++++++++-- .../Converters/Options/ThemeSettingConverter.cs | 19 +++++++++ .../Converters/ResourceConverterBase.cs | 4 -- .../HandBrakeWPF/Converters/ThemeImageConverter.cs | 16 ++++---- win/CS/HandBrakeWPF/Model/DarkThemeMode.cs | 24 ++++++++++++ .../HandBrakeWPF/Properties/Resources.Designer.cs | 45 ++++++++++++++++++++++ win/CS/HandBrakeWPF/Properties/Resources.resx | 15 ++++++++ win/CS/HandBrakeWPF/Services/UserSettingService.cs | 3 +- win/CS/HandBrakeWPF/UserSettingConstants.cs | 2 +- win/CS/HandBrakeWPF/Utilities/SystemInfo.cs | 11 ++++++ win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs | 18 +++++---- win/CS/HandBrakeWPF/Views/OptionsView.xaml | 9 ++++- 12 files changed, 164 insertions(+), 26 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Converters/Options/ThemeSettingConverter.cs create mode 100644 win/CS/HandBrakeWPF/Model/DarkThemeMode.cs diff --git a/win/CS/HandBrakeWPF/App.xaml.cs b/win/CS/HandBrakeWPF/App.xaml.cs index a7c0fad9a..bbca2024e 100644 --- a/win/CS/HandBrakeWPF/App.xaml.cs +++ b/win/CS/HandBrakeWPF/App.xaml.cs @@ -30,6 +30,8 @@ namespace HandBrakeWPF using HandBrakeWPF.ViewModels; using HandBrakeWPF.ViewModels.Interfaces; + using Microsoft.Win32; + using GeneralApplicationException = Exceptions.GeneralApplicationException; /// @@ -119,12 +121,26 @@ namespace HandBrakeWPF } } - bool useDarkTheme = userSettingService.GetUserSetting(UserSettingConstants.UseDarkTheme); - if (useDarkTheme && SystemInfo.IsWindows10()) + DarkThemeMode useDarkTheme = (DarkThemeMode)userSettingService.GetUserSetting(UserSettingConstants.DarkThemeMode); + if (SystemInfo.IsWindows10()) { ResourceDictionary darkTheme = new ResourceDictionary(); - darkTheme.Source = new Uri("Themes/Dark.xaml", UriKind.Relative); - Application.Current.Resources.MergedDictionaries.Add(darkTheme); + switch (useDarkTheme) + { + case DarkThemeMode.System: + if (SystemInfo.IsAppsUsingDarkTheme()) + { + darkTheme.Source = new Uri("Themes/Dark.xaml", UriKind.Relative); + Application.Current.Resources.MergedDictionaries.Add(darkTheme); + } + break; + case DarkThemeMode.Dark: + darkTheme.Source = new Uri("Themes/Dark.xaml", UriKind.Relative); + Application.Current.Resources.MergedDictionaries.Add(darkTheme); + break; + default: + break; + } } // NO-Hardware Mode diff --git a/win/CS/HandBrakeWPF/Converters/Options/ThemeSettingConverter.cs b/win/CS/HandBrakeWPF/Converters/Options/ThemeSettingConverter.cs new file mode 100644 index 000000000..53c44e958 --- /dev/null +++ b/win/CS/HandBrakeWPF/Converters/Options/ThemeSettingConverter.cs @@ -0,0 +1,19 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the ThemeSettingConverter type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Converters.Options +{ + using System.Windows.Data; + + using HandBrakeWPF.Model; + + public class ThemeSettingConverter : ResourceConverterBase, IValueConverter + { + } +} diff --git a/win/CS/HandBrakeWPF/Converters/ResourceConverterBase.cs b/win/CS/HandBrakeWPF/Converters/ResourceConverterBase.cs index 7f1a1b433..50cfb7640 100644 --- a/win/CS/HandBrakeWPF/Converters/ResourceConverterBase.cs +++ b/win/CS/HandBrakeWPF/Converters/ResourceConverterBase.cs @@ -10,14 +10,10 @@ namespace HandBrakeWPF.Converters { using System; - using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; - using System.Text; - using System.Threading.Tasks; - using HandBrakeWPF.Model.Options; using HandBrakeWPF.Utilities; public class ResourceConverterBase diff --git a/win/CS/HandBrakeWPF/Converters/ThemeImageConverter.cs b/win/CS/HandBrakeWPF/Converters/ThemeImageConverter.cs index 78b06c622..cc25401c6 100644 --- a/win/CS/HandBrakeWPF/Converters/ThemeImageConverter.cs +++ b/win/CS/HandBrakeWPF/Converters/ThemeImageConverter.cs @@ -12,11 +12,11 @@ namespace HandBrakeWPF.Converters using System; using System.Globalization; using System.Linq; - using System.Threading; using System.Windows.Data; using Caliburn.Micro; + using HandBrakeWPF.Model; using HandBrakeWPF.Services.Interfaces; public class ThemeImageConverter : IValueConverter @@ -28,7 +28,12 @@ namespace HandBrakeWPF.Converters public ThemeImageConverter() { this.userSettingService = IoC.Get(); - this.isDarkTheme = this.userSettingService.GetUserSetting(UserSettingConstants.UseDarkTheme); + DarkThemeMode mode = (DarkThemeMode)this.userSettingService.GetUserSetting(UserSettingConstants.DarkThemeMode); + + if (mode == DarkThemeMode.Dark || (mode == DarkThemeMode.System && Utilities.SystemInfo.IsAppsUsingDarkTheme())) + { + this.isDarkTheme = true; + } } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) @@ -36,7 +41,6 @@ namespace HandBrakeWPF.Converters string image = parameter as string; if (!string.IsNullOrEmpty(image)) { - string direcotry = "Images/"; if (image.Contains("/")) { @@ -50,10 +54,8 @@ namespace HandBrakeWPF.Converters { return direcotry + "Dark/" + image; } - else - { - return direcotry + "Light/" + image; - } + + return direcotry + "Light/" + image; } return null; diff --git a/win/CS/HandBrakeWPF/Model/DarkThemeMode.cs b/win/CS/HandBrakeWPF/Model/DarkThemeMode.cs new file mode 100644 index 000000000..da5a50a23 --- /dev/null +++ b/win/CS/HandBrakeWPF/Model/DarkThemeMode.cs @@ -0,0 +1,24 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Model +{ + using HandBrake.Interop.Attributes; + + using HandBrakeWPF.Properties; + + public enum DarkThemeMode + { + [DisplayName(typeof(Resources), "DarkTheme_light")] + Light, + + [DisplayName(typeof(Resources), "DarkTheme_dark")] + Dark, + + [DisplayName(typeof(Resources), "DarkTheme_system")] + System, + } +} \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs index ec98e4386..601dabd99 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs +++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs @@ -852,6 +852,33 @@ namespace HandBrakeWPF.Properties { } } + /// + /// Looks up a localized string similar to Use Dark. + /// + public static string DarkTheme_dark { + get { + return ResourceManager.GetString("DarkTheme_dark", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Use Light. + /// + public static string DarkTheme_light { + get { + return ResourceManager.GetString("DarkTheme_light", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Match System. + /// + public static string DarkTheme_system { + get { + return ResourceManager.GetString("DarkTheme_system", resourceCulture); + } + } + /// /// Looks up a localized string similar to Create Folder?. /// @@ -3528,6 +3555,15 @@ namespace HandBrakeWPF.Properties { } } + /// + /// Looks up a localized string similar to Theme:. + /// + public static string OptionsView_DarkThemeMode { + get { + return ResourceManager.GetString("OptionsView_DarkThemeMode", resourceCulture); + } + } + /// /// Looks up a localized string similar to Downloading.... /// @@ -3756,6 +3792,15 @@ namespace HandBrakeWPF.Properties { } } + /// + /// Looks up a localized string similar to (Requires Restart). + /// + public static string OptionsView_RequiresRestart { + get { + return ResourceManager.GetString("OptionsView_RequiresRestart", resourceCulture); + } + } + /// /// Looks up a localized string similar to Please select a folder.. /// diff --git a/win/CS/HandBrakeWPF/Properties/Resources.resx b/win/CS/HandBrakeWPF/Properties/Resources.resx index d4432a4b1..34943b9e6 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.resx @@ -2402,4 +2402,19 @@ Fields are limited to: Show crop adjustment controls. + + Use Dark + + + Use Light + + + Match System + + + Theme: + + + (Requires Restart) + \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Services/UserSettingService.cs b/win/CS/HandBrakeWPF/Services/UserSettingService.cs index 7d121bdf6..c12a5713d 100644 --- a/win/CS/HandBrakeWPF/Services/UserSettingService.cs +++ b/win/CS/HandBrakeWPF/Services/UserSettingService.cs @@ -19,6 +19,7 @@ namespace HandBrakeWPF.Services using HandBrake.Interop.Utilities; using HandBrakeWPF.Extensions; + using HandBrakeWPF.Model; using HandBrakeWPF.Properties; using HandBrakeWPF.Services.Interfaces; using HandBrakeWPF.Utilities; @@ -269,7 +270,7 @@ namespace HandBrakeWPF.Services defaults.Add(UserSettingConstants.UpdateStatus, true); defaults.Add(UserSettingConstants.LastUpdateCheckDate, DateTime.Now.Date.AddDays(-30)); defaults.Add(UserSettingConstants.DaysBetweenUpdateCheck, 1); - defaults.Add(UserSettingConstants.UseDarkTheme, false); + defaults.Add(UserSettingConstants.DarkThemeMode, DarkThemeMode.Light); defaults.Add(UserSettingConstants.ShowPreviewOnSummaryTab, true); defaults.Add(UserSettingConstants.MainWindowMinimize, false); defaults.Add(UserSettingConstants.ClearCompletedFromQueue, false); diff --git a/win/CS/HandBrakeWPF/UserSettingConstants.cs b/win/CS/HandBrakeWPF/UserSettingConstants.cs index 8ecb34ee6..2869be6c3 100644 --- a/win/CS/HandBrakeWPF/UserSettingConstants.cs +++ b/win/CS/HandBrakeWPF/UserSettingConstants.cs @@ -70,7 +70,7 @@ namespace HandBrakeWPF public const string AutonameFileCollisionBehaviour = "AutonameFileCollisionBehaviour"; public const string AutonameFilePrePostString = "AutonameFilePrePostString"; public const string WhenDonePerformActionImmediately = "WhenDonePerformActionImmediately"; - public const string UseDarkTheme = "UseDarkTheme"; + public const string DarkThemeMode = "DarkThemeMode"; public const string PreviewRotationFlip = "PreviewRotationFlip"; public const string AlwaysUseDefaultPath = "AlwaysUseDefaultPath"; public const string PauseEncodingOnLowBattery = "PauseEncodingOnLowBattery"; diff --git a/win/CS/HandBrakeWPF/Utilities/SystemInfo.cs b/win/CS/HandBrakeWPF/Utilities/SystemInfo.cs index e1452910a..ece1bdd34 100644 --- a/win/CS/HandBrakeWPF/Utilities/SystemInfo.cs +++ b/win/CS/HandBrakeWPF/Utilities/SystemInfo.cs @@ -149,5 +149,16 @@ namespace HandBrakeWPF.Utilities return false; } + + public static bool IsAppsUsingDarkTheme() + { + object value = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", "AppsUseLightTheme", null); + if (value != null) + { + return (int)value != 1; + } + + return false; + } } } diff --git a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs index acd3a2b0a..e62c45546 100644 --- a/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs @@ -102,7 +102,7 @@ namespace HandBrakeWPF.ViewModels private string prePostFilenameText; private bool showPrePostFilenameBox; private bool whenDonePerformActionImmediately; - private bool useDarkTheme; + private DarkThemeMode darkThemeMode; private bool alwaysUseDefaultPath; private bool pauseOnLowBattery; private int lowBatteryLevel; @@ -344,14 +344,16 @@ namespace HandBrakeWPF.ViewModels } } - public bool UseDarkTheme + public BindingList DarkThemeModes { get; } = new BindingList(EnumHelper.GetEnumList().ToList()); + + public DarkThemeMode DarkThemeMode { - get => this.useDarkTheme; + get => this.darkThemeMode; set { - if (value == this.useDarkTheme) return; - this.useDarkTheme = value; - this.NotifyOfPropertyChange(() => this.UseDarkTheme); + if (value == this.darkThemeMode) return; + this.darkThemeMode = value; + this.NotifyOfPropertyChange(() => this.DarkThemeMode); } } @@ -1104,7 +1106,7 @@ namespace HandBrakeWPF.ViewModels this.ShowPreviewOnSummaryTab = this.userSettingService.GetUserSetting(UserSettingConstants.ShowPreviewOnSummaryTab); this.ShowAddAllToQueue = this.userSettingService.GetUserSetting(UserSettingConstants.ShowAddAllToQueue); this.ShowAddSelectionToQueue = this.userSettingService.GetUserSetting(UserSettingConstants.ShowAddSelectionToQueue); - this.UseDarkTheme = this.userSettingService.GetUserSetting(UserSettingConstants.UseDarkTheme); + this.DarkThemeMode = (DarkThemeMode)this.userSettingService.GetUserSetting(UserSettingConstants.DarkThemeMode); // ############################# // When Done @@ -1304,7 +1306,7 @@ namespace HandBrakeWPF.ViewModels this.userSettingService.SetUserSetting(UserSettingConstants.SendFileToArgs, this.Arguments); this.userSettingService.SetUserSetting(UserSettingConstants.ShowStatusInTitleBar, this.ShowStatusInTitleBar); this.userSettingService.SetUserSetting(UserSettingConstants.ShowPreviewOnSummaryTab, this.ShowPreviewOnSummaryTab); - this.userSettingService.SetUserSetting(UserSettingConstants.UseDarkTheme, this.UseDarkTheme); + this.userSettingService.SetUserSetting(UserSettingConstants.DarkThemeMode, this.DarkThemeMode); this.userSettingService.SetUserSetting(UserSettingConstants.UiLanguage, this.SelectedLanguage?.Culture); this.userSettingService.SetUserSetting(UserSettingConstants.ShowAddAllToQueue, this.ShowAddAllToQueue); this.userSettingService.SetUserSetting(UserSettingConstants.ShowAddSelectionToQueue, this.ShowAddSelectionToQueue); diff --git a/win/CS/HandBrakeWPF/Views/OptionsView.xaml b/win/CS/HandBrakeWPF/Views/OptionsView.xaml index 837484f2c..dc7a7ab46 100644 --- a/win/CS/HandBrakeWPF/Views/OptionsView.xaml +++ b/win/CS/HandBrakeWPF/Views/OptionsView.xaml @@ -41,6 +41,7 @@ + @@ -137,8 +138,14 @@ + + + + + + - -- cgit v1.2.3