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 24 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,8 @@ 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 });

Add (completedContainer);
Add (textContainer);
Add (textChangedContainer);
Expand All @@ -46,6 +48,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,8 @@ 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 +89,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(nameof(MaxLength), typeof(int), typeof(int), int.MaxValue);

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
28 changes: 28 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,28 @@ 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());

var currentControlText = Control?.Text;

if (currentControlText.Length > Element.MaxLength)
Control.Text = currentControlText.Substring(0, Element.MaxLength);
}
}
}
34 changes: 29 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,27 @@ 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());

var currentControlText = Control?.Text;

if (currentControlText.Length > Element.MaxLength)
Control.Text = currentControlText.Substring(0, Element.MaxLength);
}
}
}
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
15 changes: 15 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,28 @@ public ScrolledTextView()
WrapMode = WrapMode.WordChar
};

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

public TextView TextView => _textView;

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

if (_textView.Buffer.CharCount > maxLength)
_textView.Buffer.Text = _textView.Buffer.Text.Substring(0, 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);
}
}
}
13 changes: 13 additions & 0 deletions Xamarin.Forms.Platform.MacOS/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
UpdateFont();
UpdateTextColor();
UpdateEditable();
UpdateMaxLength();
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
Expand All @@ -59,6 +60,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 SetBackgroundColor(Color color)
Expand Down Expand Up @@ -88,6 +91,8 @@ protected override void Dispose(bool disposing)

void HandleChanged(object sender, EventArgs e)
{
UpdateMaxLength();

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

Expand Down Expand Up @@ -124,5 +129,13 @@ void UpdateTextColor()

Control.TextColor = textColor.IsDefault ? NSColor.Black : textColor.ToNSColor();
}

void UpdateMaxLength()
{
var currentControlText = Control?.StringValue;

if (currentControlText.Length > Element?.MaxLength)
Control.StringValue = currentControlText.Substring(0, Element.MaxLength);
}
}
}
Loading