Skip to content

Commit

Permalink
Merge pull request #949 from MahApps/fix-flyout-windowcommands
Browse files Browse the repository at this point in the history
Fix WindowCommand brushes when using Flyouts
  • Loading branch information
flagbug committed Jan 17, 2014
2 parents c60df76 + 5d4229e commit db24224
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 57 deletions.
17 changes: 2 additions & 15 deletions MahApps.Metro/Behaviours/InternalCleanWindowGlueBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,8 @@ protected override void OnDetaching()

void AssociatedMetroWindow_FlyoutsStatusChanged(object sender, RoutedEventArgs e)
{
MetroWindow.FlyoutStatusChangedRoutedEventArgs args = e as MetroWindow.FlyoutStatusChangedRoutedEventArgs;
var flyout = args.ChangedFlyout;

if (flyout.Position == Position.Right || flyout.Position == Position.Top)
{
if (flyout.IsOpen)
{
this.AssociatedMetroWindow.HandleFlyout(flyout, (Brush)this.AssociatedMetroWindow.FindResource("WhiteColorBrush"));
}
else
{
this.SetWindowCommandButtonsToBlackBrush();
AssociatedMetroWindow.WindowButtonCommands.SetResourceReference(Control.ForegroundProperty, "BlackColorBrush");
}
}
var flyouts = this.AssociatedMetroWindow.Flyouts.Items.Cast<Flyout>().ToList();
this.AssociatedMetroWindow.HandleWindowCommandsForFlyouts(flyouts, (Brush) this.AssociatedMetroWindow.FindResource("BlackColorBrush"));
}
}
}
5 changes: 1 addition & 4 deletions MahApps.Metro/Controls/Flyout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,10 @@ public Flyout()
this.ChangeFlyoutTheme(accent, windowTheme.Item1);
}
}
ThemeManager.IsThemeChanged += this.ThemeManager_IsThemeChanged;
};
this.Unloaded += (sender, args) => ThemeManager.IsThemeChanged -= this.ThemeManager_IsThemeChanged;
}

private void ChangeFlyoutTheme(Accent windowAccent, Theme windowTheme)
internal void ChangeFlyoutTheme(Accent windowAccent, Theme windowTheme)
{
// Beware: Über-dumb code ahead!
switch (this.Theme)
Expand Down
34 changes: 22 additions & 12 deletions MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,27 @@ private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
{
this.Flyouts = new FlyoutsControl();
}

ThemeManager.IsThemeChanged += ThemeManagerOnIsThemeChanged;
this.Unloaded += (o, args) => ThemeManager.IsThemeChanged -= ThemeManagerOnIsThemeChanged;
}

private void ThemeManagerOnIsThemeChanged(object sender, OnThemeChangedEventArgs e)
{
if (e.Accent != null)
{
var flyouts = this.Flyouts.Items.Cast<Flyout>().ToList();

if (!flyouts.Any())
return;

foreach (Flyout flyout in flyouts)
{
flyout.ChangeFlyoutTheme(e.Accent, e.Theme);
}

this.HandleWindowCommandsForFlyouts(flyouts);
}
}

static MetroWindow()
Expand Down Expand Up @@ -543,18 +564,7 @@ internal void HandleFlyoutStatusChange(Flyout flyout, IEnumerable<Flyout> visibl
WindowCommandsPresenter.SetValue(Panel.ZIndexProperty, this.ShowWindowCommandsOnTop ? zIndex : (zIndex > 0 ? zIndex - 1 : 0));
WindowButtonCommands.SetValue(Panel.ZIndexProperty, zIndex);

// We need to adapt the window commands to the theme of the flyout
// This only needs to be done for the accent and light theme,
// the dark theme is the default one and will work out of the box
if (flyout.IsOpen)
{
this.HandleFlyout(flyout);
}

else
{
this.ResetAllWindowCommandsBrush();
}
this.HandleWindowCommandsForFlyouts(visibleFlyouts);
}

