Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing value of Shell.NavBarIsVisible should change visibility of toolbar (ShellToolbar) #17358

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ internal static void PropagatePropertyChanged(string propertyName, Element eleme
if (propertyName == null || propertyName == VisualElement.WindowProperty.PropertyName)
SetWindowFromParent(element);

if (propertyName == null || propertyName == Shell.NavBarIsVisibleProperty.PropertyName)
BaseShellItem.PropagateFromParent(Shell.NavBarIsVisibleProperty, element);

if (propertyName == null || propertyName == Shell.NavBarHasShadowProperty.PropertyName)
BaseShellItem.PropagateFromParent(Shell.NavBarHasShadowProperty, element);

Expand Down
13 changes: 12 additions & 1 deletion src/Controls/src/Core/Shell/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ static void OnBackButonBehaviorPropertyChanged(BindableObject bindable, object o

/// <summary>Bindable property for attached property <c>NavBarIsVisible</c>.</summary>
public static readonly BindableProperty NavBarIsVisibleProperty =
BindableProperty.CreateAttached("NavBarIsVisible", typeof(bool), typeof(Shell), true);
BindableProperty.CreateAttached("NavBarIsVisible", typeof(bool), typeof(Shell), true, propertyChanged: OnNavBarIsVisibleChanged);

private static void OnNavBarIsVisibleChanged(BindableObject bindable, object oldValue, object newValue)
{
// Nav bar visibility change is only interesting from the Shell down to the current Page.
// Make sure the ShellToolbar knows about any possible change.
Shell shell = bindable as Shell
?? (bindable as BaseShellItem)?.FindParentOfType<Shell>()
?? (bindable as Page)?.FindParentOfType<Shell>();

shell?.OnPropertyChanged(NavBarIsVisibleProperty.PropertyName);
}

/// <summary>Bindable property for attached property <c>NavBarHasShadow</c>.</summary>
public static readonly BindableProperty NavBarHasShadowProperty =
Expand Down
19 changes: 10 additions & 9 deletions src/Controls/src/Core/ShellToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,29 @@ internal void ApplyChanges()

UpdateTitle();

if (_currentPage != null &&
_currentPage.IsSet(Shell.NavBarIsVisibleProperty))
{
IsVisible = Shell.GetNavBarIsVisible(_currentPage);
}
else
Func<bool> getDefaultNavBarIsVisible = () =>
{
// Shell.GetEffectiveValue doesn't check the Shell itself, so check it here
if (_shell.IsSet(Shell.NavBarIsVisibleProperty))
return (bool)_shell.GetValue(Shell.NavBarIsVisibleProperty);

var flyoutBehavior = (_shell as IFlyoutView).FlyoutBehavior;
#if WINDOWS
IsVisible = (!String.IsNullOrEmpty(Title) ||
return (!String.IsNullOrEmpty(Title) ||
TitleView != null ||
_toolbarTracker.ToolbarItems.Count > 0 ||
_menuBarTracker.ToolbarItems.Count > 0 ||
flyoutBehavior == FlyoutBehavior.Flyout);
#else
IsVisible = (BackButtonVisible ||
return (BackButtonVisible ||
!String.IsNullOrEmpty(Title) ||
TitleView != null ||
_toolbarTracker.ToolbarItems.Count > 0 ||
flyoutBehavior == FlyoutBehavior.Flyout);
#endif
}
};

IsVisible = _shell.GetEffectiveValue(Shell.NavBarIsVisibleProperty, getDefaultNavBarIsVisible, observer: null);

if (currentPage != null)
DynamicOverflowEnabled = PlatformConfiguration.WindowsSpecific.Page.GetToolbarDynamicOverflowEnabled(currentPage);
Expand Down
20 changes: 18 additions & 2 deletions src/Controls/tests/Core.UnitTests/ShellToolbarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,27 @@ public async Task ShellToolbarUpdatesFromPropertyChanged()
public void NavBarIsVisibleUpdates()
{
var page = new ContentPage() { Title = "Test" };
TestShell testShell = new TestShell(page);
var testShell = new TestShell(page);
var toolBar = testShell.Toolbar;
Assert.True(toolBar.IsVisible);
Assert.True(toolBar.IsVisible); // visible by default

// Change the Shell
Shell.SetNavBarIsVisible(testShell, false);
Assert.False(toolBar.IsVisible);
testShell.ClearValue(Shell.NavBarIsVisibleProperty);
Assert.True(toolBar.IsVisible); // back to default

// Change the Page's parent
Shell.SetNavBarIsVisible(page.Parent, false);
Assert.False(toolBar.IsVisible);
page.Parent.ClearValue(Shell.NavBarIsVisibleProperty);
Assert.True(toolBar.IsVisible); // back to default

// Change the Page
Shell.SetNavBarIsVisible(page, false);
Assert.False(toolBar.IsVisible);
page.ClearValue(Shell.NavBarIsVisibleProperty);
Assert.True(toolBar.IsVisible); // back to default
}

[Fact]
Expand Down
Loading