Skip to content

Commit

Permalink
Merge pull request #4756 from LittleEndu/tabcontrol-initial-autosort
Browse files Browse the repository at this point in the history
Let `TabControl` always sort initial selection if auto sort is on
  • Loading branch information
peppy authored Sep 7, 2021
2 parents 3739063 + 0fd9563 commit 63bc389
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions osu.Framework.Tests/Visual/UserInterface/TestSceneTabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ public void TestInitialSelection(bool selectFirstByDefault, TestEnum? expectedIn
AddAssert("initial selection is correct", () => tabControl.Current.Value == expectedInitialSelection);
}

[TestCase(true, TestEnum.Test1, true)]
[TestCase(false, TestEnum.Test1, true)]
[TestCase(true, TestEnum.Test9, true)]
[TestCase(false, TestEnum.Test9, false)]
public void TestInitialSort(bool autoSort, TestEnum? initialItem, bool expected)
{
StyledTabControl tabControlWithBindable = null;
Bindable<TestEnum?> testBindable = new Bindable<TestEnum?> { Value = initialItem };

AddStep("create tab control", () =>
{
tabControlContainer.Add(tabControlWithBindable = new StyledTabControl
{
Size = new Vector2(200, 20),
Items = items.Cast<TestEnum?>().ToList(),
AutoSort = autoSort,
Current = { BindTarget = testBindable }
});
});

AddUntilStep("wait for loaded", () => tabControlWithBindable.IsLoaded);
AddAssert($"Current selection {(expected ? "visible" : "not visible")}", () => tabControlWithBindable.SelectedTab.IsPresent == expected);
}

private class StyledTabControlWithoutDropdown : TabControl<TestEnum>
{
protected override Dropdown<TestEnum> CreateDropdown() => null;
Expand Down
4 changes: 4 additions & 0 deletions osu.Framework/Graphics/UserInterface/TabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ protected override void LoadComplete()
Current.Value = Items.First();

Current.BindValueChanged(v => selectTab(v.NewValue != null ? tabMap[v.NewValue] : null), true);

// TabContainer doesn't have valid layout yet, so TabItems all have y=0 and selectTab() didn't call performTabSort() so we call it here instead
if (AutoSort && Current.Value != null)
performTabSort(tabMap[Current.Value]);
}

/// <summary>
Expand Down

0 comments on commit 63bc389

Please sign in to comment.