From 41ba1dc4bf7066b6685988e67154513c1cabb401 Mon Sep 17 00:00:00 2001 From: Adam Anderson Date: Mon, 28 Aug 2023 14:32:41 -0400 Subject: [PATCH 1/8] 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 --- .../NavigationPage/NavigationPageToolbar.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs b/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs index 6e6305fdab80..deca3a796fb1 100644 --- a/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs +++ b/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs @@ -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)) { @@ -273,7 +273,23 @@ 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; + } + + Page target = _currentPage; + + if (_currentNavigationPage?.CurrentPage is TabbedPage tabbedPage && !string.IsNullOrEmpty(tabbedPage.Title)) + { + target = tabbedPage; + } + + return target?.Title; + } VisualElement GetTitleView() { From 547da582fad834d3b136ec59052aa3b5da9de29e Mon Sep 17 00:00:00 2001 From: Adam Anderson Date: Mon, 28 Aug 2023 14:33:34 -0400 Subject: [PATCH 2/8] Add device tests --- .../Elements/Toolbar/ToolbarTests.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs index cc6286a91b80..8f71c6118e0d 100644 --- a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs @@ -117,6 +117,43 @@ await CreateHandlerAndAddToWindow(new Window(navPage), (handl }); } + [Fact(DisplayName = "Toolbar Uses TabbedPage Title When Set")] + public async Task ToolbarTabbedPageTitle() + { + SetupBuilder(); + var navPage = new NavigationPage( + new TabbedPage + { + Title = "Tabbed Page Title", + Children = { new ContentPage { Title = "Child Page Title" } }, + }); + + await CreateHandlerAndAddToWindow(new Window(navPage), (handler) => + { + string title = GetToolbarTitle(handler); + Assert.Equal("Tabbed Page Title", title); + return Task.CompletedTask; + }); + } + + [Fact(DisplayName = "Toolbar Uses TabbedPage Child Title")] + public async Task ToolbarTabbedPageChildTitle() + { + SetupBuilder(); + var navPage = new NavigationPage( + new TabbedPage + { + Children = { new ContentPage { Title = "Child Page Title" } }, + }); + + await CreateHandlerAndAddToWindow(new Window(navPage), (handler) => + { + string title = GetToolbarTitle(handler); + Assert.Equal("Child Page Title", title); + return Task.CompletedTask; + }); + } + [Theory] [InlineData($"{nameof(FlyoutPage)}WithNavigationPage, {nameof(ContentPage)}, {nameof(FlyoutPage)}WithNavigationPage")] [InlineData($"{nameof(FlyoutPage)}WithNavigationPage, {nameof(FlyoutPage)}, {nameof(FlyoutPage)}WithNavigationPage")] From b7effe9ff87278fc3cb3dfbb7511ffd5b5cbc376 Mon Sep 17 00:00:00 2001 From: Adam Anderson Date: Mon, 28 Aug 2023 14:57:29 -0400 Subject: [PATCH 3/8] Add unit tests --- .../tests/Core.UnitTests/ToolbarTests.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs index 6b903c9542b4..cd76380e7374 100644 --- a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs +++ b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs @@ -56,6 +56,39 @@ public async Task TitleAndTitleViewAreMutuallyExclusive() Assert.Equal("Test Title", toolbar.Title); } + [Fact] + public void ToolbarTitle_UsesTabbedPageChildTitle() + { + var window = new Window(); + IToolbarElement toolbarElement = window; + var childPage = new ContentPage { Title = "Child Test Title" }; + var tabbedPage = new TabbedPage + { + Children = { childPage }, + }; + window.Page = new NavigationPage(tabbedPage); + + var toolbar = (Toolbar)toolbarElement.Toolbar; + Assert.Equal(childPage.Title, toolbar.Title); + } + + + [Fact] + public void ToolbarTitle_UsesTabbedPageTitleWhenSet() + { + var window = new Window(); + 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() { From 1fc350b1e778cdfcddd8675ce6c8ede8377bc267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 21 Feb 2024 10:39:16 +0100 Subject: [PATCH 4/8] Updated test --- src/Controls/tests/Core.UnitTests/ToolbarTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs index cd76380e7374..8b7b90a0a628 100644 --- a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs +++ b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs @@ -59,7 +59,7 @@ public async Task TitleAndTitleViewAreMutuallyExclusive() [Fact] public void ToolbarTitle_UsesTabbedPageChildTitle() { - var window = new Window(); + var window = new TestWindow(); IToolbarElement toolbarElement = window; var childPage = new ContentPage { Title = "Child Test Title" }; var tabbedPage = new TabbedPage From 0efcfa40c984ead028a4dd33f478cef99d64579a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 21 Feb 2024 11:06:26 +0100 Subject: [PATCH 5/8] More tests --- .../tests/Core.UnitTests/ToolbarTests.cs | 26 ++++++++++++++++++- .../Elements/Toolbar/ToolbarTests.cs | 19 -------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs index 8b7b90a0a628..df6ccef6558a 100644 --- a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs +++ b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs @@ -76,7 +76,7 @@ public void ToolbarTitle_UsesTabbedPageChildTitle() [Fact] public void ToolbarTitle_UsesTabbedPageTitleWhenSet() { - var window = new Window(); + var window = new TestWindow(); IToolbarElement toolbarElement = window; var tabbedPage = new TabbedPage { @@ -89,6 +89,30 @@ public void ToolbarTitle_UsesTabbedPageTitleWhenSet() Assert.Equal(tabbedPage.Title, toolbar.Title); } + [Fact] + public void ToolbarTitle_UsesTabbedPageChildTitleWhenChanged() + { + var window = new TestWindow(); + IToolbarElement toolbarElement = window; + + var childPage1 = new ContentPage { Title = "Child Test Title 1" }; + var childPage2 = new ContentPage { Title = "Child Test Title 2" }; + + var tabbedPage = new TabbedPage + { + Children = + { + childPage1, + childPage2 + }, + }; + window.Page = new NavigationPage(tabbedPage); + tabbedPage.CurrentPage = childPage2; + + var toolbar = (Toolbar)toolbarElement.Toolbar; + Assert.Equal(childPage2.Title, toolbar.Title); + } + [Fact] public async Task InsertPageBeforeRootPageShowsBackButton() { diff --git a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs index 98a9b88b5292..0d942628ae84 100644 --- a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs @@ -118,25 +118,6 @@ await CreateHandlerAndAddToWindow(new Window(navPage), (handl }); } - [Fact(DisplayName = "Toolbar Uses TabbedPage Title When Set")] - public async Task ToolbarTabbedPageTitle() - { - SetupBuilder(); - var navPage = new NavigationPage( - new TabbedPage - { - Title = "Tabbed Page Title", - Children = { new ContentPage { Title = "Child Page Title" } }, - }); - - await CreateHandlerAndAddToWindow(new Window(navPage), (handler) => - { - string title = GetToolbarTitle(handler); - Assert.Equal("Tabbed Page Title", title); - return Task.CompletedTask; - }); - } - [Fact(DisplayName = "Toolbar Uses TabbedPage Child Title")] public async Task ToolbarTabbedPageChildTitle() { From 3eed94cd0ad185d3c0bdfb9d6c21299050670d32 Mon Sep 17 00:00:00 2001 From: Dustin Wojciechowski Date: Thu, 11 Apr 2024 13:11:13 -0700 Subject: [PATCH 6/8] Fixed test not being able to fail --- .../DeviceTests/Elements/Toolbar/ToolbarTests.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs index 0d942628ae84..087fb7246919 100644 --- a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs @@ -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 @@ -38,7 +39,11 @@ void SetupBuilder() handlers.AddHandler(typeof(Controls.NavigationPage), typeof(NavigationViewHandler)); handlers.AddHandler(); handlers.AddHandler(); - 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); }); @@ -128,11 +133,12 @@ public async Task ToolbarTabbedPageChildTitle() Children = { new ContentPage { Title = "Child Page Title" } }, }); - await CreateHandlerAndAddToWindow(new Window(navPage), (handler) => + await CreateHandlerAndAddToWindow(new Window(navPage), async (handler) => { string title = GetToolbarTitle(handler); + await Task.Delay(TimeSpan.FromMinutes(1)); Assert.Equal("Child Page Title", title); - return Task.CompletedTask; + //return Task.CompletedTask; }); } From 091139f01422188afbd381ad6e30da9e9fe82d8e Mon Sep 17 00:00:00 2001 From: Dustin Wojciechowski Date: Mon, 22 Apr 2024 11:56:37 -0700 Subject: [PATCH 7/8] Changed GetTitle to work with more than just TabbedPages --- .../src/Core/NavigationPage/NavigationPageToolbar.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs b/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs index deca3a796fb1..b2569e7c9c57 100644 --- a/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs +++ b/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs @@ -281,14 +281,7 @@ string GetTitle() return string.Empty; } - Page target = _currentPage; - - if (_currentNavigationPage?.CurrentPage is TabbedPage tabbedPage && !string.IsNullOrEmpty(tabbedPage.Title)) - { - target = tabbedPage; - } - - return target?.Title; + return _currentNavigationPage?.CurrentPage?.Title; } VisualElement GetTitleView() From 9f7204be0deee5242db69f09a21c5fb8b5094134 Mon Sep 17 00:00:00 2001 From: Dustin Wojciechowski Date: Mon, 22 Apr 2024 12:03:12 -0700 Subject: [PATCH 8/8] Removed tests that deal with Child page titles replacing the Toolbar title --- .../tests/Core.UnitTests/ToolbarTests.cs | 41 ------------------- .../Elements/Toolbar/ToolbarTests.cs | 19 --------- 2 files changed, 60 deletions(-) diff --git a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs index df6ccef6558a..e1c28a847032 100644 --- a/src/Controls/tests/Core.UnitTests/ToolbarTests.cs +++ b/src/Controls/tests/Core.UnitTests/ToolbarTests.cs @@ -56,23 +56,6 @@ public async Task TitleAndTitleViewAreMutuallyExclusive() Assert.Equal("Test Title", toolbar.Title); } - [Fact] - public void ToolbarTitle_UsesTabbedPageChildTitle() - { - var window = new TestWindow(); - IToolbarElement toolbarElement = window; - var childPage = new ContentPage { Title = "Child Test Title" }; - var tabbedPage = new TabbedPage - { - Children = { childPage }, - }; - window.Page = new NavigationPage(tabbedPage); - - var toolbar = (Toolbar)toolbarElement.Toolbar; - Assert.Equal(childPage.Title, toolbar.Title); - } - - [Fact] public void ToolbarTitle_UsesTabbedPageTitleWhenSet() { @@ -89,30 +72,6 @@ public void ToolbarTitle_UsesTabbedPageTitleWhenSet() Assert.Equal(tabbedPage.Title, toolbar.Title); } - [Fact] - public void ToolbarTitle_UsesTabbedPageChildTitleWhenChanged() - { - var window = new TestWindow(); - IToolbarElement toolbarElement = window; - - var childPage1 = new ContentPage { Title = "Child Test Title 1" }; - var childPage2 = new ContentPage { Title = "Child Test Title 2" }; - - var tabbedPage = new TabbedPage - { - Children = - { - childPage1, - childPage2 - }, - }; - window.Page = new NavigationPage(tabbedPage); - tabbedPage.CurrentPage = childPage2; - - var toolbar = (Toolbar)toolbarElement.Toolbar; - Assert.Equal(childPage2.Title, toolbar.Title); - } - [Fact] public async Task InsertPageBeforeRootPageShowsBackButton() { diff --git a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs index 087fb7246919..57cce7a46c29 100644 --- a/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs @@ -123,25 +123,6 @@ await CreateHandlerAndAddToWindow(new Window(navPage), (handl }); } - [Fact(DisplayName = "Toolbar Uses TabbedPage Child Title")] - public async Task ToolbarTabbedPageChildTitle() - { - SetupBuilder(); - var navPage = new NavigationPage( - new TabbedPage - { - Children = { new ContentPage { Title = "Child Page Title" } }, - }); - - await CreateHandlerAndAddToWindow(new Window(navPage), async (handler) => - { - string title = GetToolbarTitle(handler); - await Task.Delay(TimeSpan.FromMinutes(1)); - Assert.Equal("Child Page Title", title); - //return Task.CompletedTask; - }); - } - [Theory] [InlineData($"{nameof(FlyoutPage)}WithNavigationPage, {nameof(ContentPage)}, {nameof(FlyoutPage)}WithNavigationPage" #if WINDOWS