From 76d20d6c6a478802de69d5cd6966270749cdf02b Mon Sep 17 00:00:00 2001 From: eriklimakc Date: Tue, 15 Oct 2024 15:57:43 +0100 Subject: [PATCH] chore: Add comments for clarification on TabBar GetInnerContainer --- .../Tests/TabBarTests.cs | 31 +++++++++---------- src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs | 7 +++-- .../TabBarSelectionIndicatorPresenter.cs | 4 +-- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs b/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs index 217ecc9fe..f2a4ea661 100644 --- a/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs +++ b/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs @@ -319,7 +319,8 @@ public async Task Verify_Indicator_Display_On_Selection() await UnitTestsUIContentHelper.WaitForIdle(); var container = SUT.ContainerFromItem(SUT.SelectedItem); - var selectedItem = SUT.GetInnerContainer(container); + var selectedItem = SUT.GetInnerContainer(container); // see comment on GetInnerContainer + Assert.IsNotNull(selectedItem); var renderer = await SUT.TakeScreenshot(); @@ -428,7 +429,7 @@ public async Task Verify_SelectedIndex_Not_Set_Unnecessarily() } [TestMethod] - public async Task Verify_ItemTemplated_Has_No_Nested_TabBarItem() + public async Task Verify_ItemTemplate_Has_No_Nested_TabBarItem() { var source = new[] { @@ -437,11 +438,11 @@ public async Task Verify_ItemTemplated_Has_No_Nested_TabBarItem() new TestRecord("True", true) }; - var dt = XamlHelper.LoadXaml(@" - - - - "); + var dt = XamlHelper.LoadXaml(""" + + + + """); var SUT = new TabBar { @@ -453,17 +454,15 @@ public async Task Verify_ItemTemplated_Has_No_Nested_TabBarItem() await UnitTestUIContentHelperEx.SetContentAndWait(SUT); - // Ensure the container is a `ContentPresenter` and not a `TabBarItem` var container = SUT.ContainerFromItem(SUT.SelectedItem); Assert.IsInstanceOfType(container, typeof(ContentPresenter)); - // Ensure the inner container is a `TabBarItem` - var selectedItem = SUT.GetInnerContainer(container); + var selectedItem = SUT.GetInnerContainer(container); // see comment on GetInnerContainer Assert.IsInstanceOfType(selectedItem, typeof(TabBarItem)); } [TestMethod] - public async Task Verify_ItemTemplated_Disabled_Not_Selectable() + public async Task Verify_ItemTemplate_Disabled_Not_Selectable() { var source = new[] { @@ -472,11 +471,11 @@ public async Task Verify_ItemTemplated_Disabled_Not_Selectable() new TestRecord("True", true) }; - var dt = XamlHelper.LoadXaml(@" - - - - "); + var dt = XamlHelper.LoadXaml(""" + + + + """); var SUT = new TabBar { diff --git a/src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs b/src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs index df31432ac..5ed95ec11 100644 --- a/src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs +++ b/src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs @@ -148,7 +148,7 @@ e is IVectorChangedEventArgs iVCE { var item = Items[(int)iVCE.Index]; - if (GetInnerContainer(item as DependencyObject) is { IsSelected: true } selected) + if (GetInnerContainer(item as DependencyObject) is { IsSelected: true } selected) // see comment on GetInnerContainer { SelectedItem = selected; } @@ -355,7 +355,7 @@ private void SynchronizeSelection(TabBarItem? item) var containers = this.GetItemContainers(); foreach (var container in containers) { - var tbi = GetInnerContainer(container); + var tbi = GetInnerContainer(container); // see comment on GetInnerContainer if (tbi is not { }) continue; if (!tbi.IsSelected) @@ -407,6 +407,9 @@ private void RaiseSelectionChangedEvent(object? prevItem, object? nextItem) SelectionChanged?.Invoke(this, eventArgs); } + // When using an `ItemTemplate` with a `TabBarItem`, the container will be a `ContentPresenter` that wraps the `TabBarItem`. + // In that case, to access the `ContentPresenter` from a `TabBarItem`, you must first call `ContainerFromItem`. + // Afterward, pass the resulting `ContentPresenter` as a parameter to this method. internal TabBarItem? GetInnerContainer(DependencyObject? container) { if (IsUsingOwnContainerAsTemplateRoot && container is ContentPresenter cp) diff --git a/src/Uno.Toolkit.UI/Controls/TabBar/TabBarSelectionIndicatorPresenter.cs b/src/Uno.Toolkit.UI/Controls/TabBar/TabBarSelectionIndicatorPresenter.cs index b8a300444..4d5cab44d 100644 --- a/src/Uno.Toolkit.UI/Controls/TabBar/TabBarSelectionIndicatorPresenter.cs +++ b/src/Uno.Toolkit.UI/Controls/TabBar/TabBarSelectionIndicatorPresenter.cs @@ -224,8 +224,8 @@ private void UpdateSelectionIndicatorMaxSize() if (Owner is { } tabBar && GetSelectionIndicator() is { } indicator) { - var tabBarItems =tabBar.GetItemContainers() - .Select(tabBar.GetInnerContainer) + var tabBarItems = tabBar.GetItemContainers() + .Select(tabBar.GetInnerContainer) // see comment on GetInnerContainer .OfType(); var visibleItems = tabBarItems.Count(x => x.Visibility == Visibility.Visible); if (visibleItems is 0)