From d8cfdae27b9580f9e0369d3243f2c033ce409bf6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 7 Apr 2022 21:43:05 -0700 Subject: [PATCH] avoid ping-ponging focus updates with multiple windows The bug here was that each paint call in a window would update the focus state of its panes to reflect the one that had focus. However, it didn't account for the actual window focus; it would just assume that it was focused. The result was that the perceived focus would alternate between each of the windows in the wezterm process, and if you were running an application that had enabled focus tracking, those events could cause a repaint and drive up the CPU utilization. This commit addresses that by gating the focus update to only occur when we have the focus, and for extra safety, avoid generating focus events at the terminal layer if the new state matches the current state. refs: https://github.com/wez/wezterm/issues/1838 --- term/src/terminalstate/mod.rs | 3 +++ wezterm-gui/src/termwindow/render.rs | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs index 1d9098132..11b7ce911 100644 --- a/term/src/terminalstate/mod.rs +++ b/term/src/terminalstate/mod.rs @@ -719,6 +719,9 @@ impl TerminalState { /// Advise the terminal about a change in its focus state pub fn focus_changed(&mut self, focused: bool) { + if focused == self.focused { + return; + } if !focused { // notify app of release of buttons let buttons = self.current_mouse_buttons.clone(); diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 1eacb8668..6698e07ed 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -1614,14 +1614,17 @@ impl super::TermWindow { let panes = self.get_panes_to_render(); let num_panes = panes.len(); + let focused = self.focused.is_some(); for pos in panes { if pos.is_active { self.update_text_cursor(&pos.pane); - pos.pane.advise_focus(); - mux::Mux::get() - .expect("called on mux thread") - .record_focus_for_current_identity(pos.pane.pane_id()); + if focused { + pos.pane.advise_focus(); + mux::Mux::get() + .expect("called on mux thread") + .record_focus_for_current_identity(pos.pane.pane_id()); + } } self.paint_pane_opengl(&pos, num_panes)?; }