Browse Source

[TextBox] MoveEnd now correctly positions the caret before the line break

Benedikt Stebner 2 years ago
parent
commit
0fc25da0dc

+ 1 - 1
src/Avalonia.Controls/TextBox.cs

@@ -1736,7 +1736,7 @@ namespace Avalonia.Controls
                 var lineIndex = _presenter.TextLayout.GetLineIndexFromCharacterIndex(caretIndex, false);
                 var textLine = textLines[lineIndex];
 
-                var textPosition = textLine.FirstTextSourceIndex + textLine.Length;
+                var textPosition = textLine.FirstTextSourceIndex + textLine.Length - textLine.NewLineLength;
 
                 _presenter.MoveCaretToTextPosition(textPosition, true);
             }

+ 19 - 0
tests/Avalonia.Controls.UnitTests/TextBoxTests.cs

@@ -1058,6 +1058,25 @@ namespace Avalonia.Controls.UnitTests
             }
         }
 
+        [Fact]
+        public void Should_Move_Caret_To_EndOfLine()
+        {
+            using (UnitTestApplication.Start(Services))
+            {
+                var tb = new TextBox
+                {
+                    Template = CreateTemplate(),
+                    Text = "AB\nAB"
+                };
+
+                tb.Measure(Size.Infinity);
+
+                RaiseKeyEvent(tb, Key.End, KeyModifiers.Shift);
+
+                Assert.Equal(2, tb.CaretIndex);
+            }
+        }
+
         private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
             focusManager: new FocusManager(),
             keyboardDevice: () => new KeyboardDevice(),