From 9039016ed5e54c1a5537693a92b1b3fa289eaa24 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Fri, 29 Dec 2023 04:39:15 +0300 Subject: [PATCH] Update `OsuDropdown` implementation inline with framework changes --- .../UserInterface/TestSceneOsuDropdown.cs | 40 ++-------------- .../TestSceneOsuDropdownInputHandling.cs | 48 +++++++++++++++++++ .../Graphics/UserInterface/OsuDropdown.cs | 33 ++++++------- 3 files changed, 70 insertions(+), 51 deletions(-) create mode 100644 osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdownInputHandling.cs diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdown.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdown.cs index 1678890b7396..96080f586eb2 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdown.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdown.cs @@ -1,49 +1,19 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Linq; -using NUnit.Framework; using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.UserInterface; -using osu.Framework.Input.Events; -using osu.Framework.Input.States; -using osu.Framework.Testing; using osu.Game.Beatmaps; using osu.Game.Graphics.UserInterface; -using osu.Game.Input.Bindings; namespace osu.Game.Tests.Visual.UserInterface { public partial class TestSceneOsuDropdown : ThemeComparisonTestScene { - protected override Drawable CreateContent() => - new OsuEnumDropdown - { - Anchor = Anchor.Centre, - Origin = Anchor.TopCentre, - Width = 150 - }; - - [Test] - // todo: this can be written much better if ThemeComparisonTestScene has a manual input manager - public void TestBackAction() + protected override Drawable CreateContent() => new OsuEnumDropdown { - AddStep("open", () => dropdown().ChildrenOfType().Single().Open()); - AddStep("press back", () => dropdown().OnPressed(new KeyBindingPressEvent(new InputState(), GlobalAction.Back))); - AddAssert("closed", () => dropdown().ChildrenOfType().Single().State == MenuState.Closed); - - AddStep("open", () => dropdown().ChildrenOfType().Single().Open()); - AddStep("type something", () => dropdown().ChildrenOfType().Single().SearchTerm.Value = "something"); - AddAssert("search bar visible", () => dropdown().ChildrenOfType().Single().State.Value == Visibility.Visible); - AddStep("press back", () => dropdown().OnPressed(new KeyBindingPressEvent(new InputState(), GlobalAction.Back))); - AddAssert("text clear", () => dropdown().ChildrenOfType().Single().SearchTerm.Value == string.Empty); - AddAssert("search bar hidden", () => dropdown().ChildrenOfType().Single().State.Value == Visibility.Hidden); - AddAssert("still open", () => dropdown().ChildrenOfType().Single().State == MenuState.Open); - AddStep("press back", () => dropdown().OnPressed(new KeyBindingPressEvent(new InputState(), GlobalAction.Back))); - AddAssert("closed", () => dropdown().ChildrenOfType().Single().State == MenuState.Closed); - - OsuEnumDropdown dropdown() => this.ChildrenOfType>().First(); - } + Anchor = Anchor.Centre, + Origin = Anchor.TopCentre, + Width = 150 + }; } } diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdownInputHandling.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdownInputHandling.cs new file mode 100644 index 000000000000..3bd1c4a81e7a --- /dev/null +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuDropdownInputHandling.cs @@ -0,0 +1,48 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Linq; +using NUnit.Framework; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.UserInterface; +using osu.Framework.Testing; +using osu.Game.Beatmaps; +using osu.Game.Graphics.UserInterface; +using osuTK.Input; + +namespace osu.Game.Tests.Visual.UserInterface +{ + public partial class TestSceneOsuDropdownInputHandling : OsuManualInputManagerTestScene + { + private OsuDropdown dropdown = null!; + + [SetUp] + public void SetUp() => Schedule(() => + { + Child = dropdown = new OsuEnumDropdown + { + Anchor = Anchor.Centre, + Origin = Anchor.TopCentre, + Width = 150 + }; + }); + + [Test] + public void TestBackAction() + { + AddStep("open", () => dropdown.ChildrenOfType().Single().Open()); + AddStep("press back", () => InputManager.Key(Key.Escape)); + AddAssert("closed", () => dropdown.ChildrenOfType().Single().State == MenuState.Closed); + AddStep("open", () => dropdown.ChildrenOfType().Single().Open()); + AddStep("type something", () => dropdown.ChildrenOfType().Single().SearchTerm.Value = "something"); + AddAssert("search bar visible", () => dropdown.ChildrenOfType().Single().State.Value == Visibility.Visible); + AddStep("press back", () => InputManager.Key(Key.Escape)); + AddAssert("text clear", () => dropdown.ChildrenOfType().Single().SearchTerm.Value == string.Empty); + AddAssert("search bar hidden", () => dropdown.ChildrenOfType().Single().State.Value == Visibility.Hidden); + AddAssert("still open", () => dropdown.ChildrenOfType().Single().State == MenuState.Open); + AddStep("press back", () => InputManager.Key(Key.Escape)); + AddAssert("closed", () => dropdown.ChildrenOfType().Single().State == MenuState.Closed); + } + } +} diff --git a/osu.Game/Graphics/UserInterface/OsuDropdown.cs b/osu.Game/Graphics/UserInterface/OsuDropdown.cs index 632036fef984..dd354b32fa43 100644 --- a/osu.Game/Graphics/UserInterface/OsuDropdown.cs +++ b/osu.Game/Graphics/UserInterface/OsuDropdown.cs @@ -32,10 +32,11 @@ public partial class OsuDropdown : Dropdown, IKeyBindingHandler e) { - if (e.Repeat) return false; - - if (e.Action == GlobalAction.Back) - return Back(); + if (e.Action == GlobalAction.Back && Menu.State == MenuState.Open) + { + Menu.Close(); + return true; + } return false; } @@ -48,8 +49,6 @@ public void OnReleased(KeyBindingReleaseEvent e) protected partial class OsuDropdownMenu : DropdownMenu { - public override bool HandleNonPositionalInput => State == MenuState.Open; - private Sample? sampleOpen; private Sample? sampleClose; @@ -400,28 +399,30 @@ private void updateColour() Padding = new MarginPadding { Right = 26 }, }; - private partial class OsuDropdownSearchBar : DropdownSearchBar + private partial class OsuDropdownSearchBar : DropdownSearchBar, IKeyBindingHandler { protected override void PopIn() => this.FadeIn(); protected override void PopOut() => this.FadeOut(); - protected override TextBox CreateTextBox() => new DropdownSearchTextBox + protected override TextBox CreateTextBox() => new SearchTextBox { FontSize = OsuFont.Default.Size, }; - private partial class DropdownSearchTextBox : SearchTextBox + public bool OnPressed(KeyBindingPressEvent e) { - public override bool OnPressed(KeyBindingPressEvent e) + if (e.Action == GlobalAction.Back && !string.IsNullOrEmpty(SearchTerm.Value)) { - if (e.Action == GlobalAction.Back) - // this method is blocking Dropdown from receiving the back action, despite this text box residing in a separate input manager. - // to fix this properly, a local global action container needs to be added as well, but for simplicity, just don't handle the back action here. - return false; - - return base.OnPressed(e); + SearchTerm.Value = string.Empty; + return true; } + + return false; + } + + public void OnReleased(KeyBindingReleaseEvent e) + { } } }