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.
This commit is contained in:
Josh Triplett authored and GitHub committed 2026-11-12 04:29:39 +01:00
1 parent 458a100c1b
commit aa0d91a49f
1 file changed
+8 -2
+8 -2
View File
@@ -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;