Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Entry/Win] Fixed cursor jumping to the beginning of a PasswordBox #6841

Merged
merged 14 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.Controls.Platform
Expand All @@ -8,7 +9,10 @@ internal static class TextBoxExtensions
{
public static void UpdateText(this TextBox platformControl, InputView inputView)
{
platformControl.Text = TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform);
if (platformControl is MauiPasswordTextBox passwordBox)
passwordBox.Password = TextTransformUtilites.GetTransformedText(inputView.Text, passwordBox.IsPassword ? TextTransform.None : inputView.TextTransform);
else
platformControl.Text = TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform);
}
}

Expand Down
63 changes: 37 additions & 26 deletions src/Core/src/Platform/Windows/MauiPasswordTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ static void OnPasswordPropertyChanged(DependencyObject dependencyObject, Depende
bool _cachedSpellCheckSetting;
CancellationTokenSource? _cts;
bool _internalChangeFlag;
int _cachedCursorPosition;
int _cachedTextLength;

public MauiPasswordTextBox()
{
TextChanging += OnNativeTextChanging;
TextChanged += OnNativeTextChanged;
}

Expand Down Expand Up @@ -132,6 +135,29 @@ protected override void OnKeyDown(KeyRoutedEventArgs e)
base.OnKeyDown(e);
}

private void OnNativeTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
// As we are obfuscating the text by ourselves, transforming the text, or a user could be using a custom Converter;
// we are setting the Text property directly on code many times.
// This causes that we invoke the SelectionChanged event many times with SelectionStart = 0, setting the cursor to
// the beginning of the TextBox.
// To avoid this behavior let's save the current cursor position of the first time the Text is updated by the user
// and keep the same cursor position after each Text update until a new Text update by the user happens.
var updatedPassword = DetermineTextFromPassword(Password, SelectionStart, Text);

if (Password != updatedPassword)
{
_cachedCursorPosition = SelectionStart;
_cachedTextLength = updatedPassword.Length;
}
else
{
// Recalculate the cursor position, as the Text could be modified by a user Converter
_cachedCursorPosition += (updatedPassword.Length - _cachedTextLength);
SelectionStart = _cachedCursorPosition;
}
}

void OnNativeTextChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
if (IsPassword)
Expand Down Expand Up @@ -161,11 +187,7 @@ void UpdateVisibleText()
var updatedText = IsPassword ? Obfuscate(Password) : Password;

if (Text != updatedText)
{
var savedSelectionStart = SelectionStart;
Text = updatedText;
SelectionStart = savedSelectionStart;
}
}

void UpdateInputScope()
Expand Down Expand Up @@ -198,27 +220,14 @@ void UpdateInputScope()

void ImmediateObfuscation()
{
var updatedPassword = DetermineTextFromPassword(Password, SelectionStart, Text);
var updatedVisibleText = Obfuscate(updatedPassword);

if (Password != updatedPassword)
Password = updatedPassword;

if (Text != updatedVisibleText)
{
var savedSelectionStart = SelectionStart;
Text = updatedVisibleText;
SelectionStart = savedSelectionStart;
}
UpdatePasswordIfNeeded();
}

void DelayObfuscation()
{
var lengthDifference = Text.Length - Password.Length;
var updatedPassword = DetermineTextFromPassword(Password, SelectionStart, Text);

if (Password != updatedPassword)
Password = updatedPassword;
UpdatePasswordIfNeeded();

// Cancel any pending delayed obfuscation
_cts?.Cancel();
Expand All @@ -244,11 +253,7 @@ void DelayObfuscation()
}

if (Text != updatedVisibleText)
{
var savedSelectionStart = SelectionStart;
Text = updatedVisibleText;
SelectionStart = savedSelectionStart;
}

void StartTimeout(CancellationToken token)
{
Expand All @@ -260,14 +265,20 @@ void StartTimeout(CancellationToken token)

DispatcherQueue.TryEnqueue(UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
var savedSelectionStart = SelectionStart;
Text = Obfuscate(Password);
SelectionStart = savedSelectionStart;
UpdateVisibleText();
});
}, token);
}
}

void UpdatePasswordIfNeeded()
{
var updatedPassword = DetermineTextFromPassword(Password, SelectionStart, Text);

if (Password != updatedPassword)
Password = updatedPassword;
}

static string Obfuscate(string text, bool leaveLastVisible = false)
{
if (string.IsNullOrEmpty(text))
Expand Down