Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rajamatt committed May 10, 2024
1 parent fa8cfc7 commit 326841d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
12 changes: 6 additions & 6 deletions doc/helpers/Input-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Provides various attached properties for _input controls_, such as `TextBox` and

## Attached Properties

| Property | Type | Description |
|------------------------|--------------|-----------------------------------------------------------------------------------------------------------------------------------|
| `AutoDismiss` | `bool` | Whether the soft keyboard will be dismissed when the enter key is pressed. |
| `AutoFocusNext` | `bool` | Whether the focus will move to the next focusable element when the enter key is pressed.\* |
| `AutoFocusNextElement` | `Control` | Sets the next control to focus when the enter key is pressed.\* |
| `ReturnType` | `ReturnType` | The type of return button on a soft keyboard. It can be one of the following options: __Default, Done, Go, Next, Search, Send__. |
| Property | Type | Description |
|------------------------|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| `AutoDismiss` | `bool` | Whether the soft keyboard will be dismissed when the enter key is pressed. |
| `AutoFocusNext` | `bool` | Whether the focus will move to the next focusable element when the enter key is pressed.\* |
| `AutoFocusNextElement` | `Control` | Sets the next control to focus when the enter key is pressed.\* |
| `ReturnType` | `ReturnType` | The type of return button on a soft keyboard for Android/iOS. It can be one of the following options: __Default, Done, Go, Next, Search, Send__. |

`AutoFocusNext` and `AutoFocusNextElement`\*: Having either or both of the two properties set will enable the focus next behavior. `AutoFocusNextElement` will take precedence over `AutoFocusNext` when both are set.

Expand Down
54 changes: 25 additions & 29 deletions src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,31 @@ internal static bool IsEnterCommandSupportedFor(DependencyObject host)

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 (sender is TextBox || sender is PasswordBox)
{
if (e.NewValue is not ReturnType returnType)
{
returnType = ReturnType.Default;
}
#if __ANDROID__
ImeAction imeAction = GetImeActionFromReturnType(returnType);

if (control is TextBox textBox)
if (sender is TextBox textBox)
{
textBox.ImeOptions = imeAction;
}
else if (control is PasswordBox passwordBox)
else if (sender is PasswordBox passwordBox)
{
passwordBox.ImeOptions = imeAction;
}
#elif __IOS__
UIReturnKeyType returnKeyType = GetReturnKeyTypeFromReturnType(returnType);

if (control is TextBox textBox)
if (sender is TextBox textBox)
{
textBox.ReturnKeyType = returnKeyType;
}
else if (control is PasswordBox passwordBox)
else if (sender is PasswordBox passwordBox)
{
passwordBox.ReturnKeyType = returnKeyType;
}
Expand Down Expand Up @@ -241,35 +245,27 @@ private static void OnUIElementKeyUp(object sender, KeyRoutedEventArgs e)
}

#if __ANDROID__
private static ImeAction GetImeActionFromReturnType(ReturnType returnType)
private static ImeAction GetImeActionFromReturnType(ReturnType returnType) => returnType switch
{
switch (returnType)
{
case ReturnType.Next: return ImeAction.Next;
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:
default: return ImeAction.Unspecified;
}
}
ReturnType.Next => ImeAction.Next,
ReturnType.Go => ImeAction.Go,
ReturnType.Search => ImeAction.Search,
ReturnType.Send => ImeAction.Send,
ReturnType.Done => ImeAction.Done,
ReturnType.Default or _ => ImeAction.Unspecified
};
#endif

#if __IOS__
private static UIReturnKeyType GetReturnKeyTypeFromReturnType(ReturnType returnType)
private static UIReturnKeyType GetReturnKeyTypeFromReturnType(ReturnType returnType) => returnType switch
{
switch (returnType)
{
case ReturnType.Next: return UIReturnKeyType.Next;
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:
default: return UIReturnKeyType.Default;
}
}
ReturnType.Next => UIReturnKeyType.Next,
ReturnType.Go => UIReturnKeyType.Go,
ReturnType.Search => UIReturnKeyType.Search,
ReturnType.Send => UIReturnKeyType.Send,
ReturnType.Done => UIReturnKeyType.Done,
ReturnType.Default or _ => UIReturnKeyType.Default
};
#endif
}
}

0 comments on commit 326841d

Please sign in to comment.