Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to switch between tabs using keyboard controls #1363

Merged
merged 17 commits into from Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion osu.Framework.Tests/Visual/TestCaseTabControl.cs
Expand Up @@ -11,6 +11,7 @@
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing;
using osu.Framework.Input;
using OpenTK;
using OpenTK.Graphics;

Expand All @@ -33,15 +34,27 @@ public TestCaseTabControl()

StyledTabControl pinnedAndAutoSort = new StyledTabControl
{
Position = new Vector2(500, 50),
Position = new Vector2(200, 150),
Size = new Vector2(200, 30),
AutoSort = true
};
items.GetRange(0, 7).AsEnumerable().ForEach(item => pinnedAndAutoSort.AddItem(item.Value));
pinnedAndAutoSort.PinItem(TestEnum.Test5);

StyledTabControl switchingTabControl;
PlatformActionContainer platformActionContainer = new PlatformActionContainer
{
Child = switchingTabControl = new StyledTabControl
{
Position = new Vector2(200, 250),
Size = new Vector2(200, 30),
}
};
items.AsEnumerable().ForEach(item => switchingTabControl.AddItem(item.Value));

Add(simpleTabcontrol);
Add(pinnedAndAutoSort);
Add(platformActionContainer);

var nextTest = new Func<TestEnum>(() => items.AsEnumerable()
.Select(item => item.Value)
Expand Down Expand Up @@ -80,6 +93,19 @@ public TestCaseTabControl()
{
if (pinned.Count > 0) pinnedAndAutoSort.UnpinItem(pinned.Pop());
});

AddStep("Set first tab", () => switchingTabControl.Current.Value = switchingTabControl.VisibleItems.First());
AddStep("Switch forward", () => platformActionContainer.TriggerPressed(new PlatformAction(PlatformActionType.DocumentNext)));
AddAssert("Ensure second tab", () => switchingTabControl.Current.Value == switchingTabControl.VisibleItems.ElementAt(1));

AddStep("Switch backward", () => platformActionContainer.TriggerPressed(new PlatformAction(PlatformActionType.DocumentPrevious)));
AddAssert("Ensure first Tab", () => switchingTabControl.Current.Value == switchingTabControl.VisibleItems.First());

AddStep("Switch backward", () => platformActionContainer.TriggerPressed(new PlatformAction(PlatformActionType.DocumentPrevious)));
AddAssert("Ensure last tab", () => switchingTabControl.Current.Value == switchingTabControl.VisibleItems.Last());

AddStep("Switch forward", () => platformActionContainer.TriggerPressed(new PlatformAction(PlatformActionType.DocumentNext)));
AddAssert("Ensure first tab", () => switchingTabControl.Current.Value == switchingTabControl.VisibleItems.First());
}

private class StyledTabControl : TabControl<TestEnum>
Expand Down
60 changes: 59 additions & 1 deletion osu.Framework/Graphics/UserInterface/TabControl.cs
Expand Up @@ -8,6 +8,8 @@
using osu.Framework.Configuration;
using osu.Framework.Extensions;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using OpenTK;

namespace osu.Framework.Graphics.UserInterface
Expand All @@ -19,7 +21,7 @@ namespace osu.Framework.Graphics.UserInterface
/// start of the list.
/// </summary>
/// <typeparam name="T">The type of item to be represented by tabs.</typeparam>
public abstract class TabControl<T> : Container, IHasCurrentValue<T>
public abstract class TabControl<T> : Container, IHasCurrentValue<T>, IKeyBindingHandler<PlatformAction>
{
public Bindable<T> Current { get; } = new Bindable<T>();

Expand All @@ -28,6 +30,8 @@ public abstract class TabControl<T> : Container, IHasCurrentValue<T>
/// </summary>
public IEnumerable<T> Items => TabContainer.TabItems.Select(t => t.Value).Concat(Dropdown.Items.Select(kvp => kvp.Value)).Distinct();

public IEnumerable<T> VisibleItems => TabContainer.TabItems.Select(t => t.Value).Distinct();

/// <summary>
/// When true, tabs selected from the overflow dropdown will be moved to the front of the list (after pinned items).
/// </summary>
Expand All @@ -41,6 +45,11 @@ public abstract class TabControl<T> : Container, IHasCurrentValue<T>

protected TabItem<T> SelectedTab;

/// <summary>
/// When true, tabs can be switched back and forth using PlatformAction.DocumentPrevious and PlatformAction.DocumentNext respectively.
/// </summary>
public virtual bool IsSwitchable => true;

/// <summary>
/// Creates an optional overflow dropdown.
/// When implementing this dropdown make sure:
Expand Down Expand Up @@ -237,6 +246,35 @@ protected virtual void SelectTab(TabItem<T> tab)
Current.Value = SelectedTab.Value;
}

public virtual void SwitchTab(int offset, bool wrap = true)

This comment was marked as off-topic.

This comment was marked as off-topic.

