Skip to content

Commit

Permalink
Trigger user event change when directly calling SwitchTab
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Mar 18, 2024
1 parent 5d227b4 commit ba661c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,28 @@ public void TestSelectItemMethodSendsEvent()
AddAssert("selected tab queue has \"second\"", () => tabControl.UserTabSelectionChangedQueue.Dequeue().Value == TestEnum.Second);
}

[Test]
public void TestSwitchTabMethodSendsEvent()
{
AddStep("set switchable", () => tabControl.IsSwitchable = true);
AddStep("call switch tab", () => tabControl.SwitchTab(1));
AddAssert("selected tab = second", () => tabControl.Current.Value == TestEnum.Second);
AddAssert("selected tab queue has \"second\"", () => tabControl.UserTabSelectionChangedQueue.Dequeue().Value == TestEnum.Second);
AddStep("call switch tab", () => tabControl.SwitchTab(-1));
AddAssert("selected tab = second", () => tabControl.Current.Value == TestEnum.First);
AddAssert("selected tab queue has \"second\"", () => tabControl.UserTabSelectionChangedQueue.Dequeue().Value == TestEnum.First);
}

[Test]
public void TestSwitchUsingKeyBindingSendsEvent()
{
AddStep("set switchable", () => tabControl.IsSwitchable = true);
AddStep("switch forward", () => InputManager.Keys(PlatformAction.DocumentNext));
AddAssert("selected tab = second", () => tabControl.Current.Value == TestEnum.Second);
AddAssert("selected tab queue has \"second\"", () => tabControl.UserTabSelectionChangedQueue.Dequeue().Value == TestEnum.Second);
AddStep("switch backward", () => InputManager.Keys(PlatformAction.DocumentPrevious));
AddAssert("selected tab = second", () => tabControl.Current.Value == TestEnum.First);
AddAssert("selected tab queue has \"second\"", () => tabControl.UserTabSelectionChangedQueue.Dequeue().Value == TestEnum.First);
}

[Test]
Expand Down
22 changes: 15 additions & 7 deletions osu.Framework/Graphics/UserInterface/TabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,10 @@ protected virtual void RemoveTabItem(TabItem<T> tab, bool removeFromDropdown = t
{
// check all tabs as to include self (in correct iteration order)
bool anySwitchableTabsToRight = AllTabs.SkipWhile(t => t != tab).Skip(1).Any(t => t.IsSwitchable);
SwitchTab(anySwitchableTabsToRight ? 1 : -1);

// switching tab on removal is not directly caused by the user.
// call the private method to not trigger a user change event.
switchTab(anySwitchableTabsToRight ? 1 : -1);
}
}

Expand Down Expand Up @@ -403,8 +406,15 @@ private void updateSelectedTab(TabItem<T> tab)
/// </summary>
/// <param name="direction">Pass 1 to move to the next tab, or -1 to move to the previous tab.</param>
/// <param name="wrap">If <c>true</c>, moving past the start or the end of the tab list will wrap to the opposite end.</param>
/// <returns>Whether tab selection has changed as a result of this call.</returns>
public virtual bool SwitchTab(int direction, bool wrap = true)
public virtual void SwitchTab(int direction, bool wrap = true)
{
bool changed = switchTab(direction, wrap);

if (changed)
OnUserTabSelectionChanged(SelectedTab);
}

private bool switchTab(int direction, bool wrap = true)
{
if (Math.Abs(direction) != 1) throw new ArgumentException("value must be -1 or 1", nameof(direction));

Expand Down Expand Up @@ -454,13 +464,11 @@ public bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
switch (e.Action)
{
case PlatformAction.DocumentNext:
if (SwitchTab(1))
OnUserTabSelectionChanged(SelectedTab);
SwitchTab(1);
return true;

case PlatformAction.DocumentPrevious:
if (SwitchTab(-1))
OnUserTabSelectionChanged(SelectedTab);
SwitchTab(-1);
return true;
}
}
Expand Down

0 comments on commit ba661c7

Please sign in to comment.