diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 67aa4de569..edbcd467aa 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -455,14 +455,14 @@ namespace Avalonia.Controls.Presenters internal (Point, Point) GetCaretPoints() { - var x = Math.Floor(_caretBounds.X) + 0.5; - var y = Math.Floor(_caretBounds.Y) + 0.5; - var b = Math.Ceiling(_caretBounds.Bottom) - 0.5; - var caretIndex = _lastCharacterHit.FirstCharacterIndex + _lastCharacterHit.TrailingLength; var lineIndex = TextLayout.GetLineIndexFromCharacterIndex(caretIndex, _lastCharacterHit.TrailingLength > 0); var textLine = TextLayout.TextLines[lineIndex]; + var x = Math.Floor(_caretBounds.X) + 0.5; + var y = Math.Floor(_caretBounds.Y) + 0.5; + var b = Math.Ceiling(_caretBounds.Bottom) - 0.5; + if (_caretBounds.X > 0 && _caretBounds.X >= textLine.WidthIncludingTrailingWhitespace) { x -= 1; diff --git a/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs b/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs index e4edce964c..c99b210665 100644 --- a/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs +++ b/src/Avalonia.Controls/Primitives/TextSelectionCanvas.cs @@ -262,6 +262,21 @@ namespace Avalonia.Controls.Primitives { if (_presenter == textPresenter) return; + + if (_textBox != null) + { + _textBox.RemoveHandler(TextBox.TextChangingEvent, TextChanged); + _textBox.RemoveHandler(KeyDownEvent, TextBoxKeyDown); + _textBox.RemoveHandler(PointerReleasedEvent, TextBoxPointerReleased); + _textBox.RemoveHandler(Gestures.HoldingEvent, TextBoxHolding); + + _textBox.PropertyChanged -= TextBoxPropertyChanged; + _textBox.EffectiveViewportChanged -= TextBoxEffectiveViewportChanged; + _textBox.SizeChanged -= TextBox_SizeChanged; + + _textBox = null; + } + _presenter = textPresenter; if (_presenter != null) { @@ -279,22 +294,6 @@ namespace Avalonia.Controls.Primitives _textBox.SizeChanged += TextBox_SizeChanged; } } - else - { - if (_textBox != null) - { - _textBox.RemoveHandler(TextBox.TextChangingEvent, TextChanged); - _textBox.RemoveHandler(KeyDownEvent, TextBoxKeyDown); - _textBox.RemoveHandler(PointerReleasedEvent, TextBoxPointerReleased); - _textBox.RemoveHandler(Gestures.HoldingEvent, TextBoxHolding); - - _textBox.PropertyChanged -= TextBoxPropertyChanged; - _textBox.EffectiveViewportChanged -= TextBoxEffectiveViewportChanged; - _textBox.SizeChanged -= TextBox_SizeChanged; - } - - _textBox = null; - } } private void TextBox_SizeChanged(object? sender, SizeChangedEventArgs e)