Browse Source

Adjusted TextBox right-click behavior
WPF does not allow dragging to change selection for right clicks
Furthermore, it only changes the selection on mouse up of right click, not mouse down, and only if the user clicked outside the current selection.
This commit adjusts TextBox selection behavior to work like WPF

Deadpikle 5 years ago
parent
commit
f98ee717f5
1 changed files with 21 additions and 3 deletions
  1. 21 3
      src/Avalonia.Controls/TextBox.cs

+ 21 - 3
src/Avalonia.Controls/TextBox.cs

@@ -684,11 +684,12 @@ namespace Avalonia.Controls
         protected override void OnPointerPressed(PointerPressedEventArgs e)
         {
             var point = e.GetPosition(_presenter);
-            var index = CaretIndex = _presenter.GetCaretIndex(point);
+            var index = _presenter.GetCaretIndex(point);
             var text = Text;
 
-            if (text != null && e.MouseButton == MouseButton.Left)
+            if (text != null && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
             {
+                CaretIndex = index;
                 switch (e.ClickCount)
                 {
                     case 1:
@@ -714,7 +715,8 @@ namespace Avalonia.Controls
 
         protected override void OnPointerMoved(PointerEventArgs e)
         {
-            if (_presenter != null && e.Pointer.Captured == _presenter)
+            // selection should not change during pointer move if the user right clicks
+            if (_presenter != null && e.Pointer.Captured == _presenter && !e.GetCurrentPoint(this).Properties.IsRightButtonPressed)
             {
                 var point = e.GetPosition(_presenter);
 
@@ -727,6 +729,22 @@ namespace Avalonia.Controls
         {
             if (_presenter != null && e.Pointer.Captured == _presenter)
             {
+                if (e.InitialPressMouseButton == MouseButton.Right)
+                {
+                    var point = e.GetPosition(_presenter);
+                    var caretIndex = _presenter.GetCaretIndex(point);
+
+                    // see if mouse clicked inside current selection
+                    // if it did not, we change the selection to where the user clicked
+                    var firstSelection = Math.Min(SelectionStart, SelectionEnd);
+                    var lastSelection = Math.Max(SelectionStart, SelectionEnd);
+                    var didClickInSelection = SelectionStart != SelectionEnd && 
+                        caretIndex >= firstSelection && caretIndex <= lastSelection;
+                    if (!didClickInSelection)
+                    {
+                        CaretIndex = SelectionEnd = SelectionStart = caretIndex;
+                    }
+                }
                 e.Pointer.Capture(null);
             }
         }