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

Fix tabbable container not checking if its next tab stop is focusable #6364

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions osu.Framework.Tests/Visual/UserInterface/TestSceneTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,76 @@ public void TestTypingCancelsOngoingDragSelection()
AddAssert("text selected by drag", () => textBox.SelectedText == "1");
}

[Test]
public void TestTabbing()
{
AddStep("add textboxes", () =>
{
textBoxes.AddRange([
new InsertableTextBox
{
Size = new Vector2(300, 40),
Text = "first!",
ReadOnly = false,
TabbableContentContainer = textBoxes,
},
new InsertableTextBox
{
Size = new Vector2(300, 40),
Text = "second!",
ReadOnly = false,
TabbableContentContainer = textBoxes,
},
new InsertableTextBox
{
Size = new Vector2(300, 40),
Text = "third! (readonly)",
ReadOnly = true,
TabbableContentContainer = textBoxes,
},
new InsertableTextBox
{
Size = new Vector2(300, 40),
Text = "fourth!",
ReadOnly = false,
TabbableContentContainer = textBoxes,
}
]);
});

AddStep("focus first textbox", () =>
{
InputManager.MoveMouseTo(textBoxes[0]);
InputManager.Click(MouseButton.Left);
});

AddStep("press tab", () => InputManager.Key(Key.Tab));
AddAssert("second textbox focused", () => textBoxes[1].HasFocus);

AddStep("press tab", () => InputManager.Key(Key.Tab));
AddAssert("readonly textbox skipped", () => textBoxes[3].HasFocus);

AddStep("press tab", () => InputManager.Key(Key.Tab));
AddAssert("first textbox focused", () => textBoxes[0].HasFocus);

AddStep("press shift-tab", () =>
{
InputManager.PressKey(Key.ShiftLeft);
InputManager.Key(Key.Tab);
InputManager.ReleaseKey(Key.ShiftLeft);
});
AddAssert("fourth textbox focused", () => textBoxes[3].HasFocus);

AddStep("hide second textbox", () => textBoxes[1].Alpha = 0);
AddStep("press shift-tab", () =>
{
InputManager.PressKey(Key.ShiftLeft);
InputManager.Key(Key.Tab);
InputManager.ReleaseKey(Key.ShiftLeft);
});
AddAssert("first textbox focused", () => textBoxes[0].HasFocus);
}

private void prependString(InsertableTextBox textBox, string text)
{
InputManager.Keys(PlatformAction.MoveBackwardLine);
Expand Down
13 changes: 6 additions & 7 deletions osu.Framework/Graphics/Containers/TabbableContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ protected override bool OnKeyDown(KeyDownEvent e)
if (TabbableContentContainer == null || e.Key != Key.Tab)
return false;

var nextTab = nextTabStop(TabbableContentContainer, e.ShiftPressed);
if (nextTab != null) GetContainingFocusManager().AsNonNull().ChangeFocus(nextTab);
moveToNextTabStop(TabbableContentContainer, e.ShiftPressed);
return true;
}

private Drawable nextTabStop(CompositeDrawable target, bool reverse)
private void moveToNextTabStop(CompositeDrawable target, bool reverse)
{
var focusManager = GetContainingFocusManager().AsNonNull();

Stack<Drawable> stack = new Stack<Drawable>();
stack.Push(target); // Extra push for circular tabbing
stack.Push(target);
Expand All @@ -63,8 +64,8 @@ private Drawable nextTabStop(CompositeDrawable target, bool reverse)

if (!started)
started = ReferenceEquals(drawable, this);
else if (drawable is ITabbableContainer tabbable && tabbable.CanBeTabbedTo)
return drawable;
else if (drawable is ITabbableContainer tabbable && tabbable.CanBeTabbedTo && focusManager.ChangeFocus(drawable))
return;

if (drawable is CompositeDrawable composite)
{
Expand All @@ -91,8 +92,6 @@ private Drawable nextTabStop(CompositeDrawable target, bool reverse)
}
}
}

return null;
}
}
}
Loading