Skip to content

Commit 555711a

Browse files
committed
fix(input): throttle held delete acceleration while the editor lags
Extra deletes are sent only while the editor has applied the earlier ones, so a slow editor queues at most about one repeat's worth and deletion stops when the key is released. Editors that keep up keep the full speed. Fixes #972 (#971)
1 parent 260245d commit 555711a

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

‎app/src/main/java/helium314/keyboard/latin/LatinIME.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ class LatinIME : InputMethodService(),
943943
if (!isImeSuppressedByHardwareKeyboard() || currentSettingsValues.mHasHardwareKeyboard) {
944944
inputLogic.startInput(richImm.combiningRulesExtraValueOfCurrentSubtype, currentSettingsValues)
945945
resetDictionaryFacilitatorIfNecessary()
946-
946+
inputLogic.connection.onEditorReportedSelection(editorInfo.initialSelStart)
947+
947948
if (!inputLogic.connection.resetCachesUponCursorMoveAndReturnSuccess(editorInfo.initialSelStart, editorInfo.initialSelEnd, false)) {
948949
handler.postResetCaches(isDifferentTextField, 5)
949950
needToCallLoadKeyboardLater = true
@@ -1191,6 +1192,7 @@ class LatinIME : InputMethodService(),
11911192
composingSpanStart: Int, composingSpanEnd: Int
11921193
) {
11931194
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, composingSpanStart, composingSpanEnd)
1195+
inputLogic.connection.onEditorReportedSelection(newSelStart)
11941196
voiceInputManager?.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, composingSpanStart, composingSpanEnd)
11951197
val cjkManager = CjkInputManager.getInstance(this)
11961198
if (cjkManager.isCjkSubtypeActive(richImm.currentSubtype)) {

‎app/src/main/java/helium314/keyboard/latin/RichInputConnection.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class RichInputConnection(private val mParent: InputMethodService) : PrivateComm
7676
private val mCommittedTextBeforeComposingText = StringBuilder()
7777
private val mComposingText = StringBuilder()
7878
private var mPendingInFlightDeletions = 0
79+
// cursor start the editor last reported, to see how far it lags behind what we sent
80+
private var mReportedSelStart = INVALID_CURSOR_POSITION
7981

8082
private val mTempObjectForCommitText = SpannableStringBuilder()
8183

@@ -117,9 +119,24 @@ class RichInputConnection(private val mParent: InputMethodService) : PrivateComm
117119
mNestLevel = 0
118120
mIsActive = true
119121
mPendingInFlightDeletions = 0
122+
mReportedSelStart = INVALID_CURSOR_POSITION
120123
mIC = refreshInputConnection()
121124
}
122125

126+
/** Called for every selection update from the editor, before it is compared with the expected one. */
127+
fun onEditorReportedSelection(selStart: Int) {
128+
mReportedSelStart = selStart
129+
}
130+
131+
/**
132+
* How many chars we deleted that the editor has not applied yet: it handles our edits later, so
133+
* held delete can run ahead of it. 0 if unknown, e.g. before the editor reported a cursor.
134+
*/
135+
fun unappliedDeletionCount(): Int {
136+
if (mReportedSelStart < 0 || mExpectedSelStart < 0 || mExpectedSelStart != mExpectedSelEnd) return 0
137+
return (mReportedSelStart - mExpectedSelStart).coerceAtLeast(0)
138+
}
139+
123140
fun onFinishInput() {
124141
// Close any active batch edit on the underlying InputConnection before releasing
125142
// the reference - prevents leaving the IC in a dangling open-batch state if a
@@ -138,6 +155,7 @@ class RichInputConnection(private val mParent: InputMethodService) : PrivateComm
138155
lastCursorAnchorInfo = null
139156
mNestLevel = 0
140157
mPendingInFlightDeletions = 0
158+
mReportedSelStart = INVALID_CURSOR_POSITION
141159
}
142160

