Skip to content

Commit

Permalink
Respect BarTextColor in Windows MenuBar (#6964)
Browse files Browse the repository at this point in the history
* Respect BarTextColor in Windows MenuBar

Windows MauiToolbar is not respecting the BarTextColor in the MenuBar. Currently, BarTextColor is only setting the Title's Foreground. If someone sets the BarBackgroundColor, and the BarTextColor is ignored, you can get a menu bar that is not readable.

The fix is to propagate the BarTextColor to the MenuBar. However, the fix isn't as simple due to microsoft/microsoft-ui-xaml#7070. Working around that issue by setting the Button's Foreground colors in the MenuBar's ResourceDictionary.

Fix #5554

* Respond to PR feedback

- Allow for clearing the MenuBar Foreground property.
- Ensure that we only set the foreground if someone actually set the BarTextColor.

* Don't set the Title.Foreground to null.
  • Loading branch information
eerhardt authored May 9, 2022
1 parent 1ff4e63 commit adfbdb0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Controls/src/Core/NavigationPageToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void ApplyChanges(NavigationPage navigationPage)
TitleIcon = NavigationPage.GetTitleIconImageSource(currentPage);

BarBackground = navigationPage.BarBackground;
if (Brush.IsNullOrEmpty(navigationPage.BarBackground) &&
if (Brush.IsNullOrEmpty(BarBackground) &&
navigationPage.BarBackgroundColor != null)
{
BarBackground = new SolidColorBrush(navigationPage.BarBackgroundColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public static void UpdateTitle(this MauiToolbar platformToolbar, Toolbar toolbar

public static void UpdateBarTextColor(this MauiToolbar platformToolbar, Toolbar toolbar)
{
if (toolbar.BarTextColor != null)
platformToolbar.TitleColor = toolbar.BarTextColor.ToPlatform();
platformToolbar.SetBarTextColor(toolbar.BarTextColor?.ToPlatform());
}

public static void UpdateToolbarDynamicOverflowEnabled(this MauiToolbar platformToolbar, Toolbar toolbar)
Expand Down
48 changes: 37 additions & 11 deletions src/Core/src/Platform/Windows/MauiToolbar.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using WBrush = Microsoft.UI.Xaml.Media.Brush;
using WImageSource = Microsoft.UI.Xaml.Media.ImageSource;
using WImage = Microsoft.UI.Xaml.Controls.Image;
using WImageSource = Microsoft.UI.Xaml.Media.ImageSource;

namespace Microsoft.Maui.Platform
{
Expand All @@ -17,6 +17,7 @@ public static readonly DependencyProperty IsBackEnabledProperty
new PropertyMetadata(true));

MenuBar? _menuBar;
WBrush? _menuBarForeground;
private Button? _navigationViewBackButton;
private Button? _togglePaneButton;
private Graphics.Color? _iconColor;
Expand Down Expand Up @@ -83,10 +84,15 @@ internal object? TitleView
}
}

internal WBrush? TitleColor
internal void SetBarTextColor(WBrush? brush)
{
get => title.Foreground;
set => title.Foreground = value;
if (brush != null)
{
title.Foreground = brush;
}

_menuBarForeground = brush;
UpdateMenuBarForeground();
}

internal CommandBar CommandBar => commandBar;
Expand Down Expand Up @@ -179,20 +185,40 @@ void UpdateIconColor()
internal void SetMenuBar(MenuBar? menuBar)
{
_menuBar = menuBar;
UpdateMenuBar();
}

void UpdateMenuBar()
{
if (menuContent == null)
return;

menuContent.Content = _menuBar;
UpdateMenuBarForeground();

if (_menuBar == null || _menuBar.Items.Count == 0)
menuContent.Visibility = UI.Xaml.Visibility.Collapsed;
else
menuContent.Visibility = UI.Xaml.Visibility.Visible;
}

void UpdateMenuBarForeground()
{
if (_menuBar is null)
return;

// MenuBarItems currently don't respect the Foreground property due to https://github.com/microsoft/microsoft-ui-xaml/issues/7070
// Work around this by setting the Button's colors in the MenuBar's ResourceDictionary

ResourceDictionary dictionary = _menuBar.Resources;
WBrush? menuForegroundBrush = _menuBarForeground;
if (menuForegroundBrush is null)
{
dictionary.Remove("ButtonForeground");
dictionary.Remove("ButtonForegroundPointerOver");
dictionary.Remove("ButtonForegroundPressed");
dictionary.Remove("ButtonForegroundDisabled");
}
else
{
dictionary["ButtonForeground"] = menuForegroundBrush;
dictionary["ButtonForegroundPointerOver"] = menuForegroundBrush;
dictionary["ButtonForegroundPressed"] = menuForegroundBrush;
dictionary["ButtonForegroundDisabled"] = menuForegroundBrush;
}
}
}
}

0 comments on commit adfbdb0

Please sign in to comment.