flyoutModal.Visibility = visibleFlyouts.Any(x => x.IsModal) ? Visibility.Visible : Visibility.Hidden;
Expand Down
76 changes: 50 additions & 26 deletions MahApps.Metro/Controls/MetroWindowHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,32 @@ namespace MahApps.Metro.Controls
/// </summary>
internal static class MetroWindowHelpers
{
public static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush)
/// <summary>
/// Adapts the WindowCommands to the theme of the first opened, topmost && && (top || right) flyout
/// </summary>
/// <param name="flyouts">All the flyouts! Or flyouts that fall into the category described in the summary.</param>
/// <param name="resetBrush">An optional brush to reset the window commands brush to.</param>
public static void HandleWindowCommandsForFlyouts(this MetroWindow window, IEnumerable<Flyout> flyouts, Brush resetBrush = null)
{
window.ChangeWindowCommandButtonsBrush(brush);
var flyout = flyouts
.Where(x => x.IsOpen && (x.Position == Position.Right || x.Position == Position.Top))
.OrderByDescending(Panel.GetZIndex)
.FirstOrDefault();

window.WindowButtonCommands.SetValue(Control.ForegroundProperty, brush);
if (flyout != null)
{
window.UpdateWindowCommandsForFlyout(flyout);
}

else if(resetBrush == null)
{
window.ResetAllWindowCommandsBrush();
}

else
{
window.ChangeAllWindowCommandsBrush(resetBrush);
}
}

public static void ResetAllWindowCommandsBrush(this MetroWindow window)
Expand All @@ -27,25 +48,7 @@ public static void ResetAllWindowCommandsBrush(this MetroWindow window)
window.WindowButtonCommands.ClearValue(Control.ForegroundProperty);
}

public static void ChangeWindowCommandButtonsBrush(this MetroWindow window, string resourceName)
{
window.InvokeCommandButtons(x => x.SetResourceReference(Control.ForegroundProperty, resourceName));
}

public static void ChangeWindowCommandButtonsBrush(this MetroWindow window, Brush brush)
{
window.InvokeCommandButtons(x => x.SetValue(Control.ForegroundProperty, brush));
}

private static void InvokeCommandButtons(this MetroWindow window, Action<Button> action)
{
foreach (Button b in ((WindowCommands)window.WindowCommandsPresenter.Content).FindChildren<Button>())
{
action(b);
}
}

public static void HandleFlyout(this MetroWindow window, Flyout flyout, Brush darkThemeBrush = null)
public static void UpdateWindowCommandsForFlyout(this MetroWindow window, Flyout flyout)
{
Brush brush = null;

Expand All @@ -59,15 +62,36 @@ public static void HandleFlyout(this MetroWindow window, Flyout flyout, Brush da
brush = (Brush)ThemeManager.LightResource["BlackBrush"];
}

else if(flyout.ActualTheme == Theme.Dark && darkThemeBrush != null)
else if (flyout.ActualTheme == Theme.Dark)
{
brush = darkThemeBrush;
brush = (Brush)ThemeManager.DarkResource["BlackBrush"];
}

if (brush != null)
window.ChangeAllWindowCommandsBrush(brush);
}
public static void ChangeWindowCommandButtonsBrush(this MetroWindow window, string brush)
{
window.InvokeCommandButtons(x => x.SetResourceReference(Control.ForegroundProperty, brush));
}

private static void ChangeWindowCommandButtonsBrush(this MetroWindow window, Brush brush)
{
window.InvokeCommandButtons(x => x.SetValue(Control.ForegroundProperty, brush));
}

private static void InvokeCommandButtons(this MetroWindow window, Action<Button> action)
{
foreach (Button b in ((WindowCommands)window.WindowCommandsPresenter.Content).FindChildren<Button>())
{
window.ChangeAllWindowCommandsBrush(brush);
action(b);
}
}

private static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush)
{
window.ChangeWindowCommandButtonsBrush(brush);

window.WindowButtonCommands.SetValue(Control.ForegroundProperty, brush);
}
}
}

0 comments on commit db24224

Please sign in to comment.