Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[Enhancement] Possibility to change IsPassword for MacOS (#4175) fixes
Browse files Browse the repository at this point in the history
…#2223

* #2223 added possibility to change IsPassword for mac

* added test case
  • Loading branch information
AndreiMisiukevich authored and rmarinho committed Oct 25, 2018
1 parent 5b9ad75 commit ef6414e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 38 deletions.
@@ -0,0 +1,49 @@
using System;
using Xamarin.Forms;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using NUnit.Framework;
using Xamarin.UITest;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve (AllMembers = true)]
[Issue (IssueTracker.Github, 2223, "Possibility to change IsPassword", PlatformAffected.macOS)]
public class Issue2223 : TestContentPage
{
protected override void Init ()
{
var checkEntry = new Entry
{
HeightRequest = 100,
FontSize = 50,
Placeholder = "I can be both secure and non-secure"
};
Content = new StackLayout
{
Padding = new Thickness(100),
Children = {
checkEntry,
new Button
{
HeightRequest = 80,
FontSize = 40,
BackgroundColor = Color.LightBlue,
TextColor = Color.Black,
Text = "Click me to change IsPassword of the entry",
Command = new Command(() => checkEntry.IsPassword = !checkEntry.IsPassword)
}
}
};
}

#if UITEST
#endif

}
}


Expand Up @@ -829,6 +829,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue4040.xaml.cs">
<DependentUpon>Issue4040.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue2223.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">
Expand Down
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 ef6414e

Please sign in to comment.