Skip to content

Commit

Permalink
fix: [iOS] Do not clear text on next key stroke after reveal
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Dec 17, 2021
1 parent 6d3d96c commit a7e98bd
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/SinglelineTextBoxView.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public partial class SinglelineTextBoxView : UITextField, ITextBoxView, Dependen
{
private SinglelineTextBoxDelegate _delegate;
private readonly WeakReference<TextBox> _textBox;
private readonly SerialDisposable _foregroundChanged = new SerialDisposable();
private readonly SerialDisposable _foregroundChanged = new();

private string _restoreOnNextKeyStroke;

public SinglelineTextBoxView(TextBox textBox)
{
Expand All @@ -28,22 +30,34 @@ public SinglelineTextBoxView(TextBox textBox)
Initialize();
}

private void OnEditingChanged(object sender, EventArgs e)
/// <inheritdoc />
public override bool SecureTextEntry
{
OnTextChanged();
get => base.SecureTextEntry;
set
{
if (base.SecureTextEntry != value)
{
if (value)
{
// When we enable the "secure" mode, iOS will auto-magically clear the value on next key stroke
// (Without invoking the "ShouldClear" nor any callback except "DidChangeSelection" multiple times).
// The only way is to keep ref of the current text and restore it on next text change (expected to be an empty string).
_restoreOnNextKeyStroke = base.Text;
}

base.SecureTextEntry = value;
}
}
}

public override string Text
{
get
{
return base.Text;
}

get => base.Text;
set
{
// The native control will ignore a value of null and retain an empty string. We coalesce the null to prevent a spurious empty string getting bounced back via two-way binding.
value = value ?? string.Empty;
value ??= string.Empty;
if (base.Text != value)
{
base.Text = value;
Expand All @@ -52,6 +66,19 @@ public override string Text
}
}

private void OnEditingChanged(object sender, EventArgs e)
{
if (_restoreOnNextKeyStroke is { Length: > 0 } text)
{
base.Text = text + base.Text;
_restoreOnNextKeyStroke = default;
}
else
{
OnTextChanged();
}
}

private void OnTextChanged()
{
var textBox = _textBox?.GetTarget();
Expand Down

0 comments on commit a7e98bd

Please sign in to comment.