improve emoji deletion

This commit is contained in:
Helium314 2023-07-19 17:05:53 +02:00
parent 178654b0e9
commit ffe7d81ebc
4 changed files with 45 additions and 26 deletions

View file

@ -40,6 +40,7 @@ import org.dslul.openboard.inputmethod.keyboard.internal.KeyDrawParams;
import org.dslul.openboard.inputmethod.keyboard.internal.KeyVisualAttributes;
import org.dslul.openboard.inputmethod.latin.R;
import org.dslul.openboard.inputmethod.latin.common.Constants;
import org.dslul.openboard.inputmethod.latin.common.StringUtils;
import org.dslul.openboard.inputmethod.latin.settings.Settings;
import org.dslul.openboard.inputmethod.latin.settings.SettingsValues;
import org.dslul.openboard.inputmethod.latin.suggestions.MoreSuggestionsView;
@ -464,7 +465,7 @@ public class KeyboardView extends View {
// set key color only if not in emoji keyboard range
if (keyboard != null
&& (this.getClass() == MoreSuggestionsView.class ?
!containsEmoji(key.getLabel()) : // doesn't contain emoji (all can happen in MoreSuggestionsView)
!StringUtils.probablyContainsEmoji(key.getLabel()) : // doesn't contain emoji (all can happen in MoreSuggestionsView)
(keyboard.mId.mElementId < 10 || keyboard.mId.mElementId > 26) // not showing emoji keyboard (no emojis visible on main keyboard otherwise)
))
paint.setColorFilter(keyTextColorFilter);
@ -559,15 +560,6 @@ public class KeyboardView extends View {
}
}
private static boolean containsEmoji(String s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!(c <= 0xD7FF || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
return true;
}
return false;
}
// Draw popup hint "..." at the bottom right corner of the key.
protected void drawKeyPopupHint(@Nonnull final Key key, @Nonnull final Canvas canvas,
@Nonnull final Paint paint, @Nonnull final KeyDrawParams params) {

View file

@ -711,4 +711,24 @@ public final class StringUtils {
}
return false;
}
public static boolean probablyContainsEmoji(String s) {
int offset = 0;
int length = s.length();
while (offset < length) {
int c = Character.codePointAt(s, offset);
if (probablyIsEmojiCodePoint(c))
return true;
offset += Character.charCount(c);
}
return false;
}
// seemingly arbitrary ranges taken from "somewhere on the internet"
public static boolean probablyIsEmojiCodePoint(int c) {
return (0x200D <= c && c <= 0x3299) // ??
|| (0x1F004 <= c && c <= 0x1F251) // ??
|| (0x1F300 <= c && c <= 0x1FFFF) // ??
|| c == 0xFE0F; // variation selector emoji with color
}
}

View file

@ -1187,24 +1187,30 @@ public final class InputLogic {
}
final int lengthToDelete =
Character.isSupplementaryCodePoint(codePointBeforeCursor) ? 2 : 1;
mConnection.deleteTextBeforeCursor(lengthToDelete);
int totalDeletedLength = lengthToDelete;
if (mDeleteCount > Constants.DELETE_ACCELERATE_AT) {
// If this is an accelerated (i.e., double) deletion, then we need to
// consider unlearning here because we may have already reached
// the previous word, and will lose it after next deletion.
hasUnlearnedWordBeingDeleted |= unlearnWordBeingDeleted(
inputTransaction.getMSettingsValues(), currentKeyboardScriptId);
final int codePointBeforeCursorToDeleteAgain =
mConnection.getCodePointBeforeCursor();
if (codePointBeforeCursorToDeleteAgain != Constants.NOT_A_CODE) {
final int lengthToDeleteAgain = Character.isSupplementaryCodePoint(
codePointBeforeCursorToDeleteAgain) ? 2 : 1;
mConnection.deleteTextBeforeCursor(lengthToDeleteAgain);
totalDeletedLength += lengthToDeleteAgain;
if (StringUtils.probablyIsEmojiCodePoint(codePointBeforeCursor)) {
// emoji length varies, so we'd need to find out length to delete correctly
// this is not optimal, but a reasonable workaround for issues when trying to delete emojis
sendDownUpKeyEvent(KeyEvent.KEYCODE_DEL);
} else {
mConnection.deleteTextBeforeCursor(lengthToDelete);
int totalDeletedLength = lengthToDelete;
if (mDeleteCount > Constants.DELETE_ACCELERATE_AT) {
// If this is an accelerated (i.e., double) deletion, then we need to
// consider unlearning here because we may have already reached
// the previous word, and will lose it after next deletion.
hasUnlearnedWordBeingDeleted |= unlearnWordBeingDeleted(
inputTransaction.getMSettingsValues(), currentKeyboardScriptId);
final int codePointBeforeCursorToDeleteAgain =
mConnection.getCodePointBeforeCursor();
if (codePointBeforeCursorToDeleteAgain != Constants.NOT_A_CODE) {
final int lengthToDeleteAgain = Character.isSupplementaryCodePoint(
codePointBeforeCursorToDeleteAgain) ? 2 : 1;
mConnection.deleteTextBeforeCursor(lengthToDeleteAgain);
totalDeletedLength += lengthToDeleteAgain;
}
}
StatsUtils.onBackspacePressed(totalDeletedLength);
}
StatsUtils.onBackspacePressed(totalDeletedLength);
}
}
if (!hasUnlearnedWordBeingDeleted) {