@devries-cameron20 / t3code-1 / commits / 0d8958baaf
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): Android terminal Backspace deletes on the first press (#24983)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Victor committed Nov 5, 2026 at 18:49 UTC 0d8958baaf64fa8180bd064becac374e2d4f2e6f
3 files changed +178
apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt
+11
@@ -10,6 +10,7 @@ import android.view.KeyEvent
10 import android.view.View
11 import android.view.ViewGroup
12 import android.view.inputmethod.EditorInfo
13 +import android.view.inputmethod.InputConnection
14 import android.view.inputmethod.InputMethodManager
15 import android.widget.EditText
16 import android.widget.FrameLayout
@@ -21,11 +22,19 @@ import kotlin.math.max
22 class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
23 private val container = FrameLayout(context)
24 private val terminalCanvas = TerminalCanvasView(context)
25 + private val sentText = TerminalSentText()
26
27 // Newer Android can request autofill even with importantForAutofill=no.
28 // Reporting no autofill type keeps terminal input out of credential autofill.
29 private val inputView = object : EditText(context) {
30 override fun getAutofillType(): Int = View.AUTOFILL_TYPE_NONE
31 +
32 + override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? =
33 + super.onCreateInputConnection(outAttrs)?.let { connection ->
34 + TerminalInputConnection(connection, sentText, { !readOnly }) { count ->
35 + if (!readOnly) onInput(mapOf("data" to "\u007F".repeat(count)))
36 + }
37 + }
38 }
39 private val onInput by EventDispatcher()
40 private val onResize by EventDispatcher()
@@ -282,6 +291,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
291 if (event.action != KeyEvent.ACTION_DOWN) return@setOnKeyListener false
292 when {
293 keyCode == KeyEvent.KEYCODE_DEL -> {
294 + sentText.removeCodePoints(1)
295 onInput(mapOf("data" to "\u007F"))
296 true
297 }
@@ -304,6 +314,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
314 val end = (start + count).coerceAtMost(s.length)
315 if (start < end) {
316 val insertedText = s.subSequence(start, end).toString()
317 + sentText.append(insertedText)
318 onInput(mapOf("data" to insertedText))
319 }
320 }
apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalInputConnection.kt new
+70
@@ -0,0 +1,70 @@
1 +package expo.modules.t3terminal
2 +
3 +import android.view.inputmethod.InputConnection
4 +import android.view.inputmethod.InputConnectionWrapper
5 +
6 +private const val MAX_DELETE_LENGTH = 1024
7 +
8 +// Text the terminal already received from the hidden input. The input is empty
9 +// by the time an IME deletes, so this is what its UTF-16 lengths refer to.
10 +internal class TerminalSentText {
11 + private val text = StringBuilder()
12 + private var deletedHighSurrogate = false
13 +
14 + fun append(sent: CharSequence) {
15 + discardDeletedSurrogate()
16 + text.append(sent)
17 + if (text.length > MAX_DELETE_LENGTH) text.delete(0, text.length - MAX_DELETE_LENGTH)
18 + }
19 +
20 + fun removeUnits(units: Int): Int {
21 + val start = (text.length - units).coerceAtLeast(0)
22 + val removed = units - (text.length - start) + text.codePointCount(start, text.length) -
23 + if (deletedHighSurrogate && units > 0) 1 else 0
24 + // The low half already sent DEL; retain the high half only for IME unit accounting.
25 + deletedHighSurrogate = start > 0 && start < text.length &&
26 + Character.isHighSurrogate(text[start - 1]) && Character.isLowSurrogate(text[start])
27 + text.setLength(start)
28 + return removed
29 + }
30 +
31 + fun removeCodePoints(count: Int): Int {
32 + discardDeletedSurrogate()
33 + val known = count.coerceAtMost(text.codePointCount(0, text.length))
34 + text.setLength(text.offsetByCodePoints(text.length, -known))
35 + return count
36 + }
37 +
38 + private fun discardDeletedSurrogate() {
39 + if (deletedHighSurrogate) text.setLength(text.length - 1)
40 + deletedHighSurrogate = false
41 + }
42 +}
43 +
44 +// The hidden input clears after every change, so text an IME deletes was
45 +// already sent to the terminal. Some keyboards delete through these calls
46 +// instead of KEYCODE_DEL, so forward them as one DEL per code point.
47 +internal class TerminalInputConnection(
48 + target: InputConnection,
49 + private val sentText: TerminalSentText,
50 + private val canTrackDeletes: () -> Boolean,
51 + private val sendDeletes: (count: Int) -> Unit
52 +) : InputConnectionWrapper(target, true) {
53 + override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean {
54 + if (beforeLength > 0 && canTrackDeletes()) {
55 + forwardDeletes(sentText.removeUnits(beforeLength))
56 + }
57 + return super.deleteSurroundingText(beforeLength, afterLength)
58 + }
59 +
60 + override fun deleteSurroundingTextInCodePoints(beforeLength: Int, afterLength: Int): Boolean {
61 + if (beforeLength > 0 && canTrackDeletes()) {
62 + forwardDeletes(sentText.removeCodePoints(beforeLength))
63 + }
64 + return super.deleteSurroundingTextInCodePoints(beforeLength, afterLength)
65 + }
66 +
67 + private fun forwardDeletes(count: Int) {
68 + sendDeletes(count.coerceAtMost(MAX_DELETE_LENGTH))
69 + }
70 +}
apps/mobile/modules/t3-terminal/android/src/test/java/expo/modules/t3terminal/TerminalInputConnectionTest.kt new
+97
@@ -0,0 +1,97 @@
1 +package expo.modules.t3terminal
2 +
3 +import android.view.inputmethod.EditorInfo
4 +import android.widget.EditText
5 +import org.junit.Assert.assertEquals
6 +import org.junit.Test
7 +import org.junit.runner.RunWith
8 +import org.robolectric.RobolectricTestRunner
9 +import org.robolectric.RuntimeEnvironment
10 +import org.robolectric.annotation.Config
11 +
12 +@RunWith(RobolectricTestRunner::class)
13 +@Config(sdk = [36], manifest = Config.NONE)
14 +class TerminalInputConnectionTest {
15 + private val input = EditText(RuntimeEnvironment.getApplication())
16 + private val sentText = TerminalSentText()
17 + private val deletes = mutableListOf<Int>()
18 + private var writable = true
19 + private val connection = TerminalInputConnection(
20 + input.onCreateInputConnection(EditorInfo())!!,
21 + sentText,
22 + { writable },
23 + ) { if (writable) deletes += it }
24 +
25 + private fun type(text: String) {
26 + connection.commitText(text, 1)
27 + sentText.append(input.text)
28 + input.text.clear()
29 + }
30 +
31 + @Test
32 + fun imeDeletionReachesTheTerminalAfterTheInputClears() {
33 + type("abc")
34 + connection.deleteSurroundingText(1, 0)
35 + connection.deleteSurroundingTextInCodePoints(2, 0)
36 + connection.deleteSurroundingText(Int.MAX_VALUE, 0)
37 +
38 + assertEquals(listOf(1, 2, 1024), deletes)
39 + }
40 +
41 + @Test
42 + fun utf16DeletionOfAnEmojiSendsOneDelete() {
43 + type("a😀")
44 + connection.deleteSurroundingText(2, 0)
45 + connection.deleteSurroundingText(1, 0)
46 +
47 + assertEquals(listOf(1, 1), deletes)
48 +
49 + for (codePoints in listOf(false, true)) {
50 + deletes.clear()
51 + type("a😀")
52 + writable = false
53 + if (codePoints) {
54 + connection.deleteSurroundingTextInCodePoints(1, 0)
55 + } else {
56 + connection.deleteSurroundingText(2, 0)
57 + }
58 + writable = true
59 + connection.deleteSurroundingText(2, 0)
60 + connection.deleteSurroundingText(1, 0)
61 +
62 + assertEquals(listOf(1, 1), deletes)
63 + }
64 +
65 + deletes.clear()
66 + type("a😀")
67 + connection.deleteSurroundingText(1, 0)
68 + connection.deleteSurroundingText(1, 0)
69 + connection.deleteSurroundingText(1, 0)
70 +
71 + assertEquals(listOf(1, 0, 1), deletes)
72 +
73 + deletes.clear()
74 + type("a😀")
75 + connection.deleteSurroundingText(1, 0)
76 + connection.deleteSurroundingTextInCodePoints(1, 0)
77 +
78 + assertEquals(listOf(1, 1), deletes)
79 +
80 + deletes.clear()
81 + type("a😀")
82 + connection.deleteSurroundingText(1, 0)
83 + type("b")
84 + connection.deleteSurroundingText(1, 0)
85 + connection.deleteSurroundingText(1, 0)
86 +
87 + assertEquals(listOf(1, 1, 1), deletes)
88 +
89 + deletes.clear()
90 + type("x😀a")
91 + sentText.removeCodePoints(1)
92 + connection.deleteSurroundingText(2, 0)
93 + connection.deleteSurroundingText(1, 0)
94 +
95 + assertEquals(listOf(1, 1), deletes)
96 + }
97 +}