xterm: report focus changes (?1004)

Terminal.focusInput sends CSI I / CSI O while the program has enabled
focus reporting, and TerminalView calls it when its focus node gains or
loses focus (not for read-only views). The mode was parsed but never
acted on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:37:09 +00:00
parent 9b90806093
commit 822d77b1df
3 changed files with 72 additions and 0 deletions
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'xterm_fixture.dart';
void main() {
test('focusInput sends CSI I / CSI O only with ?1004 set', () {
final t = FixtureTerminal();
expect(t.terminal.focusInput(true), isFalse);
expect(t.takeReplies(), '');
t.feed('\x1b[?1004h');
expect(t.terminal.focusInput(true), isTrue);
t.terminal.focusInput(false);
expect(t.takeReplies(), '\x1b[I\x1b[O');
t.feed('\x1b[?1004l');
t.terminal.focusInput(true);
expect(t.takeReplies(), '');
});
testWidgets('TerminalView reports its focus changes', (tester) async {
final terminal = Terminal();
final replies = <String>[];
terminal.onOutput = replies.add;
final focus = FocusNode();
addTearDown(focus.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: TerminalView(
terminal,
focusNode: focus,
hardwareKeyboardOnly: true,
),
),
),
);
terminal.write('\x1b[?1004h');
focus.requestFocus();
await tester.pump();
focus.unfocus();
await tester.pump();
expect(replies.join(), '\x1b[I\x1b[O');
});
}
+10
View File
@@ -407,6 +407,16 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
return false;
}
/// Tells the program the terminal gained or lost focus (`CSI I` /
/// `CSI O`) when it enabled focus reporting with `CSI ? 1004 h`; returns
/// whether anything was sent. [TerminalView] calls this when its focus
/// changes.
bool focusInput(bool focused) {
if (!_reportFocusMode) return false;
onOutput?.call(focused ? '\x1b[I' : '\x1b[O');
return true;
}
/// Resize the terminal screen. [newWidth] and [newHeight] should be greater
/// than 0. Text reflow is currently not implemented and will be avaliable in
/// the future.
+16
View File
@@ -174,6 +174,8 @@ class TerminalViewState extends State<TerminalView> {
@override
void initState() {
_focusNode = widget.focusNode ?? FocusNode();
_focusNode.addListener(_onFocusChange);
_hadFocus = _focusNode.hasFocus;
_controller = widget.controller ?? TerminalController();
_scrollController = widget.scrollController ?? ScrollController();
_shortcutManager = ShortcutManager(
@@ -182,13 +184,26 @@ class TerminalViewState extends State<TerminalView> {
super.initState();
}
bool _hadFocus = false;
/// Reports focus changes to a program that asked for them (`CSI ? 1004 h`).
void _onFocusChange() {
final hasFocus = _focusNode.hasFocus;
if (hasFocus == _hadFocus) return;
_hadFocus = hasFocus;
if (!widget.readOnly) widget.terminal.focusInput(hasFocus);
}
@override
void didUpdateWidget(TerminalView oldWidget) {
if (oldWidget.focusNode != widget.focusNode) {
_focusNode.removeListener(_onFocusChange);
if (oldWidget.focusNode == null) {
_focusNode.dispose();
}
_focusNode = widget.focusNode ?? FocusNode();
_focusNode.addListener(_onFocusChange);
_hadFocus = _focusNode.hasFocus;
}
if (oldWidget.controller != widget.controller) {
if (oldWidget.controller == null) {
@@ -208,6 +223,7 @@ class TerminalViewState extends State<TerminalView> {
@override
void dispose() {
_focusNode.removeListener(_onFocusChange);
if (widget.focusNode == null) {
_focusNode.dispose();
}