Skip to content

Commit

Permalink
Process the enter key as "Done"
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Jan 16, 2024
1 parent a36ceae commit 2c187cc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
30 changes: 27 additions & 3 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Android.Graphics.Drawables;
using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Content;
using static Android.Views.View;
Expand Down Expand Up @@ -193,17 +194,40 @@ void OnEditorAction(object? sender, EditorActionEventArgs e)
{
var returnType = VirtualView?.ReturnType;

// Inside of the android implementations that map events to listeners, the default return value for "Handled" is always true
// This means, just by subscribing to EditorAction/KeyPressed/etc.. you change the behavior of the control
// So, we are setting handled to false here in order to maintain default behavior
bool handled = false;
if (returnType != null)
{
var currentInputImeFlag = returnType.Value.ToPlatform();
var actionId = e.ActionId;
var evt = e.Event;
ImeAction currentInputImeFlag = PlatformView.ImeOptions;

if (e.IsCompletedAction(currentInputImeFlag))
// On API 34 it looks like they fixed the issue where the actionId is ImeAction.ImeNull when using a keyboard
// so I'm just setting the actionId here to the current ImeOptions so the logic can all be simplified
if (actionId == ImeAction.ImeNull && evt?.KeyCode == Keycode.Enter)
{
actionId = currentInputImeFlag;
}

// keyboard path
if (evt?.KeyCode == Keycode.Enter && evt?.Action == KeyEventActions.Down)
{
handled = true;
}
else if (evt?.KeyCode == Keycode.Enter && evt?.Action == KeyEventActions.Up)
{
VirtualView?.Completed();
}
// InputPaneView Path
else if(evt?.KeyCode is null && (actionId == ImeAction.Done || actionId == currentInputImeFlag))
{
VirtualView?.Completed();
}
}

e.Handled = false;
e.Handled = handled;
}

private void OnSelectionChanged(object? sender, EventArgs e)
Expand Down
4 changes: 0 additions & 4 deletions src/Core/src/Handlers/Entry/EntryHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ void OnPlatformKeyUp(object? sender, KeyRoutedEventArgs args)
{
PlatformView?.TryMoveFocus(FocusNavigationDirection.Next);
}
else
{
// TODO: Hide the soft keyboard; this matches the behavior of .NET MAUI on Android/iOS
}

VirtualView?.Completed();
}
Expand Down
9 changes: 8 additions & 1 deletion src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,18 @@ internal static void SetInputType(this EditText editText, ITextInput textInput)
editText.SetSelection(previousCursorPosition);
}

internal static bool IsCompletedAction(this EditorActionEventArgs e, ImeAction? currentInputImeFlag)
internal static bool IsCompletedAction(this EditorActionEventArgs e, ImeAction currentInputImeFlag)
{
var actionId = e.ActionId;
var evt = e.Event;

// On API 34 it looks like they fixed the issue where the actionId is ImeAction.ImeNull when using a keyboard
// so I'm just setting the actionId here to whatever the user has
if (actionId == ImeAction.ImeNull && evt?.KeyCode == Keycode.Enter)
{
actionId = currentInputImeFlag;
}

return
actionId == ImeAction.Done ||
actionId == currentInputImeFlag ||
Expand Down

0 comments on commit 2c187cc

Please sign in to comment.