Skip to content

Commit

Permalink
fix: TextBox focus leak on WPF
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Nov 5, 2022
1 parent 4e86e09 commit 709b22f
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Point = Windows.Foundation.Point;
using WpfCanvas = System.Windows.Controls.Canvas;
using Uno.UI.XamlHost.Skia.Wpf.Hosting;
using Uno.Disposables;

namespace Uno.UI.Runtime.Skia.WPF.Extensions.UI.Xaml.Controls
{
Expand All @@ -20,6 +21,8 @@ internal class TextBoxViewExtension : ITextBoxViewExtension
private WpfTextViewTextBox? _currentTextBoxInputWidget;
private System.Windows.Controls.PasswordBox? _currentPasswordBoxInputWidget;

private SerialDisposable _textChangedDisposable = new SerialDisposable();

private readonly bool _isPasswordBox;
private bool _isPasswordRevealed;

Expand Down Expand Up @@ -70,7 +73,7 @@ public void StartEntry()

UpdateNativeView();
SetTextNative(textBox.Text);

ObserveInputChanges();
InvalidateLayout();

if (_isPasswordBox && !_isPasswordRevealed)
Expand All @@ -85,6 +88,7 @@ public void StartEntry()

public void EndEntry()
{
_textChangedDisposable.Disposable = null;
if (_currentTextBoxInputWidget is null)
{
// No entry is in progress.
Expand Down Expand Up @@ -231,7 +235,6 @@ private void EnsureWidgetForAcceptsReturn()
private WpfTextViewTextBox CreateInputControl()
{
var textView = new WpfTextViewTextBox();
textView.TextChanged += WpfTextViewTextChanged;
return textView;
}

Expand All @@ -241,10 +244,27 @@ private System.Windows.Controls.PasswordBox CreatePasswordControl()
passwordBox.BorderBrush = System.Windows.Media.Brushes.Transparent;
passwordBox.Background = System.Windows.Media.Brushes.Transparent;
passwordBox.BorderThickness = new Thickness(0);
passwordBox.PasswordChanged += PasswordBoxViewPasswordChanged;
return passwordBox;
}

private void ObserveInputChanges()
{
_textChangedDisposable.Disposable = null;
CompositeDisposable disposable = new();
if (_currentTextBoxInputWidget is not null)
{
_currentTextBoxInputWidget.TextChanged += WpfTextViewTextChanged;
disposable.Add(Disposable.Create(() => _currentTextBoxInputWidget.TextChanged -= WpfTextViewTextChanged));
}

if (_currentPasswordBoxInputWidget is not null)
{
_currentPasswordBoxInputWidget.PasswordChanged += PasswordBoxViewPasswordChanged;
disposable.Add(Disposable.Create(() => _currentPasswordBoxInputWidget.PasswordChanged -= PasswordBoxViewPasswordChanged));
}
_textChangedDisposable.Disposable = disposable;
}

private void PasswordBoxViewPasswordChanged(object sender, System.Windows.RoutedEventArgs e)
{
_owner.UpdateTextFromNative(_currentPasswordBoxInputWidget!.Password);
Expand Down

0 comments on commit 709b22f

Please sign in to comment.