From 94f51c92e0d4643c3821c7d9df6804d700b1fcc9 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Wed, 10 Jul 2024 15:08:30 +0300 Subject: [PATCH] Select all text when focusing a number box --- .../Visual/UserInterface/TestSceneOsuTextBox.cs | 17 +++++++++++++++++ osu.Game/Graphics/UserInterface/OsuNumberBox.cs | 5 +++++ osu.Game/Graphics/UserInterface/OsuTextBox.cs | 8 ++++++++ .../Graphics/UserInterfaceV2/LabelledTextBox.cs | 6 ++++++ 4 files changed, 36 insertions(+) diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneOsuTextBox.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuTextBox.cs index 69fe8ad105..921c5bbbfa 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestSceneOsuTextBox.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneOsuTextBox.cs @@ -11,6 +11,7 @@ using osu.Framework.Testing; using osu.Game.Graphics.UserInterface; using osu.Game.Overlays; using osuTK; +using osuTK.Input; namespace osu.Game.Tests.Visual.UserInterface { @@ -61,6 +62,22 @@ namespace osu.Game.Tests.Visual.UserInterface clearTextboxes(numberBoxes); } + [Test] + public void TestSelectAllOnFocus() + { + AddStep("create themed content", () => CreateThemedContent(OverlayColourScheme.Red)); + + AddStep("enter numbers", () => numberBoxes.ForEach(numberBox => numberBox.Text = "987654321")); + + AddAssert("nothing selected", () => string.IsNullOrEmpty(numberBoxes.First().SelectedText)); + AddStep("click on a number box", () => + { + InputManager.MoveMouseTo(numberBoxes.First()); + InputManager.Click(MouseButton.Left); + }); + AddAssert("text selected", () => numberBoxes.First().SelectedText == "987654321"); + } + private void clearTextboxes(IEnumerable textBoxes) => AddStep("clear textbox", () => textBoxes.ForEach(textBox => textBox.Text = null)); private void expectedValue(IEnumerable textBoxes, string value) => AddAssert("expected textbox value", () => textBoxes.All(textBox => textBox.Text == value)); } diff --git a/osu.Game/Graphics/UserInterface/OsuNumberBox.cs b/osu.Game/Graphics/UserInterface/OsuNumberBox.cs index e9b28f4771..db4b7b2ab3 100644 --- a/osu.Game/Graphics/UserInterface/OsuNumberBox.cs +++ b/osu.Game/Graphics/UserInterface/OsuNumberBox.cs @@ -7,6 +7,11 @@ namespace osu.Game.Graphics.UserInterface { protected override bool AllowIme => false; + public OsuNumberBox() + { + SelectAllOnFocus = true; + } + protected override bool CanAddCharacter(char character) => char.IsAsciiDigit(character); } } diff --git a/osu.Game/Graphics/UserInterface/OsuTextBox.cs b/osu.Game/Graphics/UserInterface/OsuTextBox.cs index 08d38837f6..90a000d441 100644 --- a/osu.Game/Graphics/UserInterface/OsuTextBox.cs +++ b/osu.Game/Graphics/UserInterface/OsuTextBox.cs @@ -63,6 +63,11 @@ namespace osu.Game.Graphics.UserInterface private Dictionary sampleMap = new Dictionary(); + /// + /// Whether all text should be selected when the gains focus. + /// + public bool SelectAllOnFocus { get; set; } + public OsuTextBox() { Height = 40; @@ -255,6 +260,9 @@ namespace osu.Game.Graphics.UserInterface BorderThickness = 3; base.OnFocus(e); + + if (SelectAllOnFocus) + SelectAll(); } protected override void OnFocusLost(FocusLostEvent e) diff --git a/osu.Game/Graphics/UserInterfaceV2/LabelledTextBox.cs b/osu.Game/Graphics/UserInterfaceV2/LabelledTextBox.cs index fabfde4333..b2e3ff077e 100644 --- a/osu.Game/Graphics/UserInterfaceV2/LabelledTextBox.cs +++ b/osu.Game/Graphics/UserInterfaceV2/LabelledTextBox.cs @@ -28,6 +28,12 @@ namespace osu.Game.Graphics.UserInterfaceV2 set => Component.ReadOnly = value; } + public bool SelectAllOnFocus + { + get => Component.SelectAllOnFocus; + set => Component.SelectAllOnFocus = value; + } + public LocalisableString PlaceholderText { set => Component.PlaceholderText = value;