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

Implemented MaxLength property on Entry and Editor #1880

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
cd6585f
Implemented UAP and WPF
jfversluis Feb 3, 2018
b02f8a8
Revert "Implemented UAP and WPF"
jfversluis Feb 3, 2018
2ab7fb4
Started implementation of #1663
jfversluis Feb 1, 2018
6e44c7c
Implemented iOS Editor
jfversluis Feb 2, 2018
4c75dfd
Improved Android MaxLength
jfversluis Feb 2, 2018
c2301aa
Implemented GTK naming enhancement for iOS
jfversluis Feb 2, 2018
c9283bc
Implemented UAP and WPF
jfversluis Feb 3, 2018
87d5a03
Implemented Tizen
jfversluis Feb 3, 2018
1af5e74
Removed Linq and implemented forgotten method 😅
jfversluis Feb 5, 2018
1d002b9
Fixed whitespaces -> tabs
jfversluis Feb 6, 2018
47560bd
Added null guard for iOS
jfversluis Feb 6, 2018
43eacf0
Implemented Mac OS
jfversluis Feb 6, 2018
c037575
Added samples to gallery
jfversluis Feb 6, 2018
07ad020
Polished Tizen implementation
jfversluis Feb 8, 2018
d919eba
Tizen EditorRenderer updated and converted more whitespaces to tabs
jfversluis Feb 10, 2018
947699f
Merge branch 'master' into feature/1663-entry-maxlength
jfversluis Feb 14, 2018
c41f0ba
Fixed spaces to tabs and usage of nameof
jfversluis Feb 15, 2018
8983dcc
Added trimming of current text when MaxLength is less than current va…
jfversluis Feb 15, 2018
a03182a
Reference right Entry for Tizen
jfversluis Feb 15, 2018
0263892
Trimming on MaxLength change for Tizen
jfversluis Feb 15, 2018
f5f7a26
Trimming on MaxLength change for GTK
jfversluis Feb 15, 2018
da2a571
Removed redundant GetValue calls and whitespace fiesta
jfversluis Feb 15, 2018
d78f007
And the ones I missed
jfversluis Feb 15, 2018
416e287
Updated the docs
jfversluis Feb 16, 2018
009272e
Merge branch 'master' into feature/1663-entry-maxlength
jfversluis Feb 16, 2018
3b3fc3a
Revert "Updated the docs"
jfversluis Feb 16, 2018
db6461a
Updated docs just for InputView
jfversluis Feb 16, 2018
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
Expand Up @@ -35,6 +35,9 @@ protected override void Build (StackLayout stackLayout)
var textColorDisabledContainer = new ViewContainer<Editor> (Test.Editor.TextColor,
new Editor { Text = "I should have the default disabled text color", TextColor = Color.Red, IsEnabled = false });

var maxLengthContainer = new ViewContainer<Editor>(Test.Editor.MaxLength,
new Editor { MaxLength = 3 });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funky whitespace for fun and profit :)


Add (completedContainer);
Add (textContainer);
Add (textChangedContainer);
Expand All @@ -46,6 +49,7 @@ protected override void Build (StackLayout stackLayout)
Add (textFontSizeLargeContainer);
Add (textColorContainer);
Add (textColorDisabledContainer);
Add (maxLengthContainer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ protected override void Build (StackLayout stackLayout)
var passwordColorContainer = new ViewContainer<Entry> (Test.Entry.PasswordColor,
new Entry { IsPassword = true, Text = "12345", TextColor = Color.Red });

var maxLengthContainer = new ViewContainer<Entry>(Test.Entry.MaxLength,
new Entry { MaxLength = 3 });

Add (isPasswordContainer);
Add (completedContainer);
Add (placeholderContainer);
Expand All @@ -87,6 +90,7 @@ protected override void Build (StackLayout stackLayout)
Add (textColorDisabledContainer);
Add (placeholderColorDisabledContainer);
Add (passwordColorContainer);
Add (maxLengthContainer);
}
}
}
8 changes: 8 additions & 0 deletions Xamarin.Forms.Core/InputView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ public class InputView : View
public static readonly BindableProperty KeyboardProperty = BindableProperty.Create("Keyboard", typeof(Keyboard), typeof(InputView), Keyboard.Default,
coerceValue: (o, v) => (Keyboard)v ?? Keyboard.Default);

