From 7f221b321f4b057dd928b3a4f79083fbdaa3c207 Mon Sep 17 00:00:00 2001 From: robloo Date: Wed, 12 Oct 2022 22:49:11 -0400 Subject: [PATCH] Raise text change events if set through the TextBox.Text property --- src/Avalonia.Controls/TextBox.cs | 49 +++++++++++++------ .../TextBoxTextInputMethodClient.cs | 2 +- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index bace6c7811..da4e90fb66 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -400,11 +400,19 @@ namespace Avalonia.Controls CaretIndex = CoerceCaretIndex(caretIndex, value); SelectionStart = CoerceCaretIndex(selectionStart, value); SelectionEnd = CoerceCaretIndex(selectionEnd, value); - if (SetAndRaise(TextProperty, ref _text, value) && IsUndoEnabled && !_isUndoingRedoing) + + var textChanged = SetAndRaise(TextProperty, ref _text, value); + + if (textChanged && IsUndoEnabled && !_isUndoingRedoing) { _undoRedoHelper.Clear(); SnapshotUndoRedo(); // so we always have an initial state } + + if (textChanged) + { + RaiseTextChangeEvents(); + } } } @@ -1297,7 +1305,7 @@ namespace Avalonia.Controls if (text != null && _wordSelectionStart >= 0) { - var distance = caretIndex - _wordSelectionStart; + var distance = caretIndex - _wordSelectionStart; if (distance <= 0) { @@ -1584,6 +1592,29 @@ namespace Avalonia.Controls return text.Substring(start, end - start); } + /// + /// Raises both the and events. + /// + /// + /// This must be called after the property is set. + /// + private void RaiseTextChangeEvents() + { + // Note the following sequence of these events (following WinUI) + // 1. TextChanging occurs synchronously when text starts to change but before it is rendered. + // This occurs after the Text property is set. + // 2. TextChanged occurs asynchronously after text changes and the new text is rendered. + + var textChangingEventArgs = new TextChangingEventArgs(TextChangingEvent); + RaiseEvent(textChangingEventArgs); + + Dispatcher.UIThread.Post(() => + { + var textChangedEventArgs = new TextChangedEventArgs(TextChangedEvent); + RaiseEvent(textChangedEventArgs); + }, DispatcherPriority.Normal); + } + private void SetTextInternal(string value, bool raiseTextChanged = true) { if (raiseTextChanged) @@ -1592,19 +1623,7 @@ namespace Avalonia.Controls if (textChanged) { - // Note the following sequence of these events (following WinUI) - // 1. TextChanging occurs synchronously when text starts to change but before it is rendered. - // This occurs after the Text property is set. - // 2. TextChanged occurs asynchronously after text changes and the new text is rendered. - - var textChangingEventArgs = new TextChangingEventArgs(TextChangingEvent); - RaiseEvent(textChangingEventArgs); - - Dispatcher.UIThread.Post(() => - { - var textChangedEventArgs = new TextChangedEventArgs(TextChangedEvent); - RaiseEvent(textChangedEventArgs); - }, DispatcherPriority.Normal); + RaiseTextChangeEvents(); } } else diff --git a/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs b/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs index d39d964277..5d5ffcc381 100644 --- a/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs +++ b/src/Avalonia.Controls/TextBoxTextInputMethodClient.cs @@ -64,7 +64,7 @@ namespace Avalonia.Controls return new TextInputMethodSurroundingText { - Text = lineText ?? "", + Text = lineText ?? "", AnchorOffset = anchorOffset, CursorOffset = cursorOffset };