From aa0d91a49f971cb2995950f018fad2a14401e77f Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 11 Nov 2026 19:29:39 -0800 Subject: [PATCH] term: Send current focus immediatly when focus tracking starts (#8325) Other terminals do this when enabling focus tracking, and some programs behave much better when they get it. For instance, vim enables focus tracking when resumed after being backgrounded, and then processes things on the focus event, such as noticing if a file was changed. Without this patch: saving a file in vim, Ctrl-Z, `cargo fmt --all`, and `fg`; vim doesn't immediately notice the file was changed to reload it. (and then, you might edit the file further, and get a conflict later when vim notices) With this patch: vim notices the changes right away, making for a smoother experience. --- term/src/terminalstate/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs index 6fc0e7f05..f2ad3a514 100644 --- a/term/src/terminalstate/mod.rs +++ b/term/src/terminalstate/mod.rs @@ -905,8 +905,7 @@ impl TerminalState { } } if self.focus_tracking { - write!(self.writer, "{}{}", CSI, if focused { "I" } else { "O" }).ok(); - self.writer.flush().ok(); + self.send_focus_changed(focused); } self.focused = focused; if !focused { @@ -914,6 +913,11 @@ impl TerminalState { } } + fn send_focus_changed(&mut self, focused: bool) { + write!(self.writer, "{CSI}{}", if focused { "I" } else { "O" }).ok(); + self.writer.flush().ok(); + } + /// Returns true if there is new output since the terminal /// lost focus pub fn has_unseen_output(&self) -> bool { @@ -1926,6 +1930,8 @@ impl TerminalState { Mode::SetDecPrivateMode(DecPrivateMode::Code(DecPrivateModeCode::FocusTracking)) => { self.focus_tracking = true; self.last_mouse_move.take(); + // For compatibility with other terminals, send a focus event right away. + self.send_focus_changed(self.focused); } Mode::ResetDecPrivateMode(DecPrivateMode::Code(DecPrivateModeCode::FocusTracking)) => { self.focus_tracking = false;