Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions shell/platform/android/io/flutter/plugin/editing/FlutterTextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,134 @@ public int getOffsetBefore(CharSequence text, int offset) {

return offset - deleteCharCount;
}

/**
* Gets the offset of the next character following the given offset, with consideration for
* multi-byte characters.
*
* @see <a target="_new"
* href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android10-s3-release/core/java/android/text/method/BaseKeyListener.java#111">https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android10-s3-release/core/java/android/text/method/BaseKeyListener.java#111</a>
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link the android logic that is also linked in getOffsetBefore()? Will be helpful for future readers.

public int getOffsetAfter(CharSequence text, int offset) {
final int len = text.length();

if (offset >= len - 1) {
return len;
}

int codePoint = Character.codePointAt(text, offset);
int nextCharCount = Character.charCount(codePoint);
int nextOffset = offset + nextCharCount;

if (nextOffset == 0) {
return 0;
}

// Line Feed
if (codePoint == LINE_FEED) {
codePoint = Character.codePointAt(text, nextOffset);
if (codePoint == CARRIAGE_RETURN) {
++nextCharCount;
}
return offset + nextCharCount;
}

// Flags
if (isRegionalIndicatorSymbol(codePoint)) {
if (nextOffset >= len - 1
|| !isRegionalIndicatorSymbol(Character.codePointAt(text, nextOffset))) {
return offset + nextCharCount;
}
// In this case there are at least two regional indicator symbols ahead of
// offset. If those two regional indicator symbols are a pair that
// represent a region together, the next offset should be after both of
// them.
int regionalIndicatorSymbolCount = 0;
int regionOffset = offset;
while (regionOffset > 0
&& isRegionalIndicatorSymbol(Character.codePointBefore(text, offset))) {
regionOffset -= Character.charCount(Character.codePointBefore(text, offset));
regionalIndicatorSymbolCount++;
}
if (regionalIndicatorSymbolCount % 2 == 0) {
nextCharCount += 2;
}
return offset + nextCharCount;
}

// Keycaps
if (isKeycapBase(codePoint)) {
nextCharCount += Character.charCount(codePoint);
}
if (codePoint == COMBINING_ENCLOSING_KEYCAP) {
codePoint = Character.codePointBefore(text, nextOffset);
nextOffset += Character.charCount(codePoint);
if (nextOffset < len && isVariationSelector(codePoint)) {
int tmpCodePoint = Character.codePointAt(text, nextOffset);
if (isKeycapBase(tmpCodePoint)) {
nextCharCount += Character.charCount(codePoint) + Character.charCount(tmpCodePoint);
}
} else if (isKeycapBase(codePoint)) {
nextCharCount += Character.charCount(codePoint);
}
return offset + nextCharCount;
}

if (isEmoji(codePoint)) {
boolean isZwj = false;
int lastSeenVariantSelectorCharCount = 0;
do {
if (isZwj) {
nextCharCount += Character.charCount(codePoint) + lastSeenVariantSelectorCharCount + 1;
isZwj = false;
}
lastSeenVariantSelectorCharCount = 0;
if (isEmojiModifier(codePoint)) {
break;
}

if (nextOffset < len) {
codePoint = Character.codePointAt(text, nextOffset);
nextOffset += Character.charCount(codePoint);
if (codePoint == COMBINING_ENCLOSING_KEYCAP) {
codePoint = Character.codePointBefore(text, nextOffset);
nextOffset += Character.charCount(codePoint);
if (nextOffset < len && isVariationSelector(codePoint)) {
int tmpCodePoint = Character.codePointAt(text, nextOffset);
if (isKeycapBase(tmpCodePoint)) {
nextCharCount += Character.charCount(codePoint) + Character.charCount(tmpCodePoint);
}
} else if (isKeycapBase(codePoint)) {
nextCharCount += Character.charCount(codePoint);
}
return offset + nextCharCount;
}
if (isEmojiModifier(codePoint)) {
nextCharCount += lastSeenVariantSelectorCharCount + Character.charCount(codePoint);
break;
}
if (isVariationSelector(codePoint)) {
nextCharCount += lastSeenVariantSelectorCharCount + Character.charCount(codePoint);
break;
}
if (codePoint == ZERO_WIDTH_JOINER) {
isZwj = true;
codePoint = Character.codePointAt(text, nextOffset);
nextOffset += Character.charCount(codePoint);
if (nextOffset < len && isVariationSelector(codePoint)) {
codePoint = Character.codePointAt(text, nextOffset);
lastSeenVariantSelectorCharCount = Character.charCount(codePoint);
nextOffset += Character.charCount(codePoint);
}
}
}

if (nextOffset >= len) {
break;
}
} while (isZwj && isEmoji(codePoint));
}

return offset + nextCharCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,21 +343,23 @@ public boolean sendKeyEvent(KeyEvent event) {
int selStart = Selection.getSelectionStart(mEditable);
int selEnd = Selection.getSelectionEnd(mEditable);
if (selStart == selEnd && !event.isShiftPressed()) {
int newSel = Math.max(selStart - 1, 0);
int newSel = Math.max(flutterTextUtils.getOffsetBefore(mEditable, selStart), 0);
setSelection(newSel, newSel);
} else {
int newSelEnd = Math.max(selEnd - 1, 0);
int newSelEnd = Math.max(flutterTextUtils.getOffsetBefore(mEditable, selEnd), 0);
setSelection(selStart, newSelEnd);
}
return true;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
int selStart = Selection.getSelectionStart(mEditable);
int selEnd = Selection.getSelectionEnd(mEditable);
if (selStart == selEnd && !event.isShiftPressed()) {
int newSel = Math.min(selStart + 1, mEditable.length());
int newSel =
Math.min(flutterTextUtils.getOffsetAfter(mEditable, selStart), mEditable.length());
setSelection(newSel, newSel);
} else {
int newSelEnd = Math.min(selEnd + 1, mEditable.length());
int newSelEnd =
Math.min(flutterTextUtils.getOffsetAfter(mEditable, selEnd), mEditable.length());
setSelection(selStart, newSelEnd);
}
return true;
Expand Down
Loading