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

Commit

Permalink
AutoCaptialization support for Entry/Editor (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Feb 7, 2018
1 parent 7e387eb commit adeeedb
Show file tree
Hide file tree
Showing 14 changed files with 484 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;

using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.Generic;

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

namespace Xamarin.Forms.Controls.Issues
{
[Preserve (AllMembers = true)]
[Issue (IssueTracker.Github, 1683, "Auto Capitalization Implementation")]
public class Issue1683 : ContentPage
{
const string kContainerId = "Container";
public Issue1683()
{
var layout = new StackLayout() { ClassId = kContainerId };

List<InputView> intputViews = new List<InputView>()
{
new Entry() { ClassId = "EntryNotSet" },
new Entry() { AutoCapitalization = AutoCapitalization.Default, ClassId = "EntryDefault" },
new Entry() { AutoCapitalization = AutoCapitalization.None, ClassId = "EntryNone" },
new Entry() { AutoCapitalization = AutoCapitalization.Words, ClassId = "EntryWords" },
new Entry() { AutoCapitalization = AutoCapitalization.Sentences, ClassId = "EntrySentences" },
new Entry() { AutoCapitalization = AutoCapitalization.Characters, ClassId = "EntryCharacters" },
new Editor() { ClassId = "EditorNotSet" },
new Editor() { AutoCapitalization = AutoCapitalization.Default, ClassId = "EditorDefault" },
new Editor() { AutoCapitalization = AutoCapitalization.None, ClassId = "EditorNone" },
new Editor() { AutoCapitalization = AutoCapitalization.Words, ClassId = "EditorWords" },
new Editor() { AutoCapitalization = AutoCapitalization.Sentences, ClassId = "EditorSentences" },
new Editor() { AutoCapitalization = AutoCapitalization.Characters, ClassId = "EditorCharacters" },
};


if(Device.RuntimePlatform == Device.UWP)
{
layout.Children.Add(new Label() { Text = "Sentence and Character don't do anything on UWP" });
}

foreach(InputView child in intputViews)
{
var inputs = new StackLayout()
{
Orientation = StackOrientation.Horizontal
};

if(child is Entry)
(child as Entry).Text = "All the Same.";

if(child is Editor)
(child as Editor).Text = "All the Same.";


child.HorizontalOptions = LayoutOptions.FillAndExpand;
var theLabel = new Label();

theLabel.SetBinding(Label.TextProperty, new Binding("ClassId", source: child));
inputs.Children.Add(theLabel);
inputs.Children.Add(child);
layout.Children.Add(inputs);
}

Button rotate = new Button() { Text = "Change Capitalization Settings. Ensure they update correctly" };

// This shifts everyones capitalization by one in order
// to test that updating the field works as expected
rotate.Clicked += (_, __) =>
{
for (int i = 0; i < intputViews.Count; i++)
{
var entry1 = intputViews[i];
var cap1 = ((int)entry1.AutoCapitalization + 1);

if(!entry1.IsSet(InputView.AutoCapitalizationProperty))
{
entry1.AutoCapitalization = AutoCapitalization.Default;
entry1.ClassId = $"{entry1.GetType().Name}{entry1.AutoCapitalization}";
}
else if(!Enum.IsDefined(typeof(AutoCapitalization), cap1))
{
cap1 = 0;
entry1.ClearValue(InputView.AutoCapitalizationProperty);
entry1.ClassId = $"{entry1.GetType().Name}NotSet";
}
else
{
entry1.AutoCapitalization = (AutoCapitalization)cap1;
entry1.ClassId = $"{entry1.GetType().Name}{entry1.AutoCapitalization}";
}
}
};

layout.Children.Add(rotate);


Content = new ScrollView()
{
Content = layout
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue1347.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1356.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1439.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1683.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1691.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2983.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2963.cs" />
Expand Down
31 changes: 31 additions & 0 deletions Xamarin.Forms.Core/AutoCapitalization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.Forms
{
public enum AutoCapitalization
{
/// <summary>
/// Don't set anything on the platform component.
/// Currently this only affects iOS
/// </summary>
Default = 0,
/// <summary>
/// no automatic text capitalization.
/// </summary>
None = 1,
/// <summary>
/// capitalize the first character of every word.
/// </summary>
Words = 2,
/// <summary>
/// capitalize the first character of each sentence.
/// </summary>
Sentences = 3,
/// <summary>
/// capitalize all characters.
/// </summary>
Characters = 4
}
}
10 changes: 10 additions & 0 deletions Xamarin.Forms.Core/InputView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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 AutoCapitalizationProperty = BindableProperty.Create(nameof(AutoCapitalizationProperty),
typeof(AutoCapitalization), typeof(InputView), AutoCapitalization.Default);

internal InputView()
{
}
Expand All @@ -14,5 +18,11 @@ public Keyboard Keyboard
get { return (Keyboard)GetValue(KeyboardProperty); }
set { SetValue(KeyboardProperty, value); }
}

public AutoCapitalization AutoCapitalization
{
get { return (AutoCapitalization)GetValue(AutoCapitalizationProperty); }
set { SetValue(AutoCapitalizationProperty, value); }
}
}
}
21 changes: 21 additions & 0 deletions Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateText();
else if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
UpdateInputType();
else if (e.PropertyName == InputView.AutoCapitalizationProperty.PropertyName)
UpdateInputType();
else if (e.PropertyName == Editor.TextColorProperty.PropertyName)
UpdateTextColor();
else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName)
Expand Down Expand Up @@ -154,6 +156,25 @@ void UpdateInputType()
{
edit.KeyListener = GetDigitsKeyListener(edit.InputType);
}

if (model.IsSet(InputView.AutoCapitalizationProperty))
{
var autoCap = (AutoCapitalization)model.GetValue(InputView.AutoCapitalizationProperty);

switch (autoCap)
{
case AutoCapitalization.Characters:
Control.InputType = Control.InputType | InputTypes.TextFlagCapCharacters;
break;
case AutoCapitalization.Sentences:
Control.InputType = Control.InputType | InputTypes.TextFlagCapSentences;
break;
case AutoCapitalization.Words:
Control.InputType = Control.InputType | InputTypes.TextFlagCapWords;
break;

}
}
}

