Skip to content

Commit faabc48

Browse files
committed
Enable opting out of system theme color changes
1 parent f16dd92 commit faabc48

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Win32/UxThemeWrapper.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ private static void GetThemeNameAndColor(out string themeName, out string themeC
275275
themeName = "Aero2";
276276
}
277277

278+
NameValueCollection appSettings = null;
278279
#if DEBUG
279280
// for debugging, config file can override the theme name
280-
NameValueCollection appSettings = null;
281281
try
282282
{
283283
appSettings = ConfigurationManager.AppSettings;
@@ -302,6 +302,32 @@ private static void GetThemeNameAndColor(out string themeName, out string themeC
302302
{
303303
themeName = "Fluent";
304304
themeColor = ThemeManager.IsSystemThemeLight() ? "Light" : "Dark";
305+
306+
appSettings = null;
307+
try
308+
{
309+
appSettings = ConfigurationManager.AppSettings;
310+
}
311+
catch (ConfigurationErrorsException)
312+
{
313+
}
314+
315+
if (appSettings != null)
316+
{
317+
string tc = appSettings["ThemeColorOverride"];
318+
if (!String.IsNullOrEmpty(tc))
319+
{
320+
switch(tc.ToLowerInvariant())
321+
{
322+
case "light":
323+
case "dark":
324+
themeColor = tc;
325+
ThemeManager.OverrideThemeColor(themeColor);
326+
break;
327+
}
328+
themeColor = tc;
329+
}
330+
}
305331
}
306332
}
307333
else

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeManager.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ internal static void UpdateBackdropAndImmersiveMode(IEnumerable windows = null)
102102
continue;
103103
}
104104

105-
SetImmersiveDarkMode(window, !ThemeManager.IsSystemThemeLight());
105+
if (ThemeManager.UseCustomThemeColor)
106+
{
107+
SetImmersiveDarkMode(window, ThemeManager.RequestedThemeColor == "Dark");
108+
}
109+
else
110+
{
111+
SetImmersiveDarkMode(window, !ThemeManager.IsSystemThemeLight());
112+
}
106113
WindowBackdropManager.SetBackdrop(window, SystemParameters.HighContrast ? WindowBackdropType.None : WindowBackdropType.MainWindow);
107114
}
108115
}
@@ -207,6 +214,27 @@ internal static bool IsSystemThemeLight()
207214
return useLightTheme != null && useLightTheme != 0;
208215
}
209216

217+
218+
internal static void OverrideThemeColor(string requestedThemeColor)
219+
{
220+
if (requestedThemeColor == null)
221+
{
222+
return;
223+
}
224+
225+
_requestedThemeColor = "System";
226+
if (requestedThemeColor == "Light" || requestedThemeColor == "Dark")
227+
{
228+
_requestedThemeColor = requestedThemeColor;
229+
}
230+
231+
if (IsFluentThemeEnabled)
232+
{
233+
var themeColorResourceUri = GetFluentWindowThemeColorResourceUri(_currentApplicationTheme, _currentUseLightMode);
234+
AddOrUpdateThemeResources(themeColorResourceUri);
235+
}
236+
}
237+
210238
/// <summary>
211239
/// Update the Fluent theme resources with the values in new dictionary.
212240
/// </summary>
@@ -241,6 +269,10 @@ private static void AddOrUpdateThemeResources(Uri dictionaryUri)
241269
// TODO : Find a better way to deal with different default font sizes for different themes.
242270
internal static double DefaultFluentThemeFontSize => 14;
243271

272+
internal static bool UseCustomThemeColor => _requestedThemeColor != "System";
273+
274+
internal static string RequestedThemeColor => _requestedThemeColor;
275+
244276
#endregion
245277

246278
#region Private Methods
@@ -279,5 +311,7 @@ string s when s.Contains("hc1") => "hc1.xaml",
279311

280312
private static bool _isFluentThemeInitialized = false;
281313

314+
private static string _requestedThemeColor = "System";
315+
282316
#endregion
283317
}

0 commit comments

Comments
 (0)