Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(mobile): keep Gboard's buffer in step when Backspace arrives as a key

bubu-dudube66d9be7f2e
diff --git a/mobile/third_party/xterm/README.autonomous.md b/mobile/third_party/xterm/README.autonomous.md
index 8f6c37cf8..f0fdd2c02 100644
--- a/mobile/third_party/xterm/README.autonomous.md
+++ b/mobile/third_party/xterm/README.autonomous.md
@@ -179,3 +179,16 @@ it if one is dropped.
never swallowed, and Android performs its editor action without the second
insert so nothing there matches. Regression:
`mobile/test/terminal_ime_input_test.dart`.
+
+12. **Backspace sent as a key keeps the keyboard's buffer in step**
+ (`lib/src/ui/custom_text_edit.dart`). Gboard delivers Backspace as a
+ `KeyDownEvent`, not as an edit to its buffer, and the terminal consumed the
+ key — deleting on the pty while the buffer the keyboard edits kept the
+ letter. After "xin chào" and four Backspaces the keyboard still held
+ "xin chào" and appended the next word to it; when Telex re-marked that run,
+ the diff deleted and retyped characters already gone ("chào" came out as
+ "xiaochaof"). On a phone, a plain Backspace over a non-empty buffer is now
+ applied to the buffer itself and handed back to the keyboard, and the pty
+ gets the same single delete through the usual sync. An empty buffer still
+ sends it straight on; Ctrl/Alt/Meta+Backspace and desktop platforms are
+ untouched.
diff --git a/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart b/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart
index b29aa59bb..e0f7b329c 100644
--- a/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart
+++ b/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart
@@ -168,12 +168,47 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
KeyEventResult _onKeyEvent(FocusNode focusNode, KeyEvent event) {
if (_currentEditingState.composing.isCollapsed) {
+ if (_isBufferBackspace(event)) {
+ _cancelPendingDeletes();
+ _applyNativeBackspace();
+ return KeyEventResult.handled;
+ }
return widget.onKeyEvent(focusNode, event);
}
return KeyEventResult.skipRemainingHandlers;
}
+ /// A plain Backspace from a phone keyboard, arriving as a KEY while the native buffer still holds
+ /// text this side has mirrored to the pty.
+ ///
+ /// ⚠️ **Gboard sends Backspace as a key event, not as an edit to its buffer** — and a key the
+ /// terminal consumes deletes on the pty while the buffer the keyboard edits keeps the letter. The
+ /// two then disagree about what is on the line: after "xin chào" and four Backspaces the keyboard
+ /// still holds "xin chào", appends the next word to it ("xin chàochao"), and when Telex re-marks
+ /// that run the diff against it deletes and retypes characters that were already gone — "chào"
+ /// typed again came out as "xiaochaof".
+ ///
+ /// So the deletion is made IN the buffer and the buffer handed back to the keyboard; the pty gets
+ /// the same one delete through [_syncTerminalText]. An empty buffer (a fresh prompt) still sends
+ /// the delete straight on — see [_applyNativeBackspace].
+ ///
+ /// Only without modifiers: Ctrl/Alt+Backspace mean something else to a shell, and only on a phone,
+ /// where the software keyboard is the input method. A desktop IME reaches Backspace through
+ /// `performSelector` instead, which already edits the buffer.
+ bool _isBufferBackspace(KeyEvent event) {
+ if (!_composesThroughSoftwareKeyboard) return false;
+ if (event is! KeyDownEvent && event is! KeyRepeatEvent) return false;
+ if (event.logicalKey != LogicalKeyboardKey.backspace) return false;
+ final keyboard = HardwareKeyboard.instance;
+ if (keyboard.isControlPressed ||
+ keyboard.isAltPressed ||
+ keyboard.isMetaPressed) {
+ return false;
+ }
+ return _currentEditingState.text != _initEditingState.text;
+ }
+
void _openOrCloseInputConnectionIfNeeded() {
if (widget.focusNode.hasFocus && widget.focusNode.consumeKeyboardToken()) {
_openInputConnection();