From 15d979bc2c41881f627001441cd4d91e19963069 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 5 Jul 2025 23:05:49 +0800 Subject: [PATCH 1/2] Enable Win32 dark mode if the system is in dark mode before creating all windows --- Flow.Launcher.Infrastructure/Win32Helper.cs | 30 +++++++++++++++++++++ Flow.Launcher/App.xaml.cs | 3 +++ 2 files changed, 33 insertions(+) diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 86e7b7c971c..b6fbcb43e94 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -791,5 +791,35 @@ public static unsafe void OpenFolderAndSelectFile(string filePath) } #endregion + + #region Win32 Dark Mode + + /* + * Inspired by https://github.com/ysc3839/win32-darkmode + */ + + [DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true)] + private static extern int SetPreferredAppMode(int appMode); + + public static void EnableWin32DarkMode() + { + try + { + // From Windows 10 1809 + // AppMode: 0=Default, 1=AllowDark, 2=ForceDark, 3=ForceLight, 4=Max + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && + Environment.OSVersion.Version.Build >= 17763) + { + _ = SetPreferredAppMode(1); + } + + } + catch + { + // Ignore errors on unsupported OS + } + } + + #endregion } } diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 5df1f88aeba..a9cefa55413 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -188,6 +188,9 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () => Notification.Install(); + // Enable Win32 dark mode if the system is in dark mode before creating all windows + Win32Helper.EnableWin32DarkMode(); + Ioc.Default.GetRequiredService().PreStartCleanUpAfterPortabilityUpdate(); API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------"); From c71da5413a5716b8f464036de70cbc13384ec0ab Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 6 Jul 2025 09:08:20 +0800 Subject: [PATCH 2/2] Support for color scheme change --- Flow.Launcher.Infrastructure/Win32Helper.cs | 14 ++++++++++---- Flow.Launcher/App.xaml.cs | 2 +- .../ViewModels/SettingsPaneThemeViewModel.cs | 1 + 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index b6fbcb43e94..32ed3113738 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -801,16 +801,22 @@ public static unsafe void OpenFolderAndSelectFile(string filePath) [DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true)] private static extern int SetPreferredAppMode(int appMode); - public static void EnableWin32DarkMode() + public static void EnableWin32DarkMode(string colorScheme) { try { - // From Windows 10 1809 - // AppMode: 0=Default, 1=AllowDark, 2=ForceDark, 3=ForceLight, 4=Max + // Undocumented API from Windows 10 1809 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.OSVersion.Version.Build >= 17763) { - _ = SetPreferredAppMode(1); + var flag = colorScheme switch + { + Constant.Light => 3, // ForceLight + Constant.Dark => 2, // ForceDark + Constant.System => 1, // AllowDark + _ => 0 // Default + }; + _ = SetPreferredAppMode(flag); } } diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index a9cefa55413..e99f5e643e3 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -189,7 +189,7 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () => Notification.Install(); // Enable Win32 dark mode if the system is in dark mode before creating all windows - Win32Helper.EnableWin32DarkMode(); + Win32Helper.EnableWin32DarkMode(_settings.ColorScheme); Ioc.Default.GetRequiredService().PreStartCleanUpAfterPortabilityUpdate(); diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs index b62a3549529..3bee2a2b6d3 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs @@ -136,6 +136,7 @@ public string ColorScheme }; Settings.ColorScheme = value; _ = _theme.RefreshFrameAsync(); + Win32Helper.EnableWin32DarkMode(value); } }