From 99f6d30be721ee32b58353a3e45b6094296a67b0 Mon Sep 17 00:00:00 2001 From: Niklas Therning Date: Thu, 15 Mar 2018 18:18:07 +0100 Subject: [PATCH] Added the Entry.IsTextPredictionEnabled property (#2038) fixes #1677 Makes it easier for users to disable text prediction/autocomplete/autosuggest. Fixes #1677. --- .../Issue1677.cs | 45 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Core/Entry.cs | 8 ++++ .../Renderers/EntryRenderer.cs | 21 +++++++-- Xamarin.Forms.Platform.UAP/EntryRenderer.cs | 7 ++- .../Renderers/EntryRenderer.cs | 21 +++++++-- .../Xamarin.Forms/Entry.xml | 31 +++++++++++++ 7 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1677.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1677.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1677.cs new file mode 100644 index 000000000..33e27944e --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1677.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +namespace Xamarin.Forms.Controls +{ + [Preserve (AllMembers=true)] + [Issue (IssueTracker.Github, 1677, "[Enhancement] Entry: Control over text-prediction", PlatformAffected.All)] + public class Issue1677 + : TestContentPage + { + protected override void Init() + { + var entryDefaults = new Entry(); + var entryNoTextPrediction = new Entry { IsTextPredictionEnabled = false }; + // IsTextPredictionEnabled should be ignored for email Entry + var entryEmail = new Entry { Text = "foo@example.com", Keyboard = Keyboard.Email, IsTextPredictionEnabled = true }; + // IsTextPredictionEnabled should be ignored for numeric Entry + var entryNumeric = new Entry { Text = "01234", Keyboard = Keyboard.Numeric, IsTextPredictionEnabled = true }; + // On Android disabling either spell checking or text prediction both turn off text suggestions so this Entry + // should behave the same as entryNoTextPrediction above + var entryNoSpellChecking = new Entry { IsSpellCheckEnabled = false }; + + var stackLayout = new StackLayout(); + stackLayout.Children.Add(new Label { Text = "Defaults" }); + stackLayout.Children.Add(entryDefaults); + stackLayout.Children.Add(new Label { Text = "Text prediction disabled" }); + stackLayout.Children.Add(entryNoTextPrediction); + stackLayout.Children.Add(new Label { Text = "Spell checking disabled" }); + stackLayout.Children.Add(entryNoSpellChecking); + stackLayout.Children.Add(new Label { Text = "Email" }); + stackLayout.Children.Add(entryEmail); + stackLayout.Children.Add(new Label { Text = "Numeric" }); + stackLayout.Children.Add(entryNumeric); + + stackLayout.Padding = new Thickness(0, 20, 0, 0); + Content = stackLayout; + } + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index dd038dac9..845233490 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -238,6 +238,7 @@ + diff --git a/Xamarin.Forms.Core/Entry.cs b/Xamarin.Forms.Core/Entry.cs index 8936a3fec..e55e88e14 100644 --- a/Xamarin.Forms.Core/Entry.cs +++ b/Xamarin.Forms.Core/Entry.cs @@ -33,6 +33,8 @@ namespace Xamarin.Forms public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty; + public static readonly BindableProperty IsTextPredictionEnabledProperty = BindableProperty.Create(nameof(IsTextPredictionEnabled), typeof(bool), typeof(Entry), true, BindingMode.OneTime); + readonly Lazy> _platformConfigurationRegistry; public Entry() @@ -95,6 +97,12 @@ namespace Xamarin.Forms set { SetValue(FontSizeProperty, value); } } + public bool IsTextPredictionEnabled + { + get { return (bool)GetValue(IsTextPredictionEnabledProperty); } + set { SetValue(IsTextPredictionEnabledProperty, value); } + } + public ReturnType ReturnType { get => (ReturnType)GetValue(ReturnTypeProperty); diff --git a/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs index 28517b789..4355235fb 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs @@ -144,6 +144,8 @@ namespace Xamarin.Forms.Platform.Android UpdateInputType(); else if (e.PropertyName == InputView.IsSpellCheckEnabledProperty.PropertyName) UpdateInputType(); + else if (e.PropertyName == Entry.IsTextPredictionEnabledProperty.PropertyName) + UpdateInputType(); else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName) UpdateAlignment(); else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName) @@ -205,12 +207,23 @@ namespace Xamarin.Forms.Platform.Android var keyboard = model.Keyboard; Control.InputType = keyboard.ToInputType(); - if (!(keyboard is Internals.CustomKeyboard) && model.IsSet(InputView.IsSpellCheckEnabledProperty)) + if (!(keyboard is Internals.CustomKeyboard)) { - if ((Control.InputType & InputTypes.TextFlagNoSuggestions) != InputTypes.TextFlagNoSuggestions) + if (model.IsSet(InputView.IsSpellCheckEnabledProperty)) { - if (!model.IsSpellCheckEnabled) - Control.InputType = Control.InputType | InputTypes.TextFlagNoSuggestions; + if ((Control.InputType & InputTypes.TextFlagNoSuggestions) != InputTypes.TextFlagNoSuggestions) + { + if (!model.IsSpellCheckEnabled) + Control.InputType = Control.InputType | InputTypes.TextFlagNoSuggestions; + } + } + if (model.IsSet(Entry.IsTextPredictionEnabledProperty)) + { + if ((Control.InputType & InputTypes.TextFlagNoSuggestions) != InputTypes.TextFlagNoSuggestions) + { + if (!model.IsTextPredictionEnabled) + Control.InputType = Control.InputType | InputTypes.TextFlagNoSuggestions; + } } } diff --git a/Xamarin.Forms.Platform.UAP/EntryRenderer.cs b/Xamarin.Forms.Platform.UAP/EntryRenderer.cs index bf1941510..2830a0c18 100644 --- a/Xamarin.Forms.Platform.UAP/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/EntryRenderer.cs @@ -80,6 +80,8 @@ namespace Xamarin.Forms.Platform.UWP UpdateInputScope(); else if (e.PropertyName == InputView.IsSpellCheckEnabledProperty.PropertyName) UpdateInputScope(); + else if (e.PropertyName == Entry.IsTextPredictionEnabledProperty.PropertyName) + UpdateInputScope(); else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName) UpdateFont(); else if (e.PropertyName == Entry.FontFamilyProperty.PropertyName) @@ -181,7 +183,10 @@ namespace Xamarin.Forms.Platform.UWP } else { - Control.ClearValue(TextBox.IsTextPredictionEnabledProperty); + if (entry.IsSet(Entry.IsTextPredictionEnabledProperty)) + Control.IsTextPredictionEnabled = entry.IsTextPredictionEnabled; + else + Control.ClearValue(TextBox.IsTextPredictionEnabledProperty); if (entry.IsSet(InputView.IsSpellCheckEnabledProperty)) Control.IsSpellCheckEnabled = entry.IsSpellCheckEnabled; else diff --git a/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs index 27809aa2f..914dd0df9 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs @@ -125,6 +125,8 @@ namespace Xamarin.Forms.Platform.iOS UpdateKeyboard(); else if (e.PropertyName == Xamarin.Forms.InputView.IsSpellCheckEnabledProperty.PropertyName) UpdateKeyboard(); + else if (e.PropertyName == Entry.IsTextPredictionEnabledProperty.PropertyName) + UpdateKeyboard(); else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName) UpdateAlignment(); else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName) @@ -215,12 +217,23 @@ namespace Xamarin.Forms.Platform.iOS void UpdateKeyboard() { - Control.ApplyKeyboard(Element.Keyboard); - if (!(Element.Keyboard is Internals.CustomKeyboard) && Element.IsSet(Xamarin.Forms.InputView.IsSpellCheckEnabledProperty)) + var keyboard = Element.Keyboard; + Control.ApplyKeyboard(keyboard); + if (!(keyboard is Internals.CustomKeyboard)) { - if (!Element.IsSpellCheckEnabled) + if (Element.IsSet(Xamarin.Forms.InputView.IsSpellCheckEnabledProperty)) { - Control.SpellCheckingType = UITextSpellCheckingType.No; + if (!Element.IsSpellCheckEnabled) + { + Control.SpellCheckingType = UITextSpellCheckingType.No; + } + } + if (Element.IsSet(Xamarin.Forms.Entry.IsTextPredictionEnabledProperty)) + { + if (!Element.IsTextPredictionEnabled) + { + Control.AutocorrectionType = UITextAutocorrectionType.No; + } } } Control.ReloadInputViews(); diff --git a/docs/Xamarin.Forms.Core/Xamarin.Forms/Entry.xml b/docs/Xamarin.Forms.Core/Xamarin.Forms/Entry.xml index 2689041a1..a1c369e9c 100644 --- a/docs/Xamarin.Forms.Core/Xamarin.Forms/Entry.xml +++ b/docs/Xamarin.Forms.Core/Xamarin.Forms/Entry.xml @@ -314,6 +314,37 @@ View CreateLoginForm () + + + + Property + + 2.0.0.0 + + + System.Boolean + + + To be added. + To be added. + To be added. + + + + + + Field + + 2.0.0.0 + + + Xamarin.Forms.BindableProperty + + + To be added. + To be added. + +