public static readonly BindableProperty MaxLengthProperty = BindableProperty.Create("MaxLength", typeof(int), typeof(int), int.MaxValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use -1 as default value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use nameof(MaxLength)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we usually use tabs for indentation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the #1663 we sort of decided to go with int.MaxValue. What would be the benefit of using -1?
Fixed nameof and tabs.

Copy link
Member

@StephaneDelcroix StephaneDelcroix Feb 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll discuss the defaultValue with @hartez. both are fine, both make sense. But having Int32.MaxValue prevents us from having a really long text in, while -1 does not.


public int MaxLength
{
get { return (int)GetValue(MaxLengthProperty); }
set { SetValue(MaxLengthProperty, value); }
}

internal InputView()
{
}
Expand Down
6 changes: 4 additions & 2 deletions Xamarin.Forms.CustomAttributes/TestAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ public enum Editor
TextColor,
FontAttributes,
FontFamily,
FontSize
FontSize,
MaxLength
}

public enum Entry
Expand All @@ -521,7 +522,8 @@ public enum Entry
PlaceholderColor,
TextDisabledColor,
PlaceholderDisabledColor,
PasswordColor
PasswordColor,
MaxLength
}

public enum Frame
Expand Down
23 changes: 23 additions & 0 deletions Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Android.Content;
using Android.Content.Res;
using Android.OS;
Expand Down Expand Up @@ -82,6 +84,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
UpdateInputType();
UpdateTextColor();
UpdateFont();
UpdateMaxLength();
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
Expand All @@ -98,6 +101,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateFont();
else if (e.PropertyName == Editor.FontSizeProperty.PropertyName)
UpdateFont();
else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName)
UpdateMaxLength();

base.OnElementPropertyChanged(sender, e);
}
Expand Down Expand Up @@ -177,5 +182,23 @@ void OnKeyboardBackPressed(object sender, EventArgs eventArgs)
ElementController?.SendCompleted();
Control?.ClearFocus();
}

void UpdateMaxLength()
{
var currentFilters = new List<IInputFilter>(Control?.GetFilters() ?? new IInputFilter[0]);

for (var i = 0; i < currentFilters.Count; i++)
{
if (currentFilters[i] is InputFilterLengthFilter)
{
currentFilters.RemoveAt(i);
break;
}
}

currentFilters.Add(new InputFilterLengthFilter(Element.MaxLength));

Control?.SetFilters(currentFilters.ToArray());
}
}
}
29 changes: 24 additions & 5 deletions Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Android.Content;
using Android.Content.Res;
Expand Down Expand Up @@ -83,8 +84,6 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
_textColorSwitcher = new TextColorSwitcher(textView.TextColors, useLegacyColorManagement);
_hintColorSwitcher = new TextColorSwitcher(textView.HintTextColors, useLegacyColorManagement);



SetNativeControl(textView);
}

Expand All @@ -96,6 +95,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
UpdateAlignment();
UpdateFont();
UpdatePlaceholderColor();
UpdateMaxLength();
}

protected override void Dispose(bool disposing)
Expand All @@ -117,7 +117,7 @@ protected override void Dispose(bool disposing)

base.Dispose(disposing);
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Entry.PlaceholderProperty.PropertyName)
Expand Down Expand Up @@ -152,14 +152,16 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdatePlaceholderColor();
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateAlignment();
else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName)
UpdateMaxLength();

base.OnElementPropertyChanged(sender, e);
}

protected virtual NumberKeyListener GetDigitsKeyListener(InputTypes inputTypes)
{
// Override this in a custom renderer to use a different NumberKeyListener
// or to filter out input types you don't want to allow
// Override this in a custom renderer to use a different NumberKeyListener
// or to filter out input types you don't want to allow
// (e.g., inputTypes &= ~InputTypes.NumberFlagSigned to disallow the sign)
return LocalizedDigitsKeyListener.Create(inputTypes);
}
Expand Down Expand Up @@ -207,5 +209,22 @@ void OnKeyboardBackPressed(object sender, EventArgs eventArgs)
{
Control?.ClearFocus();
}
void UpdateMaxLength()
{
var currentFilters = new List<IInputFilter>(Control?.GetFilters() ?? new IInputFilter[0]);

for (var i = 0; i < currentFilters.Count; i++)
{
if (currentFilters[i] is InputFilterLengthFilter)
{
currentFilters.RemoveAt(i);
break;
}
}

currentFilters.Add(new InputFilterLengthFilter(Element.MaxLength));

Control?.SetFilters(currentFilters.ToArray());
}
}
}
5 changes: 5 additions & 0 deletions Xamarin.Forms.Platform.GTK/Controls/EntryWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public void SetFont(FontDescription fontDescription)
_placeholder.ModifyFont(fontDescription);
}

public void SetMaxLength(int maxLength)
{
_entry.MaxLength = maxLength;
}

