Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Do not erase the entire text when backspace is long-pressed (#2324)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and bluemarvin committed Nov 20, 2019
1 parent 5811237 commit 289ee63
Showing 1 changed file with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,6 @@ public void onLongPress(Keyboard.Key popupKey) {
} else if (popupKey.codes[0] == CustomKeyboard.KEYCODE_SHIFT) {
mIsLongPress = !mIsCapsLock;

} else if (popupKey.codes[0] == Keyboard.KEYCODE_DELETE) {
handleBackspace(true);
}
}

Expand All @@ -489,7 +487,7 @@ public void onKey(int primaryCode, int[] keyCodes, boolean hasPopup) {
handleShift(!mKeyboardView.isShifted());
break;
case Keyboard.KEYCODE_DELETE:
handleBackspace(false);
handleBackspace();
break;
case Keyboard.KEYCODE_DONE:
handleDone();
Expand Down Expand Up @@ -645,7 +643,7 @@ private void handleShift(boolean isShifted) {
mKeyboardView.setShifted(shifted || mIsCapsLock);
}

private void handleBackspace(final boolean isLongPress) {
private void handleBackspace() {
final InputConnection connection = mInputConnection;
if (mComposingText.length() > 0) {
CharSequence selectedText = mInputConnection.getSelectedText(0);
Expand All @@ -668,25 +666,18 @@ private void handleBackspace(final boolean isLongPress) {
return;
}

if (isLongPress) {
CharSequence currentText = connection.getExtractedText(new ExtractedTextRequest(), 0).text;
CharSequence beforeCursorText = connection.getTextBeforeCursor(currentText.length(), 0);
CharSequence afterCursorText = connection.getTextAfterCursor(currentText.length(), 0);
connection.deleteSurroundingText(beforeCursorText.length(), afterCursorText.length());
} else {
if (mCurrentKeyboard.usesTextOverride()) {
String beforeText = getTextBeforeCursor(connection);
String newBeforeText = mCurrentKeyboard.overrideBackspace(beforeText);
if (newBeforeText != null) {
// Replace whole before text
connection.deleteSurroundingText(beforeText.length(), 0);
connection.commitText(newBeforeText, 1);
return;
}
if (mCurrentKeyboard.usesTextOverride()) {
String beforeText = getTextBeforeCursor(connection);
String newBeforeText = mCurrentKeyboard.overrideBackspace(beforeText);
if (newBeforeText != null) {
// Replace whole before text
connection.deleteSurroundingText(beforeText.length(), 0);
connection.commitText(newBeforeText, 1);
return;
}
// Remove the character before the cursor.
connection.deleteSurroundingText(1, 0);
}
// Remove the character before the cursor.
connection.deleteSurroundingText(1, 0);
});
}

Expand Down Expand Up @@ -1030,7 +1021,7 @@ public boolean dispatchKeyEvent(final KeyEvent event) {

switch (keyCode) {
case KeyEvent.KEYCODE_DEL:
handleBackspace(event.isLongPress());
handleBackspace();
return true;
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_NUMPAD_ENTER:
Expand Down

0 comments on commit 289ee63

Please sign in to comment.