Skip to content

Commit 1e9eaf5

Browse files
authored
Merge pull request #3798 from Flow-Launcher/win32_dark_mode
Enable dark mode if the system is in dark mode for Win32 windows
2 parents 8e6eaf7 + c71da54 commit 1e9eaf5

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,5 +791,41 @@ public static unsafe void OpenFolderAndSelectFile(string filePath)
791791
}
792792

793793
#endregion
794+
795+
#region Win32 Dark Mode
796+
797+
/*
798+
* Inspired by https://github.com/ysc3839/win32-darkmode
799+
*/
800+
801+
[DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true)]
802+
private static extern int SetPreferredAppMode(int appMode);
803+
804+
public static void EnableWin32DarkMode(string colorScheme)
805+
{
806+
try
807+
{
808+
// Undocumented API from Windows 10 1809
809+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
810+
Environment.OSVersion.Version.Build >= 17763)
811+
{
812+
var flag = colorScheme switch
813+
{
814+
Constant.Light => 3, // ForceLight
815+
Constant.Dark => 2, // ForceDark
816+
Constant.System => 1, // AllowDark
817+
_ => 0 // Default
818+
};
819+
_ = SetPreferredAppMode(flag);
820+
}
821+
822+
}
823+
catch
824+
{
825+
// Ignore errors on unsupported OS
826+
}
827+
}
828+
829+
#endregion
794830
}
795831
}

Flow.Launcher/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
188188

189189
Notification.Install();
190190

191+
// Enable Win32 dark mode if the system is in dark mode before creating all windows
192+
Win32Helper.EnableWin32DarkMode(_settings.ColorScheme);
193+
191194
Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();
192195

193196
API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------");

Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public string ColorScheme
136136
};
137137
Settings.ColorScheme = value;
138138
_ = _theme.RefreshFrameAsync();
139+
Win32Helper.EnableWin32DarkMode(value);
139140
}
140141
}
141142

0 commit comments

Comments
 (0)