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 467205c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
36 changes: 22 additions & 14 deletions src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,13 @@ public async Task Verify_Indicator_Display_On_Selection()
SUT.SelectedIndex = i;
await UnitTestsUIContentHelper.WaitForIdle();

// 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 `GetInnerContainer`.
// See https://github.com/unoplatform/uno.toolkit.ui/pull/1261#discussion_r1799794554 for reference.
var container = SUT.ContainerFromItem(SUT.SelectedItem);
var selectedItem = SUT.GetInnerContainer(container);

Assert.IsNotNull(selectedItem);

var renderer = await SUT.TakeScreenshot();
Expand Down Expand Up @@ -428,7 +433,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 +442,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 +458,20 @@ public async Task Verify_ItemTemplated_Has_No_Nested_TabBarItem()

await UnitTestUIContentHelperEx.SetContentAndWait(SUT);

// Ensure the container is a `ContentPresenter` and not a `TabBarItem`
// 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 `GetInnerContainer`.
// See https://github.com/unoplatform/uno.toolkit.ui/pull/1261#discussion_r1799794554 for reference.

var container = SUT.ContainerFromItem(SUT.SelectedItem);
Assert.IsInstanceOfType(container, typeof(ContentPresenter));

// Ensure the inner container is a `TabBarItem`
var selectedItem = SUT.GetInnerContainer(container);
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 +480,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
12 changes: 12 additions & 0 deletions src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ e is IVectorChangedEventArgs iVCE
{
var item = Items[(int)iVCE.Index];

// 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 `GetInnerContainer`.
// See https://github.com/unoplatform/uno.toolkit.ui/pull/1261#discussion_r1799794554 for reference.
if (GetInnerContainer(item as DependencyObject) is { IsSelected: true } selected)
{
SelectedItem = selected;
Expand Down Expand Up @@ -355,6 +359,10 @@ private void SynchronizeSelection(TabBarItem? item)
var containers = this.GetItemContainers<UIElement>();
foreach (var container in containers)
{
// 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 `GetInnerContainer`.
// See https://github.com/unoplatform/uno.toolkit.ui/pull/1261#discussion_r1799794554 for reference.
var tbi = GetInnerContainer(container);
if (tbi is not { }) continue;

Expand Down Expand Up @@ -407,6 +415,10 @@ 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.
// See https://github.com/unoplatform/uno.toolkit.ui/pull/1261#discussion_r1799794554 for reference.
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,7 +224,11 @@ private void UpdateSelectionIndicatorMaxSize()
if (Owner is { } tabBar
&& GetSelectionIndicator() is { } indicator)
{
var tabBarItems =tabBar.GetItemContainers<UIElement>()
var tabBarItems = tabBar.GetItemContainers<UIElement>()
// 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 `GetInnerContainer`.
// See https://github.com/unoplatform/uno.toolkit.ui/pull/1261#discussion_r1799794554 for reference.
.Select(tabBar.GetInnerContainer)
.OfType<TabBarItem>();
var visibleItems = tabBarItems.Count(x => x.Visibility == Visibility.Visible);
Expand Down

0 comments on commit 467205c

Please sign in to comment.