Skip to content

Commit

Permalink
[Windows] Fix Shell FlyoutBackground property (#13132)
Browse files Browse the repository at this point in the history
* Fix issue using Shell FlyoutBackground on Windows

* Changes based on PR feedback

* Changes based on PR feedback

* Updated test

* Update src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs

Co-authored-by: Manuel de la Pena <mandel@microsoft.com>

* Add missing ;

---------

Co-authored-by: Manuel de la Pena <mandel@microsoft.com>
  • Loading branch information
jsuarezruiz and mandel-macaque authored Feb 17, 2023
1 parent f6d8e15 commit 46cfab5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml;
using Xunit;
Expand All @@ -24,6 +25,53 @@ protected Task CheckFlyoutState(ShellHandler handler, bool desiredState)
return Task.CompletedTask;
}

[Theory(DisplayName = "Shell FlyoutBackground Initializes Correctly")]
[InlineData("#FF0000")]
[InlineData("#00FF00")]
[InlineData("#0000FF")]
[InlineData("#000000")]
public async Task ShellFlyoutBackgroundInitializesCorrectly(string colorHex)
{
SetupBuilder();

var expectedColor = Color.FromArgb(colorHex);

var shell = await CreateShellAsync((shell) =>
{
shell.FlyoutBehavior = FlyoutBehavior.Locked;
shell.FlyoutBackground = new SolidColorBrush(expectedColor);

var shellItem = new FlyoutItem();
shellItem.Items.Add(new ContentPage());
shell.Items.Add(shellItem);
});

await InvokeOnMainThreadAsync(async () =>
{
await CreateHandlerAndAddToWindow<ShellHandler>(shell, (handler) =>
{
var rootNavView = handler.PlatformView;
var shellItemView = shell.CurrentItem.Handler.PlatformView as MauiNavigationView;
var expectedRoot = UI.Xaml.Controls.NavigationViewPaneDisplayMode.Left;
var expectedShellItems = UI.Xaml.Controls.NavigationViewPaneDisplayMode.LeftMinimal;

Assert.Equal(expectedRoot, rootNavView.PaneDisplayMode);
Assert.NotNull(shellItemView);
Assert.Equal(expectedShellItems, shellItemView.PaneDisplayMode);

return Task.CompletedTask;
});

await AssertionExtensions.Wait(() =>
{
var platformView = shell.Handler.PlatformView as FrameworkElement;
return platformView is not null && (platformView.Height > 0 || platformView.Width > 0);
});
});

await ValidateHasColor(shell, expectedColor, typeof(ShellHandler));
}

[Fact(DisplayName = "Back Button Enabled/Disabled")]
public async Task BackButtonEnabledAndDisabled()
{
Expand Down Expand Up @@ -661,4 +709,4 @@ async Task TapToSelect(ContentPage page)
await OnNavigatedToAsync(page);
}
}
}
}
34 changes: 18 additions & 16 deletions src/Core/src/Platform/Windows/NavigationViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static void UpdateTopNavigationViewItemTextColor(this MauiNavigationView
navigationView.TopNavArea.Resources["TopNavigationViewItemForegroundPressed"] = brush;
navigationView.TopNavArea.Resources["TopNavigationViewItemForegroundDisabled"] = brush;
}

navigationView.TopNavArea.RefreshThemeResources();
}

if (navigationView.MenuItemsSource is IList<NavigationViewItemViewModel> items)
Expand Down Expand Up @@ -70,6 +72,8 @@ public static void UpdateTopNavigationViewItemBackgroundUnselectedColor(this Mau
navigationView.TopNavArea.Resources["TopNavigationViewItemBackgroundPointerOver"] = brush;
navigationView.TopNavArea.Resources["TopNavigationViewItemBackgroundPressed"] = brush;
}

navigationView.TopNavArea.RefreshThemeResources();
}

if (navigationView.MenuItemsSource is IList<NavigationViewItemViewModel> items)
Expand Down Expand Up @@ -98,6 +102,8 @@ public static void UpdateTopNavigationViewItemBackgroundSelectedColor(this MauiN
navigationView.TopNavArea.Resources["TopNavigationViewItemBackgroundSelectedPointerOver"] = brush;
navigationView.TopNavArea.Resources["TopNavigationViewItemBackgroundSelectedPressed"] = brush;
}

navigationView.TopNavArea.RefreshThemeResources();
}

if (navigationView.MenuItemsSource is IList<NavigationViewItemViewModel> items)
Expand All @@ -111,32 +117,28 @@ public static void UpdateTopNavigationViewItemBackgroundSelectedColor(this MauiN

public static void UpdatePaneBackground(this MauiNavigationView navigationView, Paint? paint)
{
var rootSplitView = navigationView.RootSplitView;
var paneContentGrid = navigationView.PaneContentGrid;

if (paneContentGrid is null)
return;

var brush = paint?.ToPlatform();

if (brush == null)
if (brush is null)
{
object? color = null;
object? color;
if (navigationView.IsPaneOpen)
color = navigationView.Resources["NavigationViewExpandedPaneBackground"];
else
color = navigationView.Resources["NavigationViewDefaultPaneBackground"];

if (rootSplitView != null)
{
if (color is WBrush colorBrush)
rootSplitView.PaneBackground = colorBrush;
else if (color is global::Windows.UI.Color uiColor)
rootSplitView.PaneBackground = new WSolidColorBrush(uiColor);
}
if (color is WBrush colorBrush)
paneContentGrid.Background = colorBrush;
else if (color is global::Windows.UI.Color uiColor)
paneContentGrid.Background = new WSolidColorBrush(uiColor);
}
else
{
if (rootSplitView != null)
{
rootSplitView.PaneBackground = brush;
}
}
paneContentGrid.Background = brush;
}

public static void UpdateFlyoutVerticalScrollMode(this MauiNavigationView navigationView, ScrollMode scrollMode)
Expand Down

0 comments on commit 46cfab5

Please sign in to comment.