protected override void OnSizeAllocated(Gdk.Rectangle allocation)
{
base.OnSizeAllocated(allocation);
Expand Down
12 changes: 12 additions & 0 deletions Xamarin.Forms.Platform.GTK/Controls/ScrolledTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Xamarin.Forms.Platform.GTK.Controls
public class ScrolledTextView : ScrolledWindow
{
private TextView _textView;
private int _maxLength;

public ScrolledTextView()
{
Expand All @@ -18,14 +19,25 @@ public ScrolledTextView()
WrapMode = WrapMode.WordChar
};

_textView.Buffer.InsertText += InsertText;
Add(_textView);
}

public TextView TextView => _textView;

public void SetMaxLength(int maxLength)
{
_maxLength = maxLength;
}

protected override void OnFocusGrabbed()
{
_textView?.GrabFocus();
}

void InsertText(object o, InsertTextArgs args)
{
args.RetVal = args.Length <= _maxLength;
}
}
}
8 changes: 8 additions & 0 deletions Xamarin.Forms.Platform.GTK/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
UpdateText();
UpdateFont();
UpdateTextColor();
UpdateMaxLength();
}

base.OnElementChanged(e);
Expand All @@ -66,6 +67,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateFont();
else if (e.PropertyName == Editor.FontSizeProperty.PropertyName)
UpdateFont();
else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName)
UpdateMaxLength();
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -153,5 +156,10 @@ private static void AdjustMinimumHeight(TextView textView, FontDescription font
textView.HeightRequest = minHeight;
}
}

private void UpdateMaxLength()
{
Control.SetMaxLength(Element.MaxLength);
}
}
}
9 changes: 9 additions & 0 deletions Xamarin.Forms.Platform.GTK/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Xamarin.Forms.Platform.GTK.Controls;
using Xamarin.Forms.Platform.GTK.Extensions;
using Xamarin.Forms.Platform.GTK.Helpers;
using System;

namespace Xamarin.Forms.Platform.GTK.Renderers
{
Expand Down Expand Up @@ -35,6 +36,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
UpdateFont();
UpdateTextVisibility();
UpdatePlaceholder();
UpdateMaxLength();
}

base.OnElementChanged(e);
Expand All @@ -60,6 +62,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdatePlaceholder();
else if (e.PropertyName == Entry.PlaceholderColorProperty.PropertyName)
UpdatePlaceholder();
else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName)
UpdateMaxLength();

base.OnElementPropertyChanged(sender, e);
}
Expand Down Expand Up @@ -148,5 +152,10 @@ private void OnKeyReleased(object o, KeyReleaseEventArgs args)
EntryController.SendCompleted();
}
}

private void UpdateMaxLength()
{
Control.SetMaxLength(Element.MaxLength);
}
}
}
3 changes: 3 additions & 0 deletions Xamarin.Forms.Platform.MacOS/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ protected override void Dispose(bool disposing)

void HandleChanged(object sender, EventArgs e)
{
if (Control?.StringValue?.Length > Element?.MaxLength)
Control.StringValue = Control.StringValue.Substring(0, Element.MaxLength);

ElementController.SetValueFromRenderer(Editor.TextProperty, Control.StringValue);
}

Expand Down
3 changes: 3 additions & 0 deletions Xamarin.Forms.Platform.MacOS/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ void OnEditingBegan(object sender, EventArgs e)

void OnChanged(object sender, EventArgs eventArgs)
{
if (Control?.StringValue?.Length > Element?.MaxLength)
Control.StringValue = Control.StringValue.Substring(0, Element.MaxLength);

ElementController.SetValueFromRenderer(Entry.TextProperty, Control.StringValue);
}

Expand Down
10 changes: 10 additions & 0 deletions Xamarin.Forms.Platform.Tizen/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
entry.Focused += OnFocused;
entry.Unfocused += OnUnfocused;
entry.TextChanged += OnTextChanged;
entry.Unfocused += OnCompleted;
entry.PrependMarkUpFilter(MaxLengthFilter);

SetNativeControl(entry);
}
Expand Down Expand Up @@ -115,5 +117,13 @@ void UpdateKeyboard(bool initialize)

Control.Keyboard = Element.Keyboard.ToNative();
}

string MaxLengthFilter(Entry entry, string s)
{
if (entry.Text.Length < Element.MaxLength)
return s;

return null;
}
}
}
10 changes: 10 additions & 0 deletions Xamarin.Forms.Platform.Tizen/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using ElmSharp;
using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Entry;

namespace Xamarin.Forms.Platform.Tizen
Expand Down Expand Up @@ -36,6 +37,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
entry.SetVerticalTextAlignment("elm.guide", 0.5);
entry.TextChanged += OnTextChanged;
entry.Activated += OnCompleted;
entry.PrependMarkUpFilter(MaxLengthFilter);
SetNativeControl(entry);
}
base.OnElementChanged(e);
Expand Down Expand Up @@ -129,5 +131,13 @@ void UpdateFontWeight()
{
Control.FontWeight = Specific.GetFontWeight(Element);
}

string MaxLengthFilter(Entry entry, string s)
{
if (entry.Text.Length < Element.MaxLength)
return s;

return null;
}
}
}
Loading