Skip to content

Commit

Permalink
feat: (macOS) Add IsPasswordRevealButtonEnabled for PasswordBox
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpinedam committed May 4, 2020
1 parent 7eee1ef commit b249253
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public string PasswordChar
#endif
// Skipping already declared property Password
// Skipping already declared property MaxLength
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || false
[global::Uno.NotImplemented]
public bool IsPasswordRevealButtonEnabled
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public bool CanPasteClipboardContent
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __MACOS__
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || false
[global::Uno.NotImplemented]
public static global::Windows.UI.Xaml.DependencyProperty IsPasswordRevealButtonEnabledProperty { get; } =
Windows.UI.Xaml.DependencyProperty.Register(
Expand Down
48 changes: 43 additions & 5 deletions src/Uno.UI/UI/Xaml/Controls/PasswordBox/PasswordBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ private void RegisterSetPasswordScope()
_revealButton.RemoveHandler(PointerCanceledEvent, endReveal);
_revealButton.RemoveHandler(PointerCaptureLostEvent, endReveal);
});

#if __MACOS__
IsPasswordRevealButtonEnabled = false;
#endif
}

SetPasswordScope(true);
Expand Down Expand Up @@ -103,11 +107,7 @@ private void OnPasswordChanged(DependencyPropertyChangedEventArgs e)
{
Text = (string)e.NewValue;

var handler = PasswordChanged;
if (handler != null)
{
handler(this, new RoutedEventArgs(this));
}
PasswordChanged?.Invoke(this, new RoutedEventArgs(this));

OnPasswordChangedPartial(e);
}
Expand All @@ -133,5 +133,43 @@ public override string GetAccessibilityInnerText()
// We don't want to reveal the password
return null;
}

public bool IsPasswordRevealButtonEnabled
{
get => (bool)this.GetValue(IsPasswordRevealButtonEnabledProperty);
set
{
this.SetValue(IsPasswordRevealButtonEnabledProperty, value);
}
}

public static readonly DependencyProperty IsPasswordRevealButtonEnabledProperty =
DependencyProperty.Register(
nameof(IsPasswordRevealButtonEnabled),
typeof(bool),
typeof(PasswordBox),
new PropertyMetadata(
defaultValue: true,
propertyChangedCallback: (s, e) => ((PasswordBox)s)?.OnIsPasswordRevealButtonEnabledChanged(e)
)
);

private void OnIsPasswordRevealButtonEnabledChanged(DependencyPropertyChangedEventArgs e)
{
_isButtonEnabled = IsPasswordRevealButtonEnabled;
UpdateButtonStates();
}

private void UpdateButtonStates()
{
if(IsPasswordRevealButtonEnabled)
{
VisualStateManager.GoToState(this, "ButtonVisible", true);
}
else
{
VisualStateManager.GoToState(this, "ButtonCollapsed", true);
}
}
}
}
4 changes: 3 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public partial class TextBox : Control, IFrameworkTemplatePoolAware

private ContentPresenter _header;
private bool _isPassword;
protected bool _isButtonEnabled = true;

public event TextChangedEventHandler TextChanged;
public event TypedEventHandler<TextBox, TextBoxTextChangingEventArgs> TextChanging;
Expand Down Expand Up @@ -656,7 +657,8 @@ private void UpdateButtonStates()
this.Log().LogDebug(nameof(UpdateButtonStates));
}

if (Text.HasValue()
if (_isButtonEnabled
&& Text.HasValue()
&& FocusState != FocusState.Unfocused
&& !IsReadOnly
&& !AcceptsReturn
Expand Down

0 comments on commit b249253

Please sign in to comment.