Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,9 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
KeyCode.ARROW_DOWN
}
repeat(abs(steps)) {
// up at the start or down at the end of the text moves the focus to another field (#891)
val next = if (steps < 0) connection.getTextBeforeCursorFromEditor(1) else connection.getTextAfterCursor(1, 0)
if (next != null && next.isEmpty()) return true
onCodeInput(code, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false)
}
return true
Expand Down Expand Up @@ -1058,6 +1061,14 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
inputLogic.finishInput()
}

// An arrow key at the edge of the text moves the focus to another field (#891), so stop at the edge.
// Steps < 0 move back in the text (also for RTL). Without an answer from the editor, move as before.
val available = if (steps < 0) connection.getTextBeforeCursorFromEditor(abs(steps))
else connection.getTextAfterCursor(abs(steps), 0)
val moveCount = if (available == null) abs(steps)
else minOf(abs(steps), Character.codePointCount(available, 0, available.length))
if (moveCount == 0) return true

cursorMoveStepCount++
val shouldHaptic = cursorMoveStepCount % 2 != 0
val code = if (steps < 0) {
Expand All @@ -1067,7 +1078,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
if (shouldHaptic) gestureMoveForwardHaptics(true)
if (rtl) KeyCode.ARROW_LEFT else KeyCode.ARROW_RIGHT
}
repeat(abs(steps)) {
repeat(moveCount) {
onCodeInput(code, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false)
}
return true
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/helium314/keyboard/latin/RichInputConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,17 @@ class RichInputConnection(private val mParent: InputMethodService) : PrivateComm
return !TextUtils.isEmpty(after)
}

/** Text before the cursor asked from the editor, not from the cache: arrow key events don't update the cache */
fun getTextBeforeCursorFromEditor(n: Int): CharSequence? {
if (n <= 0 || !isConnected()) return null
return try {
mIC?.getTextBeforeCursor(n, 0)
} catch (e: Exception) {
Log.w(TAG, "getTextBeforeCursor threw in host app", e)
null
}
}

fun hasComposingText(): Boolean = mComposingText.isNotEmpty()

fun getCursorCapsMode(
Expand Down
Loading