Skip to content

Commit

Permalink
(GH-3623) Fix using wrong theme based styles at WindowCommands and Wi…
Browse files Browse the repository at this point in the history
…ndowButtonCommands

- 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.
  • Loading branch information
punker76 committed Oct 29, 2019
1 parent d72548e commit 7eafe5a
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 67 deletions.
97 changes: 48 additions & 49 deletions src/MahApps.Metro/Controls/MetroWindowHelpers.cs
Expand Up @@ -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);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/MahApps.Metro/Styles/Clean/CleanWindow.xaml
Expand Up @@ -4,7 +4,6 @@

<Style x:Key="MahApps.Styles.MetroWindow.Clean" TargetType="{x:Type Controls:MetroWindow}">
<Setter Property="NonActiveWindowTitleBrush" Value="{DynamicResource MahApps.Brushes.TransparentWhite}" />
<Setter Property="OverrideDefaultWindowCommandsBrush" Value="{DynamicResource MahApps.Brushes.Black}" />
<Setter Property="TitleAlignment" Value="Center" />
<Setter Property="TitleForeground" Value="{DynamicResource MahApps.Brushes.Black}" />
<Setter Property="WindowTitleBrush" Value="{DynamicResource MahApps.Brushes.TransparentWhite}" />
Expand Down
Expand Up @@ -3,12 +3,19 @@
xmlns:Controls="clr-namespace:MahApps.Metro.Controls">

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/WindowButtonCommands.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Clean/CleanWindowButtons.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="MahApps.Styles.WindowButtonCommands.Clean" TargetType="{x:Type Controls:WindowButtonCommands}">
<Style x:Key="MahApps.Styles.WindowButtonCommands.Clean"
BasedOn="{StaticResource MahApps.Styles.WindowButtonCommands.Base}"
TargetType="{x:Type Controls:WindowButtonCommands}">
<Setter Property="DarkCloseButtonStyle" Value="{StaticResource MahApps.Styles.Button.CleanWindowClose.Dark}" />
<Setter Property="DarkMaxButtonStyle" Value="{DynamicResource MahApps.Styles.Button.CleanWindow.Dark}" />
<Setter Property="DarkMinButtonStyle" Value="{DynamicResource MahApps.Styles.Button.CleanWindow.Dark}" />
<Setter Property="LightCloseButtonStyle" Value="{StaticResource MahApps.Styles.Button.CleanWindowClose.Light}" />
<Setter Property="LightMaxButtonStyle" Value="{DynamicResource MahApps.Styles.Button.CleanWindow.Light}" />
<Setter Property="LightMinButtonStyle" Value="{DynamicResource MahApps.Styles.Button.CleanWindow.Light}" />
</Style>

</ResourceDictionary>
42 changes: 39 additions & 3 deletions src/MahApps.Metro/Styles/Clean/CleanWindowButtons.xaml
Expand Up @@ -5,14 +5,50 @@
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Buttons.xaml" />
</ResourceDictionary.MergedDictionaries>

<!-- light button style for min, max and close window buttons -->
<Style x:Key="MahApps.Styles.Button.CleanWindow.Light"
BasedOn="{StaticResource MahApps.Styles.Button.MetroWindow.Light}"
TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.SemiTransparentGray}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Accent}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForeground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForegroundDisabled}" />
</Trigger>
</Style.Triggers>
</Style>

<!-- dark button style for min, max and close window buttons -->
<Style x:Key="MahApps.Styles.Button.CleanWindow.Dark"
BasedOn="{StaticResource MahApps.Styles.Button.MetroWindow.Light}"
TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.SemiTransparentGray}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Accent}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForeground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.DarkIdealForegroundDisabled}" />
</Trigger>
</Style.Triggers>
</Style>

<!-- light button style for close window button (clean style) -->
<Style x:Key="MahApps.Styles.Button.CleanWindowClose.Light"
BasedOn="{StaticResource MahApps.Styles.Button.MetroWindow.Light}"
BasedOn="{StaticResource MahApps.Styles.Button.CleanWindow.Light}"
TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.CleanWindowCloseButton.Background}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.White}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Black}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.CleanWindowCloseButton.PressedBackground}" />
Expand All @@ -22,7 +58,7 @@

