Skip to content

Commit bbca50c

Browse files
jjw24TBM13
authored andcommitted
Merge pull request Flow-Launcher#3798 from Flow-Launcher/win32_dark_mode
Enable dark mode if the system is in dark mode for Win32 windows
1 parent c5eb009 commit bbca50c

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
@@ -500,5 +500,41 @@ public static unsafe void OpenFolderAndSelectFile(string filePath)
500500
}
501501

502502
#endregion
503+
504+
#region Win32 Dark Mode
505+
506+
/*
507+
* Inspired by https://github.com/ysc3839/win32-darkmode
508+
*/
509+
510+
[DllImport("uxtheme.dll", EntryPoint = "#135", SetLastError = true)]
511+
private static extern int SetPreferredAppMode(int appMode);
512+
513+
public static void EnableWin32DarkMode(string colorScheme)
514+
{
515+
try
516+
{
517+
// Undocumented API from Windows 10 1809
518+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
519+
Environment.OSVersion.Version.Build >= 17763)
520+
{
521+
var flag = colorScheme switch
522+
{
523+
Constant.Light => 3, // ForceLight
524+
Constant.Dark => 2, // ForceDark
525+
Constant.System => 1, // AllowDark
526+
_ => 0 // Default
527+
};
528+
_ = SetPreferredAppMode(flag);
529+
}
530+
531+
}
532+
catch
533+
{
534+
// Ignore errors on unsupported OS
535+
}
536+
}
537+
538+
#endregion
503539
}
504540
}

Flow.Launcher/App.xaml.cs

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

172172
Notification.Install();
173173

174+
// Enable Win32 dark mode if the system is in dark mode before creating all windows
175+
Win32Helper.EnableWin32DarkMode(_settings.ColorScheme);
176+
174177
API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------");
175178
API.LogInfo(ClassName, $"Runtime info:{ErrorReporting.RuntimeInfo()}");
176179

Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public string ColorScheme
113113
};
114114
Settings.ColorScheme = value;
115115
_ = _theme.RefreshFrameAsync();
116+
Win32Helper.EnableWin32DarkMode(value);
116117
}
117118
}
118119

0 commit comments

Comments
 (0)