Skip to content

Commit

Permalink
xamarin#2223 added possibility to change IsPassword for mac
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Oct 21, 2018
1 parent ded3d0f commit e467e57
Showing 1 changed file with 54 additions and 38 deletions.
92 changes: 54 additions & 38 deletions Xamarin.Forms.Platform.MacOS/Renderers/EntryRenderer.cs
Expand Up @@ -95,33 +95,12 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)

if (Control == null)
{
NSTextField textField;
if (e.NewElement.IsPassword)
textField = new NSSecureTextField();
else
{
textField = new FormsNSTextField();
(textField as FormsNSTextField).FocusChanged += TextFieldFocusChanged;
(textField as FormsNSTextField).Completed += OnCompleted;
}

SetNativeControl(textField);

_defaultTextColor = textField.TextColor;

textField.Changed += OnChanged;
textField.EditingBegan += OnEditingBegan;
textField.EditingEnded += OnEditingEnded;
CreateControl();
}

if (e.NewElement != null)
{
UpdatePlaceholder();
UpdateText();
UpdateColor();
UpdateFont();
UpdateAlignment();
UpdateMaxLength();
UpdateControl();
}
}

Expand Down Expand Up @@ -171,22 +150,59 @@ protected override void Dispose(bool disposing)
if (disposing && !_disposed)
{
_disposed = true;
if (Control != null)
ClearControl();
}

base.Dispose(disposing);
}

void CreateControl()
{
NSTextField textField;
if (Element.IsPassword)
textField = new NSSecureTextField();
else
{
textField = new FormsNSTextField();
(textField as FormsNSTextField).FocusChanged += TextFieldFocusChanged;
(textField as FormsNSTextField).Completed += OnCompleted;
}

SetNativeControl(textField);

_defaultTextColor = textField.TextColor;

textField.Changed += OnChanged;
textField.EditingBegan += OnEditingBegan;
textField.EditingEnded += OnEditingEnded;
}

void ClearControl()
{
if (Control != null)
{
Control.EditingBegan -= OnEditingBegan;
Control.Changed -= OnChanged;
Control.EditingEnded -= OnEditingEnded;
var formsNSTextField = (Control as FormsNSTextField);
if (formsNSTextField != null)
{
Control.EditingBegan -= OnEditingBegan;
Control.Changed -= OnChanged;
Control.EditingEnded -= OnEditingEnded;
var formsNSTextField = (Control as FormsNSTextField);
if (formsNSTextField != null)
{
formsNSTextField.FocusChanged -= TextFieldFocusChanged;
formsNSTextField.Completed -= OnCompleted;
}
formsNSTextField.FocusChanged -= TextFieldFocusChanged;
formsNSTextField.Completed -= OnCompleted;
}
}
}

base.Dispose(disposing);
void UpdateControl()
{
UpdatePlaceholder();
UpdateText();
UpdateColor();
UpdateFont();
UpdateAlignment();
UpdateMaxLength();
}

void TextFieldFocusChanged(object sender, BoolEventArgs e)
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, e.Value);
Expand Down Expand Up @@ -231,10 +247,10 @@ void UpdateColor()

void UpdatePassword()
{
if (Element.IsPassword && (Control is NSSecureTextField))
return;
if (!Element.IsPassword && !(Control is NSSecureTextField))
return;
ClearControl();
CreateControl();
UpdateControl();
Layout();
}

void UpdateFont()
Expand Down

0 comments on commit e467e57

Please sign in to comment.