{
TabItem<T>[] switchableTabs = TabContainer.TabItems.Where(tab => tab.IsSwitchable).ToArray();
int tabCount = switchableTabs.Length;

if (tabCount == 0)
return;

if (tabCount == 1 || SelectedTab == null)
{
SelectTab(switchableTabs[0]);
return;
}

int selectedIndex = Array.IndexOf(switchableTabs, SelectedTab);
int targetIndex = selectedIndex + offset;

if (wrap)
{
targetIndex = targetIndex % tabCount;
if (targetIndex < 0)
targetIndex += tabCount;
}

targetIndex = Math.Min(tabCount - 1, Math.Max(0, targetIndex));

SelectTab(switchableTabs[targetIndex]);
}

private void performTabSort(TabItem<T> tab)
{
TabContainer.SetLayoutPosition(tab, getTabDepth(tab));
Expand All @@ -248,6 +286,26 @@ private void performTabSort(TabItem<T> tab)

private float getTabDepth(TabItem<T> tab) => tab.Pinned ? float.MinValue : --depthCounter;

public bool OnPressed(PlatformAction action)
{
if (IsSwitchable)
{
switch (action.ActionType)
{
case PlatformActionType.DocumentNext:
SwitchTab(1);
return true;

case PlatformActionType.DocumentPrevious:
SwitchTab(-1);
return true;
}
}
return false;
}

public bool OnReleased(PlatformAction action) => false;

protected virtual TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
Direction = FillDirection.Full,
Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Graphics/UserInterface/TabItem.cs
Expand Up @@ -26,6 +26,11 @@ public abstract class TabItem<T> : TabItem

public override bool IsRemovable => false;

/// <summary>
/// When true, this tab can be switched to using PlatformAction.DocumentPrevious and PlatformAction.DocumentNext. Otherwise, it will be skipped.
/// </summary>
public virtual bool IsSwitchable => true;

public readonly T Value;

protected TabItem(T value)
Expand Down
4 changes: 3 additions & 1 deletion osu.Framework/Input/PlatformActionContainer.cs
Expand Up @@ -54,7 +54,9 @@ public enum PlatformActionType
WordPrevious,
WordNext,
LineStart,
LineEnd
LineEnd,
DocumentPrevious,
DocumentNext
}

public enum PlatformActionMethod
Expand Down
4 changes: 4 additions & 0 deletions osu.Framework/Platform/GameHost.cs
Expand Up @@ -697,6 +697,10 @@ public void Dispose()
new KeyBinding(InputKey.End, new PlatformAction(PlatformActionType.LineEnd, PlatformActionMethod.Move)),
new KeyBinding(new KeyCombination(new[] { InputKey.Shift, InputKey.Home }), new PlatformAction(PlatformActionType.LineStart, PlatformActionMethod.Select)),
new KeyBinding(new KeyCombination(new[] { InputKey.Shift, InputKey.End }), new PlatformAction(PlatformActionType.LineEnd, PlatformActionMethod.Select)),
new KeyBinding(new KeyCombination(new[] { InputKey.Control, InputKey.PageUp }), new PlatformAction(PlatformActionType.DocumentPrevious)),
new KeyBinding(new KeyCombination(new[] { InputKey.Control, InputKey.PageDown }), new PlatformAction(PlatformActionType.DocumentNext)),
new KeyBinding(new KeyCombination(new[] { InputKey.Control, InputKey.Tab }), new PlatformAction(PlatformActionType.DocumentNext)),
new KeyBinding(new KeyCombination(new[] { InputKey.Control, InputKey.Shift, InputKey.Tab }), new PlatformAction(PlatformActionType.DocumentPrevious)),
};
}

Expand Down
4 changes: 4 additions & 0 deletions osu.Framework/Platform/MacOS/MacOSGameHost.cs
Expand Up @@ -50,6 +50,10 @@ internal MacOSGameHost(string gameName, bool bindIPC = false)
new KeyBinding(new KeyCombination(new[] { InputKey.Super, InputKey.Delete }), new PlatformAction(PlatformActionType.LineEnd, PlatformActionMethod.Delete)),
new KeyBinding(new KeyCombination(new[] { InputKey.Super, InputKey.Shift, InputKey.Left }), new PlatformAction(PlatformActionType.LineStart, PlatformActionMethod.Select)),
new KeyBinding(new KeyCombination(new[] { InputKey.Super, InputKey.Shift, InputKey.Right }), new PlatformAction(PlatformActionType.LineEnd, PlatformActionMethod.Select)),
new KeyBinding(new KeyCombination(new[] { InputKey.Alt, InputKey.Super, InputKey.Left }), new PlatformAction(PlatformActionType.DocumentPrevious)),

This comment was marked as off-topic.

This comment was marked as off-topic.

new KeyBinding(new KeyCombination(new[] { InputKey.Alt, InputKey.Super, InputKey.Right }), new PlatformAction(PlatformActionType.DocumentNext)),
new KeyBinding(new KeyCombination(new[] { InputKey.Control, InputKey.Tab }), new PlatformAction(PlatformActionType.DocumentNext)),
new KeyBinding(new KeyCombination(new[] { InputKey.Control, InputKey.Shift, InputKey.Tab }), new PlatformAction(PlatformActionType.DocumentPrevious)),
};
}
}