mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
refactor: ReactEditTextInputConnectionWrapper implemented in kotlin (#50573)
Summary: Implemented ReactEditTextInputConnectionWrapper in Kotlin as part of Kotlin-ifying RN ## Changelog: - Removed ReactEditTextInputConnectionWrapper.java - Added ReactEditTextInputConnectionWrapper.kt Pick one each for the category and type tags: [ANDROID] [CHANGED] - Kotlinify ReactEditTextInputConnectionWrapper Pull Request resolved: https://github.com/facebook/react-native/pull/50573 Test Plan: Run RN-Tester and interacted with TextInputs with both new architecture enabled and disabled Reviewed By: rshest Differential Revision: D72704246 Pulled By: cortinico fbshipit-source-id: fec358d157c6ba09fecfa98bf431b18eee7824b0
This commit is contained in:
1 parent
0617accecd
commit
5c9883b018
2 files changed
+151
-160
No files matched your search
-160
@@ -1,160 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.views.textinput;
|
||||
|
||||
import android.view.KeyEvent;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputConnection;
|
||||
import android.view.inputmethod.InputConnectionWrapper;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.uimanager.events.EventDispatcher;
|
||||
|
||||
/**
|
||||
* A class to implement the TextInput 'onKeyPress' API on android for soft keyboards. It is
|
||||
* instantiated in {@link ReactEditText#onCreateInputConnection(EditorInfo)}.
|
||||
*
|
||||
* <p>Android IMEs interface with EditText views through the {@link InputConnection} interface, so
|
||||
* any observable change in state of the EditText via the soft-keyboard, should be a side effect of
|
||||
* one or more of the methods in {@link InputConnectionWrapper}.
|
||||
*
|
||||
* <p>{@link InputConnection#setComposingText(CharSequence, int)} is used to set the composing
|
||||
* region (the underlined text) in the {@link android.widget.EditText} view, i.e. when React
|
||||
* Native's TextInput has the property 'autoCorrect' set to true. When text is being composed in the
|
||||
* composing state within the EditText, each key press will result in a call to {@link
|
||||
* InputConnection#setComposingText(CharSequence, int)} with a CharSequence argument equal to that
|
||||
* of the entire composing region, rather than a single character diff. We can reason about the
|
||||
* keyPress based on the resultant cursor position changes of the EditText after applying this
|
||||
* change. For example if the cursor moved backwards by one character when composing, it's likely it
|
||||
* was a delete; if it moves forward by a character, likely to be a key press of that character.
|
||||
*
|
||||
* <p>IMEs can also call {@link InputConnection#beginBatchEdit()} to signify a batch of operations.
|
||||
* One such example is committing a word currently in composing state with the press of the space
|
||||
* key. It is IME dependent but the stock Android keyboard behavior seems to be to commit the
|
||||
* currently composing text with {@link InputConnection#setComposingText(CharSequence, int)} and
|
||||
* commits a space character with a separate call to {@link
|
||||
* InputConnection#setComposingText(CharSequence, int)}. Here we chose to emit the last input of a
|
||||
* batch edit as that tends to be the user input, but it's completely arbitrary.
|
||||
*
|
||||
* <p>Another function of this class is to detect backspaces when the cursor at the beginning of the
|
||||
* {@link android.widget.EditText}, i.e no text is deleted.
|
||||
*
|
||||
* <p>N.B. this class is only applicable for soft keyboards behavior. For hardware keyboards {@link
|
||||
* android.view.View#onKeyDown(int, KeyEvent)} can be overridden to obtain the keycode of the key
|
||||
* pressed.
|
||||
*/
|
||||
class ReactEditTextInputConnectionWrapper extends InputConnectionWrapper {
|
||||
public static final String NEWLINE_RAW_VALUE = "\n";
|
||||
public static final String BACKSPACE_KEY_VALUE = "Backspace";
|
||||
public static final String ENTER_KEY_VALUE = "Enter";
|
||||
|
||||
private ReactEditText mEditText;
|
||||
private EventDispatcher mEventDispatcher;
|
||||
private boolean mIsBatchEdit;
|
||||
private @Nullable String mKey = null;
|
||||
|
||||
public ReactEditTextInputConnectionWrapper(
|
||||
InputConnection target,
|
||||
final ReactContext reactContext,
|
||||
final ReactEditText editText,
|
||||
EventDispatcher eventDispatcher) {
|
||||
super(target, false);
|
||||
mEventDispatcher = eventDispatcher;
|
||||
mEditText = editText;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean beginBatchEdit() {
|
||||
mIsBatchEdit = true;
|
||||
return super.beginBatchEdit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean endBatchEdit() {
|
||||
mIsBatchEdit = false;
|
||||
if (mKey != null) {
|
||||
dispatchKeyEvent(mKey);
|
||||
mKey = null;
|
||||
}
|
||||
return super.endBatchEdit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setComposingText(CharSequence text, int newCursorPosition) {
|
||||
int previousSelectionStart = mEditText.getSelectionStart();
|
||||
int previousSelectionEnd = mEditText.getSelectionEnd();
|
||||
String key;
|
||||
boolean consumed = super.setComposingText(text, newCursorPosition);
|
||||
int currentSelectionStart = mEditText.getSelectionStart();
|
||||
boolean noPreviousSelection = previousSelectionStart == previousSelectionEnd;
|
||||
boolean cursorDidNotMove = currentSelectionStart == previousSelectionStart;
|
||||
boolean cursorMovedBackwardsOrAtBeginningOfInput =
|
||||
(currentSelectionStart < previousSelectionStart) || currentSelectionStart <= 0;
|
||||
if (cursorMovedBackwardsOrAtBeginningOfInput || (!noPreviousSelection && cursorDidNotMove)) {
|
||||
key = BACKSPACE_KEY_VALUE;
|
||||
} else {
|
||||
key = String.valueOf(mEditText.getText().charAt(currentSelectionStart - 1));
|
||||
}
|
||||
dispatchKeyEventOrEnqueue(key);
|
||||
return consumed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean commitText(CharSequence text, int newCursorPosition) {
|
||||
String key = text.toString();
|
||||
// Assume not a keyPress if length > 1 (or 2 if unicode)
|
||||
if (key.length() <= 2) {
|
||||
if (key.equals("")) {
|
||||
key = BACKSPACE_KEY_VALUE;
|
||||
}
|
||||
dispatchKeyEventOrEnqueue(key);
|
||||
}
|
||||
|
||||
return super.commitText(text, newCursorPosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
|
||||
dispatchKeyEvent(BACKSPACE_KEY_VALUE);
|
||||
return super.deleteSurroundingText(beforeLength, afterLength);
|
||||
}
|
||||
|
||||
// Called by SwiftKey when cursor at beginning of input when there is a delete
|
||||
// or when enter is pressed anywhere in the text. Whereas stock Android Keyboard calls
|
||||
// {@link InputConnection#deleteSurroundingText} & {@link InputConnection#commitText}
|
||||
// in each case, respectively.
|
||||
@Override
|
||||
public boolean sendKeyEvent(KeyEvent event) {
|
||||
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
||||
boolean isNumberKey = event.getUnicodeChar() < 58 && event.getUnicodeChar() > 47;
|
||||
if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
|
||||
dispatchKeyEvent(BACKSPACE_KEY_VALUE);
|
||||
} else if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
|
||||
dispatchKeyEvent(ENTER_KEY_VALUE);
|
||||
} else if (isNumberKey) {
|
||||
dispatchKeyEvent(String.valueOf(event.getNumber()));
|
||||
}
|
||||
}
|
||||
return super.sendKeyEvent(event);
|
||||
}
|
||||
|
||||
private void dispatchKeyEventOrEnqueue(String key) {
|
||||
if (mIsBatchEdit) {
|
||||
mKey = key;
|
||||
} else {
|
||||
dispatchKeyEvent(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void dispatchKeyEvent(String key) {
|
||||
if (key.equals(NEWLINE_RAW_VALUE)) {
|
||||
key = ENTER_KEY_VALUE;
|
||||
}
|
||||
mEventDispatcher.dispatchEvent(new ReactTextInputKeyPressEvent(mEditText.getId(), key));
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.views.textinput
|
||||
|
||||
import android.view.KeyEvent
|
||||
import android.view.inputmethod.InputConnection
|
||||
import android.view.inputmethod.InputConnectionWrapper
|
||||
import com.facebook.react.bridge.ReactContext
|
||||
import com.facebook.react.uimanager.UIManagerHelper
|
||||
import com.facebook.react.uimanager.events.EventDispatcher
|
||||
|
||||
/**
|
||||
* A class to implement the TextInput 'onKeyPress' API on android for soft keyboards. It is
|
||||
* instantiated in [ReactEditText.onCreateInputConnection].
|
||||
*
|
||||
* <p>Android IMEs interface with EditText views through the [InputConnection] interface, so any
|
||||
* observable change in state of the EditText via the soft-keyboard, should be a side effect of one
|
||||
* or more of the methods in [InputConnectionWrapper].
|
||||
*
|
||||
* <p>[InputConnection.setComposingText] is used to set the composing region (the underlined text)
|
||||
* in the [android.widget.EditText] view, i.e. when React Native's TextInput has the property
|
||||
* 'autoCorrect' set to true. When text is being composed in the composing state within the
|
||||
* EditText, each key press will result in a call to [InputConnection.setComposingText] with a
|
||||
* CharSequence argument equal to that of the entire composing region, rather than a single
|
||||
* character diff. We can reason about the keyPress based on the resultant cursor position changes
|
||||
* of the EditText after applying this change. For example if the cursor moved backwards by one
|
||||
* character when composing, it's likely it was a delete; if it moves forward by a character, likely
|
||||
* to be a key press of that character.
|
||||
*
|
||||
* <p>IMEs can also call [InputConnection.beginBatchEdit] to signify a batch of operations. One such
|
||||
* example is committing a word currently in composing state with the press of the space key. It is
|
||||
* IME dependent but the stock Android keyboard behavior seems to be to commit the currently
|
||||
* composing text with [InputConnection.setComposingText] and commits a space character with a
|
||||
* separate call to [InputConnection.setComposingText]. Here we chose to emit the last input of a
|
||||
* batch edit as that tends to be the user input, but it's completely arbitrary.
|
||||
*
|
||||
* <p>Another function of this class is to detect backspaces when the cursor is at the beginning of
|
||||
* the [android.widget.EditText], i.e no text is deleted.
|
||||
*
|
||||
* <p>N.B. this class is only applicable for soft keyboards behavior. For hardware keyboards
|
||||
* [android.view.View.onKeyDown] can be overridden to obtain the keycode of the key pressed.
|
||||
*/
|
||||
internal class ReactEditTextInputConnectionWrapper(
|
||||
target: InputConnection,
|
||||
reactContext: ReactContext,
|
||||
private val editText: ReactEditText,
|
||||
private val eventDispatcher: EventDispatcher
|
||||
) : InputConnectionWrapper(target, false) {
|
||||
|
||||
private var isBatchEdit = false
|
||||
private var key: String? = null
|
||||
|
||||
override fun beginBatchEdit(): Boolean {
|
||||
isBatchEdit = true
|
||||
return super.beginBatchEdit()
|
||||
}
|
||||
|
||||
override fun endBatchEdit(): Boolean {
|
||||
isBatchEdit = false
|
||||
key?.let { k ->
|
||||
dispatchKeyEvent(k)
|
||||
key = null
|
||||
}
|
||||
return super.endBatchEdit()
|
||||
}
|
||||
|
||||
override fun setComposingText(text: CharSequence, newCursorPosition: Int): Boolean {
|
||||
val previousSelectionStart = editText.selectionStart
|
||||
val previousSelectionEnd = editText.selectionEnd
|
||||
|
||||
val consumed = super.setComposingText(text, newCursorPosition)
|
||||
|
||||
val currentSelectionStart = editText.selectionStart
|
||||
val noPreviousSelection = previousSelectionStart == previousSelectionEnd
|
||||
val cursorDidNotMove = currentSelectionStart == previousSelectionStart
|
||||
val cursorMovedBackwardsOrAtBeginningOfInput =
|
||||
currentSelectionStart < previousSelectionStart || currentSelectionStart <= 0
|
||||
|
||||
val inputKey =
|
||||
if (cursorMovedBackwardsOrAtBeginningOfInput ||
|
||||
(!noPreviousSelection && cursorDidNotMove)) {
|
||||
BACKSPACE_KEY_VALUE
|
||||
} else {
|
||||
editText.text?.get(currentSelectionStart - 1).toString()
|
||||
}
|
||||
|
||||
dispatchKeyEventOrEnqueue(inputKey)
|
||||
return consumed
|
||||
}
|
||||
|
||||
override fun commitText(text: CharSequence, newCursorPosition: Int): Boolean {
|
||||
var inputKey = text.toString()
|
||||
// Assume not a keyPress if length > 1 (or 2 if unicode)
|
||||
if (inputKey.length <= 2) {
|
||||
if (inputKey.isEmpty()) {
|
||||
inputKey = BACKSPACE_KEY_VALUE
|
||||
}
|
||||
dispatchKeyEventOrEnqueue(inputKey)
|
||||
}
|
||||
return super.commitText(text, newCursorPosition)
|
||||
}
|
||||
|
||||
override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean {
|
||||
dispatchKeyEvent(BACKSPACE_KEY_VALUE)
|
||||
return super.deleteSurroundingText(beforeLength, afterLength)
|
||||
}
|
||||
|
||||
// Called by SwiftKey when cursor at beginning of input when there is a delete
|
||||
// or when enter is pressed anywhere in the text. Whereas stock Android Keyboard calls
|
||||
// [InputConnection.deleteSurroundingText] & [InputConnection.commitText]
|
||||
// in each case, respectively.
|
||||
override fun sendKeyEvent(event: KeyEvent): Boolean {
|
||||
if (event.action == KeyEvent.ACTION_DOWN) {
|
||||
val isNumberKey = event.unicodeChar in 48..57
|
||||
when (event.keyCode) {
|
||||
KeyEvent.KEYCODE_DEL -> dispatchKeyEvent(BACKSPACE_KEY_VALUE)
|
||||
KeyEvent.KEYCODE_ENTER -> dispatchKeyEvent(ENTER_KEY_VALUE)
|
||||
else ->
|
||||
if (isNumberKey) {
|
||||
dispatchKeyEvent(event.number.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.sendKeyEvent(event)
|
||||
}
|
||||
|
||||
private fun dispatchKeyEventOrEnqueue(inputKey: String) {
|
||||
if (isBatchEdit) {
|
||||
key = inputKey
|
||||
} else {
|
||||
dispatchKeyEvent(inputKey)
|
||||
}
|
||||
}
|
||||
|
||||
private fun dispatchKeyEvent(inputKey: String) {
|
||||
val resolvedKey = if (inputKey == NEWLINE_RAW_VALUE) ENTER_KEY_VALUE else inputKey
|
||||
val surfaceId = UIManagerHelper.getSurfaceId(editText)
|
||||
eventDispatcher.dispatchEvent(ReactTextInputKeyPressEvent(surfaceId, editText.id, resolvedKey))
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val NEWLINE_RAW_VALUE: String = "\n"
|
||||
const val BACKSPACE_KEY_VALUE: String = "Backspace"
|
||||
const val ENTER_KEY_VALUE: String = "Enter"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user