Skip to content

Commit

Permalink
Fix TabbedPage title displaying incorrectly (#17039)
Browse files Browse the repository at this point in the history
* Fix TabbedPage title displaying incorrectly

Presently, when a navigation page is created and a tabbed page is added to it with children, the navigation page uses the tabbed page's selected child title as its title. This behavior is unexpected when using standard tabs (tabs on the top) in a navigation page on Android.

Without a custom tabbed page, this behavior makes sense, especially for bottom tabs; however, when a custom tabbed page is defined, it seems that the title should not be overridden.

Fixes #8577

* Add device tests

* Add unit tests

* Updated test

* More tests

* Fixed test not being able to fail

* Changed GetTitle to work with more than just TabbedPages

* Removed tests that deal with Child page titles replacing the Toolbar title

---------

Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
Co-authored-by: Dustin Wojciechowski <dustin.wojciechowski@microsoft.com>
Co-authored-by: Dustin Wojciechowski <dustinwo@microsoft.com>
  • Loading branch information
4 people authored Apr 29, 2024
1 parent 8a54162 commit 6328b76
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ void UpdateBackButton()

// Once we have better logic inside core to handle backbutton visiblity this
// code should all go away.
// Windows currently doesn't have logic in core to handle back button visibility
// Windows currently doesn't have logic in core to handle back button visibility
// Android just handles it as part of core which means you get cool animations
// that we don't want to interrupt here.
// that we don't want to interrupt here.
// Once it's all built into core we can remove this code and simplify visibility logic
if (_currentPage.IsSet(NavigationPage.HasBackButtonProperty))
{
Expand Down Expand Up @@ -273,7 +273,16 @@ void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEvent

Color GetBarTextColor() => _currentNavigationPage?.BarTextColor;
Color GetIconColor() => (_currentPage != null) ? NavigationPage.GetIconColor(_currentPage) : null;
string GetTitle() => GetTitleView() != null ? String.Empty : _currentPage?.Title;

string GetTitle()
{
if (GetTitleView() != null)
{
return string.Empty;
}

return _currentNavigationPage?.CurrentPage?.Title;
}

VisualElement GetTitleView()
{
Expand Down
16 changes: 16 additions & 0 deletions src/Controls/tests/Core.UnitTests/ToolbarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public async Task TitleAndTitleViewAreMutuallyExclusive()
Assert.Equal("Test Title", toolbar.Title);
}

[Fact]
public void ToolbarTitle_UsesTabbedPageTitleWhenSet()
{
var window = new TestWindow();
IToolbarElement toolbarElement = window;
var tabbedPage = new TabbedPage
{
Title = "Test Title",
Children = { new ContentPage { Title = "Child Test Title" } },
};
window.Page = new NavigationPage(tabbedPage);

var toolbar = (Toolbar)toolbarElement.Toolbar;
Assert.Equal(tabbedPage.Title, toolbar.Title);
}

[Fact]
public async Task InsertPageBeforeRootPageShowsBackButton()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#if IOS || MACCATALYST
using FlyoutViewHandler = Microsoft.Maui.Controls.Handlers.Compatibility.PhoneFlyoutPageRenderer;
using NavigationViewHandler = Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer;
using TabbedRenderer = Microsoft.Maui.Controls.Handlers.Compatibility.TabbedRenderer;
#endif

namespace Microsoft.Maui.DeviceTests
Expand All @@ -38,7 +39,11 @@ void SetupBuilder()
handlers.AddHandler(typeof(Controls.NavigationPage), typeof(NavigationViewHandler));
handlers.AddHandler<Page, PageHandler>();
handlers.AddHandler<Controls.Window, WindowHandlerStub>();
handlers.AddHandler(typeof(TabbedPage), typeof(TabbedViewHandler));
#if IOS || MACCATALYST
handlers.AddHandler(typeof(TabbedPage), typeof(TabbedRenderer));
#else
handlers.AddHandler(typeof(TabbedPage), typeof(TabbedViewHandler));
#endif
SetupShellHandlers(handlers);
});
Expand Down

0 comments on commit 6328b76

Please sign in to comment.