osu-framework icon indicating copy to clipboard operation
osu-framework copied to clipboard

`TabControl<T>` may not preserve selection on reset when last tab is selected

Open frenzibyte opened this issue 3 years ago • 0 comments

        [Test]
        public void TestResetTabsPreservesSelectionOnLastTab()
        {
            AddStep("select item 12", () => simpleTabcontrol.Current.Value = TestEnum.Test12);
            AddStep("reset tabs", () =>
            {
                simpleTabcontrol.Clear();

                foreach (var item in items)
                    simpleTabcontrol.AddItem(item);
            });
            AddStep("re-select item 12", () => simpleTabcontrol.Current.Value = TestEnum.Test12);
            AddAssert("item 12 selected", () => simpleTabcontrol.SelectedTab?.Value == TestEnum.Test12);
            AddAssert("item 12 visually selected", () => simpleTabcontrol.SelectedTab.Active.Value);
        }

This looks to be happening due to this specific line on tab removal: https://github.com/ppy/osu-framework/blob/04c5dd65bfb42e48afaa3aab2f9dd0bab28db3b8/osu.Framework/Graphics/UserInterface/TabControl.cs#L320-L327

When clearing all tabs and the last tab is selected, the first branch will execute and SelectedTab will become null, without changing Current.

Afterwards, the test will attempt to re-select the item but Current is already set to it, while SelectedTab is still null and therefore no tab is shown to be selected.

Using SelectTab(null) should make this work, but that'll set Current to the default value, which could be an existing tab enum in that case, so this may not be an easy one to solve.

frenzibyte avatar Sep 15 '22 01:09 frenzibyte