143161
private fun checkConsistencyForDebug() {

‎app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ class InputLogic(
16161616
private fun getBackspaceExtraDeletes(deleteCount: Int): Int {
16171617
if (deleteCount <= Constants.DELETE_ACCELERATE_AT) return 0
16181618
val count = deleteCount - Constants.DELETE_ACCELERATE_AT
1619-
return when {
1619+
val extraDeletes = when {
16201620
count < 10 -> 1
16211621
count < 20 -> 2
16221622
count < 30 -> 3
@@ -1626,6 +1626,10 @@ class InputLogic(
16261626
count < 70 -> 13
16271627
else -> (13 + (count - 70) / 2).coerceAtMost(20)
16281628
}
1629+
// A slow editor queues deletes it has not applied yet, and they go on deleting after the key is
1630+
// released (#972). Send extra deletes only while it keeps up, so the queue stays at one repeat's worth.
1631+
val unapplied = mConnection.unappliedDeletionCount()
1632+
return extraDeletes.coerceAtMost((MAX_UNAPPLIED_BACKSPACE_DELETES - unapplied).coerceAtLeast(0))
16291633
}
16301634

16311635
/**
@@ -3541,6 +3545,8 @@ class InputLogic(
35413545
/** longer text typed in place of a deleted swipe is not a single word correcting it */
35423546
private const val MAX_RETYPED_SWIPE_LENGTH = 48
35433547
private const val KEY_REPEAT_INTERVAL_MS = 50
3548+
// most chars held delete may have sent that the editor has not applied, about one fast repeat's worth
3549+
private const val MAX_UNAPPLIED_BACKSPACE_DELETES = 21
35443550
/** interval before the second word, it gets shorter with each word until the minimum */
35453551
private const val BACKSPACE_WORD_START_INTERVAL = 350f
35463552
private const val BACKSPACE_WORD_ACCELERATION = 0.8f

‎app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,40 @@ class InputLogicTest {
17851785
assertEquals("", text)
17861786
}
17871787

1788+
@Test fun `held backspace sends no extra deletes while the editor lags behind`() {
1789+
reset()
1790+
setText("x".repeat(120) + " ")
1791+
connection.onEditorReportedSelection(selectionStart) // the test field reports no initial cursor
1792+
// the editor applies nothing it is sent, so 20 single deletes are still in flight
1793+
for (i in 1..20) {
1794+
functionalKeyPress(KeyCode.DELETE, isKeyRepeat = true, editorReports = false)
1795+
}
1796+
assertEquals(101, text.length)
1797+
// accelerated repeat, but the editor has not caught up: only the normal single delete
1798+
functionalKeyPress(KeyCode.DELETE, isKeyRepeat = true, editorReports = false)
1799+
assertEquals(100, text.length)
1800+
functionalKeyPress(KeyCode.DELETE, isKeyRepeat = true) // editor catches up after this one
1801+
assertEquals(99, text.length)
1802+
// caught up: acceleration is back (1 + 1 extra)
1803+
functionalKeyPress(KeyCode.DELETE, isKeyRepeat = true)
1804+
assertEquals(97, text.length)
1805+
}
1806+
1807+
@Test fun `held backspace in a lagging web editor queues at most one repeat of extra deletes`() {
1808+
reset()
1809+
currentInputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT
1810+
setText("x".repeat(200) + " ")
1811+
connection.onEditorReportedSelection(selectionStart) // the test field reports no initial cursor
1812+
for (i in 1..60) {
1813+
functionalKeyPress(KeyCode.DELETE, isKeyRepeat = true, editorReports = false)
1814+
}
1815+
// 60 normal deletes plus the one extra that still fit (unthrottled: 60 + 105 extra)
1816+
assertEquals(201 - 61, text.length)
1817+
connection.onEditorReportedSelection(selectionStart) // the editor catches up
1818+
functionalKeyPress(KeyCode.DELETE, isKeyRepeat = true)
1819+
assertEquals(201 - 61 - 7, text.length) // 61st delete: 1 + 6 extra
1820+
}
1821+
17881822
@Test fun `key repeat backspace does not restart intermediate composing words`() {
17891823
reset()
17901824
setText("first second ")
@@ -2132,10 +2166,12 @@ class InputLogicTest {
21322166
functionalKeyPress(KeyCode.SHIFT)
21332167
}
21342168

2135-
private fun functionalKeyPress(keyCode: Int, isKeyRepeat: Boolean = false) {
2169+
// editorReports = false: the editor has not yet told the keyboard where its cursor is now, like a slow app
2170+
private fun functionalKeyPress(keyCode: Int, isKeyRepeat: Boolean = false, editorReports: Boolean = true) {
21362171
require(keyCode < 0) { "not a functional key code: $keyCode" }
21372172
latinIME.onEvent(Event.createSoftwareKeypressEvent(Event.NOT_A_CODE_POINT, keyCode, 0, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, isKeyRepeat))
21382173
handleMessages()
2174+
if (editorReports) connection.onEditorReportedSelection(selectionStart)
21392175
checkConnectionConsistency()
21402176
}
21412177

0 commit comments

Comments
 (0)