void UpdateText()
Expand Down
22 changes: 21 additions & 1 deletion Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateColor();
else if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
UpdateInputType();
else if (e.PropertyName == InputView.AutoCapitalizationProperty.PropertyName)
UpdateInputType();
else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName)
UpdateAlignment();
else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName)
Expand Down Expand Up @@ -191,11 +193,29 @@ void UpdateInputType()
{
Control.KeyListener = GetDigitsKeyListener(Control.InputType);
}

if (model.IsPassword && ((Control.InputType & InputTypes.ClassText) == InputTypes.ClassText))
Control.InputType = Control.InputType | InputTypes.TextVariationPassword;
if (model.IsPassword && ((Control.InputType & InputTypes.ClassNumber) == InputTypes.ClassNumber))
Control.InputType = Control.InputType | InputTypes.NumberVariationPassword;

if(model.IsSet(InputView.AutoCapitalizationProperty))
{
var autoCap = (AutoCapitalization)model.GetValue(InputView.AutoCapitalizationProperty);

switch (autoCap)
{
case AutoCapitalization.Characters:
Control.InputType = Control.InputType | InputTypes.TextFlagCapCharacters;
break;
case AutoCapitalization.Sentences:
Control.InputType = Control.InputType | InputTypes.TextFlagCapSentences;
break;
case AutoCapitalization.Words:
Control.InputType = Control.InputType | InputTypes.TextFlagCapWords;
break;
}
}
}

void UpdatePlaceholderColor()
Expand Down
20 changes: 20 additions & 0 deletions Xamarin.Forms.Platform.UAP/EditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
UpdateFont();
UpdateTextAlignment();
UpdateFlowDirection();
UpdateAutoCapitalization();
}

base.OnElementChanged(e);
Expand Down Expand Up @@ -91,6 +92,10 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateTextAlignment();
UpdateFlowDirection();
}
else if (e.PropertyName == InputView.AutoCapitalizationProperty.PropertyName)
{
UpdateAutoCapitalization();
}
}

void OnLostFocus(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -201,5 +206,20 @@ void UpdateFlowDirection()
{
Control.UpdateFlowDirection(Element);
}

void UpdateAutoCapitalization()
{
if (Element.IsSet(Xamarin.Forms.InputView.AutoCapitalizationProperty))
{
Control.AutoCapitalization = Element.AutoCapitalization;
switch (Element.AutoCapitalization)
{
case AutoCapitalization.Sentences:
case AutoCapitalization.Words:
Internals.Log.Warning(nameof(Entry), $"{Element.AutoCapitalization} is not supported on windows because they are controlled by Settings->Typing");
break;
}
}
}
}
}
20 changes: 19 additions & 1 deletion Xamarin.Forms.Platform.UAP/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
UpdateInputScope();
UpdateAlignment();
UpdatePlaceholderColor();
UpdateAutoCapitalization();
}
}

Expand Down Expand Up @@ -72,6 +73,8 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE
UpdateTextColor();
else if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
UpdateInputScope();
else if (e.PropertyName == InputView.AutoCapitalizationProperty.PropertyName)
UpdateAutoCapitalization();
else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName)
UpdateFont();
else if (e.PropertyName == Entry.FontFamilyProperty.PropertyName)
Expand Down Expand Up @@ -101,7 +104,7 @@ protected override void UpdateBackgroundColor()
}

void OnNativeTextChanged(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs args)
{
{
Element.SetValueCore(Entry.TextProperty, Control.Text);
}

Expand Down Expand Up @@ -204,5 +207,20 @@ void UpdateTextColor()
BrushHelpers.UpdateColor(textColor, ref _defaultTextColorFocusBrush,
() => Control.ForegroundFocusBrush, brush => Control.ForegroundFocusBrush = brush);
}

void UpdateAutoCapitalization()
{
if (Element.IsSet(Xamarin.Forms.InputView.AutoCapitalizationProperty))
{
Control.AutoCapitalization = Element.AutoCapitalization;
switch (Element.AutoCapitalization)
{
case AutoCapitalization.Sentences:
case AutoCapitalization.Words:
Internals.Log.Warning(nameof(Entry), $"{Element.AutoCapitalization} is not supported on windows because they are controlled by Settings->Typing");
break;
}
}
}
}
}
Loading

0 comments on commit adeeedb

Please sign in to comment.