Skip to content

Commit ebb3926

Browse files
authored
Merge pull request #6645 from Kuuuube/triple-click-textbox
Add triple click to select all in TextBox
2 parents 384e062 + 599bef2 commit ebb3926

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

osu.Framework.Tests/Visual/UserInterface/TestSceneTextBox.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,50 @@ public void TestTextChangedDuringDoubleClickDrag()
917917
AddStep("end drag", () => InputManager.ReleaseButton(MouseButton.Left));
918918
}
919919

920+
[Test]
921+
public void TestTripleClickSelectAll()
922+
{
923+
InsertableTextBox textBox = null;
924+
925+
AddStep("add textbox", () =>
926+
{
927+
textBoxes.Add(textBox = new InsertableTextBox
928+
{
929+
Size = new Vector2(300, 40),
930+
Text = "multiple words so a double click cant select it all",
931+
});
932+
});
933+
934+
AddStep("move to textbox", () => InputManager.MoveMouseTo(textBox));
935+
936+
AddStep("triple click", () =>
937+
{
938+
InputManager.Click(MouseButton.Left);
939+
InputManager.Click(MouseButton.Left);
940+
InputManager.Click(MouseButton.Left);
941+
});
942+
AddAssert("all text selected", () => textBox.SelectedText, () => Is.EqualTo(textBox.Text));
943+
944+
AddStep("double click", () =>
945+
{
946+
InputManager.Click(MouseButton.Left);
947+
InputManager.Click(MouseButton.Left);
948+
});
949+
AddWaitStep("wait to fail triple click", 2);
950+
AddStep("third click", () => InputManager.Click(MouseButton.Left));
951+
AddAssert("no text selected", () => textBox.SelectedText, () => Is.EqualTo(string.Empty));
952+
953+
AddStep("triple click drag", () =>
954+
{
955+
InputManager.Click(MouseButton.Left);
956+
InputManager.Click(MouseButton.Left);
957+
InputManager.PressButton(MouseButton.Left);
958+
});
959+
AddStep("start drag", () => InputManager.MoveMouseTo(textBox.ScreenSpaceDrawQuad.TopLeft - new Vector2(200f, 0f)));
960+
AddStep("end drag", () => InputManager.ReleaseButton(MouseButton.Left));
961+
AddAssert("all text selected", () => textBox.SelectedText, () => Is.EqualTo(textBox.Text));
962+
}
963+
920964
[Test]
921965
public void TestSelectAll()
922966
{

osu.Framework/Graphics/UserInterface/TextBox.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ protected override void OnDrag(DragEvent e)
13081308

13091309
FinalizeImeComposition(true);
13101310

1311-
if (ignoreOngoingDragSelection)
1311+
if (ignoreOngoingDragSelection || tripleClickOngoing)
13121312
return;
13131313

13141314
var lastSelectionBounds = getTextSelectionBounds();
@@ -1349,8 +1349,12 @@ protected override void OnDrag(DragEvent e)
13491349
onTextSelectionChanged(doubleClickWord != null ? TextSelectionType.Word : TextSelectionType.Character, lastSelectionBounds);
13501350
}
13511351

1352+
private double? lastDoubleClickTime;
1353+
13521354
protected override bool OnDoubleClick(DoubleClickEvent e)
13531355
{
1356+
lastDoubleClickTime = Time.Current;
1357+
13541358
FinalizeImeComposition(true);
13551359

13561360
var lastSelectionBounds = getTextSelectionBounds();
@@ -1396,6 +1400,8 @@ private static int findSeparatorIndex(string input, int searchPos, int direction
13961400
return -1;
13971401
}
13981402

1403+
private bool tripleClickOngoing;
1404+
13991405
protected override bool OnMouseDown(MouseDownEvent e)
14001406
{
14011407
if (ReadOnly)
@@ -1405,6 +1411,21 @@ protected override bool OnMouseDown(MouseDownEvent e)
14051411

14061412
var lastSelectionBounds = getTextSelectionBounds();
14071413

1414+
float tripleClickTime = GetContainingInputManager().AsNonNull().GetButtonEventManagerFor(e.Button).DoubleClickTime;
1415+
1416+
if (lastDoubleClickTime != null && Time.Current - lastDoubleClickTime < tripleClickTime)
1417+
{
1418+
lastDoubleClickTime = null;
1419+
1420+
SelectAll();
1421+
1422+
onTextSelectionChanged(TextSelectionType.All, lastSelectionBounds);
1423+
1424+
tripleClickOngoing = true;
1425+
1426+
return true;
1427+
}
1428+
14081429
selectionStart = selectionEnd = getCharacterClosestTo(e.MousePosition);
14091430

14101431
cursorAndLayout.Invalidate();
@@ -1417,6 +1438,7 @@ protected override bool OnMouseDown(MouseDownEvent e)
14171438
protected override void OnMouseUp(MouseUpEvent e)
14181439
{
14191440
doubleClickWord = null;
1441+
tripleClickOngoing = false;
14201442
}
14211443

14221444
protected override void OnFocusLost(FocusLostEvent e)

0 commit comments

Comments
 (0)