From 92ee7638d8ac5cdf672bd23f9ba06a252da2049e Mon Sep 17 00:00:00 2001 From: DB p Date: Tue, 23 Nov 2021 14:33:11 +0900 Subject: [PATCH] Add Darkmode in setting Add System darkmode detection helper Add InitializeDarkmode in onload --- .../UserSettings/Settings.cs | 1 + Flow.Launcher/App.xaml | 3 +- Flow.Launcher/Flow.Launcher.csproj | 1 + Flow.Launcher/Helper/SystemTheme.cs | 76 +++++++++++ Flow.Launcher/MainWindow.xaml.cs | 16 ++- .../Resources/CustomControlTemplate.xaml | 78 ++++++------ Flow.Launcher/SettingWindow.xaml | 119 +++++++++--------- Flow.Launcher/SettingWindow.xaml.cs | 111 +++------------- .../ViewModel/SettingWindowViewModel.cs | 19 +++ 9 files changed, 227 insertions(+), 197 deletions(-) create mode 100644 Flow.Launcher/Helper/SystemTheme.cs diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index 34e86a3ed..04d5bdca9 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -15,6 +15,7 @@ namespace Flow.Launcher.Infrastructure.UserSettings private string language = "en"; public string Hotkey { get; set; } = $"{KeyConstant.Alt} + {KeyConstant.Space}"; public string OpenResultModifiers { get; set; } = KeyConstant.Alt; + public string DarkMode { get; set; } public bool ShowOpenResultHotkey { get; set; } = true; public double WindowSize { get; set; } = 580; diff --git a/Flow.Launcher/App.xaml b/Flow.Launcher/App.xaml index 73da9918d..1b119d528 100644 --- a/Flow.Launcher/App.xaml +++ b/Flow.Launcher/App.xaml @@ -8,7 +8,7 @@ - + @@ -19,7 +19,6 @@ - diff --git a/Flow.Launcher/Flow.Launcher.csproj b/Flow.Launcher/Flow.Launcher.csproj index fd2f5f1d9..b0560fbce 100644 --- a/Flow.Launcher/Flow.Launcher.csproj +++ b/Flow.Launcher/Flow.Launcher.csproj @@ -96,6 +96,7 @@ + diff --git a/Flow.Launcher/Helper/SystemTheme.cs b/Flow.Launcher/Helper/SystemTheme.cs new file mode 100644 index 000000000..3a05c3149 --- /dev/null +++ b/Flow.Launcher/Helper/SystemTheme.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; +using System.Management; +using System.Security.Principal; + +namespace Flow.Launcher.Helper +{ + class SystemTheme + { + + private const string PersonalizeKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; + private const string SysThemeValueName = "SystemUsesLightTheme"; + + public static event EventHandler SystemThemeChanged; + + public static bool GetIsSystemLightTheme() => ReadDWord(SysThemeValueName); + + private static bool ReadDWord(string valueName) + { + var regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(PersonalizeKey); + if (regkey == null) + { + return false; + } + + return (int)regkey.GetValue(valueName, 0) > 0; + } + + public static void Initialize() + { + UpdateTheme(); + try + { + var currentUser = WindowsIdentity.GetCurrent(); + var query = new WqlEventQuery(string.Format("SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_USERS' AND KeyPath='{0}\\\\{1}' AND ValueName='{2}'", + currentUser.User.Value, PersonalizeKey.Replace("\\", "\\\\"), SysThemeValueName)); + + ManagementEventWatcher watcher = new(query); + + watcher.EventArrived += new EventArrivedEventHandler(HandleEvent); + + watcher.Start(); + } + catch (ManagementException managementException) + { + Debug.WriteLine($"{nameof(SystemTheme)}: " + managementException.Message); + } + } + + private static void HandleEvent(object sender, EventArrivedEventArgs e) + { + UpdateTheme(); + } + + private static void UpdateTheme() + { + bool isSystemLightTheme = GetIsSystemLightTheme(); + var args = new SystemThemeChangedEventArgs(isSystemLightTheme); + SystemThemeChanged?.Invoke(null, args); + } + } + + public class SystemThemeChangedEventArgs : EventArgs + { + public SystemThemeChangedEventArgs(bool isSystemLightTheme) + { + IsSystemLightTheme = isSystemLightTheme; + } + + public bool IsSystemLightTheme { get; private set; } + } +} diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 16e4be8a0..261d83151 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -67,7 +67,7 @@ namespace Flow.Launcher { // show notify icon when flowlauncher is hidden InitializeNotifyIcon(); - + InitializeDarkMode(); WindowsInteropHelper.DisableControlBox(this); InitProgressbarAnimation(); InitializePosition(); @@ -372,5 +372,19 @@ namespace Flow.Launcher { QueryTextBox.CaretIndex = QueryTextBox.Text.Length; } + + public void InitializeDarkMode() + { + if (_settings.DarkMode == "Light") + { + ModernWpf.ThemeManager.Current.ApplicationTheme = ModernWpf.ApplicationTheme.Light; + } + else if (_settings.DarkMode == "Dark") + { + ModernWpf.ThemeManager.Current.ApplicationTheme = ModernWpf.ApplicationTheme.Dark; + } + else + { } + } } } \ No newline at end of file diff --git a/Flow.Launcher/Resources/CustomControlTemplate.xaml b/Flow.Launcher/Resources/CustomControlTemplate.xaml index ad0340f67..94e298860 100644 --- a/Flow.Launcher/Resources/CustomControlTemplate.xaml +++ b/Flow.Launcher/Resources/CustomControlTemplate.xaml @@ -7,13 +7,13 @@ @@ -53,7 +53,7 @@ - + @@ -439,14 +439,14 @@ Margin="4,0,0,0" VerticalAlignment="Center" FontSize="12" - Foreground="{StaticResource Color05B}" + Foreground="{DynamicResource Color05B}" Text="{DynamicResource flowlauncher_settings}" />