Skip to content

Commit

Permalink
chore: Add comments for clarification on TabBar GetInnerContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Oct 15, 2024
1 parent 8b35f42 commit 76d20d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
31 changes: 15 additions & 16 deletions src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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[]
{
Expand All @@ -437,11 +438,11 @@ public async Task Verify_ItemTemplated_Has_No_Nested_TabBarItem()
new TestRecord("True", true)
};

var dt = XamlHelper.LoadXaml<DataTemplate>(@"
<DataTemplate>
<utu:TabBarItem Content=""{Binding Name}"" IsSelectable=""{Binding IsSelectable}"" />
</DataTemplate>
");
var dt = XamlHelper.LoadXaml<DataTemplate>("""
<DataTemplate>
<utu:TabBarItem Content="{Binding Name}" IsSelectable="{Binding IsSelectable}" />
</DataTemplate>
""");

var SUT = new TabBar
{
Expand All @@ -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[]
{
Expand All @@ -472,11 +471,11 @@ public async Task Verify_ItemTemplated_Disabled_Not_Selectable()
new TestRecord("True", true)
};

var dt = XamlHelper.LoadXaml<DataTemplate>(@"
<DataTemplate>
<utu:TabBarItem Content=""{Binding Name}"" IsSelectable=""{Binding IsSelectable}"" />
</DataTemplate>
");
var dt = XamlHelper.LoadXaml<DataTemplate>("""
<DataTemplate>
<utu:TabBarItem Content="{Binding Name}" IsSelectable="{Binding IsSelectable}" />
</DataTemplate>
""");

var SUT = new TabBar
{
Expand Down
7 changes: 5 additions & 2 deletions src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -355,7 +355,7 @@ private void SynchronizeSelection(TabBarItem? item)
var containers = this.GetItemContainers<UIElement>();
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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ private void UpdateSelectionIndicatorMaxSize()
if (Owner is { } tabBar
&& GetSelectionIndicator() is { } indicator)
{
var tabBarItems =tabBar.GetItemContainers<UIElement>()
.Select(tabBar.GetInnerContainer)
var tabBarItems = tabBar.GetItemContainers<UIElement>()
.Select(tabBar.GetInnerContainer) // see comment on GetInnerContainer
.OfType<TabBarItem>();
var visibleItems = tabBarItems.Count(x => x.Visibility == Visibility.Visible);
if (visibleItems is 0)
Expand Down

0 comments on commit 76d20d6

Please sign in to comment.