Skip to content

Commit

Permalink
Merge pull request #6298 from smoogipoo/dropdown-external-management
Browse files Browse the repository at this point in the history
Add back support for opening dropdown by setting internal states
  • Loading branch information
peppy authored May 22, 2024
2 parents a21df55 + b233393 commit 11c2d23
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
51 changes: 51 additions & 0 deletions osu.Framework.Tests/Visual/UserInterface/TestSceneDropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,57 @@ void checkOrder(int index, string item) => AddAssert($"item #{index + 1} is '{it
() => Is.EqualTo(item));
}

[Test]
[Solo]
public void TestExternalManagement()
{
TestDropdown dropdown = null!;
Drawable openButton = null!;

AddStep("setup dropdown", () =>
{
dropdown = createDropdown();
dropdown.AlwaysShowSearchBar = true;

Add(openButton = new BasicButton
{
Size = new Vector2(150, 30),
Position = new Vector2(225, 50),
Text = "Open dropdown",
Action = openExternally
});
});

// Open via setting state directly

AddStep("open dropdown directly", openExternally);
AddAssert("dropdown open", () => dropdown.Menu.State, () => Is.EqualTo(MenuState.Open));
AddAssert("search bar visible", () => dropdown.ChildrenOfType<DropdownSearchBar>().Single().State.Value, () => Is.EqualTo(Visibility.Visible));
AddStep("press escape", () => InputManager.Key(Key.Escape));

// Open via clicking on an external button

AddStep("open dropdown via external button", () =>
{
InputManager.MoveMouseTo(openButton);
InputManager.Click(MouseButton.Left);
});

AddAssert("dropdown open", () => dropdown.Menu.State, () => Is.EqualTo(MenuState.Open));
AddAssert("search bar visible", () => dropdown.ChildrenOfType<DropdownSearchBar>().Single().State.Value, () => Is.EqualTo(Visibility.Visible));
AddStep("press escape", () => InputManager.Key(Key.Escape));

// Close via setting state directly

AddStep("open dropdown directly", openExternally);
AddAssert("dropdown open", () => dropdown.Menu.State, () => Is.EqualTo(MenuState.Open));
AddStep("close dropdown directly", () => dropdown.ChildrenOfType<Menu>().Single().Close());
AddAssert("dropdown closed", () => dropdown.Menu.State, () => Is.EqualTo(MenuState.Closed));
AddAssert("search bar not visible", () => dropdown.ChildrenOfType<DropdownSearchBar>().Single().State.Value, () => Is.EqualTo(Visibility.Hidden));

void openExternally() => dropdown.ChildrenOfType<Menu>().Single().Open();
}

[Test]
public void TestItemReplacementDoesNotAffectScroll()
{
Expand Down
8 changes: 7 additions & 1 deletion osu.Framework/Graphics/UserInterface/DropdownSearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,22 @@ private void onTextBoxCommit(TextBox sender, bool newText)
/// </summary>
private void onMenuStateChanged(MenuState state)
{
// Reset states when the menu is closed by any means.
if (state == MenuState.Closed)
{
// Reset states when the menu is closed by any means.
SearchTerm.Value = string.Empty;

if (textBox.HasFocus)
dropdown.ChangeFocus(null);

dropdown.CloseMenu();
}
else
{
// This exists because the menu is _sometimes_ opened via external means rather than a direct click.
// _Sometimes_, this occurs via a click on an external button (such as a test scene step button), and so it needs to be scheduled for the next frame.
Schedule(() => dropdown.ChangeFocus(textBox));
}
}

/// <summary>
Expand Down

0 comments on commit 11c2d23

Please sign in to comment.