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:
+10
@@ -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.
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user