Skip to content

Commit

Permalink
android cursor position
Browse files Browse the repository at this point in the history
  • Loading branch information
perunt committed Mar 24, 2023
1 parent 9837731 commit 1c2d784
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,16 @@ public ReactSelectionWatcher(ReactEditText editText) {

@Override
public void onSelectionChanged(int start, int end) {
// Calculate cursor position
// Credit: yuku
// https://stackoverflow.com/questions/5044342/how-to-get-cursor-position-x-y-in-edittext-android
Layout layout = mReactEditText.getLayout();
int line = layout.getLineForOffset(start);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
float cursorPositionX = layout.getPrimaryHorizontal(start);
float cursorPositionY = baseline + ascent;

// Android will call us back for both the SELECTION_START span and SELECTION_END span in text
// To prevent double calling back into js we cache the result of the previous call and only
// forward it on if we have new values
Expand All @@ -1210,7 +1220,12 @@ public void onSelectionChanged(int start, int end) {
if (mPreviousSelectionStart != realStart || mPreviousSelectionEnd != realEnd) {
mEventDispatcher.dispatchEvent(
new ReactTextInputSelectionEvent(
mSurfaceId, mReactEditText.getId(), realStart, realEnd));
mSurfaceId,
mReactEditText.getId(),
realStart,
realEnd,
PixelUtil.toDIPFromPixel(cursorPositionX),
PixelUtil.toDIPFromPixel(cursorPositionY)));

mPreviousSelectionStart = realStart;
mPreviousSelectionEnd = realEnd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,31 @@

private int mSelectionStart;
private int mSelectionEnd;
private float mCursorPositionX;
private float mCursorPositionY;

@Deprecated
public ReactTextInputSelectionEvent(int viewId, int selectionStart, int selectionEnd) {
this(-1, viewId, selectionStart, selectionEnd);
public ReactTextInputSelectionEvent(int viewId,
int selectionStart,
int selectionEnd,
float cursorPositionX,
float cursorPositionY) {
this(-1, viewId, selectionStart, selectionEnd, cursorPositionX, cursorPositionY);
}

public ReactTextInputSelectionEvent(
int surfaceId, int viewId, int selectionStart, int selectionEnd) {
int surfaceId,
int viewId,
int selectionStart,
int selectionEnd,
float cursorPositionX,
float cursorPositionY
) {
super(surfaceId, viewId);
mSelectionStart = selectionStart;
mSelectionEnd = selectionEnd;
mCursorPositionX = cursorPositionX;
mCursorPositionY = cursorPositionY;
}

@Override
Expand All @@ -45,6 +59,8 @@ protected WritableMap getEventData() {
WritableMap selectionData = Arguments.createMap();
selectionData.putInt("end", mSelectionEnd);
selectionData.putInt("start", mSelectionStart);
selectionData.putDouble("cursorPositionX", mCursorPositionX);
selectionData.putDouble("cursorPositionY", mCursorPositionY);

eventData.putMap("selection", selectionData);
return eventData;
Expand Down

0 comments on commit 1c2d784

Please sign in to comment.