Skip to content

TextBox: clear selection when moving the caret horizontally #18255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Avalonia.Controls/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1983,9 +1983,13 @@ private void MoveHorizontal(int direction, bool wholeWord, bool isSelecting, boo
{
if (selectionStart != selectionEnd)
{
_presenter.MoveCaretToTextPosition(direction > 0 ?
// clear the selection and move to the appropriate side of previous selection
var newPosition = direction > 0 ?
Math.Max(selectionStart, selectionEnd) :
Math.Min(selectionStart, selectionEnd));
Math.Min(selectionStart, selectionEnd);
SetCurrentValue(SelectionStartProperty, newPosition);
SetCurrentValue(SelectionEndProperty, newPosition);
_presenter.MoveCaretToTextPosition(newPosition);
}
else
{
Expand Down
166 changes: 166 additions & 0 deletions tests/Avalonia.Controls.UnitTests/TextBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,172 @@ public void Should_Move_Caret_To_EndOfLine()
}
}

[Theory]
[InlineData(2,4)]
[InlineData(0,4)]
[InlineData(2,6)]
[InlineData(0,6)]
public void When_Selection_From_Left_To_Right_Pressing_Right_Should_Remove_Selection_Moving_Caret_To_End_Of_Previous_Selection(int selectionStart, int selectionEnd)
{
using (UnitTestApplication.Start(Services))
{
var tb = new TextBox
{
Template = CreateTemplate(),
Text = "ABCDEF"
};

tb.Measure(Size.Infinity);
tb.CaretIndex = selectionStart;
tb.SelectionStart = selectionStart;
tb.SelectionEnd = selectionEnd;

RaiseKeyEvent(tb, Key.Right, KeyModifiers.None);

Assert.Equal(selectionEnd, tb.SelectionStart);
Assert.Equal(selectionEnd, tb.SelectionEnd);
Assert.Equal(selectionEnd, tb.CaretIndex);
}
}

[Theory]
[InlineData(2,4)]
[InlineData(0,4)]
[InlineData(2,6)]
[InlineData(0,6)]
public void When_Selection_From_Left_To_Right_Pressing_Left_Should_Remove_Selection_Moving_Caret_To_Start_Of_Previous_Selection(int selectionStart, int selectionEnd)
{
using (UnitTestApplication.Start(Services))
{
var tb = new TextBox
{
Template = CreateTemplate(),
Text = "ABCDEF"
};

tb.Measure(Size.Infinity);
tb.CaretIndex = selectionStart;
tb.SelectionStart = selectionStart;
tb.SelectionEnd = selectionEnd;

RaiseKeyEvent(tb, Key.Left, KeyModifiers.None);

Assert.Equal(selectionStart, tb.SelectionStart);
Assert.Equal(selectionStart, tb.SelectionEnd);
Assert.Equal(selectionStart, tb.CaretIndex);
}
}

[Theory]
[InlineData(4,2)]
[InlineData(4,0)]
[InlineData(6,2)]
[InlineData(6,0)]
public void When_Selection_From_Right_To_Left_Pressing_Right_Should_Remove_Selection_Moving_Caret_To_Start_Of_Previous_Selection(int selectionStart, int selectionEnd)
{
using (UnitTestApplication.Start(Services))
{
var tb = new TextBox
{
Template = CreateTemplate(),
Text = "ABCDEF"
};

tb.Measure(Size.Infinity);
tb.CaretIndex = selectionStart;
tb.SelectionStart = selectionStart;
tb.SelectionEnd = selectionEnd;

RaiseKeyEvent(tb, Key.Right, KeyModifiers.None);

Assert.Equal(selectionStart, tb.SelectionStart);
Assert.Equal(selectionStart, tb.SelectionEnd);
Assert.Equal(selectionStart, tb.CaretIndex);
}
}

[Theory]
[InlineData(4,2)]
[InlineData(4,0)]
[InlineData(6,2)]
[InlineData(6,0)]
public void When_Selection_From_Right_To_Left_Pressing_Left_Should_Remove_Selection_Moving_Caret_To_End_Of_Previous_Selection(int selectionStart, int selectionEnd)
{
using (UnitTestApplication.Start(Services))
{
var tb = new TextBox
{
Template = CreateTemplate(),
Text = "ABCDEF"
};

tb.Measure(Size.Infinity);
tb.CaretIndex = selectionStart;
tb.SelectionStart = selectionStart;
tb.SelectionEnd = selectionEnd;

RaiseKeyEvent(tb, Key.Left, KeyModifiers.None);

Assert.Equal(selectionEnd, tb.SelectionStart);
Assert.Equal(selectionEnd, tb.SelectionEnd);
Assert.Equal(selectionEnd, tb.CaretIndex);
}
}

[Theory]
[InlineData(0)]
[InlineData(2)]
[InlineData(4)]
[InlineData(6)]
public void When_Select_All_From_Position_Left_Should_Remove_Selection_Moving_Caret_To_Start(int caretIndex)
{
using (UnitTestApplication.Start(Services))
{
var tb = new TextBox
{
Template = CreateTemplate(),
Text = "ABCDEF"
};

tb.Measure(Size.Infinity);
tb.CaretIndex = caretIndex;

RaiseKeyEvent(tb, Key.A, KeyModifiers.Control);
RaiseKeyEvent(tb, Key.Left, KeyModifiers.None);

Assert.Equal(0, tb.SelectionStart);
Assert.Equal(0, tb.SelectionEnd);
Assert.Equal(0, tb.CaretIndex);
}
}

[Theory]
[InlineData(0)]
[InlineData(2)]
[InlineData(4)]
[InlineData(6)]
public void When_Select_All_From_Position_Right_Should_Remove_Selection_Moving_Caret_To_End(int caretIndex)
{
using (UnitTestApplication.Start(Services))
{
var tb = new TextBox
{
Template = CreateTemplate(),
Text = "ABCDEF"
};

tb.Measure(Size.Infinity);
tb.CaretIndex = caretIndex;

RaiseKeyEvent(tb, Key.A, KeyModifiers.Control);
RaiseKeyEvent(tb, Key.Right, KeyModifiers.None);

Assert.Equal(tb.Text.Length, tb.SelectionStart);
Assert.Equal(tb.Text.Length, tb.SelectionEnd);
Assert.Equal(tb.Text.Length, tb.CaretIndex);
}
}

[Fact]
public void TextBox_In_AdornerLayer_Will_Not_Cause_Collection_Modified_In_VisualLayerManager_Measure()
{
Expand Down