Skip to content

Commit

Permalink
fix: Avoid text modification after TextChanged is raised
Browse files Browse the repository at this point in the history
Fixes #6289 by removing text native TextBoxView text changes after TextChanged event is raised. This is problematic as it could cause race conditions in case the native view text changes but didn't have yet time to notify the managed code (this occurs for example on GTK).
  • Loading branch information
MartinZikmund committed Feb 7, 2023
1 parent 4092e2f commit f5729d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,31 +272,35 @@ protected virtual void OnTextChanged(DependencyPropertyChangedEventArgs e)
}
}

/// <summary>
/// This is called asynchronously after the UI changes in line with WinUI.
/// Note that no further native text box view text modification should
/// be performed in this method to avoid potential race conditions
/// (see #)
/// </summary>
private void RaiseTextChanged()
{
if (!_isInvokingTextChanged)
if (_isInvokingTextChanged)
{
return;
}

#if !HAS_EXPENSIVE_TRYFINALLY // Try/finally incurs a very large performance hit in mono-wasm - https://github.com/dotnet/runtime/issues/50783
try
try
#endif
{
_isInvokingTextChanged = true;
TextChanged?.Invoke(this, new TextChangedEventArgs(this));
}
{
_isInvokingTextChanged = true;
TextChanged?.Invoke(this, new TextChangedEventArgs(this));
}
#if !HAS_EXPENSIVE_TRYFINALLY // Try/finally incurs a very large performance hit in mono-wasm - https://github.com/dotnet/runtime/issues/50783
finally
finally
#endif
{
_isInvokingTextChanged = false;
_isTextChangedPending = false;
}
{
_isInvokingTextChanged = false;
_isTextChangedPending = false;
}

_textBoxView?.SetTextNative(Text);

}


private void UpdatePlaceholderVisibility()
{
if (_placeHolder != null)
Expand Down
1 change: 1 addition & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ internal void UpdateTextFromNative(string newText)
if (textBox != null)
{
var text = textBox.ProcessTextInput(newText);
DisplayBlock.Text = text;
if (text != newText)
{
SetTextNative(text);
Expand Down

0 comments on commit f5729d9

Please sign in to comment.