Android text input now works like iOS, where you get text in progress and then backspaces and new text if autocomplete changes it or the IME commits it.

This commit is contained in:
Sam Lantinga 2022-09-30 17:25:58 -07:00
parent 28572702bf
commit 257cacab18
1 changed files with 43 additions and 118 deletions

View File

@ -1876,7 +1876,7 @@ class DummyEdit extends View implements View.OnKeyListener {
class SDLInputConnection extends BaseInputConnection { class SDLInputConnection extends BaseInputConnection {
protected EditText mEditText; protected EditText mEditText;
protected int m_nLastContentLength = 0; protected String mCommittedText = "";
public SDLInputConnection(View targetView, boolean fullEditor) { public SDLInputConnection(View targetView, boolean fullEditor) {
super(targetView, fullEditor); super(targetView, fullEditor);
@ -1913,149 +1913,74 @@ class SDLInputConnection extends BaseInputConnection {
@Override @Override
public boolean commitText(CharSequence text, int newCursorPosition) { public boolean commitText(CharSequence text, int newCursorPosition) {
replaceText(text, newCursorPosition, false); if (!super.commitText(text, newCursorPosition)) {
return false;
return super.commitText(text, newCursorPosition); }
updateText();
return true;
} }
@Override @Override
public boolean setComposingText(CharSequence text, int newCursorPosition) { public boolean setComposingText(CharSequence text, int newCursorPosition) {
replaceText(text, newCursorPosition, true); if (!super.setComposingText(text, newCursorPosition)) {
return false;
return super.setComposingText(text, newCursorPosition);
}
@Override
public boolean setComposingRegion(int start, int end) {
final Editable content = getEditable();
if (content != null) {
int a = start;
int b = end;
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
// Clip the end points to be within the content bounds.
final int length = content.length();
if (a < 0) {
a = 0;
}
if (b < 0) {
b = 0;
}
if (a > length) {
a = length;
}
if (b > length) {
b = length;
}
deleteText(a, b);
} }
updateText();
return super.setComposingRegion(start, end); return true;
} }
@Override @Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) { public boolean deleteSurroundingText(int beforeLength, int afterLength) {
final Editable content = getEditable(); if (!super.deleteSurroundingText(beforeLength, afterLength)) {
if (content != null) { return false;
int a = Selection.getSelectionStart(content);
int b = Selection.getSelectionEnd(content);
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
// ignore the composing text.
int ca = getComposingSpanStart(content);
int cb = getComposingSpanEnd(content);
if (cb < ca) {
int tmp = ca;
ca = cb;
cb = tmp;
}
if (ca != -1 && cb != -1) {
if (ca < a) {
a = ca;
}
if (cb > b) {
b = cb;
}
}
if (beforeLength > 0) {
int start = a - beforeLength;
if (start < 0) {
start = 0;
}
deleteText(start, a);
}
} }
updateText();
return super.deleteSurroundingText(beforeLength, afterLength); return true;
} }
protected void replaceText(CharSequence text, int newCursorPosition, boolean composing) { protected void updateText() {
final Editable content = getEditable(); final Editable content = getEditable();
if (content == null) { if (content == null) {
return; return;
} }
// delete composing text set previously.
int a = getComposingSpanStart(content);
int b = getComposingSpanEnd(content);
if (b < a) { String text = content.toString();
int tmp = a; int compareLength = Math.min(text.length(), mCommittedText.length());
a = b; int matchLength, offset;
b = tmp;
/* Backspace over characters that are no longer in the string */
for (matchLength = 0; matchLength < compareLength; ) {
int codePoint = mCommittedText.codePointAt(matchLength);
if (codePoint != text.codePointAt(matchLength)) {
break;
}
matchLength += Character.charCount(codePoint);
} }
if (a == -1 || b == -1) { /* FIXME: This doesn't handle graphemes, like '🌬️' */
a = Selection.getSelectionStart(content); for (offset = matchLength; offset < mCommittedText.length(); ) {
b = Selection.getSelectionEnd(content); int codePoint = mCommittedText.codePointAt(offset);
if (a < 0) { nativeGenerateScancodeForUnichar('\b');
a = 0; offset += Character.charCount(codePoint);
}
if (b < 0) {
b = 0;
}
if (b < a) {
int tmp = a;
a = b;
b = tmp;
}
} }
deleteText(a, b); if (matchLength < text.length()) {
String pendingText = text.subSequence(matchLength, text.length()).toString();
if (composing) { for (offset = 0; offset < pendingText.length(); ) {
nativeSetComposingText(text.toString(), newCursorPosition); int codePoint = pendingText.codePointAt(offset);
} else { if (codePoint == '\n') {
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '\n') {
if (SDLActivity.onNativeSoftReturnKey()) { if (SDLActivity.onNativeSoftReturnKey()) {
return; return;
} }
} }
++m_nLastContentLength; /* Higher code points don't generate simulated scancodes */
nativeGenerateScancodeForUnichar(c); if (codePoint < 128) {
nativeGenerateScancodeForUnichar((char)codePoint);
}
offset += Character.charCount(codePoint);
} }
SDLInputConnection.nativeCommitText(text.toString(), newCursorPosition); SDLInputConnection.nativeCommitText(pendingText, 0);
}
}
protected void deleteText(int start, int end) {
while (m_nLastContentLength > start) {
--m_nLastContentLength;
nativeGenerateScancodeForUnichar('\b');
} }
mCommittedText = text;
} }
public static native void nativeCommitText(String text, int newCursorPosition); public static native void nativeCommitText(String text, int newCursorPosition);