From 967decd49bf9be547c963269d908a46598caa305 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Tue, 8 Feb 2022 19:22:34 +0100 Subject: [PATCH] Only call ScrollIntoView when the user moved the caret --- .../Presenters/TextPresenter.cs | 75 +++++++++++-------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index bcb0a9b53b..8629af5243 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -410,38 +410,40 @@ namespace Avalonia.Controls.Presenters internal void CaretChanged() { - if (this.GetVisualParent() != null) + if (this.GetVisualParent() == null) { - if (_caretTimer.IsEnabled) - { - _caretBlink = true; - _caretTimer.Stop(); - _caretTimer.Start(); - InvalidateVisual(); - } - else - { - _caretTimer.Start(); - InvalidateVisual(); - _caretTimer.Stop(); - } + return; + } - if (IsMeasureValid) - { - this.BringIntoView(_caretBounds); - } - else - { - // The measure is currently invalid so there's no point trying to bring the - // current char into view until a measure has been carried out as the scroll - // viewer extents may not be up-to-date. - Dispatcher.UIThread.Post( - () => - { - this.BringIntoView(_caretBounds); - }, - DispatcherPriority.Render); - } + if (_caretTimer.IsEnabled) + { + _caretBlink = true; + _caretTimer.Stop(); + _caretTimer.Start(); + InvalidateVisual(); + } + else + { + _caretTimer.Start(); + InvalidateVisual(); + _caretTimer.Stop(); + } + + if (IsMeasureValid) + { + this.BringIntoView(_caretBounds); + } + else + { + // The measure is currently invalid so there's no point trying to bring the + // current char into view until a measure has been carried out as the scroll + // viewer extents may not be up-to-date. + Dispatcher.UIThread.Post( + () => + { + this.BringIntoView(_caretBounds); + }, + DispatcherPriority.Render); } } @@ -527,6 +529,7 @@ namespace Avalonia.Controls.Presenters private void CaretTimerTick(object? sender, EventArgs e) { _caretBlink = !_caretBlink; + InvalidateVisual(); } @@ -554,6 +557,8 @@ namespace Avalonia.Controls.Presenters } _navigationPosition = _caretBounds.Position; + + CaretChanged(); } public void MoveCaretToPoint(Point point) @@ -563,6 +568,8 @@ namespace Avalonia.Controls.Presenters UpdateCaret(hit.CharacterHit); _navigationPosition = _caretBounds.Position; + + CaretChanged(); } public void MoveCaretVertical(LogicalDirection direction = LogicalDirection.Forward) @@ -604,6 +611,8 @@ namespace Avalonia.Controls.Presenters MoveCaretToPoint(new Point(currentX, currentY)); _navigationPosition = navigationPosition.WithY(_caretBounds.Y); + + CaretChanged(); } public void MoveCaretHorizontal(LogicalDirection direction = LogicalDirection.Forward) @@ -691,6 +700,8 @@ namespace Avalonia.Controls.Presenters UpdateCaret(characterHit); _navigationPosition = _caretBounds.Position; + + CaretChanged(); } private void UpdateCaret(CharacterHit characterHit) @@ -720,9 +731,7 @@ namespace Avalonia.Controls.Presenters CaretBoundsChanged?.Invoke(this, EventArgs.Empty); } - - CaretChanged(); - + SetAndRaise(CaretIndexProperty, ref _caretIndex, caretIndex); }