Skip to content

Commit

Permalink
fix deleted character still being received in getTextBeforeCursorAndD…
Browse files Browse the repository at this point in the history
…etectLaggyConnection

fixes openboard-team/openboard#811
reproducible e.g. using https://f-droid.org/en/packages/com.farmerbb.notepad/
  • Loading branch information
Helium314 committed Aug 14, 2023
1 parent ffa9a01 commit ce0bf06
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ public TextRange getWordRangeAtCursor(final SpacingAndPunctuations spacingAndPun
if (!isConnected()) {
return null;
}
final CharSequence before = getTextBeforeCursorAndDetectLaggyConnection(
CharSequence before = getTextBeforeCursorAndDetectLaggyConnection(
OPERATION_GET_WORD_RANGE_AT_CURSOR,
SLOW_INPUT_CONNECTION_ON_PARTIAL_RELOAD_MS,
NUM_CHARS_TO_GET_BEFORE_CURSOR,
Expand All @@ -718,6 +718,41 @@ public TextRange getWordRangeAtCursor(final SpacingAndPunctuations spacingAndPun
return null;
}

// what is sometimes happening (depending on app, or maybe input field attributes):
// we just pressed delete, and getTextBeforeCursor gets the correct text,
// but getTextBeforeCursorAndDetectLaggyConnection returns the old word, before the deletion (not sure why)
// -> detect this difference, and try to fix it
// interestingly, getTextBeforeCursor seems to only get the correct text because it uses
// mCommittedTextBeforeComposingText where the text is cached
// (this check is really annoyingly long for the simple thing to be done)
if (before.length() > 0 && after.length() == 0) {
final int lastBeforeCodePoint = Character.codePointBefore(before, before.length());
if (!isPartOfCompositionForScript(lastBeforeCodePoint, spacingAndPunctuations, scriptId)) {
// before ends with separator or similar -> check whether text before cursor ends with the same codepoint
int lastBeforeLength = Character.charCount(lastBeforeCodePoint);
CharSequence codePointBeforeCursor = getTextBeforeCursor(lastBeforeLength, 0);
if (Character.codePointAt(codePointBeforeCursor, 0) != lastBeforeCodePoint) {
// they are different, as is expected from the issue
// now check whether they are the same if the last codepoint of before is removed
final CharSequence beforeWithoutLast = before.subSequence(0, before.length() - lastBeforeLength);
final CharSequence beforeCursor = getTextBeforeCursor(beforeWithoutLast.length(), 0);
if (beforeCursor.length() == beforeWithoutLast.length()) {
boolean same = true;
// CharSequence has undefined equals
for (int i = 0; i < beforeCursor.length(); i++) {
if (beforeCursor.charAt(i) != beforeWithoutLast.charAt(i)) {
same = false;
break;
}
}
if (same) {
before = beforeWithoutLast;
}
}
}
}
}

// Going backward, find the first breaking point (separator)
int startIndexInBefore = before.length();
while (startIndexInBefore > 0) {
Expand Down

0 comments on commit ce0bf06

Please sign in to comment.