Skip to content

Commit 0acd9bd

Browse files
rastislavsvobodaMrJul
authored andcommitted
TextBox: clear selection when moving the caret horizontally (#18255)
* TextBox: clear selection when moving the caret horizontally * fix and add unit tests
1 parent 8aedae9 commit 0acd9bd

File tree

2 files changed

+172
-2
lines changed

2 files changed

+172
-2
lines changed

src/Avalonia.Controls/TextBox.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,9 +1967,13 @@ private void MoveHorizontal(int direction, bool wholeWord, bool isSelecting, boo
19671967
{
19681968
if (selectionStart != selectionEnd)
19691969
{
1970-
_presenter.MoveCaretToTextPosition(direction > 0 ?
1970+
// clear the selection and move to the appropriate side of previous selection
1971+
var newPosition = direction > 0 ?
19711972
Math.Max(selectionStart, selectionEnd) :
1972-
Math.Min(selectionStart, selectionEnd));
1973+
Math.Min(selectionStart, selectionEnd);
1974+
SetCurrentValue(SelectionStartProperty, newPosition);
1975+
SetCurrentValue(SelectionEndProperty, newPosition);
1976+
_presenter.MoveCaretToTextPosition(newPosition);
19731977
}
19741978
else
19751979
{

tests/Avalonia.Controls.UnitTests/TextBoxTests.cs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,172 @@ public void Should_Move_Caret_To_EndOfLine()
14251425
}
14261426
}
14271427

1428+
[Theory]
1429+
[InlineData(2,4)]
1430+
[InlineData(0,4)]
1431+
[InlineData(2,6)]
1432+
[InlineData(0,6)]
1433+
public void When_Selection_From_Left_To_Right_Pressing_Right_Should_Remove_Selection_Moving_Caret_To_End_Of_Previous_Selection(int selectionStart, int selectionEnd)
1434+
{
1435+
using (UnitTestApplication.Start(Services))
1436+
{
1437+
var tb = new TextBox
1438+
{
1439+
Template = CreateTemplate(),
1440+
Text = "ABCDEF"
1441+
};
1442+
1443+
tb.Measure(Size.Infinity);
1444+
tb.CaretIndex = selectionStart;
1445+
tb.SelectionStart = selectionStart;
1446+
tb.SelectionEnd = selectionEnd;
1447+
1448+
RaiseKeyEvent(tb, Key.Right, KeyModifiers.None);
1449+
1450+
Assert.Equal(selectionEnd, tb.SelectionStart);
1451+
Assert.Equal(selectionEnd, tb.SelectionEnd);
1452+
Assert.Equal(selectionEnd, tb.CaretIndex);
1453+
}
1454+
}
1455+
1456+
[Theory]
1457+
[InlineData(2,4)]
1458+
[InlineData(0,4)]
1459+
[InlineData(2,6)]
1460+
[InlineData(0,6)]
1461+
public void When_Selection_From_Left_To_Right_Pressing_Left_Should_Remove_Selection_Moving_Caret_To_Start_Of_Previous_Selection(int selectionStart, int selectionEnd)
1462+
{
1463+
using (UnitTestApplication.Start(Services))
1464+
{
1465+
var tb = new TextBox
1466+
{
1467+
Template = CreateTemplate(),
1468+
Text = "ABCDEF"
1469+
};
1470+
1471+
tb.Measure(Size.Infinity);
1472+
tb.CaretIndex = selectionStart;
1473+
tb.SelectionStart = selectionStart;
1474+
tb.SelectionEnd = selectionEnd;
1475+
1476+
RaiseKeyEvent(tb, Key.Left, KeyModifiers.None);
1477+
1478+
Assert.Equal(selectionStart, tb.SelectionStart);
1479+
Assert.Equal(selectionStart, tb.SelectionEnd);
1480+
Assert.Equal(selectionStart, tb.CaretIndex);
1481+
}
1482+
}
1483+
1484+
[Theory]
1485+
[InlineData(4,2)]
1486+
[InlineData(4,0)]
1487+
[InlineData(6,2)]
1488+
[InlineData(6,0)]
1489+
public void When_Selection_From_Right_To_Left_Pressing_Right_Should_Remove_Selection_Moving_Caret_To_Start_Of_Previous_Selection(int selectionStart, int selectionEnd)
1490+
{
1491+
using (UnitTestApplication.Start(Services))
1492+
{
1493+
var tb = new TextBox
1494+
{
1495+
Template = CreateTemplate(),
1496+
Text = "ABCDEF"
1497+
};
1498+
1499+
tb.Measure(Size.Infinity);
1500+
tb.CaretIndex = selectionStart;
1501+
tb.SelectionStart = selectionStart;
1502+
tb.SelectionEnd = selectionEnd;
1503+
1504+
RaiseKeyEvent(tb, Key.Right, KeyModifiers.None);
1505+
1506+
Assert.Equal(selectionStart, tb.SelectionStart);
1507+
Assert.Equal(selectionStart, tb.SelectionEnd);
1508+
Assert.Equal(selectionStart, tb.CaretIndex);
1509+
}
1510+
}
1511+
1512+
[Theory]
1513+
[InlineData(4,2)]
1514+
[InlineData(4,0)]
1515+
[InlineData(6,2)]
1516+
[InlineData(6,0)]
1517+
public void When_Selection_From_Right_To_Left_Pressing_Left_Should_Remove_Selection_Moving_Caret_To_End_Of_Previous_Selection(int selectionStart, int selectionEnd)
1518+
{
1519+
using (UnitTestApplication.Start(Services))
1520+
{
1521+
var tb = new TextBox
1522+
{
1523+
Template = CreateTemplate(),
1524+
Text = "ABCDEF"
1525+
};
1526+
1527+
tb.Measure(Size.Infinity);
1528+
tb.CaretIndex = selectionStart;
1529+
tb.SelectionStart = selectionStart;
1530+
tb.SelectionEnd = selectionEnd;
1531+
1532+
RaiseKeyEvent(tb, Key.Left, KeyModifiers.None);
1533+
1534+
Assert.Equal(selectionEnd, tb.SelectionStart);
1535+
Assert.Equal(selectionEnd, tb.SelectionEnd);
1536+
Assert.Equal(selectionEnd, tb.CaretIndex);
1537+
}
1538+
}
1539+
1540+
[Theory]
1541+
[InlineData(0)]
1542+
[InlineData(2)]
1543+
[InlineData(4)]
1544+
[InlineData(6)]
1545+
public void When_Select_All_From_Position_Left_Should_Remove_Selection_Moving_Caret_To_Start(int caretIndex)
1546+
{
1547+
using (UnitTestApplication.Start(Services))
1548+
{
1549+
var tb = new TextBox
1550+
{
1551+
Template = CreateTemplate(),
1552+
Text = "ABCDEF"
1553+
};
1554+
1555+
tb.Measure(Size.Infinity);
1556+
tb.CaretIndex = caretIndex;
1557+
1558+
RaiseKeyEvent(tb, Key.A, KeyModifiers.Control);
1559+
RaiseKeyEvent(tb, Key.Left, KeyModifiers.None);
1560+
1561+
Assert.Equal(0, tb.SelectionStart);
1562+
Assert.Equal(0, tb.SelectionEnd);
1563+
Assert.Equal(0, tb.CaretIndex);
1564+
}
1565+
}
1566+
1567+
[Theory]
1568+
[InlineData(0)]
1569+
[InlineData(2)]
1570+
[InlineData(4)]
1571+
[InlineData(6)]
1572+
public void When_Select_All_From_Position_Right_Should_Remove_Selection_Moving_Caret_To_End(int caretIndex)
1573+
{
1574+
using (UnitTestApplication.Start(Services))
1575+
{
1576+
var tb = new TextBox
1577+
{
1578+
Template = CreateTemplate(),
1579+
Text = "ABCDEF"
1580+
};
1581+
1582+
tb.Measure(Size.Infinity);
1583+
tb.CaretIndex = caretIndex;
1584+
1585+
RaiseKeyEvent(tb, Key.A, KeyModifiers.Control);
1586+
RaiseKeyEvent(tb, Key.Right, KeyModifiers.None);
1587+
1588+
Assert.Equal(tb.Text.Length, tb.SelectionStart);
1589+
Assert.Equal(tb.Text.Length, tb.SelectionEnd);
1590+
Assert.Equal(tb.Text.Length, tb.CaretIndex);
1591+
}
1592+
}
1593+
14281594
[Fact]
14291595
public void TextBox_In_AdornerLayer_Will_Not_Cause_Collection_Modified_In_VisualLayerManager_Measure()
14301596
{

0 commit comments

Comments
 (0)