<!-- dark button style for close window button (clean style) -->
<Style x:Key="MahApps.Styles.Button.CleanWindowClose.Dark"
BasedOn="{StaticResource MahApps.Styles.Button.MetroWindow.Dark}"
BasedOn="{StaticResource MahApps.Styles.Button.CleanWindow.Dark}"
TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
Expand Down
8 changes: 7 additions & 1 deletion src/MahApps.Metro/Styles/Clean/CleanWindowCommands.xaml
Expand Up @@ -2,7 +2,13 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls">

<Style x:Key="MahApps.Styles.WindowCommands.Clean" TargetType="{x:Type Controls:WindowCommands}">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/WindowCommands.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="MahApps.Styles.WindowCommands.Clean"
BasedOn="{StaticResource MahApps.Styles.WindowCommands.Base}"
TargetType="{x:Type Controls:WindowCommands}">
<Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper" />
<Setter Property="SeparatorHeight" Value="11" />
<Setter Property="ShowLastSeparator" Value="False" />
Expand Down
16 changes: 8 additions & 8 deletions src/MahApps.Metro/Styles/Controls.Buttons.xaml
Expand Up @@ -305,8 +305,8 @@
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.SemiTransparentWhite}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Accent}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.AccentBase}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForeground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForegroundDisabled}" />
Expand All @@ -326,11 +326,11 @@
<Setter Property="Width" Value="34" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.SemiTransparentWhite}" />
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.WindowButtonCommands.HoverBackground}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Accent}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.AccentBase}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForeground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForegroundDisabled}" />
Expand All @@ -344,11 +344,11 @@
TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.SemiTransparentGray}" />
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.WindowButtonCommands.HoverBackground}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.Accent}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.AccentBase}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForeground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.DarkIdealForegroundDisabled}" />
Expand Down
4 changes: 3 additions & 1 deletion src/MahApps.Metro/Styles/Themes/Theme.Template.xaml
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
Expand Down Expand Up @@ -200,6 +200,8 @@
<SolidColorBrush x:Key="MahApps.Brushes.IdealForegroundDisabled" Opacity="0.4" Color="{StaticResource MahApps.Colors.IdealForeground}" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Brushes.AccentSelectedColor" Color="{StaticResource MahApps.Colors.IdealForeground}" options:Freeze="True" />

<SolidColorBrush x:Key="MahApps.Brushes.WindowButtonCommands.HoverBackground" Color="#66DCDCDC" options:Freeze="True" />

<!-- DataGrid brushes -->

<SolidColorBrush x:Key="MahApps.Brushes.DataGrid.Highlight" Color="{StaticResource MahApps.Colors.Accent}" options:Freeze="True" />
Expand Down
2 changes: 2 additions & 0 deletions src/MahApps.Metro/Styles/VS/Colors.xaml
Expand Up @@ -50,6 +50,8 @@
<GradientStop Offset="1" Color="{StaticResource MahApps.Colors.White}" />
</LinearGradientBrush>

<SolidColorBrush x:Key="MahApps.Brushes.WindowButtonCommands.HoverBackground" Color="#40808080" options:Freeze="True" />

<!-- Foreground -->
<SolidColorBrush x:Key="MahApps.Brushes.Foreground" Color="#FFFFFF" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Brushes.WindowTitle.Foreground" Color="#999988" options:Freeze="True" />
Expand Down
7 changes: 6 additions & 1 deletion src/MahApps.Metro/Themes/WindowButtonCommands.xaml
Expand Up @@ -231,7 +231,7 @@
</ControlTemplate.Triggers>
</ControlTemplate>

<Style x:Key="MahApps.Styles.WindowButtonCommands" TargetType="{x:Type Controls:WindowButtonCommands}">
<Style x:Key="MahApps.Styles.WindowButtonCommands.Base" TargetType="{x:Type Controls:WindowButtonCommands}">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.TransparentWhite}" />
<Setter Property="DarkCloseButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Dark}" />
<Setter Property="DarkMaxButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Dark}" />
Expand All @@ -244,6 +244,11 @@
<Setter Property="LightMinButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Light}" />
<Setter Property="Template" Value="{StaticResource MahApps.Templates.WindowButtonCommands}" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>

<Style x:Key="MahApps.Styles.WindowButtonCommands"
BasedOn="{StaticResource MahApps.Styles.WindowButtonCommands.Base}"
TargetType="{x:Type Controls:WindowButtonCommands}">
<Style.Triggers>
<DataTrigger Binding="{Binding ParentWindow.ShowTitleBar, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForeground}" />
Expand Down

0 comments on commit 7eafe5a

Please sign in to comment.