1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 12:35:34 +08:00

Select all text when focusing a number box

This commit is contained in:
Salman Ahmed 2024-07-10 15:08:30 +03:00
parent d879527329
commit 94f51c92e0
4 changed files with 36 additions and 0 deletions

View File

@ -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<OsuTextBox> textBoxes) => AddStep("clear textbox", () => textBoxes.ForEach(textBox => textBox.Text = null));
private void expectedValue(IEnumerable<OsuTextBox> textBoxes, string value) => AddAssert("expected textbox value", () => textBoxes.All(textBox => textBox.Text == value));
}

View File

@ -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);
}
}

View File

@ -63,6 +63,11 @@ namespace osu.Game.Graphics.UserInterface
private Dictionary<FeedbackSampleType, Sample?[]> sampleMap = new Dictionary<FeedbackSampleType, Sample?[]>();
/// <summary>
/// Whether all text should be selected when the <see cref="OsuTextBox"/> gains focus.
/// </summary>
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)

View File

@ -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;