From dbfa47c978d4fe9f8ed53a16a57a4de778010e3a Mon Sep 17 00:00:00 2001 From: Noam Yogev Date: Thu, 1 Nov 2018 18:03:23 +0200 Subject: [PATCH] Add Editor.IsTextPredictionEnabled property (#4023) * Add Editor.IsTextPredictionEnabled property * simplfy UpdateInputType logic * modify IsTextPredictionEnabledProperty BindingMode to Default * undo changes of commit 5db2456d * [UITests] update namespace for UI Tests - Fixes #3555 --- .../Issue3555.cs | 46 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Core/Editor.cs | 8 ++++ .../Renderers/EditorRenderer.cs | 13 ++++-- Xamarin.Forms.Platform.UAP/EditorRenderer.cs | 9 +++- .../Renderers/EditorRenderer.cs | 21 +++++++-- 6 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue3555.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue3555.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue3555.cs new file mode 100644 index 000000000..5ff37e7bc --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue3555.cs @@ -0,0 +1,46 @@ +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.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 3555, "[Enhancement] Editor: Control over text-prediction", PlatformAffected.All)] + public class Issue3555 + : TestContentPage + { + protected override void Init() + { + var editorDefaults = new Editor(); + var editorFull = new Editor(); + editorFull.Keyboard = Keyboard.Create(KeyboardFlags.All); + var editorNoTextPrediction = new Editor { IsTextPredictionEnabled = false }; + // IsTextPredictionEnabled should be ignored for email in Editor + var editorEmail = new Editor { Text = "moses@example.com", Keyboard = Keyboard.Email, IsTextPredictionEnabled = true }; + // IsTextPredictionEnabled should be ignored for numeric Editor + var editorNumeric = new Editor { Text = "01234", Keyboard = Keyboard.Numeric, IsTextPredictionEnabled = true }; + // On Android disabling either spell checking or text prediction both turn off text suggestions so this Editor + // should behave the same as editorNoTextPrediction above + var editorNoSpellChecking = new Editor { IsSpellCheckEnabled = false }; + var stackLayout = new StackLayout(); + stackLayout.Children.Add(new Label { Text = "Defaults" }); + stackLayout.Children.Add(editorDefaults); + stackLayout.Children.Add(new Label { Text = "Text prediction disabled" }); + stackLayout.Children.Add(editorNoTextPrediction); + stackLayout.Children.Add(new Label { Text = "Spell checking disabled" }); + stackLayout.Children.Add(editorNoSpellChecking); + stackLayout.Children.Add(new Label { Text = "Email" }); + stackLayout.Children.Add(editorEmail); + stackLayout.Children.Add(new Label { Text = "Numeric" }); + stackLayout.Children.Add(editorNumeric); + stackLayout.Children.Add(new Label { Text = "Full" }); + stackLayout.Children.Add(editorFull); + 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 d969e54ae..43cc9e563 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 @@ -20,6 +20,7 @@ + diff --git a/Xamarin.Forms.Core/Editor.cs b/Xamarin.Forms.Core/Editor.cs index ba8d12ddf..6ddf34bcc 100644 --- a/Xamarin.Forms.Core/Editor.cs +++ b/Xamarin.Forms.Core/Editor.cs @@ -23,6 +23,8 @@ namespace Xamarin.Forms public static readonly BindableProperty PlaceholderColorProperty = PlaceholderElement.PlaceholderColorProperty; + public static readonly BindableProperty IsTextPredictionEnabledProperty = BindableProperty.Create(nameof(IsTextPredictionEnabled), typeof(bool), typeof(Editor), true, BindingMode.Default); + public static readonly BindableProperty AutoSizeProperty = BindableProperty.Create(nameof(AutoSize), typeof(EditorAutoSizeOption), typeof(Editor), defaultValue: EditorAutoSizeOption.Disabled, propertyChanged: (bindable, oldValue, newValue) => ((Editor)bindable)?.InvalidateMeasure()); @@ -62,6 +64,12 @@ namespace Xamarin.Forms set { SetValue(FontAttributesProperty, value); } } + public bool IsTextPredictionEnabled + { + get { return (bool)GetValue(IsTextPredictionEnabledProperty); } + set { SetValue(IsTextPredictionEnabledProperty, value); } + } + public string FontFamily { get { return (string)GetValue(FontFamilyProperty); } diff --git a/Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs index fefcbc613..f72ef694c 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs @@ -100,6 +100,8 @@ namespace Xamarin.Forms.Platform.Android UpdateInputType(); else if (e.PropertyName == InputView.IsSpellCheckEnabledProperty.PropertyName) UpdateInputType(); + else if (e.PropertyName == Editor.IsTextPredictionEnabledProperty.PropertyName) + UpdateInputType(); else if (e.PropertyName == Editor.TextColorProperty.PropertyName) UpdateTextColor(); else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName) @@ -165,12 +167,17 @@ namespace Xamarin.Forms.Platform.Android var keyboard = model.Keyboard; edit.InputType = keyboard.ToInputType() | InputTypes.TextFlagMultiLine; - if (!(keyboard is Internals.CustomKeyboard) && model.IsSet(InputView.IsSpellCheckEnabledProperty)) + if (!(keyboard is Internals.CustomKeyboard)) { - if ((edit.InputType & InputTypes.TextFlagNoSuggestions) != InputTypes.TextFlagNoSuggestions) + if (model.IsSet(InputView.IsSpellCheckEnabledProperty)) { if (!model.IsSpellCheckEnabled) - edit.InputType = edit.InputType | InputTypes.TextFlagNoSuggestions; + edit.InputType = edit.InputType | InputTypes.TextFlagNoSuggestions; + } + if (model.IsSet(Editor.IsTextPredictionEnabledProperty)) + { + if (!model.IsTextPredictionEnabled) + edit.InputType = edit.InputType | InputTypes.TextFlagNoSuggestions; } } diff --git a/Xamarin.Forms.Platform.UAP/EditorRenderer.cs b/Xamarin.Forms.Platform.UAP/EditorRenderer.cs index 09e6b9549..188ae47c7 100644 --- a/Xamarin.Forms.Platform.UAP/EditorRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/EditorRenderer.cs @@ -94,6 +94,10 @@ namespace Xamarin.Forms.Platform.UWP { UpdateInputScope(); } + else if (e.PropertyName == Editor.IsTextPredictionEnabledProperty.PropertyName) + { + UpdateInputScope(); + } else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName) { UpdateFont(); @@ -282,7 +286,10 @@ namespace Xamarin.Forms.Platform.UWP } else { - Control.ClearValue(TextBox.IsTextPredictionEnabledProperty); + if (editor.IsSet(Editor.IsTextPredictionEnabledProperty)) + Control.IsTextPredictionEnabled = editor.IsTextPredictionEnabled; + else + Control.ClearValue(TextBox.IsTextPredictionEnabledProperty); if (editor.IsSet(InputView.IsSpellCheckEnabledProperty)) Control.IsSpellCheckEnabled = editor.IsSpellCheckEnabled; else diff --git a/Xamarin.Forms.Platform.iOS/Renderers/EditorRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/EditorRenderer.cs index b9df27903..465432942 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/EditorRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/EditorRenderer.cs @@ -132,6 +132,8 @@ namespace Xamarin.Forms.Platform.iOS UpdateKeyboard(); else if (e.PropertyName == Xamarin.Forms.InputView.IsSpellCheckEnabledProperty.PropertyName) UpdateKeyboard(); + else if (e.PropertyName == Editor.IsTextPredictionEnabledProperty.PropertyName) + UpdateKeyboard(); else if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName) UpdateEditable(); else if (e.PropertyName == Editor.TextColorProperty.PropertyName) @@ -202,12 +204,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(Editor.IsTextPredictionEnabledProperty)) + { + if (!Element.IsTextPredictionEnabled) + { + Control.AutocorrectionType = UITextAutocorrectionType.No; + } } } Control.ReloadInputViews();