From 4e02fbaa3d56689a5318c418db36e5839ddaeb03 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 19 Aug 2021 13:23:26 +0200 Subject: [PATCH] fix: Correctly check if the TextBox is PasswordBox befo re obscuring text --- .../UI/Xaml/Controls/TextBoxViewExtension.cs | 23 ++++++++++++------- .../Xaml/Controls/TextBox/TextBoxView.skia.cs | 22 ++++++++++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/Uno.UI.Runtime.Skia.Gtk/UI/Xaml/Controls/TextBoxViewExtension.cs b/src/Uno.UI.Runtime.Skia.Gtk/UI/Xaml/Controls/TextBoxViewExtension.cs index 211fa6779fcc..1660d62a50b7 100644 --- a/src/Uno.UI.Runtime.Skia.Gtk/UI/Xaml/Controls/TextBoxViewExtension.cs +++ b/src/Uno.UI.Runtime.Skia.Gtk/UI/Xaml/Controls/TextBoxViewExtension.cs @@ -53,8 +53,7 @@ public void StartEntry() } _contentElement = textBox.ContentElement; - - EnsureWidgetForAcceptsReturn(textBox.AcceptsReturn, isPassword: textBox is PasswordBox); + EnsureWidget(textBox); var textInputLayer = GetWindowTextInputLayer(); textInputLayer.Put(_currentInputWidget!, 0, 0); @@ -108,7 +107,7 @@ public void UpdateNativeView() return; } - EnsureWidgetForAcceptsReturn(textBox.AcceptsReturn, isPassword: textBox is PasswordBox); + EnsureWidget(textBox); var fontDescription = new FontDescription { @@ -173,12 +172,20 @@ private void UpdateEntryProperties(Entry entry, TextBox textBox) entry.MaxLength = textBox.MaxLength; } - private void EnsureWidgetForAcceptsReturn(bool acceptsReturn, bool isPassword) + private void EnsureWidget(TextBox textBox) { + var isPassword = false; + var isPasswordVisible = true; + if (textBox is PasswordBox passwordBox) + { + isPassword = true; + isPasswordVisible = passwordBox.PasswordRevealMode == PasswordRevealMode.Visible; + } + // On UWP, A PasswordBox doesn't have AcceptsReturn property. // The property exists on Uno because PasswordBox incorrectly inherits TextBox. // If we have PasswordBox, ignore AcceptsReturnValue and always use Gtk.Entry - acceptsReturn = acceptsReturn && !isPassword; + var acceptsReturn = textBox.AcceptsReturn && !isPassword; var isIncompatibleInputType = (acceptsReturn && !(_currentInputWidget is TextView)) || @@ -186,12 +193,12 @@ private void EnsureWidgetForAcceptsReturn(bool acceptsReturn, bool isPassword) if (isIncompatibleInputType) { var inputText = GetInputText(); - _currentInputWidget = CreateInputWidget(acceptsReturn, isPassword); + _currentInputWidget = CreateInputWidget(acceptsReturn, isPassword, isPasswordVisible); SetWidgetText(inputText ?? string.Empty); } } - private Widget CreateInputWidget(bool acceptsReturn, bool isPassword) + private Widget CreateInputWidget(bool acceptsReturn, bool isPassword, bool isPasswordVisible) { Debug.Assert(!acceptsReturn || !isPassword); Widget widget; @@ -208,7 +215,7 @@ private Widget CreateInputWidget(bool acceptsReturn, bool isPassword) if (isPassword) { entry.InputPurpose = InputPurpose.Password; - entry.Visibility = false; + entry.Visibility = isPasswordVisible; } entry.Changed += WidgetTextChanged; diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.skia.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.skia.cs index fe641aeb06f0..bf23c23eef2f 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.skia.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.skia.cs @@ -14,10 +14,13 @@ internal class TextBoxView private readonly ITextBoxViewExtension _textBoxExtension; private readonly WeakReference _textBox; + private readonly bool _isPasswordBox; + private bool _isPasswordRevealed; public TextBoxView(TextBox textBox) { _textBox = new WeakReference(textBox); + _isPasswordBox = textBox is PasswordBox; if (!ApiExtensibility.CreateInstance(this, out _textBoxExtension)) { if (this.Log().IsEnabled(LogLevel.Warning)) @@ -45,7 +48,18 @@ public TextBox? TextBox internal void SetTextNative(string text) { - DisplayBlock.Text = new string('•', text.Length); + // TODO: Inheritance hierarchy is wrong in Uno. PasswordBox shouldn't inherit TextBox. + // This needs to be moved to PasswordBox when it's separated from TextBox (likely in Uno 4). + if (_isPasswordBox && !_isPasswordRevealed) + { + // TODO: PasswordChar isn't currently implemented. It should be used here when implemented. + DisplayBlock.Text = new string('•', text.Length); + } + else + { + DisplayBlock.Text = text; + } + _textBoxExtension?.SetTextNative(text); } @@ -65,7 +79,11 @@ internal void OnFocusStateChanged(FocusState focusState) } } - internal void SetIsPassword(bool isPassword) => _textBoxExtension?.SetIsPassword(isPassword); + internal void SetIsPassword(bool isPassword) + { + _isPasswordRevealed = !isPassword; + _textBoxExtension?.SetIsPassword(isPassword); + } internal void UpdateTextFromNative(string newText) {