diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj index 08dff73c8e8..0f5ba7b3ebd 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/PresentationFramework.csproj @@ -1088,7 +1088,6 @@ - diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DWMColorization.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DWMColorization.cs deleted file mode 100644 index 120d39cfbf8..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DWMColorization.cs +++ /dev/null @@ -1,173 +0,0 @@ -using System; -using System.Diagnostics; -using System.Windows.Appearance; -using System.Windows.Media; -using Microsoft.Win32; -using MS.Internal; -using System.Runtime.InteropServices; -using MS.Internal.WindowsRuntime.Windows.UI.ViewManagement; - -namespace System.Windows; -internal static class DwmColorization -{ - private static Color _currentApplicationAccentColor = Color.FromArgb(0xff, 0x00, 0x78, 0xd4); - - private static UISettings _uiSettings = null; - - /// - /// Gets the current application accent color. - /// - internal static Color CurrentApplicationAccentColor - { - get { return _currentApplicationAccentColor; } - } - - internal static UISettings _UISettings - { - get - { - if (_uiSettings == null) - { - _uiSettings = new UISettings(); - } - - return _uiSettings; - } - } - - /// - /// Gets the system accent color. - /// - /// Updated Accent Color. - internal static Color GetSystemAccentColor() - { - _UISettings.TryGetColorValue(UISettingsRCW.UIColorType.Accent, out Color systemAccent); - return systemAccent; - } - - /// - /// Computes the current Accent Colors and calls for updating of accent color values in resource dictionary - /// - internal static void UpdateAccentColors() - { - Color systemAccent = GetSystemAccentColor(); - Color primaryAccent, secondaryAccent, tertiaryAccent; - - if (systemAccent != _currentApplicationAccentColor) - { - _UISettings.TryUpdateAccentColors(); - } - - if (ThemeManager.IsSystemThemeLight()) - { - // In light mode, we use darker shades of the accent color - primaryAccent = _UISettings.AccentDark1; - secondaryAccent = _UISettings.AccentDark2; - tertiaryAccent = _UISettings.AccentDark3; - } - else - { - // In dark mode, we use lighter shades of the accent color - primaryAccent = _UISettings.AccentLight1; - secondaryAccent = _UISettings.AccentLight2; - tertiaryAccent = _UISettings.AccentLight3; - } - - UpdateColorResources(systemAccent, primaryAccent, secondaryAccent, tertiaryAccent); - _currentApplicationAccentColor = systemAccent; - } - - /// - /// Updates application resources. - /// - private static void UpdateColorResources( - Color systemAccent, - Color primaryAccent, - Color secondaryAccent, - Color tertiaryAccent) - { -#if DEBUG - System.Diagnostics.Debug.WriteLine("INFO | SystemAccentColor: " + systemAccent, "System.Windows.Accent"); - System - .Diagnostics - .Debug - .WriteLine("INFO | SystemAccentColorPrimary: " + primaryAccent, "System.Windows.Accent"); - System - .Diagnostics - .Debug - .WriteLine("INFO | SystemAccentColorSecondary: " + secondaryAccent, "System.Windows.Accent"); - System - .Diagnostics - .Debug - .WriteLine("INFO | SystemAccentColorTertiary: " + tertiaryAccent, "System.Windows.Accent"); -#endif - - if (!ThemeManager.IsSystemThemeLight()) - { -#if DEBUG - System.Diagnostics.Debug.WriteLine("INFO | Text on accent is DARK", "System.Windows.Accent"); -#endif - Application.Current.Resources["TextOnAccentFillColorPrimary"] = - Color.FromArgb( 0xFF, 0x00, 0x00, 0x00); - - Application.Current.Resources["TextOnAccentFillColorSecondary"] = - Color.FromArgb( 0x80, 0x00, 0x00, 0x00); - - Application.Current.Resources["TextOnAccentFillColorDisabled"] = - Color.FromArgb( 0x77, 0x00, 0x00, 0x00); - - Application.Current.Resources["TextOnAccentFillColorSelectedText"] = - Color.FromArgb( 0x00, 0x00, 0x00, 0x00); - - Application.Current.Resources["AccentTextFillColorDisabled"] = - Color.FromArgb( 0x5D, 0x00, 0x00, 0x00); - } - else - { -#if DEBUG - System.Diagnostics.Debug.WriteLine("INFO | Text on accent is LIGHT", "System.Windows.Accent"); -#endif - Application.Current.Resources["TextOnAccentFillColorPrimary"] = - Color.FromArgb( 0xFF, 0xFF, 0xFF, 0xFF); - - Application.Current.Resources["TextOnAccentFillColorSecondary"] = - Color.FromArgb( 0x80, 0xFF, 0xFF, 0xFF); - - Application.Current.Resources["TextOnAccentFillColorDisabled"] = - Color.FromArgb( 0x87, 0xFF, 0xFF, 0xFF); - - Application.Current.Resources["TextOnAccentFillColorSelectedText"] = - Color.FromArgb( 0xFF, 0xFF, 0xFF, 0xFF); - - Application.Current.Resources["AccentTextFillColorDisabled"] = - Color.FromArgb( 0x5D, 0xFF, 0xFF, 0xFF); - } - - Application.Current.Resources["SystemAccentColor"] = systemAccent; - Application.Current.Resources["SystemAccentColorPrimary"] = primaryAccent; - Application.Current.Resources["SystemAccentColorSecondary"] = secondaryAccent; - Application.Current.Resources["SystemAccentColorTertiary"] = tertiaryAccent; - - Application.Current.Resources["SystemAccentBrush"] = ToBrush(systemAccent); - Application.Current.Resources["SystemFillColorAttentionBrush"] = ToBrush(secondaryAccent); - Application.Current.Resources["AccentTextFillColorPrimaryBrush"] = ToBrush(tertiaryAccent); - Application.Current.Resources["AccentTextFillColorSecondaryBrush"] = ToBrush(tertiaryAccent); - Application.Current.Resources["AccentTextFillColorTertiaryBrush"] = ToBrush(secondaryAccent); - Application.Current.Resources["AccentFillColorSelectedTextBackgroundBrush"] = ToBrush(systemAccent); - Application.Current.Resources["AccentFillColorDefaultBrush"] = ToBrush(secondaryAccent); - - Application.Current.Resources["AccentFillColorSecondaryBrush"] = ToBrush(secondaryAccent, 0.9); - Application.Current.Resources["AccentFillColorTertiaryBrush"] = ToBrush(secondaryAccent, 0.8); - } - - /// - /// Creates a from a with defined brush opacity. - /// - /// Input color. - /// Degree of opacity. - /// Brush converted to color with modified opacity. - private static SolidColorBrush ToBrush(Color color, double opacity = 1.0) - { - return new SolidColorBrush { Color = color, Opacity = opacity }; - } -} \ No newline at end of file diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeManager.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeManager.cs index 22821304c41..82cb6873eeb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeManager.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeManager.cs @@ -43,7 +43,6 @@ internal static void InitializeFluentTheme() var themeColorResourceUri = GetFluentWindowThemeColorResourceUri(_currentApplicationTheme, _currentUseLightMode); Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = themeColorResourceUri }); - DwmColorization.UpdateAccentColors(); _isFluentThemeInitialized = true; } } @@ -78,7 +77,7 @@ internal static void ApplySystemTheme(IEnumerable windows = null, bool forceUpda string systemTheme = GetSystemTheme(); bool useLightMode = IsSystemThemeLight(); - Color systemAccentColor = DwmColorization.GetSystemAccentColor(); + Color systemAccentColor = AccentColorHelper.SystemAccentColor; ApplyTheme(windows , systemTheme, useLightMode, systemAccentColor, forceUpdate); } @@ -101,9 +100,8 @@ private static void ApplyTheme( if(forceUpdate || requestedTheme != _currentApplicationTheme || requestedUseLightMode != _currentUseLightMode || - DwmColorization.GetSystemAccentColor() != DwmColorization.CurrentApplicationAccentColor) + requestedAccentColor != _currentSystemAccentColor) { - DwmColorization.UpdateAccentColors(); Uri dictionaryUri = GetFluentWindowThemeColorResourceUri(requestedTheme, requestedUseLightMode); AddOrUpdateThemeResources(dictionaryUri); @@ -121,6 +119,7 @@ private static void ApplyTheme( _currentApplicationTheme = requestedTheme; _currentUseLightMode = requestedUseLightMode; + _currentSystemAccentColor = requestedAccentColor; } } @@ -192,18 +191,14 @@ private static void AddOrUpdateThemeResources(Uri dictionaryUri) var newDictionary = new ResourceDictionary() { Source = dictionaryUri }; - ResourceDictionary currentDictionary = Application.Current?.Resources; - foreach (var key in newDictionary.Keys) + FindFluentThemeAndColorDictionary(out ResourceDictionary fluentDictionary, out ResourceDictionary colorDictionary); + + if (colorDictionary != null) { - if (currentDictionary.Contains(key)) - { - currentDictionary[key] = newDictionary[key]; - } - else - { - currentDictionary.Add(key, newDictionary[key]); - } + Application.Current.Resources.MergedDictionaries.Remove(colorDictionary); } + + Application.Current.Resources.MergedDictionaries.Add(newDictionary); } #endregion @@ -232,6 +227,31 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool return new Uri("pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/" + themeColorFileName, UriKind.Absolute); } + private static void FindFluentThemeAndColorDictionary(out ResourceDictionary fluentThemeDictionary, out ResourceDictionary fluentColorDictionary) + { + fluentThemeDictionary = null; + fluentColorDictionary = null; + + if (Application.Current == null) return; + + foreach (ResourceDictionary mergedDictionary in Application.Current.Resources.MergedDictionaries) + { + if (mergedDictionary.Source != null) + { + if (mergedDictionary.Source.ToString() == fluentResourceDictionaryUri) + { + fluentThemeDictionary = mergedDictionary; + } + else if (mergedDictionary.Source.ToString().StartsWith(fluentColorResourceUriPart)) + { + fluentColorDictionary = mergedDictionary; + } + } + } + } + + + #endregion #region Private Members @@ -240,6 +260,9 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool private static readonly string _regPersonalizeKeyPath = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; + private static readonly string fluentResourceDictionaryUri = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Fluent.xaml"; + private static readonly string fluentColorResourceUriPart = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/"; + private static string _currentApplicationTheme; private static bool _currentUseLightMode = true; @@ -248,5 +271,7 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool private static bool _isFluentThemeInitialized = false; + private static Color _currentSystemAccentColor = AccentColorHelper.SystemAccentColor; + #endregion } \ No newline at end of file diff --git a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Accent.xaml b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Accent.xaml deleted file mode 100644 index 1cd063de398..00000000000 --- a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Accent.xaml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - #3379d9 - - - - - #559ce4 - - #80b9ee - - #add8ff - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Fluent.xaml b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Fluent.xaml index 4903465b787..997374bd26b 100644 --- a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Fluent.xaml +++ b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Fluent.xaml @@ -6,7 +6,6 @@ --> - diff --git a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Dark.xaml b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Dark.xaml index 0a7c1919056..870c7365dbd 100644 --- a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Dark.xaml +++ b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Dark.xaml @@ -128,6 +128,34 @@ #2E2E2E #2E2E2E + + + + + + + + + + + + + + + + + + + + + + + + + + + + #FF00FF #FF00FF #FF00FF @@ -145,14 +173,6 @@ - - - - - - - - @@ -181,8 +201,6 @@ - - @@ -290,10 +308,6 @@ - - - - @@ -301,9 +315,9 @@ - - - + + + @@ -331,9 +345,9 @@ - - - + + + @@ -359,9 +373,9 @@ - - - + + + @@ -381,11 +395,11 @@ - + - + @@ -404,7 +418,7 @@ - + @@ -434,9 +448,9 @@ - - - + + + @@ -454,7 +468,7 @@ - + @@ -462,15 +476,15 @@ - - - + + + - + @@ -494,7 +508,7 @@ - + @@ -510,26 +524,26 @@ - + - + - - + + - - - + + + @@ -539,7 +553,7 @@ - + @@ -560,8 +574,8 @@ - - + + @@ -588,13 +602,13 @@ - + - + @@ -614,9 +628,9 @@ - - - + + + @@ -636,11 +650,11 @@ - - + + - - + + @@ -670,5 +684,5 @@ - + diff --git a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/HC.xaml b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/HC.xaml index 40c7d110c8d..521942f2314 100644 --- a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/HC.xaml +++ b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/HC.xaml @@ -16,11 +16,6 @@ https://github.com/microsoft/microsoft-ui-xaml/blob/main/dev/CommonStyles/Common_themeresources_any.xaml --> - - #4cc2ff - #4cc2ff - #4cc2ff - @@ -39,21 +34,42 @@ - + - - - - - + + + + + + + - + + + + - + + + + + + + + + + + + + + + + + @@ -83,8 +99,6 @@ - - @@ -161,10 +175,6 @@ - - - - @@ -546,9 +556,6 @@ - - - #FFFFFF diff --git a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Light.xaml b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Light.xaml index 8f72db4da69..d5b5f3d867a 100644 --- a/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Light.xaml +++ b/src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Resources/Theme/Light.xaml @@ -128,6 +128,34 @@ #F7F7F7 #F3F3F3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + #FF00FF #FF00FF #FF00FF @@ -145,14 +173,6 @@ - - - - - - - - @@ -181,8 +201,6 @@ - - @@ -285,19 +303,15 @@ - - - - - - - + + + @@ -325,9 +339,9 @@ - - - + + + @@ -353,9 +367,9 @@ - - - + + + @@ -375,11 +389,11 @@ - + - + @@ -398,7 +412,7 @@ - + @@ -428,9 +442,9 @@ - - - + + + @@ -448,7 +462,7 @@ - + @@ -457,15 +471,15 @@ #FFF2F2F2 - - - + + + - + @@ -489,7 +503,7 @@ - + @@ -505,26 +519,26 @@ - + - + - - + + - - - + + + @@ -534,7 +548,7 @@ - + @@ -555,8 +569,8 @@ - - + + @@ -583,13 +597,13 @@ - + - + @@ -609,9 +623,9 @@ - - - + + + @@ -631,11 +645,11 @@ - - + + - - + + @@ -665,5 +679,5 @@ - +