Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

perf(gui): load the Live Overview's fonts before it first opens

RoversX66ffe85e9a81
diff --git a/wezterm-gui/src/native_settings.rs b/wezterm-gui/src/native_settings.rs
index 103760f5..9ee3e5ff 100644
--- a/wezterm-gui/src/native_settings.rs
+++ b/wezterm-gui/src/native_settings.rs
@@ -2712,8 +2712,8 @@ pub(crate) fn home_font_size(settings: &ThinkTermNativeSettings) -> f64 {
.clamp(10.0, 28.0)
}
-pub(crate) fn sidebar_font_size() -> f64 {
- load()
+pub(crate) fn sidebar_font_size(settings: &ThinkTermNativeSettings) -> f64 {
+ settings
.chrome
.sidebar_font_size
.unwrap_or(DEFAULT_SIDEBAR_FONT_SIZE)
@@ -2955,8 +2955,8 @@ pub(crate) fn tab_font_size() -> f64 {
.clamp(10.0, 28.0)
}
-pub(crate) fn pane_header_font_size() -> f64 {
- load()
+pub(crate) fn pane_header_font_size(settings: &ThinkTermNativeSettings) -> f64 {
+ settings
.chrome
.pane_header_font_size
.unwrap_or(DEFAULT_PANE_HEADER_FONT_SIZE)
diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs
index d764832a..42522d26 100644
--- a/wezterm-gui/src/termwindow/mod.rs
+++ b/wezterm-gui/src/termwindow/mod.rs
@@ -2936,6 +2936,13 @@ pub struct TermWindow {
/// When a paint last asked for card previews; None once the caches
/// have been released. Drives the idle release of `preview_quad_cache`.
preview_cache_last_wanted: Option<Instant>,
+ /// When a key, the pointer or an input method last reached this window,
+ /// or it came into focus: what `maybe_warm_overview_fonts` waits to see
+ /// a pause in.
+ last_user_input: Instant,
+ /// How far the Live Overview's fonts have been loaded ahead of its
+ /// first open since the font caches were last emptied.
+ overview_font_warmup: OverviewFontWarmup,
/// When each card's terminal first showed visible content, keyed like
/// `preview_quad_cache`. Drives the blank->content fade-in; survives
/// overview closes and atlas repacks so neither replays the fade.
@@ -4128,6 +4135,14 @@ impl TermWindow {
}
}
+ /// A menu, modal or rename over the window.
+ pub(crate) fn menu_or_overlay_open(&self) -> bool {
+ self.modal.borrow().is_some()
+ || self.context_menu.is_some()
+ || self.native_context_menu_open
+ || self.inline_tab_rename.is_some()
+ }
+
pub(crate) fn owes_frames_regardless_of_focus(&self) -> bool {
self.content_view_fade.is_some()
|| self.active_content_view_index().is_some()
@@ -4225,6 +4240,48 @@ impl TermWindow {
}
}
+ /// Load the Live Overview's fonts before it is opened, once input has
+ /// paused. Loading them was most of what opening the overview cost --
+ /// measured, 64ms of a 90ms frame on a first open and 232ms of 240ms
+ /// after a configuration reload, which empties the font caches -- and
+ /// that frame holds the window still before the opening animation can
+ /// start. A font takes 16-58ms, so they go one at a time, a second
+ /// apart, and only in the focused window with nothing over its
+ /// terminal and nothing animating (the onboarding intro among them):
+ /// not on someone typing or using a menu, nor on another window in use.
+ /// Starts over wherever the caches are emptied: a reload, or a move to
+ /// a screen of another density.
+ fn maybe_warm_overview_fonts(&mut self) {
+ let next = self.overview_font_warmup.loaded;
+ let Some(font) = crate::termwindow::render::paint::OverviewFont::ALL.get(next).copied()
+ else {
+ return;
+ };
+ // A menu or modal open takes no pointer or key events while it is
+ // used, and the palette sits untouched while its list is read; a
+ // stall would freeze either just the same.
+ let busy = self.focused.is_none()
+ || self.content_view_foreground()
+ || self.owes_frames_regardless_of_focus()
+ || self.menu_or_overlay_open()
+ || self.command_palette.is_some();
+ if !overview_font_warmup_due(
+ self.last_user_input.elapsed(),
+ self.overview_font_warmup.last_load.map(|at| at.elapsed()),
+ busy,
+ ) {
+ return;
+ }
+ let settings = crate::native_settings::load_shared();
+ if let Err(err) = self.overview_font(&settings, font) {
+ log::debug!("loading the overview's {font:?} font ahead of time: {err:#}");
+ }
+ self.overview_font_warmup = OverviewFontWarmup {
+ loaded: next + 1,
+ last_load: Some(Instant::now()),
+ };
+ }
+
/// The card previews outlive the overview on purpose: reopening it
/// replays them instead of rebuilding every card in the revealing
/// frame. But an overview closed for good keeps its quads and card
@@ -4795,6 +4852,8 @@ impl TermWindow {
preview_quad_cache: RefCell::new(HashMap::new()),
preview_rebuild_partial: RefCell::new(None),
preview_cache_last_wanted: None,
+ last_user_input: Instant::now(),
+ overview_font_warmup: OverviewFontWarmup::default(),
preview_content_fade: RefCell::new(HashMap::new()),
pending_card_renders: RefCell::new(Vec::new()),
card_frame_verts: RefCell::new(Vec::new()),
@@ -5357,6 +5416,37 @@ impl TermWindow {
window: &Window,
) -> anyhow::Result<bool> {
log::debug!("{event:?}");
+ // Anything the user does to the window: what
+ // `maybe_warm_overview_fonts` waits to see a pause in. Some input
+ // arrives only in another shape -- an input method composing as
+ // dead-key status, a menu shortcut as a key assignment, an edge
+ // drag as live resizes -- and coming back to a window starts using
+ // it before the first key does.
+ if matches!(
+ event,
+ WindowEvent::MouseEvent(_)
+ | WindowEvent::MouseLeave
+ | WindowEvent::RawKeyEvent(_)
+ | WindowEvent::KeyEvent(_)
+ | WindowEvent::AdviseDeadKeyStatus(_)
+ | WindowEvent::NativeTextInputReplace { .. }
+ | WindowEvent::FocusChanged(true)
+ | WindowEvent::PerformKeyAssignment(_)
+ | WindowEvent::PerformContextMenuAction(_)
+ | WindowEvent::ContextMenuDismissed
+ | WindowEvent::ToggleWorkspaceSidebar
+ | WindowEvent::WorkspaceSidebarButtonHover(_)
+ | WindowEvent::DraggedFile { .. }
+ | WindowEvent::DroppedFile { .. }
+ | WindowEvent::DroppedUrl(_)
+ | WindowEvent::DroppedString(_)
+ | WindowEvent::Resized {
+ live_resizing: true,
+ ..
+ }
+ ) {
+ self.last_user_input = Instant::now();
+ }
match event {
WindowEvent::Destroyed => {
crate::main_window_placement::window_closed(self.mux_window_id);
@@ -6270,6 +6360,7 @@ impl TermWindow {
self.terminal_render_watchdog();
self.maybe_release_occluded_memory();
self.maybe_release_idle_preview_cache();
+ self.maybe_warm_overview_fonts();
}
TermWindowNotif::OpenProjectPath(path) => {
let path = path.to_string_lossy();
@@ -7900,6 +7991,8 @@ impl TermWindow {
if let Err(err) = self.fonts.config_changed(&config) {
log::error!("Failed to load font configuration: {:#}", err);
}
+ // The overview's fonts went with the caches, failed reload or not.
+ self.overview_font_warmup = OverviewFontWarmup::default();
if let Some(window) = mux.get_window(self.mux_window_id) {
let term_config: Arc<dyn TerminalConfiguration> =
@@ -12510,6 +12603,89 @@ mod content_view_output_tests {
}
}
+/// How far the Live Overview's fonts have been loaded ahead of its first
+/// open; reset wherever the font caches are emptied.
+#[derive(Default)]
+struct OverviewFontWarmup {
+ /// How many of `OverviewFont::ALL` are loaded.
+ loaded: usize,
+ /// When the last of them was.
+ last_load: Option<Instant>,
+}
+
+/// How long input must have paused before one of the Live Overview's fonts
+/// is loaded ahead of time: past a burst of typing, not inside one.
+const OVERVIEW_FONT_WARMUP_IDLE: Duration = Duration::from_secs(2);
+
+/// The least time between two of those loads. The heartbeat that drives
+/// them also runs on every title change, which an agent can make several
+/// times a second.
+const OVERVIEW_FONT_WARMUP_SPACING: Duration = Duration::from_secs(1);
+
+/// Whether this heartbeat may load one of the overview's fonts: input has
+/// paused, the last load was a while ago, and the window is not busy --
+/// out of focus while another is used, or showing or fading a view, when a
+/// frame must not be held up.
+fn overview_font_warmup_due(
+ since_input: Duration,
+ since_last_load: Option<Duration>,
+ busy: bool,
+) -> bool {
+ !busy
+ && since_input >= OVERVIEW_FONT_WARMUP_IDLE
+ && since_last_load.map_or(true, |since| since >= OVERVIEW_FONT_WARMUP_SPACING)
+}
+
+#[cfg(test)]
+mod overview_font_warmup_tests {
+ use super::{
+ overview_font_warmup_due, OVERVIEW_FONT_WARMUP_IDLE, OVERVIEW_FONT_WARMUP_SPACING,
+ };
+ use std::time::Duration;
+
+ #[test]
+ fn a_pause_in_input_lets_a_font_load() {
+ assert!(overview_font_warmup_due(
+ OVERVIEW_FONT_WARMUP_IDLE,
+ None,
+ false
+ ));
+ }
+
+ #[test]
+ fn typing_holds_the_fonts_back() {
+ assert!(!overview_font_warmup_due(
+ OVERVIEW_FONT_WARMUP_IDLE - Duration::from_millis(1),
+ None,
+ false
+ ));
+ }
+
+ #[test]
+ fn a_busy_window_holds_the_fonts_back() {
+ assert!(!overview_font_warmup_due(
+ Duration::from_secs(60),
+ None,
+ true
+ ));
+ }
+
+ #[test]
+ fn loads_stay_spaced_however_often_the_heartbeat_runs() {
+ let idle = Duration::from_secs(60);
+ assert!(!overview_font_warmup_due(
+ idle,
+ Some(OVERVIEW_FONT_WARMUP_SPACING - Duration::from_millis(1)),
+ false
+ ));
+ assert!(overview_font_warmup_due(
+ idle,
+ Some(OVERVIEW_FONT_WARMUP_SPACING),
+ false
+ ));
+ }
+}
+
/// How long the Live Overview must stay closed before its card previews
/// are released. Long enough that flipping the overview open and closed
/// while working never pays the rebuild; matches the idle image release.
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index 36018868..2fadffe1 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -959,10 +959,7 @@ impl super::TermWindow {
use crate::termwindow::sidebar_hover::{HoverInput, PointerZone};
// A menu, modal or rename opened from the revealed panel holds it
// out; one open while it is away keeps it from revealing.
- let overlay_open = self.modal.borrow().is_some()
- || self.context_menu.is_some()
- || self.native_context_menu_open
- || self.inline_tab_rename.is_some();
+ let overlay_open = self.menu_or_overlay_open();
let eligible = crate::native_settings::workspace_sidebar_hover_reveal_enabled()
&& self.workspace_sidebar_collapsed
&& !self.content_view_foreground()
diff --git a/wezterm-gui/src/termwindow/render/paint.rs b/wezterm-gui/src/termwindow/render/paint.rs
index 9fa1aeb7..2a32f565 100644
--- a/wezterm-gui/src/termwindow/render/paint.rs
+++ b/wezterm-gui/src/termwindow/render/paint.rs
@@ -68,6 +68,24 @@ const WORKSPACE_SIDEBAR_SCRATCH_KEEP_BYTES: usize = 8 << 20;
/// drains at one per frame.
const MAX_PREVIEW_QUAD_REBUILDS_PER_FRAME: usize = 1;
+/// The fonts the Live Overview draws with, beyond the terminal's.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(crate) enum OverviewFont {
+ Controls,
+ GroupTitle,
+ CardName,
+ CardCaption,
+}
+
+impl OverviewFont {
+ pub(crate) const ALL: [Self; 4] = [
+ Self::Controls,
+ Self::GroupTitle,
+ Self::CardName,
+ Self::CardCaption,
+ ];
+}
+
/// How long one frame may spend rebuilding a card before the rest of the work
/// is carried over to the next frame.
///
@@ -2277,6 +2295,36 @@ impl crate::TermWindow {
Ok(())
}
+ /// One of the fonts the Live Overview draws with. Also loaded ahead of
+ /// the overview's first open (`maybe_warm_overview_fonts`), so the two
+ /// have to ask for exactly the same fonts.
+ pub(crate) fn overview_font(
+ &self,
+ settings: &crate::native_settings::ThinkTermNativeSettings,
+ font: OverviewFont,
+ ) -> anyhow::Result<Rc<wezterm_font::LoadedFont>> {
+ match font {
+ OverviewFont::Controls => self.fonts.command_palette_font_with_size_and_weight(
+ crate::native_settings::settings_font_size(settings),
+ crate::native_settings::settings_font_weight(settings),
+ ),
+ OverviewFont::GroupTitle => self
+ .fonts
+ .title_font_with_size(crate::native_settings::sidebar_font_size(settings)),
+ // A card names a thread in a capsule the size of a control, not a
+ // heading: a shade under the pane header size, and a caption a
+ // step below that in the same family.
+ OverviewFont::CardName => self.fonts.title_font_with_size_and_weight(
+ (crate::native_settings::pane_header_font_size(settings) * 0.95).max(9.0),
+ 600,
+ ),
+ OverviewFont::CardCaption => self.fonts.title_font_with_size_and_weight(
+ (crate::native_settings::pane_header_font_size(settings) * 0.78).max(8.0),
+ 500,
+ ),
+ }
+ }
+
pub fn paint_content_view(
&mut self,
layers: &mut TripleLayerQuadAllocator<'_>,
@@ -2310,23 +2358,10 @@ impl crate::TermWindow {
)
}
ContentViewTypography::Overview => (
- self.fonts.command_palette_font_with_size_and_weight(
- crate::native_settings::settings_font_size(&settings),
- font_weight,
- )?,
- self.fonts
- .title_font_with_size(crate::native_settings::sidebar_font_size())?,
- // A card names a thread in a capsule the size of a control,
- // not a heading: a shade under the pane header size, and a
- // caption a step below that in the same family.
- self.fonts.title_font_with_size_and_weight(
- (crate::native_settings::pane_header_font_size() * 0.95).max(9.0),
- 600,
- )?,
- self.fonts.title_font_with_size_and_weight(
- (crate::native_settings::pane_header_font_size() * 0.78).max(8.0),
- 500,
- )?,
+ self.overview_font(&settings, OverviewFont::Controls)?,
+ self.overview_font(&settings, OverviewFont::GroupTitle)?,
+ self.overview_font(&settings, OverviewFont::CardName)?,
+ self.overview_font(&settings, OverviewFont::CardCaption)?,
),
};
let render_metrics =
diff --git a/wezterm-gui/src/termwindow/render/pane.rs b/wezterm-gui/src/termwindow/render/pane.rs
index 11c83790..36b4249d 100644
--- a/wezterm-gui/src/termwindow/render/pane.rs
+++ b/wezterm-gui/src/termwindow/render/pane.rs
@@ -891,7 +891,9 @@ impl crate::TermWindow {
let ui_font = self
.fonts
- .title_font_with_size(crate::native_settings::pane_header_font_size())
+ .title_font_with_size(crate::native_settings::pane_header_font_size(
+ &crate::native_settings::load_shared(),
+ ))
.context("collapsed pane nav title font")?;
let ui_metrics = RenderMetrics::with_font_metrics(&ui_font.metrics());
let text_x = icon_right + self.ui_px(PANE_NAV_ICON_GAP);
@@ -1391,7 +1393,9 @@ impl crate::TermWindow {
let ui_font = self
.fonts
- .title_font_with_size(crate::native_settings::pane_header_font_size())
+ .title_font_with_size(crate::native_settings::pane_header_font_size(
+ &crate::native_settings::load_shared(),
+ ))
.context("pane nav title font")?;
let ui_metrics = RenderMetrics::with_font_metrics(&ui_font.metrics());
let text_x = icon_right + self.ui_px(PANE_NAV_ICON_GAP);
diff --git a/wezterm-gui/src/termwindow/resize.rs b/wezterm-gui/src/termwindow/resize.rs
index ace60691..e51532fb 100644
--- a/wezterm-gui/src/termwindow/resize.rs
+++ b/wezterm-gui/src/termwindow/resize.rs
@@ -2799,6 +2799,11 @@ impl super::TermWindow {
}
let (prior_font, prior_dpi) = self.fonts.change_scaling(font_scale, dimensions.dpi);
+ if prior_dpi != dimensions.dpi {
+ // A new density empties the caches the overview's fonts were
+ // loaded into.
+ self.overview_font_warmup = Default::default();
+ }
match RenderMetrics::new(&self.fonts) {
Ok(metrics) => {
self.render_metrics = metrics;
diff --git a/wezterm-gui/src/termwindow/ui/mod.rs b/wezterm-gui/src/termwindow/ui/mod.rs
index 1cfccc52..2d024075 100644
--- a/wezterm-gui/src/termwindow/ui/mod.rs
+++ b/wezterm-gui/src/termwindow/ui/mod.rs
@@ -222,7 +222,9 @@ impl crate::TermWindow {
pub(crate) fn pane_nav_bar_height(&self) -> usize {
let metrics = self
.fonts
- .title_font_with_size(crate::native_settings::pane_header_font_size())
+ .title_font_with_size(crate::native_settings::pane_header_font_size(
+ &crate::native_settings::load_shared(),
+ ))
.map(|font| RenderMetrics::with_font_metrics(&font.metrics()))
.unwrap_or(self.render_metrics);
pane_nav_bar_height_for_metrics(metrics)
diff --git a/wezterm-gui/src/termwindow/ui/sidebar.rs b/wezterm-gui/src/termwindow/ui/sidebar.rs
index d10fb49f..f8c93269 100644
--- a/wezterm-gui/src/termwindow/ui/sidebar.rs
+++ b/wezterm-gui/src/termwindow/ui/sidebar.rs
@@ -832,7 +832,8 @@ impl crate::TermWindow {
let count_font = self
.fonts
.title_font_with_size_and_weight(
- crate::native_settings::sidebar_font_size() * 0.55,
+ crate::native_settings::sidebar_font_size(&crate::native_settings::load_shared())
+ * 0.55,
700,
)
.context("sidebar thread count font")?;
@@ -1166,7 +1167,9 @@ impl crate::TermWindow {
/// whenever the sidebar font size was customized.
fn workspace_sidebar_cell_height(&self) -> usize {
self.fonts
- .title_font_with_size(crate::native_settings::sidebar_font_size())
+ .title_font_with_size(crate::native_settings::sidebar_font_size(
+ &crate::native_settings::load_shared(),
+ ))
.map(|font| {
RenderMetrics::with_font_metrics(&font.metrics())
.cell_size
@@ -1424,7 +1427,9 @@ impl crate::TermWindow {
let muted_fg = chrome.secondary_text;
let ui_font = self
.fonts
- .title_font_with_size(crate::native_settings::sidebar_font_size())
+ .title_font_with_size(crate::native_settings::sidebar_font_size(
+ &crate::native_settings::load_shared(),
+ ))
.context("sidebar ui font")?;
let ui_metrics = RenderMetrics::with_font_metrics(&ui_font.metrics());
let row_marks = self.sidebar_thread_row_marks()?;