Terminal: text input for IMEs, Capture browser keys, touch key row

- Desktop and web terminals use the text-input path too, so IMEs, dead
  keys, the emoji picker and dictation work; the key handler still
  encodes special keys and chords first (handled keys are never typed
  again), composing text is drawn at the cursor and sent on commit, the
  platform editable sits at the cursor, and a closed input reopens on
  the next key. The editable terminal is a semantics text field so web
  typing also works with semantics on.
- Web: "Capture browser keys" (terminal menu and right-click menu) goes
  full screen and calls navigator.keyboard.lock(); hold Esc to leave.
  No-op where unsupported; cheatsheet and menu note give alternatives.
- Touch: scrollable key row with sticky Ctrl/Alt (tap once, double-tap
  lock), Esc, Tab, arrows, Home/End, PgUp/PgDn and symbols through the
  hardware encoder; soft keyboard types per key (delete detection,
  Return as Enter); the line field moves behind the row's Line button.
- Capture kit: compose=, --semantics false, --user-agent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 02:43:57 +00:00
parent e526150b1e
commit c5af000453
36 changed files with 1730 additions and 173 deletions
+13
View File
@@ -52,6 +52,7 @@ class TerminalView extends StatefulWidget {
this.readOnly = false,
this.hardwareKeyboardOnly = false,
this.simulateScroll = true,
this.onTextInput,
});
/// The underlying terminal that this widget renders.
@@ -143,6 +144,12 @@ class TerminalView extends StatefulWidget {
/// also prevent any on-screen keyboard to be shown.
final bool hardwareKeyboardOnly;
/// Receives committed text from the text input (an IME, dead keys, the
/// emoji picker, dictation, a soft keyboard) instead of the default, which
/// types it as a key where one matches and as text otherwise. Composing
/// text is never passed here: it is drawn at the cursor until committed.
final void Function(String text)? onTextInput;
/// If true, when the terminal is in alternate buffer (for example running
/// vim, man, etc), if the application does not declare that it can handle
/// scrolling, the terminal will simulate scrolling by sending up/down arrow
@@ -412,6 +419,12 @@ class TerminalViewState extends State<TerminalView> {
}
void _onInsert(String text) {
final onTextInput = widget.onTextInput;
if (onTextInput != null) {
onTextInput(text);
_scrollToBottom();
return;
}
final key = charToTerminalKey(text.trim());
// On mobile platforms there is no guarantee that virtual keyboard will
+47 -7
View File
@@ -87,12 +87,27 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
@override
Widget build(BuildContext context) {
return Focus(
final child = Focus(
focusNode: widget.focusNode,
autofocus: widget.autofocus,
onKeyEvent: _onKeyEvent,
includeSemantics: widget.readOnly,
child: widget.child,
);
if (widget.readOnly) return child;
// An editable terminal is a focusable text field to assistive
// technology, as EditableText is. On the web with semantics on, the
// browser's text input is this node's own element, so typing needs it
// focused: the node is a leaf (the scroll view's semantics left out),
// which the engine keeps focused instead of moving the focus away.
return Semantics(
textField: true,
enabled: true,
focusable: true,
focused: widget.focusNode.hasFocus,
onFocus: requestKeyboard,
child: ExcludeSemantics(child: child),
);
}
bool get hasInputConnection => _connection != null && _connection!.attached;
@@ -121,24 +136,43 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
return;
}
// The editable sits at the cursor, so an IME's candidate window (and
// the web's hidden input) opens there; the caret is in its own space.
_connection?.setEditableSizeAndTransform(
rect.size,
Matrix4.translationValues(0, 0, 0),
Matrix4.translationValues(rect.left, rect.top, 0),
);
_connection?.setCaretRect(caretRect);
_connection?.setCaretRect(caretRect.shift(-rect.topLeft));
}
void _onFocusChange() {
_openOrCloseInputConnectionIfNeeded();
// The semantics' focused flag follows.
if (mounted) setState(() {});
}
KeyEventResult _onKeyEvent(FocusNode focusNode, KeyEvent event) {
if (_currentEditingState.composing.isCollapsed) {
return widget.onKeyEvent(focusNode, event);
if (!_currentEditingState.composing.isCollapsed) {
return KeyEventResult.skipRemainingHandlers;
}
final result = widget.onKeyEvent(focusNode, event);
if (result != KeyEventResult.ignored || widget.readOnly) return result;
// A printing key the handlers left alone is typed by the text input. If
// the platform closed it (a browser window that lost focus, say), the
// key's own character is typed and the input reopened for what follows.
if (hasInputConnection || event is KeyUpEvent) return result;
final character = event.character;
if (character == null || !_printable(character)) return result;
if (widget.focusNode.hasFocus) _openInputConnection();
widget.onInsert(character);
return KeyEventResult.handled;
}
return KeyEventResult.skipRemainingHandlers;
static bool _printable(String text) {
if (text.isEmpty) return false;
final code = text.runes.first;
return code >= 0x20 && code != 0x7f;
}
void _openOrCloseInputConnectionIfNeeded() {
@@ -254,9 +288,15 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
// print('showAutocorrectionPromptRect');
}
/// The platform ended the connection (a browser closes it when its window
/// loses focus): it is forgotten, with any unfinished composition, so the
/// next key or focus opens a new one.
@override
void connectionClosed() {
// print('connectionClosed');
_connection?.connectionClosedReceived();
_connection = null;
if (!_currentEditingState.composing.isCollapsed) widget.onComposing(null);
_currentEditingState = _initEditingState.copyWith();
}
@override
+7 -4
View File
@@ -166,6 +166,8 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
markNeedsLayout();
}
/// The IME's composing (preedit) text drawn at the cursor, if any.
String? get composingText => _composingText;
String? _composingText;
set composingText(String? value) {
if (value == _composingText) return;
@@ -497,10 +499,6 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
if (_terminal.buffer.absoluteCursorY >= effectFirstLine &&
_terminal.buffer.absoluteCursorY <= effectLastLine) {
if (_isComposingText) {
_paintComposingText(canvas, offset + cursorOffset);
}
if (_shouldShowCursor) {
final buffer = _terminal.buffer;
final cell = CellData.empty();
@@ -514,6 +512,11 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
cell: _isComposingText ? null : cell,
);
}
// The IME's preedit is drawn over the cursor, so a block cursor does
// not hide its first character.
if (_isComposingText) {
_paintComposingText(canvas, offset + cursorOffset);
}
}
_paintHighlights(