Skip to content

Commit

Permalink
Respect BarTextColor in Windows MenuBar
Browse files Browse the repository at this point in the history
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 dotnet#5554
  • Loading branch information
eerhardt committed May 9, 2022
1 parent 0f9c79e commit c6423ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 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,9 @@ 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();
Color barTextColor = toolbar.BarTextColor;
if (barTextColor != null)
platformToolbar.SetBarTextColor(barTextColor.ToPlatform());
}

public static void UpdateToolbarDynamicOverflowEnabled(this MauiToolbar platformToolbar, Toolbar toolbar)
Expand Down
34 changes: 24 additions & 10 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 Down Expand Up @@ -83,10 +83,12 @@ internal object? TitleView
}
}

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

menuContent.Foreground = brush;
UpdateMenuBarForeground();
}

internal CommandBar CommandBar => commandBar;
Expand Down Expand Up @@ -179,20 +181,32 @@ 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;

WBrush menuForegroundBrush = menuContent.Foreground;

// 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;
dictionary["ButtonForeground"] = menuForegroundBrush;
dictionary["ButtonForegroundPointerOver"] = menuForegroundBrush;
dictionary["ButtonForegroundPressed"] = menuForegroundBrush;
dictionary["ButtonForegroundDisabled"] = menuForegroundBrush;
}
}
}

0 comments on commit c6423ad

Please sign in to comment.