diff --git a/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs b/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs index 44f1f9f7b..b2103f580 100644 --- a/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs +++ b/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs @@ -11,6 +11,12 @@ using Windows.System; using Windows.UI.ViewManagement; +#if __ANDROID__ +using Android.Views.InputMethods; +#elif __IOS__ +using UIKit; +#endif + #if IS_WINUI using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -27,6 +33,30 @@ public static class InputExtensions { private static readonly ILogger _logger = typeof(InputExtensions).Log(); + public enum ReturnType { + Default, + Done, + Go, + Next, + Search, + Send + } + + #region DependencyProperty: ReturnType + + /// + /// Backing property for what type of return the soft-keyboard will show. + /// + public static DependencyProperty ReturnTypeProperty { get; } = DependencyProperty.RegisterAttached( + "ReturnType", + typeof(ReturnType), + typeof(InputExtensions), + new PropertyMetadata(ReturnType.Default, OnReturnTypeChanged)); + + public static ReturnType GetReturnType(DependencyObject obj) => (ReturnType)obj.GetValue(ReturnTypeProperty); + public static void SetReturnType(DependencyObject obj, ReturnType value) => obj.SetValue(ReturnTypeProperty, value); + + #endregion #region DependencyProperty: AutoDismiss /// @@ -109,6 +139,36 @@ internal static bool IsEnterCommandSupportedFor(DependencyObject host) return host is TextBox || host is PasswordBox; } + private static void OnReturnTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) + { + if (sender is Control control && (sender is TextBox || sender is PasswordBox) && e.NewValue is ReturnType returnType) + { +#if __ANDROID__ + ImeAction imeAction = GetImeActionFromReturnType(returnType); + + if (control is TextBox textBox) + { + textBox.ImeOptions = imeAction; + } + else if (control is PasswordBox passwordBox) + { + passwordBox.ImeOptions = imeAction; + } +#elif __IOS__ + UIReturnKeyType returnKeyType = GetReturnKeyTypeFromReturnType(returnType); + + if (control is TextBox textBox) + { + textBox.ReturnKeyType = returnKeyType; + } + else if (control is PasswordBox passwordBox) + { + passwordBox.ReturnKeyType = returnKeyType; + } +#endif + } + } + private static void OnAutoDismissChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) => UpdateSubscription(sender); private static void OnAutoFocusNextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) => UpdateSubscription(sender); private static void OnAutoFocusNextElementChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) => UpdateSubscription(sender); @@ -177,5 +237,37 @@ private static void OnUIElementKeyUp(object sender, KeyRoutedEventArgs e) _ => default, }; } + +#if __ANDROID__ + private static ImeAction GetImeActionFromReturnType(ReturnType returnType) + { + switch (returnType) + { + case ReturnType.Go: return ImeAction.Go; + case ReturnType.Search: return ImeAction.Search; + case ReturnType.Send: return ImeAction.Send; + case ReturnType.Done: return ImeAction.Done; + case ReturnType.Default: + case ReturnType.Next: + default: return ImeAction.Next; + } + } +#endif + +#if __IOS__ + private static UIReturnKeyType GetReturnKeyTypeFromReturnType(ReturnType returnType) + { + switch (returnType) + { + case ReturnType.Go: return UIReturnKeyType.Go; + case ReturnType.Search: return UIReturnKeyType.Search; + case ReturnType.Send: return UIReturnKeyType.Send; + case ReturnType.Done: return UIReturnKeyType.Done; + case ReturnType.Default: + case ReturnType.Next: + default: return UIReturnKeyType.Next; + } + } +#endif } }