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
+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();
}