From 7b1a9fbb84e14124a67d54748571344f6db74bc4 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 4 Jun 2020 15:26:10 +0200 Subject: [PATCH 1/2] Fix caret not displaying when focused via Tab key. Also set caret position to end when selecting all, as textboxes when selected should scroll to display the end of the text. --- src/Avalonia.Controls/TextBox.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 42e16e6979..c2f496287c 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -347,7 +347,7 @@ namespace Avalonia.Controls if (IsFocused) { - DecideCaretVisibility(); + _presenter?.ShowCaret(); } } @@ -364,14 +364,7 @@ namespace Avalonia.Controls { SelectAll(); } - else - { - DecideCaretVisibility(); - } - } - private void DecideCaretVisibility() - { _presenter?.ShowCaret(); } @@ -975,6 +968,7 @@ namespace Avalonia.Controls { SelectionStart = 0; SelectionEnd = Text?.Length ?? 0; + CaretIndex = SelectionEnd; } private bool DeleteSelection() From c2ad34a4ce072ead90aa23323b76544a6fd91a9b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 4 Jun 2020 17:14:43 +0200 Subject: [PATCH 2/2] Fix moving caret left/right when selection present. When a selection is present, pressing the left key should move the caret to the start of the selection and pressing the right key should move the caret to the end of the selection. --- src/Avalonia.Controls/TextBox.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index c2f496287c..394699ce64 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -573,15 +573,15 @@ namespace Avalonia.Controls switch (e.Key) { case Key.Left: - MoveHorizontal(-1, hasWholeWordModifiers); - movement = true; selection = DetectSelection(); + MoveHorizontal(-1, hasWholeWordModifiers, selection); + movement = true; break; case Key.Right: - MoveHorizontal(1, hasWholeWordModifiers); - movement = true; selection = DetectSelection(); + MoveHorizontal(1, hasWholeWordModifiers, selection); + movement = true; break; case Key.Up: @@ -826,13 +826,21 @@ namespace Avalonia.Controls return result; } - private void MoveHorizontal(int direction, bool wholeWord) + private void MoveHorizontal(int direction, bool wholeWord, bool isSelecting) { var text = Text ?? string.Empty; var caretIndex = CaretIndex; if (!wholeWord) { + if (SelectionStart != SelectionEnd && !isSelecting) + { + var start = Math.Min(SelectionStart, SelectionEnd); + var end = Math.Max(SelectionStart, SelectionEnd); + CaretIndex = direction < 0 ? start : end; + return; + } + var index = caretIndex + direction; if (index < 0 || index > text.Length) @@ -1049,14 +1057,14 @@ namespace Avalonia.Controls private void SetSelectionForControlBackspace() { SelectionStart = CaretIndex; - MoveHorizontal(-1, true); + MoveHorizontal(-1, true, false); SelectionEnd = CaretIndex; } private void SetSelectionForControlDelete() { SelectionStart = CaretIndex; - MoveHorizontal(1, true); + MoveHorizontal(1, true, false); SelectionEnd = CaretIndex; }