Skip to content

Commit f71aa72

Browse files
authored
Replace accent color resources to use SystemColors.AccentColor (#9404)
* Replaced accent color usage in Fluent resources with SystemColor.AccentColor's * Removed usage of DWMColoization * Added accent colors and brushes in HC.xaml * Removed extra files * Merging main branch into fluent/use-systemcolor-accentcolor-fluent
1 parent 8529feb commit f71aa72

File tree

8 files changed

+206
-368
lines changed

8 files changed

+206
-368
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,6 @@
10881088
<Compile Include="System\Windows\Documents\XPSS0ValidatingLoader.cs" />
10891089
<Compile Include="System\Windows\Documents\ZoomPercentageConverter.cs" />
10901090
<Compile Include="System\Windows\AccentColorHelper.cs" />
1091-
<Compile Include="System\Windows\DwmColorization.cs" />
10921091
<Compile Include="System\Windows\DynamicResourceExtension.cs" />
10931092
<Compile Include="System\Windows\DynamicResourceExtensionConverter.cs" />
10941093
<Compile Include="System\Windows\EventSetter.cs" />

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

Lines changed: 0 additions & 173 deletions
This file was deleted.

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ internal static void InitializeFluentTheme()
4343
var themeColorResourceUri = GetFluentWindowThemeColorResourceUri(_currentApplicationTheme, _currentUseLightMode);
4444
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = themeColorResourceUri });
4545

46-
DwmColorization.UpdateAccentColors();
4746
_isFluentThemeInitialized = true;
4847
}
4948
}
@@ -78,7 +77,7 @@ internal static void ApplySystemTheme(IEnumerable windows = null, bool forceUpda
7877

7978
string systemTheme = GetSystemTheme();
8079
bool useLightMode = IsSystemThemeLight();
81-
Color systemAccentColor = DwmColorization.GetSystemAccentColor();
80+
Color systemAccentColor = AccentColorHelper.SystemAccentColor;
8281
ApplyTheme(windows , systemTheme, useLightMode, systemAccentColor, forceUpdate);
8382
}
8483

@@ -101,9 +100,8 @@ private static void ApplyTheme(
101100
if(forceUpdate ||
102101
requestedTheme != _currentApplicationTheme ||
103102
requestedUseLightMode != _currentUseLightMode ||
104-
DwmColorization.GetSystemAccentColor() != DwmColorization.CurrentApplicationAccentColor)
103+
requestedAccentColor != _currentSystemAccentColor)
105104
{
106-
DwmColorization.UpdateAccentColors();
107105

108106
Uri dictionaryUri = GetFluentWindowThemeColorResourceUri(requestedTheme, requestedUseLightMode);
109107
AddOrUpdateThemeResources(dictionaryUri);
@@ -121,6 +119,7 @@ private static void ApplyTheme(
121119

122120
_currentApplicationTheme = requestedTheme;
123121
_currentUseLightMode = requestedUseLightMode;
122+
_currentSystemAccentColor = requestedAccentColor;
124123
}
125124
}
126125

@@ -192,18 +191,14 @@ private static void AddOrUpdateThemeResources(Uri dictionaryUri)
192191

193192
var newDictionary = new ResourceDictionary() { Source = dictionaryUri };
194193

195-
ResourceDictionary currentDictionary = Application.Current?.Resources;
196-
foreach (var key in newDictionary.Keys)
194+
FindFluentThemeAndColorDictionary(out ResourceDictionary fluentDictionary, out ResourceDictionary colorDictionary);
195+
196+
if (colorDictionary != null)
197197
{
198-
if (currentDictionary.Contains(key))
199-
{
200-
currentDictionary[key] = newDictionary[key];
201-
}
202-
else
203-
{
204-
currentDictionary.Add(key, newDictionary[key]);
205-
}
198+
Application.Current.Resources.MergedDictionaries.Remove(colorDictionary);
206199
}
200+
201+
Application.Current.Resources.MergedDictionaries.Add(newDictionary);
207202
}
208203

209204
#endregion
@@ -232,6 +227,31 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool
232227
return new Uri("pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/" + themeColorFileName, UriKind.Absolute);
233228
}
234229

230+
private static void FindFluentThemeAndColorDictionary(out ResourceDictionary fluentThemeDictionary, out ResourceDictionary fluentColorDictionary)
231+
{
232+
fluentThemeDictionary = null;
233+
fluentColorDictionary = null;
234+
235+
if (Application.Current == null) return;
236+
237+
foreach (ResourceDictionary mergedDictionary in Application.Current.Resources.MergedDictionaries)
238+
{
239+
if (mergedDictionary.Source != null)
240+
{
241+
if (mergedDictionary.Source.ToString() == fluentResourceDictionaryUri)
242+
{
243+
fluentThemeDictionary = mergedDictionary;
244+
}
245+
else if (mergedDictionary.Source.ToString().StartsWith(fluentColorResourceUriPart))
246+
{
247+
fluentColorDictionary = mergedDictionary;
248+
}
249+
}
250+
}
251+
}
252+
253+
254+
235255
#endregion
236256

237257
#region Private Members
@@ -240,6 +260,9 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool
240260

241261
private static readonly string _regPersonalizeKeyPath = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
242262

263+
private static readonly string fluentResourceDictionaryUri = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Fluent.xaml";
264+
private static readonly string fluentColorResourceUriPart = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/";
265+
243266
private static string _currentApplicationTheme;
244267

245268
private static bool _currentUseLightMode = true;
@@ -248,5 +271,7 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool
248271

249272
private static bool _isFluentThemeInitialized = false;
250273

274+
private static Color _currentSystemAccentColor = AccentColorHelper.SystemAccentColor;
275+
251276
#endregion
252277
}

src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Accent.xaml

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Fluent.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
-->
77
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
88
<ResourceDictionary.MergedDictionaries>
9-
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Resources/Accent.xaml" />
109
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Resources/DefaultContextMenu.xaml" />
1110
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Resources/DefaultFocusVisualStyle.xaml" />
1211
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Resources/Fonts.xaml" />

0 commit comments

Comments
 (0)