Browse Source

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.
Steven Kirk 5 years ago
parent
commit
c2ad34a4ce
1 changed files with 15 additions and 7 deletions
  1. 15 7
      src/Avalonia.Controls/TextBox.cs

+ 15 - 7
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;
         }