From 7eafe5a5e5c4817167b2e2d5cecedac71529706f Mon Sep 17 00:00:00 2001 From: punker76 Date: Tue, 29 Oct 2019 16:44:11 +0100 Subject: [PATCH] (GH-3623) Fix using wrong theme based styles at WindowCommands and WindowButtonCommands - Add new brush `MahApps.Brushes.WindowButtonCommands.HoverBackground` - Add new style `MahApps.Styles.WindowCommands.Base` - Derive style `MahApps.Styles.WindowCommands.Clean` from `MahApps.Styles.WindowCommands.Base` - Add new style `MahApps.Styles.WindowButtonCommands.Base` - Derive style `MahApps.Styles.WindowButtonCommands.Clean` from `MahApps.Styles.WindowButtonCommands.Base` - `MahApps.Styles.MetroWindow.Clean` style doesn't set the `OverrideDefaultWindowCommandsBrush` to `MahApps.Brushes.Black` (it's not necessary anymore) - Add new styles `MahApps.Styles.Button.CleanWindow.Light` and `MahApps.Styles.Button.CleanWindow.Dark` - Fix disabled brush at `WindowCommands` and `WindowButtonCommands` - Set the pressed background of the Min/Max/Restore/Close buttons to `MahApps.Brushes.AccentBase` and the foreground to `MahApps.Brushes.IdealForeground` - Do not set the theme for WindowCommands and WindowButtonCommands by the Foreground. Instead look for the current theme of the application, window or flyout. --- .../Controls/MetroWindowHelpers.cs | 97 +++++++++---------- .../Styles/Clean/CleanWindow.xaml | 1 - .../Clean/CleanWindowButtonCommands.xaml | 9 +- .../Styles/Clean/CleanWindowButtons.xaml | 42 +++++++- .../Styles/Clean/CleanWindowCommands.xaml | 8 +- .../Styles/Controls.Buttons.xaml | 16 +-- .../Styles/Themes/Theme.Template.xaml | 4 +- src/MahApps.Metro/Styles/VS/Colors.xaml | 2 + .../Themes/WindowButtonCommands.xaml | 7 +- src/MahApps.Metro/Themes/WindowCommands.xaml | 15 ++- 10 files changed, 134 insertions(+), 67 deletions(-) diff --git a/src/MahApps.Metro/Controls/MetroWindowHelpers.cs b/src/MahApps.Metro/Controls/MetroWindowHelpers.cs index 3b8274e09c..25e2258cf9 100644 --- a/src/MahApps.Metro/Controls/MetroWindowHelpers.cs +++ b/src/MahApps.Metro/Controls/MetroWindowHelpers.cs @@ -105,84 +105,83 @@ public static void HandleWindowCommandsForFlyouts(this MetroWindow window, IEnum public static void ResetAllWindowCommandsBrush(this MetroWindow window) { - window.ChangeAllWindowCommandsBrush(window.OverrideDefaultWindowCommandsBrush); - window.ChangeAllWindowButtonCommandsBrush(window.OverrideDefaultWindowCommandsBrush); + var currentAppTheme = ThemeManager.DetectTheme(window) + ?? (Application.Current.MainWindow is MetroWindow metroWindow ? ThemeManager.DetectTheme(metroWindow) : null) + ?? ThemeManager.DetectTheme(Application.Current); + + window.ChangeAllWindowCommandsBrush(window.OverrideDefaultWindowCommandsBrush, currentAppTheme); + window.ChangeAllWindowButtonCommandsBrush(window.OverrideDefaultWindowCommandsBrush, currentAppTheme); } public static void UpdateWindowCommandsForFlyout(this MetroWindow window, Flyout flyout) { - window.ChangeAllWindowCommandsBrush(window.OverrideDefaultWindowCommandsBrush); - window.ChangeAllWindowButtonCommandsBrush(window.OverrideDefaultWindowCommandsBrush ?? flyout.Foreground, flyout.Position); + var currentAppTheme = ThemeManager.DetectTheme(window) + ?? (Application.Current.MainWindow is MetroWindow metroWindow ? ThemeManager.DetectTheme(metroWindow) : null) + ?? ThemeManager.DetectTheme(Application.Current); + + window.ChangeAllWindowCommandsBrush(window.OverrideDefaultWindowCommandsBrush, currentAppTheme); + window.ChangeAllWindowButtonCommandsBrush(window.OverrideDefaultWindowCommandsBrush ?? flyout.Foreground, currentAppTheme, flyout.Theme, flyout.Position); } - private static bool NeedLightTheme(this Brush brush) + private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush foregroundBrush, Metro.Theme currentAppTheme) { - if (brush == null) + if (foregroundBrush == null) { - return true; + window.LeftWindowCommands?.ClearValue(Control.ForegroundProperty); + window.RightWindowCommands?.ClearValue(Control.ForegroundProperty); } - // calculate brush color lightness - var color = ((SolidColorBrush)brush).Color; - - var r = color.R / 255.0f; - var g = color.G / 255.0f; - var b = color.B / 255.0f; - - var max = r; - var min = r; - - if (g > max) max = g; - if (b > max) max = b; - - if (g < min) min = g; - if (b < min) min = b; - - var lightness = (max + min) / 2; - - return lightness > 0.1; - } - - private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush) - { - // set the theme based on color lightness - var theme = brush.NeedLightTheme() ? Theme.Light : Theme.Dark; + // set the theme based on current application or window theme + var theme = currentAppTheme != null && currentAppTheme.Type == ThemeType.Dark + ? Theme.Dark + : Theme.Light; // set the theme to light by default window.LeftWindowCommands?.SetValue(WindowCommands.ThemeProperty, theme); window.RightWindowCommands?.SetValue(WindowCommands.ThemeProperty, theme); // clear or set the foreground property - if (brush != null) + if (foregroundBrush != null) { - window.LeftWindowCommands?.SetValue(Control.ForegroundProperty, brush); - window.RightWindowCommands?.SetValue(Control.ForegroundProperty, brush); - } - else - { - window.LeftWindowCommands?.ClearValue(Control.ForegroundProperty); - window.RightWindowCommands?.ClearValue(Control.ForegroundProperty); + window.LeftWindowCommands?.SetValue(Control.ForegroundProperty, foregroundBrush); + window.RightWindowCommands?.SetValue(Control.ForegroundProperty, foregroundBrush); } } - private static void ChangeAllWindowButtonCommandsBrush(this MetroWindow window, Brush brush, Position position = Position.Top) + private static void ChangeAllWindowButtonCommandsBrush(this MetroWindow window, Brush foregroundBrush, Metro.Theme currentAppTheme, FlyoutTheme flyoutTheme = FlyoutTheme.Adapt, Position position = Position.Top) { - // set the theme to light by default if (position == Position.Right || position == Position.Top) { + if (foregroundBrush == null) + { + window.WindowButtonCommands?.ClearValue(Control.ForegroundProperty); + } + // set the theme based on color lightness - var theme = brush.NeedLightTheme() ? Theme.Light : Theme.Dark; + // otherwise set the theme based on current application or window theme + var theme = currentAppTheme != null && currentAppTheme.Type == ThemeType.Dark + ? Theme.Dark + : Theme.Light; + + if (flyoutTheme == FlyoutTheme.Light) + { + theme = Theme.Light; + } + else if (flyoutTheme == FlyoutTheme.Dark) + { + theme = Theme.Dark; + } + else if (flyoutTheme == FlyoutTheme.Inverse) + { + theme = theme == Theme.Light ? Theme.Dark : Theme.Light; + } window.WindowButtonCommands?.SetValue(WindowButtonCommands.ThemeProperty, theme); // clear or set the foreground property - if (brush != null) - { - window.WindowButtonCommands?.SetValue(Control.ForegroundProperty, brush); - } - else + if (foregroundBrush != null) { - window.WindowButtonCommands?.ClearValue(Control.ForegroundProperty); + window.WindowButtonCommands?.SetValue(Control.ForegroundProperty, foregroundBrush); } } } diff --git a/src/MahApps.Metro/Styles/Clean/CleanWindow.xaml b/src/MahApps.Metro/Styles/Clean/CleanWindow.xaml index cf2822390d..cbedc099a3 100644 --- a/src/MahApps.Metro/Styles/Clean/CleanWindow.xaml +++ b/src/MahApps.Metro/Styles/Clean/CleanWindow.xaml @@ -4,7 +4,6 @@ \ No newline at end of file diff --git a/src/MahApps.Metro/Styles/Clean/CleanWindowButtons.xaml b/src/MahApps.Metro/Styles/Clean/CleanWindowButtons.xaml index d5532f9ba7..337a42b8e1 100644 --- a/src/MahApps.Metro/Styles/Clean/CleanWindowButtons.xaml +++ b/src/MahApps.Metro/Styles/Clean/CleanWindowButtons.xaml @@ -5,14 +5,50 @@ + + + + + + + + @@ -287,7 +293,7 @@ - + +