From b45872e728a8dd7de0fdaabd013ec7c1c0378dbc Mon Sep 17 00:00:00 2001 From: Joseph Sawyer Date: Wed, 22 Jan 2025 00:08:39 +0000 Subject: [PATCH] feat: add hidesuggestions property to textbox (#17815) * feat: add hidesuggestions property to textbox * fix: set HideSuggestions in FromStyledElement * refactor: api review recommendations * refactor: pr suggestions --- samples/ControlCatalog/Pages/TextBoxPage.xaml | 1 + .../Platform/Input/AndroidInputMethod.cs | 3 ++ .../Input/TextInput/TextInputOptions.cs | 36 ++++++++++++++++++- .../TextInputResponder.Properties.cs | 11 ++++-- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/samples/ControlCatalog/Pages/TextBoxPage.xaml b/samples/ControlCatalog/Pages/TextBoxPage.xaml index 058bcff84d..e2230e3cb6 100644 --- a/samples/ControlCatalog/Pages/TextBoxPage.xaml +++ b/samples/ControlCatalog/Pages/TextBoxPage.xaml @@ -38,6 +38,7 @@ UseFloatingWatermark="True" PasswordChar="*" Text="Password" /> + diff --git a/src/Android/Avalonia.Android/Platform/Input/AndroidInputMethod.cs b/src/Android/Avalonia.Android/Platform/Input/AndroidInputMethod.cs index cb105197cb..89e7dddb78 100644 --- a/src/Android/Avalonia.Android/Platform/Input/AndroidInputMethod.cs +++ b/src/Android/Avalonia.Android/Platform/Input/AndroidInputMethod.cs @@ -172,6 +172,9 @@ namespace Avalonia.Android.Platform.Input if (options.Multiline) outAttrs.InputType |= InputTypes.TextFlagMultiLine; + if (outAttrs.InputType is InputTypes.ClassText && options.ShowSuggestions == false) + outAttrs.InputType |= InputTypes.TextVariationPassword | InputTypes.TextFlagNoSuggestions; + outAttrs.ImeOptions = options.ReturnKeyType switch { TextInputReturnKeyType.Return => ImeFlags.NoEnterAction, diff --git a/src/Avalonia.Base/Input/TextInput/TextInputOptions.cs b/src/Avalonia.Base/Input/TextInput/TextInputOptions.cs index c33f5641a8..deed705ef4 100644 --- a/src/Avalonia.Base/Input/TextInput/TextInputOptions.cs +++ b/src/Avalonia.Base/Input/TextInput/TextInputOptions.cs @@ -12,7 +12,8 @@ public class TextInputOptions AutoCapitalization = GetAutoCapitalization(avaloniaObject), IsSensitive = GetIsSensitive(avaloniaObject), Lowercase = GetLowercase(avaloniaObject), - Uppercase = GetUppercase(avaloniaObject) + Uppercase = GetUppercase(avaloniaObject), + ShowSuggestions = GetShowSuggestions(avaloniaObject), }; return result; @@ -253,4 +254,37 @@ public class TextInputOptions /// Text contains sensitive data like card numbers and should not be stored /// public bool IsSensitive { get; set; } + + /// + /// Defines the property. + /// + public static readonly AttachedProperty ShowSuggestionsProperty = + AvaloniaProperty.RegisterAttached( + "ShowSuggestions", + inherits: true); + + /// + /// Sets the value of the attached on a control. + /// + /// The control. + /// The property value to set. + public static void SetShowSuggestions(StyledElement avaloniaObject, bool? value) + { + avaloniaObject.SetValue(ShowSuggestionsProperty, value); + } + + /// + /// Gets the value of the attached . + /// + /// The target. + /// true if ShowSuggestions + public static bool? GetShowSuggestions(StyledElement avaloniaObject) + { + return avaloniaObject.GetValue(ShowSuggestionsProperty); + } + + /// + /// Show virtual keyboard suggestions + /// + public bool? ShowSuggestions { get; set; } } diff --git a/src/iOS/Avalonia.iOS/TextInputResponder.Properties.cs b/src/iOS/Avalonia.iOS/TextInputResponder.Properties.cs index ab0d92e5fa..0614205a04 100644 --- a/src/iOS/Avalonia.iOS/TextInputResponder.Properties.cs +++ b/src/iOS/Avalonia.iOS/TextInputResponder.Properties.cs @@ -12,7 +12,10 @@ partial class AvaloniaView public UITextAutocapitalizationType AutocapitalizationType { get; private set; } [Export("autocorrectionType")] - public UITextAutocorrectionType AutocorrectionType => UITextAutocorrectionType.Yes; + public UITextAutocorrectionType AutocorrectionType => + _view._options?.ShowSuggestions == false ? + UITextAutocorrectionType.No : + UITextAutocorrectionType.Yes; [Export("keyboardType")] public UIKeyboardType KeyboardType => @@ -64,7 +67,11 @@ partial class AvaloniaView _view._options?.ContentType is TextInputContentType.Password or TextInputContentType.Pin || (_view._options?.IsSensitive ?? false); - [Export("spellCheckingType")] public UITextSpellCheckingType SpellCheckingType => UITextSpellCheckingType.Yes; + [Export("spellCheckingType")] + public UITextSpellCheckingType SpellCheckingType => + _view._options?.ShowSuggestions == false ? + UITextSpellCheckingType.No : + UITextSpellCheckingType.Yes; [Export("textContentType")] public NSString TextContentType { get; set; } = new NSString("text/plain");