From 494a0983eef8f883ccd1a390d17a272a8d3293ee Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 28 Dec 2021 12:12:19 -0700 Subject: [PATCH] gui: cache shaping and layout of fancy tab bar, allow placing at bottom We only need to recompute when the tab content changes, or when the window is resized, plus invalidations of the shape cache of texture atlas filling up. Hover events don't need to re-shape. We can now also place the tab bar at the bottom of the screen again. --- config/src/lib.rs | 7 -- docs/changelog.md | 2 +- wezterm-gui/src/termwindow/box_model.rs | 2 +- wezterm-gui/src/termwindow/mod.rs | 6 +- wezterm-gui/src/termwindow/mouseevent.rs | 4 +- wezterm-gui/src/termwindow/render.rs | 83 ++++++++++++------------ 6 files changed, 49 insertions(+), 55 deletions(-) diff --git a/config/src/lib.rs b/config/src/lib.rs index 513f2f4a6..47c3ebacf 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -1598,13 +1598,6 @@ impl Config { Self::default().compute_extra_defaults(None) } - /// Compute effective value of tab_bar_at_bottom. - /// If use_fancy_tab_bar is enabled, we cannot put the tab bar at - /// the bottom. - pub fn tab_bar_at_bottom(&self) -> bool { - self.tab_bar_at_bottom && !self.use_fancy_tab_bar - } - pub fn key_bindings(&self) -> HashMap<(KeyCode, Modifiers), KeyAssignment> { let mut map = HashMap::new(); diff --git a/docs/changelog.md b/docs/changelog.md index 369a91611..9564bfa1f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -13,7 +13,7 @@ As features stabilize some brief notes about them will accumulate here. #### New -* Fancy Tab Bars are now the default. The default tab bar colors have changed to accommodate the new more native look. They are not compatible with `tab_bar_at_bottom`. You can turn them off by setting `use_fancy_tab_bar = false`. +* Fancy Tab Bars are now the default. The default tab bar colors have changed to accommodate the new more native look. You can turn them off by setting `use_fancy_tab_bar = false`. * Support for the [Kitty Image Protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/) is now enabled by default. Most of the protocol is supported; animation support is not yet implemented. Try the amazing [notcurses](https://notcurses.com/) if you want to see what modern terminal graphics can do! [#986](https://github.com/wez/wezterm/issues/986) * unix domains now support an optional `proxy_command` to use in place of a direct unix socket connection. [Read more about multiplexing unix domains](multiplexing.html#unix-domains) * [ScrollToTop](config/lua/keyassignment/ScrollToTop.md) and [ScrollToBottom](config/lua/keyassignment/ScrollToBottom.md) key assignments [#1360](https://github.com/wez/wezterm/issues/1360) diff --git a/wezterm-gui/src/termwindow/box_model.rs b/wezterm-gui/src/termwindow/box_model.rs index 94c2ca04a..517c1e118 100644 --- a/wezterm-gui/src/termwindow/box_model.rs +++ b/wezterm-gui/src/termwindow/box_model.rs @@ -375,7 +375,7 @@ pub struct ComputedElement { } impl ComputedElement { - fn translate(&mut self, delta: euclid::Vector2D) { + pub fn translate(&mut self, delta: euclid::Vector2D) { self.bounds = self.bounds.translate(delta); self.border_rect = self.border_rect.translate(delta); self.padding = self.padding.translate(delta); diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 213934823..43b60d0dd 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -281,6 +281,7 @@ pub struct TermWindow { show_tab_bar: bool, show_scroll_bar: bool, tab_bar: TabBarState, + fancy_tab_bar: Option, pub right_status: String, last_ui_item: Option, /// Tracks whether the current mouse-down event is part of click-focus. @@ -698,6 +699,7 @@ impl TermWindow { show_tab_bar, show_scroll_bar: config.enable_scroll_bar, tab_bar: TabBarState::default(), + fancy_tab_bar: None, right_status: String::new(), last_mouse_coords: (0, -1), last_mouse_terminal_coords: (0, 0), @@ -1302,6 +1304,7 @@ impl TermWindow { self.show_scroll_bar = config.enable_scroll_bar; self.shape_cache.borrow_mut().clear(); + self.fancy_tab_bar.take(); self.input_map = InputMap::new(&config); self.leader_is_down = None; let dimensions = self.dimensions; @@ -1389,7 +1392,7 @@ impl TermWindow { .bottom .evaluate_as_pixels(v_context) as u16; - let tab_bar_y = if self.config.tab_bar_at_bottom() { + let tab_bar_y = if self.config.tab_bar_at_bottom { let avail_height = self .dimensions .pixel_height @@ -1417,6 +1420,7 @@ impl TermWindow { ); if new_tab_bar != self.tab_bar { self.tab_bar = new_tab_bar; + self.invalidate_fancy_tab_bar(); if let Some(window) = self.window.as_ref() { window.invalidate(); } diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs index e29f3a68d..af356f675 100644 --- a/wezterm-gui/src/termwindow/mouseevent.rs +++ b/wezterm-gui/src/termwindow/mouseevent.rs @@ -58,7 +58,7 @@ impl super::TermWindow { self.current_mouse_event.replace(event.clone()); - let first_line_offset = if self.show_tab_bar && !self.config.tab_bar_at_bottom() { + let first_line_offset = if self.show_tab_bar && !self.config.tab_bar_at_bottom { self.tab_bar_pixel_height().unwrap_or(0.) as isize } else { 0 @@ -224,7 +224,7 @@ impl super::TermWindow { current_viewport, &self.dimensions, self.tab_bar_pixel_height().unwrap_or(0.), - self.config.tab_bar_at_bottom(), + self.config.tab_bar_at_bottom, ); self.set_viewport(pane.pane_id(), Some(row), dims); context.invalidate(); diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 7aa5eed5d..2a4ba60db 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -1,3 +1,4 @@ +use super::box_model::*; use crate::customglyph::BlockKey; use crate::customglyph::*; use crate::glium::texture::SrgbTexture2d; @@ -20,7 +21,8 @@ use ::window::glium::{uniform, BlendingFunction, LinearBlendingFactor, Surface}; use ::window::{PointF, RectF, Size, WindowOps}; use anyhow::anyhow; use config::{ - ConfigHandle, DimensionContext, HsbTransform, TabBarColors, TextStyle, VisualBellTarget, + ConfigHandle, Dimension, DimensionContext, HsbTransform, TabBarColors, TextStyle, + VisualBellTarget, }; use euclid::num::Zero; use mux::pane::Pane; @@ -215,6 +217,7 @@ impl super::TermWindow { log::trace!("grow texture atlas to {}", size); self.recreate_texture_atlas(Some(size)) }; + self.invalidate_fancy_tab_bar(); if let Err(err) = result { if self.allow_images { @@ -234,6 +237,7 @@ impl super::TermWindow { } } } else if err.root_cause().downcast_ref::().is_some() { + self.invalidate_fancy_tab_bar(); self.shape_cache.borrow_mut().clear(); } else { log::error!("paint_opengl_pass failed: {:#}", err); @@ -422,10 +426,11 @@ impl super::TermWindow { Self::tab_bar_pixel_height_impl(&self.config, &self.fonts, &self.render_metrics) } - fn paint_fancy_tab_bar(&self, palette: &ColorPalette) -> anyhow::Result> { - use super::box_model::*; - use config::Dimension; + pub fn invalidate_fancy_tab_bar(&mut self) { + self.fancy_tab_bar.take(); + } + pub fn build_fancy_tab_bar(&self, palette: &ColorPalette) -> anyhow::Result { let font = self.fonts.title_font()?; let items = self.tab_bar.items(); let colors = self @@ -665,9 +670,10 @@ impl super::TermWindow { .display(DisplayType::Block) .item_type(UIItemType::TabBar(TabBarItem::None)) .colors(bar_colors); + let metrics = RenderMetrics::with_font_metrics(&font.metrics()); - let computed = self.compute_element( + let mut computed = self.compute_element( &LayoutContext { height: DimensionContext { dpi: self.dimensions.dpi as f32, @@ -691,56 +697,47 @@ impl super::TermWindow { &tabs, )?; - if false { - fn summarize(ele: &ComputedElement) { - log::info!( - "ele {:?} with bounds={:?} content_rect={:?} padding={:?} border={:?}", - ele.item_type, - ele.bounds, - ele.content_rect, - ele.padding, - ele.border_rect, - ); - match &ele.content { - ComputedElementContent::Text(_) => {} - ComputedElementContent::Children(kids) => { - for kid in kids { - summarize(kid); - } - } - } - } - log::info!("Computed:"); - summarize(&computed); + if self.config.tab_bar_at_bottom { + computed.translate(euclid::vec2( + 0., + self.dimensions.pixel_height as f32 - computed.bounds.height(), + )); } + Ok(computed) + } + + fn paint_fancy_tab_bar(&self) -> anyhow::Result> { + let computed = self + .fancy_tab_bar + .as_ref() + .ok_or_else(|| anyhow::anyhow!("paint_tab_bar called but fancy_tab_bar is None"))?; let ui_items = computed.ui_items(); let gl_state = self.render_state.as_ref().unwrap(); - let vb = [&gl_state.vb[0], &gl_state.vb[1], &gl_state.vb[2]]; - let mut vb_mut0 = vb[0].current_vb_mut(); - let mut vb_mut1 = vb[1].current_vb_mut(); - let mut vb_mut2 = vb[2].current_vb_mut(); - let mut layers = [ - vb[0].map(&mut vb_mut0), - vb[1].map(&mut vb_mut1), - vb[2].map(&mut vb_mut2), - ]; - self.render_element(&computed, &mut layers[1], None)?; + let vb = &gl_state.vb[1]; + let mut vb_mut = vb.current_vb_mut(); + let mut layer1 = vb.map(&mut vb_mut); + self.render_element(&computed, &mut layer1, None)?; Ok(ui_items) } fn paint_tab_bar(&mut self) -> anyhow::Result<()> { - let palette = self.palette().clone(); if self.config.use_fancy_tab_bar { - self.ui_items - .append(&mut self.paint_fancy_tab_bar(&palette)?); + if self.fancy_tab_bar.is_none() { + let palette = self.palette().clone(); + self.fancy_tab_bar + .replace(self.build_fancy_tab_bar(&palette)?); + } + + self.ui_items.append(&mut self.paint_fancy_tab_bar()?); return Ok(()); } + let palette = self.palette().clone(); let tab_bar_height = self.tab_bar_pixel_height()?; - let tab_bar_y = if self.config.tab_bar_at_bottom() { + let tab_bar_y = if self.config.tab_bar_at_bottom { ((self.dimensions.pixel_height as f32) - tab_bar_height).max(0.) } else { 0. @@ -847,7 +844,7 @@ impl super::TermWindow { let (padding_left, padding_top) = self.padding_left_top(); - let tab_bar_height = if self.show_tab_bar && !self.config.tab_bar_at_bottom() { + let tab_bar_height = if self.show_tab_bar && !self.config.tab_bar_at_bottom { self.tab_bar_pixel_height()? } else { 0. @@ -1115,7 +1112,7 @@ impl super::TermWindow { current_viewport, &self.dimensions, tab_bar_height, - config.tab_bar_at_bottom(), + config.tab_bar_at_bottom, ); let thumb_top = info.top as f32; let thumb_size = info.height as f32; @@ -1360,7 +1357,7 @@ impl super::TermWindow { let cell_width = self.render_metrics.cell_size.width as f32; let cell_height = self.render_metrics.cell_size.height as f32; - let first_row_offset = if self.show_tab_bar && !self.config.tab_bar_at_bottom() { + let first_row_offset = if self.show_tab_bar && !self.config.tab_bar_at_bottom { self.tab_bar_pixel_height()? } else { 0.