From f4fab10e6989b5e6b2e38adf9bd2ab12b2f88c0b Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 26 Dec 2021 21:25:19 -0700 Subject: [PATCH] gui: box model style layout/render for fancy tab bar This commit adds a CSS box model inspired element / layout facility, and replaces the hand implemented fancy tab bar element render. This makes the code for fancy tab bar much easier to read and update. The right status area now expands to the full height of the tab bar area, and uses a line height of 2.0, which makes it line up nicely in the tab bar. --- config/src/units.rs | 2 +- wezterm-font/src/lib.rs | 13 + wezterm-gui/src/customglyph.rs | 9 +- wezterm-gui/src/termwindow/box_model.rs | 948 +++++++++++++++++++++ wezterm-gui/src/termwindow/mod.rs | 1 + wezterm-gui/src/termwindow/render.rs | 1013 +++++++++-------------- wezterm-gui/src/utilsprites.rs | 18 + wezterm-input-types/src/lib.rs | 1 + window/src/lib.rs | 2 + 9 files changed, 1364 insertions(+), 643 deletions(-) create mode 100644 wezterm-gui/src/termwindow/box_model.rs diff --git a/config/src/units.rs b/config/src/units.rs index 8ee075592..2a54c6a1e 100644 --- a/config/src/units.rs +++ b/config/src/units.rs @@ -80,7 +80,7 @@ impl<'de> serde::de::Visitor<'de> for DefaultUnit { } } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq)] pub enum Dimension { /// A value expressed in points, where 72 points == 1 inch. Points(f32), diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index 910141140..db700070b 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -52,6 +52,19 @@ pub struct LoadedFont { text_style: TextStyle, } +impl std::fmt::Debug for LoadedFont { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + fmt.debug_struct("LoadedFont") + .field("handles", &self.handles) + .field("metrics", &self.metrics) + .field("font_size", &self.font_size) + .field("dpi", &self.dpi) + .field("pending_fallback", &self.pending_fallback) + .field("text_style", &self.text_style) + .finish() + } +} + impl LoadedFont { pub fn metrics(&self) -> FontMetrics { self.metrics diff --git a/wezterm-gui/src/customglyph.rs b/wezterm-gui/src/customglyph.rs index ad0b8e27b..9bd16ad35 100644 --- a/wezterm-gui/src/customglyph.rs +++ b/wezterm-gui/src/customglyph.rs @@ -236,11 +236,12 @@ impl BlockKey { } pub fn from_str(s: &str) -> Option { - let chars = s.chars().collect::>(); - if chars.len() == 1 { - Self::from_char(chars[0]) - } else { + let mut chars = s.chars(); + let first_char = chars.next()?; + if chars.next().is_some() { None + } else { + Self::from_char(first_char) } } diff --git a/wezterm-gui/src/termwindow/box_model.rs b/wezterm-gui/src/termwindow/box_model.rs new file mode 100644 index 000000000..54877e774 --- /dev/null +++ b/wezterm-gui/src/termwindow/box_model.rs @@ -0,0 +1,948 @@ +#![allow(dead_code)] +use crate::color::LinearRgba; +use crate::customglyph::{BlockKey, Poly}; +use crate::glyphcache::CachedGlyph; +use crate::termwindow::render::rgbcolor_to_window_color; +use crate::termwindow::{ + MappedQuads, RenderState, SrgbTexture2d, TermWindowNotif, UIItem, UIItemType, +}; +use crate::utilsprites::RenderMetrics; +use ::window::{RectF, WindowOps}; +use anyhow::anyhow; +use config::{Dimension, DimensionContext}; +use std::ops::Sub; +use std::rc::Rc; +use termwiz::cell::{grapheme_column_width, Presentation}; +use termwiz::surface::Line; +use unicode_segmentation::UnicodeSegmentation; +use wezterm_font::units::PixelUnit; +use wezterm_font::LoadedFont; +use wezterm_term::color::{ColorAttribute, ColorPalette}; +use window::bitmaps::atlas::Sprite; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum DisplayType { + Block, + Inline, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Float { + None, + Right, +} + +impl Default for Float { + fn default() -> Self { + Self::None + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct PixelDimension { + pub left: f32, + pub top: f32, + pub right: f32, + pub bottom: f32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct PixelSizedPoly { + pub poly: &'static [Poly], + pub width: f32, + pub height: f32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct SizedPoly { + pub poly: &'static [Poly], + pub width: Dimension, + pub height: Dimension, +} + +impl SizedPoly { + pub fn to_pixels(&self, context: &LayoutContext) -> PixelSizedPoly { + PixelSizedPoly { + poly: self.poly, + width: self.width.evaluate_as_pixels(context.width), + height: self.height.evaluate_as_pixels(context.height), + } + } + + pub fn none() -> Self { + Self { + poly: &[], + width: Dimension::default(), + height: Dimension::default(), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct PixelCorners { + pub top_left: PixelSizedPoly, + pub top_right: PixelSizedPoly, + pub bottom_left: PixelSizedPoly, + pub bottom_right: PixelSizedPoly, +} + +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct Corners { + pub top_left: SizedPoly, + pub top_right: SizedPoly, + pub bottom_left: SizedPoly, + pub bottom_right: SizedPoly, +} + +impl Corners { + pub fn to_pixels(&self, context: &LayoutContext) -> PixelCorners { + PixelCorners { + top_left: self.top_left.to_pixels(context), + top_right: self.top_right.to_pixels(context), + bottom_left: self.bottom_left.to_pixels(context), + bottom_right: self.bottom_left.to_pixels(context), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct BoxDimension { + pub left: Dimension, + pub top: Dimension, + pub right: Dimension, + pub bottom: Dimension, +} + +impl BoxDimension { + pub const fn new(dim: Dimension) -> Self { + Self { + left: dim, + top: dim, + right: dim, + bottom: dim, + } + } + + pub fn to_pixels(&self, context: &LayoutContext) -> PixelDimension { + PixelDimension { + left: self.left.evaluate_as_pixels(context.width), + top: self.top.evaluate_as_pixels(context.height), + right: self.right.evaluate_as_pixels(context.width), + bottom: self.bottom.evaluate_as_pixels(context.height), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum InheritableColor { + Inherited, + Color(LinearRgba), +} + +impl Default for InheritableColor { + fn default() -> Self { + Self::Inherited + } +} + +impl From for InheritableColor { + fn from(color: LinearRgba) -> InheritableColor { + InheritableColor::Color(color) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct BorderColor { + pub left: LinearRgba, + pub top: LinearRgba, + pub right: LinearRgba, + pub bottom: LinearRgba, +} + +impl BorderColor { + pub const fn new(color: LinearRgba) -> Self { + Self { + left: color, + top: color, + right: color, + bottom: color, + } + } +} + +#[derive(Debug, Clone, Copy, Default)] +pub struct ElementColors { + pub border: BorderColor, + pub bg: InheritableColor, + pub text: InheritableColor, +} + +impl ElementColors { + pub fn resolve_bg(&self, inherited_colors: Option<&ElementColors>) -> LinearRgba { + match self.bg { + InheritableColor::Inherited => match inherited_colors { + Some(colors) => colors.resolve_bg(None), + None => LinearRgba::TRANSPARENT, + }, + InheritableColor::Color(color) => color, + } + } + + pub fn resolve_text(&self, inherited_colors: Option<&ElementColors>) -> LinearRgba { + match self.text { + InheritableColor::Inherited => match inherited_colors { + Some(colors) => colors.resolve_text(None), + None => LinearRgba::TRANSPARENT, + }, + InheritableColor::Color(color) => color, + } + } +} + +#[derive(Debug, Clone)] +pub struct Element { + pub item_type: Option, + pub zindex: i8, + pub display: DisplayType, + pub float: Float, + pub padding: BoxDimension, + pub margin: BoxDimension, + pub border: BoxDimension, + pub border_corners: Option, + pub colors: ElementColors, + pub hover_colors: Option, + pub font: Rc, + pub content: ElementContent, + pub presentation: Option, + pub line_height: Option, +} + +impl Element { + pub fn new(font: &Rc, content: ElementContent) -> Self { + Self { + item_type: None, + zindex: 0, + display: DisplayType::Inline, + float: Float::None, + padding: BoxDimension::default(), + margin: BoxDimension::default(), + border: BoxDimension::default(), + border_corners: None, + colors: ElementColors::default(), + hover_colors: None, + font: Rc::clone(font), + content, + presentation: None, + line_height: None, + } + } + + pub fn with_line(font: &Rc, line: &Line, palette: &ColorPalette) -> Self { + let mut content = vec![]; + + for cluster in line.cluster() { + let child = + Element::new(font, ElementContent::Text(cluster.text)).colors(ElementColors { + border: BorderColor::default(), + bg: if cluster.attrs.background() == ColorAttribute::Default { + InheritableColor::Inherited + } else { + rgbcolor_to_window_color(palette.resolve_bg(cluster.attrs.background())) + .into() + }, + text: if cluster.attrs.foreground() == ColorAttribute::Default { + InheritableColor::Inherited + } else { + rgbcolor_to_window_color(palette.resolve_fg(cluster.attrs.foreground())) + .into() + }, + }); + + content.push(child); + } + + Self::new(font, ElementContent::Children(content)) + } + + pub fn item_type(mut self, item_type: UIItemType) -> Self { + self.item_type.replace(item_type); + self + } + + pub fn display(mut self, display: DisplayType) -> Self { + self.display = display; + self + } + + pub fn float(mut self, float: Float) -> Self { + self.float = float; + self + } + + pub fn colors(mut self, colors: ElementColors) -> Self { + self.colors = colors; + self + } + + pub fn hover_colors(mut self, colors: Option) -> Self { + self.hover_colors = colors; + self + } + + pub fn line_height(mut self, line_height: Option) -> Self { + self.line_height = line_height; + self + } + + pub fn zindex(mut self, zindex: i8) -> Self { + self.zindex = zindex; + self + } + + pub fn padding(mut self, padding: BoxDimension) -> Self { + self.padding = padding; + self + } + + pub fn border(mut self, border: BoxDimension) -> Self { + self.border = border; + self + } + + pub fn border_corners(mut self, corners: Option) -> Self { + self.border_corners = corners; + self + } + + pub fn margin(mut self, margin: BoxDimension) -> Self { + self.margin = margin; + self + } +} + +#[derive(Debug, Clone)] +pub enum ElementContent { + Text(String), + Children(Vec), +} + +pub struct LayoutContext<'a> { + pub width: DimensionContext, + pub height: DimensionContext, + pub bounds: RectF, + pub metrics: &'a RenderMetrics, + pub gl_state: &'a RenderState, +} + +#[derive(Debug, Clone)] +pub struct ComputedElement { + pub item_type: Option, + pub zindex: i8, + /// The outer bounds of the element box (its margin) + pub bounds: RectF, + /// The outer bounds of the area enclosed by its border + pub border_rect: RectF, + pub border: PixelDimension, + pub border_corners: Option, + pub colors: ElementColors, + pub hover_colors: Option, + /// The outer bounds of the area enclosed by the padding + pub padding: RectF, + /// The outer bounds of the content + pub content_rect: RectF, + pub baseline: f32, + + pub content: ComputedElementContent, +} + +impl ComputedElement { + 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); + self.content_rect = self.content_rect.translate(delta); + + match &mut self.content { + ComputedElementContent::Children(kids) => { + for kid in kids { + kid.translate(delta) + } + } + ComputedElementContent::Text(_) => {} + } + } + + pub fn ui_items(&self) -> Vec { + let mut items = vec![]; + self.ui_item_impl(&mut items); + items + } + + fn ui_item_impl(&self, items: &mut Vec) { + if let Some(item_type) = &self.item_type { + items.push(UIItem { + x: self.bounds.min_x().max(0.) as usize, + y: self.bounds.min_y().max(0.) as usize, + width: self.bounds.width().max(0.) as usize, + height: self.bounds.height().max(0.) as usize, + item_type: item_type.clone(), + }); + } + + match &self.content { + ComputedElementContent::Text(_) => {} + ComputedElementContent::Children(kids) => { + for kid in kids { + kid.ui_item_impl(items); + } + } + } + } +} + +#[derive(Debug, Clone)] +pub enum ComputedElementContent { + Text(Vec), + Children(Vec), +} + +#[derive(Debug, Clone)] +pub enum ElementCell { + Sprite(Sprite), + Glyph(Rc>), +} + +impl super::TermWindow { + pub fn compute_element<'a>( + &self, + context: &LayoutContext, + element: &Element, + ) -> anyhow::Result { + let local_metrics; + let local_context; + let context = if let Some(line_height) = element.line_height { + local_metrics = context.metrics.scale_line_height(line_height); + local_context = LayoutContext { + height: DimensionContext { + dpi: context.height.dpi, + pixel_max: context.height.pixel_max, + pixel_cell: context.height.pixel_cell * line_height as f32, + }, + width: context.width, + bounds: context.bounds, + gl_state: context.gl_state, + metrics: &local_metrics, + }; + &local_context + } else { + context + }; + let padding = element.padding.to_pixels(context); + let margin = element.margin.to_pixels(context); + let border = element.border.to_pixels(context); + let border_corners = element + .border_corners + .as_ref() + .map(|c| c.to_pixels(context)); + let style = element.font.style(); + let baseline = context.height.pixel_cell + context.metrics.descender.get() as f32; + + match &element.content { + ElementContent::Text(s) => { + let window = self.window.as_ref().unwrap().clone(); + let infos = element.font.shape( + &s, + move || window.notify(TermWindowNotif::InvalidateShapeCache), + BlockKey::filter_out_synthetic, + element.presentation, + )?; + let mut computed_cells = vec![]; + let mut glyph_cache = context.gl_state.glyph_cache.borrow_mut(); + let mut pixel_width = 0.0; + let mut min_y = 0.0f32; + + for info in infos { + let cell_start = &s[info.cluster as usize..]; + let mut iter = cell_start.graphemes(true).peekable(); + let grapheme = iter + .next() + .ok_or_else(|| anyhow!("info.cluster didn't map into string"))?; + if let Some(key) = BlockKey::from_str(grapheme) { + pixel_width += context.width.pixel_cell; + let sprite = glyph_cache.cached_block(key, context.metrics)?; + computed_cells.push(ElementCell::Sprite(sprite)); + } else { + let next_grapheme: Option<&str> = iter.peek().map(|s| *s); + let followed_by_space = next_grapheme == Some(" "); + let num_cells = grapheme_column_width(grapheme, None); + let glyph = glyph_cache.cached_glyph( + &info, + style, + followed_by_space, + &element.font, + context.metrics, + num_cells as u8, + )?; + + min_y = + min_y.min(baseline - (glyph.y_offset + glyph.bearing_y).get() as f32); + + pixel_width += glyph.x_advance.get() as f32; + computed_cells.push(ElementCell::Glyph(glyph)); + } + } + + let content_rect = euclid::rect(0., 0., pixel_width, context.height.pixel_cell); + + let padding = euclid::rect( + content_rect.min_x() - padding.left, + content_rect.min_y() - padding.top, + content_rect.width() + padding.left + padding.right, + content_rect.height() + padding.top + padding.bottom, + ); + + let border_rect = euclid::rect( + padding.min_x() - border.left, + padding.min_y() - border.top, + padding.width() + border.left + border.right, + padding.height() + border.top + border.bottom, + ); + + let bounds = euclid::rect( + border_rect.min_x() - margin.left, + border_rect.min_y() - margin.top, + border_rect.width() + margin.left + margin.right, + border_rect.height() + margin.top + margin.bottom, + ); + + let translate = euclid::vec2( + context.bounds.min_x() - bounds.min_x(), + context.bounds.min_y() - bounds.min_y(), + ); + + Ok(ComputedElement { + item_type: element.item_type.clone(), + zindex: element.zindex, + baseline, + border, + border_corners, + colors: element.colors, + hover_colors: element.hover_colors, + bounds: bounds.translate(translate), + border_rect: border_rect.translate(translate), + padding: padding.translate(translate), + content_rect: content_rect.translate(translate), + content: ComputedElementContent::Text(computed_cells), + }) + } + ElementContent::Children(kids) => { + let mut pixel_width: f32 = 0.; + let mut pixel_height: f32 = 0.; + let mut computed_kids = vec![]; + let mut max_x: f32 = 0.; + + for child in kids { + let mut kid = self.compute_element( + &LayoutContext { + bounds: match child.float { + Float::None => euclid::rect( + pixel_width, + context.bounds.min_y(), + context.bounds.max_x() - (context.bounds.min_x() + pixel_width), + context.bounds.height(), + ), + Float::Right => euclid::rect( + 0., + context.bounds.min_y(), + context.bounds.width(), + context.bounds.height(), + ), + }, + gl_state: context.gl_state, + height: context.height, + metrics: context.metrics, + width: context.width, + }, + child, + )?; + match child.float { + Float::Right => { + let padded_max_x = context + .bounds + .max_x() + .sub(padding.left) + .sub(padding.right) + .sub(margin.left) + .sub(margin.right) + .sub(border.left) + .sub(border.right) + .max(0.); + + kid.translate(euclid::vec2(padded_max_x - kid.bounds.width(), 0.)); + max_x = max_x.max(padded_max_x); + } + Float::None => { + pixel_width += kid.bounds.width(); + max_x = max_x.max(pixel_width); + } + } + pixel_height = pixel_height.max(kid.bounds.height()); + + computed_kids.push(kid); + } + + computed_kids.sort_by(|a, b| a.zindex.cmp(&b.zindex)); + + let content_rect = euclid::rect(0., 0., max_x, pixel_height); + + let padding = euclid::rect( + content_rect.min_x() - padding.left, + content_rect.min_y() - padding.top, + content_rect.width() + padding.left + padding.right, + content_rect.height() + padding.top + padding.bottom, + ); + + let border_rect = euclid::rect( + padding.min_x() - border.left, + padding.min_y() - border.top, + padding.width() + border.left + border.right, + padding.height() + border.top + border.bottom, + ); + + let bounds = euclid::rect( + border_rect.min_x() - margin.left, + border_rect.min_y() - margin.top, + border_rect.width() + margin.left + margin.right, + border_rect.height() + margin.top + margin.bottom, + ); + + let translate = euclid::vec2( + context.bounds.min_x() - bounds.min_x(), + context.bounds.min_y() - bounds.min_y(), + ); + + for kid in &mut computed_kids { + kid.translate(translate); + } + + Ok(ComputedElement { + item_type: element.item_type.clone(), + zindex: element.zindex, + baseline, + border, + border_corners, + colors: element.colors, + hover_colors: element.hover_colors, + bounds: bounds.translate(translate), + border_rect: border_rect.translate(translate), + padding: padding.translate(translate), + content_rect: content_rect.translate(translate), + content: ComputedElementContent::Children(computed_kids), + }) + } + } + } + + pub fn render_element<'a>( + &self, + element: &ComputedElement, + layer: &'a mut MappedQuads, + inherited_colors: Option<&ElementColors>, + ) -> anyhow::Result<()> { + let colors = match &element.hover_colors { + Some(hc) => { + let hovering = match &self.current_mouse_event { + Some(event) => { + let mouse_x = event.coords.x as f32; + let mouse_y = event.coords.y as f32; + mouse_x >= element.bounds.min_x() + && mouse_x <= element.bounds.max_x() + && mouse_y >= element.bounds.min_y() + && mouse_y <= element.bounds.max_y() + } + None => false, + }; + if hovering { + hc + } else { + &element.colors + } + } + None => &element.colors, + }; + + self.render_element_background(element, colors, layer, inherited_colors)?; + let left = self.dimensions.pixel_width as f32 / -2.0; + let top = self.dimensions.pixel_height as f32 / -2.0; + match &element.content { + ComputedElementContent::Text(cells) => { + let mut pos_x = element.content_rect.min_x() as f32; + for cell in cells { + match cell { + ElementCell::Sprite(sprite) => { + let mut quad = layer.allocate()?; + let width = sprite.coords.width(); + let height = sprite.coords.height(); + let pos_y = top + element.content_rect.min_y() as f32; + quad.set_position( + pos_x + left, + pos_y, + pos_x + left + width as f32, + pos_y + height as f32, + ); + quad.set_fg_color(colors.resolve_text(inherited_colors)); + quad.set_texture(sprite.texture_coords()); + quad.set_hsv(None); + pos_x += width as f32; + } + ElementCell::Glyph(glyph) => { + if let Some(texture) = glyph.texture.as_ref() { + let mut quad = layer.allocate()?; + let pos_y = element.content_rect.min_y() as f32 + top + - (glyph.y_offset + glyph.bearing_y).get() as f32 + + element.baseline; + + let width = texture.coords.size.width as f32 * glyph.scale as f32; + let height = texture.coords.size.height as f32 * glyph.scale as f32; + + quad.set_position( + pos_x + left, + pos_y, + pos_x + left + width, + pos_y + height, + ); + quad.set_fg_color(colors.resolve_text(inherited_colors)); + quad.set_texture(texture.texture_coords()); + quad.set_has_color(glyph.has_color); + quad.set_hsv(None); + } + pos_x += glyph.x_advance.get() as f32; + } + } + } + } + ComputedElementContent::Children(kids) => { + for kid in kids { + self.render_element(kid, layer, Some(colors))?; + } + } + } + + Ok(()) + } + + fn render_element_background<'a>( + &self, + element: &ComputedElement, + colors: &ElementColors, + layer: &'a mut MappedQuads, + inherited_colors: Option<&ElementColors>, + ) -> anyhow::Result<()> { + let mut top_left_width = 0; + let mut top_left_height = 0; + let mut top_right_width = 0; + let mut top_right_height = 0; + + let mut bottom_left_width = 0; + let mut bottom_left_height = 0; + let mut bottom_right_width = 0; + let mut bottom_right_height = 0; + + if let Some(c) = &element.border_corners { + top_left_width = c.top_left.width as isize; + top_left_height = c.top_left.height as isize; + top_right_width = c.top_right.width as isize; + top_right_height = c.top_right.height as isize; + + bottom_left_width = c.bottom_left.width as isize; + bottom_left_height = c.bottom_left.height as isize; + bottom_right_width = c.bottom_right.width as isize; + bottom_right_height = c.bottom_right.height as isize; + + let underline_height = 1; + if top_left_width > 0 && top_left_height > 0 { + self.poly_quad( + layer, + element.border_rect.origin, + c.top_left.poly, + underline_height, + euclid::size2(top_left_width, top_left_height), + colors.border.top, + )?; + } + if top_right_width > 0 && top_right_height > 0 { + self.poly_quad( + layer, + euclid::point2( + element.border_rect.max_x() - top_right_width as f32, + element.border_rect.min_y(), + ), + c.top_right.poly, + underline_height, + euclid::size2(top_right_width, top_right_height), + colors.border.top, + )?; + } + if bottom_left_width > 0 && bottom_left_height > 0 { + self.poly_quad( + layer, + euclid::point2( + element.border_rect.min_x(), + element.border_rect.max_y() - bottom_left_height as f32, + ), + c.bottom_left.poly, + underline_height, + euclid::size2(bottom_left_width, bottom_left_height), + colors.border.bottom, + )?; + } + if bottom_right_width > 0 && bottom_right_height > 0 { + self.poly_quad( + layer, + euclid::point2( + element.border_rect.max_x() - bottom_right_width as f32, + element.border_rect.max_y() - bottom_right_height as f32, + ), + c.bottom_right.poly, + underline_height, + euclid::size2(bottom_right_width, bottom_right_height), + colors.border.bottom, + )?; + } + + // Filling the background is more complex because we can't + // simply fill the padding rect--we'd clobber the corner + // graphics. + // Instead, we consider the element as consisting of: + // + // TL T TR + // L C R + // BL B BR + // + // We already rendered the corner pieces, so now we need + // to do the rest + + // The `T` piece + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.min_x() + top_left_width as f32, + element.border_rect.min_y(), + element.border_rect.width() - (top_left_width + top_right_width) as f32, + top_left_height.max(top_right_height) as f32, + ), + colors.resolve_bg(inherited_colors), + )?; + + // The `B` piece + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.min_x() + bottom_left_width as f32, + element.border_rect.max_y() + - bottom_left_height.max(bottom_right_height) as f32, + element.border_rect.width() - (bottom_left_width + bottom_right_width) as f32, + bottom_left_height.max(bottom_right_height) as f32, + ), + colors.resolve_bg(inherited_colors), + )?; + + // The `L` piece + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.min_x(), + element.border_rect.min_y() + top_left_height as f32, + top_left_width.max(bottom_left_width) as f32, + element.border_rect.height() - (top_left_height + bottom_left_height) as f32, + ), + colors.resolve_bg(inherited_colors), + )?; + + // The `R` piece + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.max_x() - top_right_width as f32, + element.border_rect.min_y() + top_right_height as f32, + top_right_width.max(bottom_right_width) as f32, + element.border_rect.height() - (top_right_height + bottom_right_height) as f32, + ), + colors.resolve_bg(inherited_colors), + )?; + + // The `C` piece + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.min_x() + top_left_width as f32, + element.border_rect.min_y() + top_right_height.min(top_left_height) as f32, + element.border_rect.width() - (top_left_width + top_right_width) as f32, + element.border_rect.height() + - (top_right_height.min(top_left_height) + + bottom_right_height.min(bottom_left_height)) + as f32, + ), + colors.resolve_bg(inherited_colors), + )?; + } else if colors.bg != InheritableColor::Color(LinearRgba::TRANSPARENT) { + self.filled_rectangle(layer, element.padding, colors.resolve_bg(inherited_colors))?; + } + + if element.border_rect == element.padding { + // There's no border to be drawn + return Ok(()); + } + + if element.border.top > 0. && colors.border.top != LinearRgba::TRANSPARENT { + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.min_x() + top_left_width as f32, + element.border_rect.min_y(), + element.border_rect.width() - (top_left_width + top_right_width) as f32, + element.border.top, + ), + colors.border.top, + )?; + } + if element.border.bottom > 0. && colors.border.bottom != LinearRgba::TRANSPARENT { + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.min_x() + bottom_left_width as f32, + element.border_rect.max_y() - element.border.bottom, + element.border_rect.width() - (bottom_left_width + bottom_right_width) as f32, + element.border.bottom, + ), + colors.border.bottom, + )?; + } + if element.border.left > 0. && colors.border.left != LinearRgba::TRANSPARENT { + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.min_x(), + element.border_rect.min_y() + top_left_height as f32, + element.border.left, + element.border_rect.height() - (top_left_height + bottom_left_height) as f32, + ), + colors.border.left, + )?; + } + if element.border.right > 0. && colors.border.right != LinearRgba::TRANSPARENT { + self.filled_rectangle( + layer, + euclid::rect( + element.border_rect.max_x() - element.border.right, + element.border_rect.min_y() + top_right_height as f32, + element.border.left, + element.border_rect.height() - (top_right_height + bottom_right_height) as f32, + ), + colors.border.right, + )?; + } + + Ok(()) + } +} diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index a6170538e..213934823 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -51,6 +51,7 @@ use wezterm_term::color::ColorPalette; use wezterm_term::input::LastMouseClick; use wezterm_term::{Alert, SemanticZone, StableRowIndex, TerminalConfiguration}; +pub mod box_model; pub mod clipboard; mod keyevent; mod mouseevent; diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index dc32f7729..63496e0bb 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -4,7 +4,7 @@ use crate::glium::texture::SrgbTexture2d; use crate::glyphcache::{CachedGlyph, GlyphCache}; use crate::quad::Quad; use crate::shapecache::*; -use crate::tabbar::{TabBarItem, TabEntry}; +use crate::tabbar::TabBarItem; use crate::termwindow::{ BorrowedShapeCacheKey, MappedQuads, RenderState, ScrollHit, ShapedInfo, TermWindowNotif, UIItem, UIItemType, @@ -17,7 +17,7 @@ use ::window::glium::uniforms::{ MagnifySamplerFilter, MinifySamplerFilter, Sampler, SamplerWrapFunction, }; use ::window::glium::{uniform, BlendingFunction, LinearBlendingFactor, Surface}; -use ::window::{Point, Rect, Size, WindowOps}; +use ::window::{PointF, RectF, Size, WindowOps}; use anyhow::anyhow; use config::{ ConfigHandle, DimensionContext, HsbTransform, TabBarColors, TextStyle, VisualBellTarget, @@ -34,13 +34,41 @@ use termwiz::cell::Blink; use termwiz::cellcluster::CellCluster; use termwiz::surface::{CursorShape, CursorVisibility}; use wezterm_font::units::{IntPixelLength, PixelLength}; -use wezterm_font::{ClearShapeCache, FontMetrics, GlyphInfo, LoadedFont}; +use wezterm_font::{ClearShapeCache, GlyphInfo, LoadedFont}; use wezterm_term::color::{ColorAttribute, ColorPalette, RgbColor}; use wezterm_term::{CellAttributes, Line, StableRowIndex}; use window::bitmaps::atlas::SpriteSlice; use window::bitmaps::Texture2d; use window::color::LinearRgba; +const TOP_LEFT_ROUNDED_CORNER: &[Poly] = &[Poly { + path: &[ + PolyCommand::MoveTo(BlockCoord::One, BlockCoord::One), + PolyCommand::LineTo(BlockCoord::One, BlockCoord::Zero), + PolyCommand::QuadTo { + control: (BlockCoord::Zero, BlockCoord::Zero), + to: (BlockCoord::Zero, BlockCoord::One), + }, + PolyCommand::Close, + ], + intensity: BlockAlpha::Full, + style: PolyStyle::Fill, +}]; + +const TOP_RIGHT_ROUNDED_CORNER: &[Poly] = &[Poly { + path: &[ + PolyCommand::MoveTo(BlockCoord::Zero, BlockCoord::One), + PolyCommand::LineTo(BlockCoord::Zero, BlockCoord::Zero), + PolyCommand::QuadTo { + control: (BlockCoord::One, BlockCoord::Zero), + to: (BlockCoord::One, BlockCoord::One), + }, + PolyCommand::Close, + ], + intensity: BlockAlpha::Full, + style: PolyStyle::Fill, +}]; + pub struct RenderScreenLineOpenGLParams<'a> { /// zero-based offset from top of the window viewport to the line that /// needs to be rendered, measured in pixels @@ -312,10 +340,10 @@ impl super::TermWindow { None } - fn filled_rectangle<'a>( + pub fn filled_rectangle<'a>( &self, layer: &'a mut MappedQuads, - rect: Rect, + rect: RectF, color: LinearRgba, ) -> anyhow::Result> { let mut quad = layer.allocate()?; @@ -336,10 +364,10 @@ impl super::TermWindow { Ok(quad) } - fn poly_quad<'a>( + pub fn poly_quad<'a>( &self, layer: &'a mut MappedQuads, - point: Point, + point: PointF, polys: &'static [Poly], underline_height: IntPixelLength, cell_size: Size, @@ -364,10 +392,10 @@ impl super::TermWindow { let mut quad = layer.allocate()?; quad.set_position( - point.x as f32 - left_offset, - point.y as f32 - top_offset, - (point.x + cell_size.width) as f32 - left_offset, - (point.y + cell_size.height) as f32 - top_offset, + point.x - left_offset, + point.y - top_offset, + (point.x + cell_size.width as f32) - left_offset, + (point.y + cell_size.height as f32) - top_offset, ); quad.set_texture_adjust(0., 0., 0., 0.); quad.set_texture(sprite); @@ -377,444 +405,6 @@ impl super::TermWindow { Ok(quad) } - fn tab_background<'a>( - &self, - layer: &'a mut MappedQuads, - padding_width: isize, - padding_height: isize, - rect: Rect, - color: LinearRgba, - ) -> anyhow::Result<()> { - // Top rectangle that joins the top left and top right rounded corners - self.filled_rectangle(layer, rect.inflate(-padding_width, 0), color)?; - - // Main body rectangle that sits under the other three to flesh out - // the rest of the tab background area - self.filled_rectangle( - layer, - rect.inflate(0, -padding_height / 2) - .translate(euclid::vec2(0, padding_height / 2)), - color, - )?; - - // Top left rounded corner - self.poly_quad( - layer, - rect.origin, - &[Poly { - path: &[ - PolyCommand::MoveTo(BlockCoord::One, BlockCoord::One), - PolyCommand::LineTo(BlockCoord::One, BlockCoord::Zero), - PolyCommand::QuadTo { - control: (BlockCoord::Zero, BlockCoord::Zero), - to: (BlockCoord::Zero, BlockCoord::One), - }, - PolyCommand::Close, - ], - intensity: BlockAlpha::Full, - style: PolyStyle::Fill, - }], - 1, - euclid::size2(padding_width, padding_height), - color, - )?; - - // Top right rounded corner - self.poly_quad( - layer, - rect.origin + euclid::vec2(rect.width() - padding_width, 0), - &[Poly { - path: &[ - PolyCommand::MoveTo(BlockCoord::Zero, BlockCoord::One), - PolyCommand::LineTo(BlockCoord::Zero, BlockCoord::Zero), - PolyCommand::QuadTo { - control: (BlockCoord::One, BlockCoord::Zero), - to: (BlockCoord::One, BlockCoord::One), - }, - PolyCommand::Close, - ], - intensity: BlockAlpha::Full, - style: PolyStyle::Fill, - }], - 1, - euclid::size2(padding_width, padding_height), - color, - )?; - - Ok(()) - } - - fn paint_one_tab( - &self, - pos_x: f32, - max_tab_width: usize, - palette: &ColorPalette, - item: &TabEntry, - colors: &TabBarColors, - font: &Rc, - metrics: &FontMetrics, - layers: &mut [MappedQuads; 3], - ) -> anyhow::Result<(f32, UIItem)> { - let gl_state = self.render_state.as_ref().unwrap(); - - let white_space = gl_state.util_sprites.white_space.texture_coords(); - let filled_box = gl_state.util_sprites.filled_box.texture_coords(); - let window_is_transparent = - self.window_background.is_some() || self.config.window_background_opacity != 1.0; - let default_bg = rgbcolor_alpha_to_window_color( - palette.resolve_bg(ColorAttribute::Default), - if window_is_transparent { - 0. - } else { - self.config.text_background_opacity - }, - ); - - let tab_bounding_rect: Rect = euclid::rect( - pos_x as isize, - metrics.cell_height.get() as isize / 4, - max_tab_width as isize, - (metrics.cell_height.get() * 1.75) as isize, - ); - - let text_bounding_rect = tab_bounding_rect - .inflate(0, metrics.cell_height.get() as isize / -4) - .translate(euclid::vec2(metrics.cell_width.get() as isize / 2, 0)); - - // log::info!("tab bounds {:?}, text bounds {:?}", tab_bounding_rect, text_bounding_rect); - - let params = RenderScreenLineOpenGLParams { - top_pixel_y: 0., - left_pixel_x: 0., - pixel_width: text_bounding_rect.width() as f32, - stable_line_idx: None, - line: &item.title, - selection: 0..0, - cursor: &Default::default(), - palette: &palette, - dims: &RenderableDimensions { - cols: self.terminal_size.cols as _, - physical_top: 0, - scrollback_rows: 0, - scrollback_top: 0, - viewport_rows: 1, - }, - config: &self.config, - cursor_border_color: LinearRgba::default(), - foreground: rgbcolor_to_window_color(palette.foreground), - pane: None, - is_active: true, - selection_fg: LinearRgba::default(), - selection_bg: LinearRgba::default(), - cursor_fg: LinearRgba::default(), - cursor_bg: LinearRgba::default(), - white_space, - filled_box, - window_is_transparent, - default_bg, - font: Some(Rc::clone(font)), - style: Some(font.style()), - use_pixel_positioning: true, - pre_shaped: None, - render_metrics: RenderMetrics::with_font_metrics(metrics), - }; - let cell_clusters = item.title.cluster(); - let shaped = self.cluster_and_shape(&cell_clusters, ¶ms)?; - let width = shaped.iter().map(|s| s.pixel_width).sum::() as isize; - - let desired_width = - (width + metrics.cell_width.get() as isize / 2).min(text_bounding_rect.width()); - - let text_bounding_rect: Rect = euclid::rect( - text_bounding_rect.min_x(), - text_bounding_rect.min_y(), - desired_width, - text_bounding_rect.height(), - ); - - let tab_bounding_rect = text_bounding_rect.inflate( - metrics.cell_width.get() as isize / 2, - metrics.cell_height.get() as isize / 4, - ); - let tab_bounding_rect = euclid::rect( - tab_bounding_rect.min_x(), - tab_bounding_rect.min_y(), - tab_bounding_rect.width(), - (metrics.cell_height.get() as isize * 2) - tab_bounding_rect.min_y(), - ); - - match item.item { - TabBarItem::Tab { active, .. } => { - let hover_x_start = tab_bounding_rect.min_x(); - let hover_x_end = tab_bounding_rect.max_x(); - - let hover = match &self.current_mouse_event { - Some(event) => { - let mouse_x = event.coords.x as isize; - let mouse_y = event.coords.y as isize; - mouse_x >= hover_x_start - && mouse_x <= hover_x_end - && mouse_y >= tab_bounding_rect.min_y() - && mouse_y <= tab_bounding_rect.max_y() - } - None => false, - }; - - let c = if active { - &colors.active_tab - } else if hover { - &colors.inactive_tab_hover - } else { - &colors.inactive_tab - }; - - let bg_color = match item.title.cells()[0].attrs().background() { - ColorAttribute::Default => c.bg_color, - color => palette.resolve_bg(color), - }; - let fg_color = match item.title.cells()[0].attrs().foreground() { - ColorAttribute::Default => c.fg_color, - color => palette.resolve_bg(color), - }; - - self.tab_background( - &mut layers[1], - metrics.cell_width.get() as isize / 2, - metrics.cell_height.get() as isize / 2, - tab_bounding_rect, - rgbcolor_to_window_color(bg_color), - )?; - - self.render_screen_line_opengl( - RenderScreenLineOpenGLParams { - top_pixel_y: text_bounding_rect.min_y() as f32, - left_pixel_x: text_bounding_rect.min_x() as f32, - pixel_width: text_bounding_rect.width() as f32, - foreground: rgbcolor_to_window_color(fg_color), - pre_shaped: Some(&shaped), - font: Some(Rc::clone(font)), - selection: 0..0, - ..params - }, - layers, - )?; - - if !active { - // Draw a partial dividing line on the edge of the tab. - // This gives a bit more definition on runs of inactive - // tabs without making everything feel like it is filled - // with lots of full lines - self.filled_rectangle( - &mut layers[1], - euclid::rect( - tab_bounding_rect.max_x().saturating_sub(1), - text_bounding_rect.min_y() + metrics.cell_height.get() as isize / 3, - 1, - (metrics.cell_height.get() * 0.75) as isize, - ), - rgbcolor_to_window_color(if hover { - colors.inactive_tab_edge_hover - } else { - colors.inactive_tab_edge - }), - )?; - } - - if hover || active { - // Overwrite any prior dividing line with the hover color - self.filled_rectangle( - &mut layers[1], - euclid::rect( - tab_bounding_rect.min_x().saturating_sub(1), - text_bounding_rect.min_y() + metrics.cell_height.get() as isize / 3, - 1, - (metrics.cell_height.get() * 0.75) as isize, - ), - rgbcolor_to_window_color(colors.inactive_tab_edge_hover), - )?; - } - - Ok(( - tab_bounding_rect.max_x() as f32, - UIItem { - x: tab_bounding_rect.min_x() as usize, - width: tab_bounding_rect.width() as usize, - y: tab_bounding_rect.min_y() as usize, - height: tab_bounding_rect.height() as usize, - item_type: UIItemType::TabBar(item.item.clone()), - }, - )) - } - TabBarItem::NewTabButton => { - let cross_height = (metrics.cell_width.get() * 0.75) as isize; - let line_thickness = self.render_metrics.underline_height * 2; - let tab_bar_height = metrics.cell_height.get() as isize * 2; - let cross_top = (tab_bar_height - cross_height) / 2; - let padding = (metrics.cell_width.get() / 3.) as isize; - - let tab_bounding_rect: Rect = euclid::rect( - text_bounding_rect.min_x(), - cross_top - padding, - 2 * padding + cross_height, - 2 * padding + cross_height, - ); - - let hover_x_start = tab_bounding_rect.min_x(); - let hover_x_end = tab_bounding_rect.max_x(); - - let hover = match &self.current_mouse_event { - Some(event) => { - let mouse_x = event.coords.x as isize; - let mouse_y = event.coords.y as isize; - mouse_x >= hover_x_start - && mouse_x <= hover_x_end - && mouse_y >= tab_bounding_rect.min_y() - && mouse_y <= tab_bounding_rect.max_y() - } - None => false, - }; - - let c = if hover { - &colors.new_tab_hover - } else { - &colors.new_tab - }; - - // Repaint the titlebar background in the gaps around the - // button, to cover over any right-statusbar text that - // renders underneath us - self.filled_rectangle( - &mut layers[1], - euclid::rect( - pos_x as isize, - 0, - (tab_bounding_rect.max_x() - pos_x as isize) - + metrics.cell_width.get() as isize / 2, - metrics.cell_height.get() as isize * 2, - ), - rgbcolor_to_window_color(if self.focused.is_some() { - self.config.window_frame.active_titlebar_bg - } else { - self.config.window_frame.inactive_titlebar_bg - }), - )?; - - // Background color for hover - self.filled_rectangle( - &mut layers[1], - tab_bounding_rect, - rgbcolor_to_window_color(c.bg_color), - )?; - - // Horizontal line - self.filled_rectangle( - &mut layers[1], - euclid::rect( - padding + tab_bounding_rect.min_x(), - cross_top + cross_height / 2 - line_thickness / 2, - cross_height, - line_thickness, - ), - rgbcolor_to_window_color(c.fg_color), - )?; - - // Vertical line - self.filled_rectangle( - &mut layers[1], - euclid::rect( - padding + tab_bounding_rect.min_x() + cross_height / 2 - line_thickness / 2, - cross_top, - line_thickness, - cross_height, - ), - rgbcolor_to_window_color(c.fg_color), - )?; - - Ok(( - tab_bounding_rect.max_x() as f32 + metrics.cell_width.get() as f32 / 2., - UIItem { - x: tab_bounding_rect.min_x() as usize, - width: tab_bounding_rect.width() as usize, - y: tab_bounding_rect.min_y() as usize, - height: tab_bounding_rect.height() as usize, - item_type: UIItemType::TabBar(item.item.clone()), - }, - )) - } - TabBarItem::None => { - // log::info!("{:#?}", shaped); - // Right align to window width - let tab_bounding_rect: Rect = euclid::rect( - tab_bounding_rect.min_x(), - tab_bounding_rect.min_y(), - self.dimensions.pixel_width as isize - tab_bounding_rect.min_x(), - tab_bounding_rect.height(), - ); - - let text_bounding_rect = tab_bounding_rect.inflate( - metrics.cell_width.get() as isize / -2, - metrics.cell_height.get() as isize / -4, - ); - - let desired_width = desired_width.min(text_bounding_rect.width()); - let text_bounding_rect: Rect = euclid::rect( - text_bounding_rect.max_x() - desired_width, - text_bounding_rect.min_y(), - desired_width, - text_bounding_rect.height(), - ); - - let tab_bounding_rect: Rect = euclid::rect( - text_bounding_rect.min_x(), - tab_bounding_rect.min_y(), - self.dimensions.pixel_width as isize - text_bounding_rect.min_x(), - tab_bounding_rect.height(), - ); - - if width > 0 { - let bg_color = match item.title.cells()[0].attrs().background() { - ColorAttribute::Default => colors.background, - color => palette.resolve_bg(color), - }; - let fg_color = match item.title.cells()[0].attrs().foreground() { - ColorAttribute::Default => colors.inactive_tab.fg_color, - color => palette.resolve_bg(color), - }; - - self.filled_rectangle( - &mut layers[0], - tab_bounding_rect, - rgbcolor_to_window_color(bg_color), - )?; - self.render_screen_line_opengl( - RenderScreenLineOpenGLParams { - top_pixel_y: text_bounding_rect.min_y() as f32, - left_pixel_x: text_bounding_rect.min_x() as f32, - pixel_width: text_bounding_rect.width() as f32, - foreground: rgbcolor_to_window_color(fg_color), - pre_shaped: Some(&shaped), - font: Some(Rc::clone(font)), - selection: 0..0, - ..params - }, - layers, - )?; - } - - Ok(( - tab_bounding_rect.max_x() as f32, - UIItem { - x: 0, - width: tab_bounding_rect.max_x() as usize, - y: tab_bounding_rect.min_y() as usize, - height: tab_bounding_rect.height() as usize, - item_type: UIItemType::TabBar(item.item.clone()), - }, - )) - } - } - } - pub fn tab_bar_pixel_height_impl( config: &ConfigHandle, fontconfig: &wezterm_font::FontConfiguration, @@ -833,6 +423,11 @@ impl super::TermWindow { } fn paint_fancy_tab_bar(&self, palette: &ColorPalette) -> anyhow::Result> { + use super::box_model::*; + use config::Dimension; + + let font = self.fonts.title_font()?; + let items = self.tab_bar.items(); let colors = self .config .colors @@ -840,13 +435,240 @@ impl super::TermWindow { .and_then(|c| c.tab_bar.as_ref()) .cloned() .unwrap_or_else(TabBarColors::default); - let font = self.fonts.title_font()?; - let metrics = font.metrics(); - let mut ui_items = vec![]; + let content = ElementContent::Children( + items + .into_iter() + .map(|item| { + let element = Element::with_line(&font, &item.title, palette); - let items = self.tab_bar.items(); - let max_tab_width = self.dimensions.pixel_width / items.len().saturating_sub(1).max(1); + match item.item { + TabBarItem::None => element + .item_type(UIItemType::TabBar(TabBarItem::None)) + .zindex(-1) + .float(Float::Right) + .line_height(Some(2.0)) + .margin(BoxDimension { + left: Dimension::Cells(0.), + right: Dimension::Cells(0.), + top: Dimension::Cells(0.0), + bottom: Dimension::Cells(0.), + }) + .padding(BoxDimension { + left: Dimension::Cells(0.5), + right: Dimension::Cells(0.), + top: Dimension::Cells(0.), + bottom: Dimension::Cells(0.), + }) + .border(BoxDimension::new(Dimension::Pixels(0.))) + .colors(ElementColors { + border: BorderColor::default(), + bg: rgbcolor_to_window_color(colors.inactive_tab.bg_color).into(), + text: rgbcolor_to_window_color(colors.inactive_tab.fg_color).into(), + }), + TabBarItem::NewTabButton => element + .item_type(UIItemType::TabBar(item.item.clone())) + .margin(BoxDimension { + left: Dimension::Cells(0.5), + right: Dimension::Cells(0.), + top: Dimension::Cells(0.25), + bottom: Dimension::Cells(0.), + }) + .padding(BoxDimension { + left: Dimension::Cells(0.5), + right: Dimension::Cells(0.5), + top: Dimension::Cells(0.25), + bottom: Dimension::Cells(0.25), + }) + .border(BoxDimension::new(Dimension::Pixels(1.))) + .colors(ElementColors { + border: BorderColor::default(), + bg: rgbcolor_to_window_color(colors.new_tab.bg_color).into(), + text: rgbcolor_to_window_color(colors.new_tab.fg_color).into(), + }) + .hover_colors(Some(ElementColors { + border: BorderColor::default(), + bg: rgbcolor_to_window_color(colors.inactive_tab_hover.bg_color) + .into(), + text: rgbcolor_to_window_color(colors.inactive_tab_hover.fg_color) + .into(), + })), + TabBarItem::Tab { active, .. } if active => element + .item_type(UIItemType::TabBar(item.item.clone())) + .margin(BoxDimension { + left: Dimension::Cells(0.), + right: Dimension::Cells(0.), + top: Dimension::Cells(0.25), + bottom: Dimension::Cells(0.), + }) + .padding(BoxDimension { + left: Dimension::Cells(0.5), + right: Dimension::Cells(0.5), + top: Dimension::Cells(0.25), + bottom: Dimension::Cells(0.5), + }) + .border(BoxDimension::new(Dimension::Pixels(1.))) + .border_corners(Some(Corners { + top_left: SizedPoly { + width: Dimension::Cells(0.5), + height: Dimension::Cells(0.5), + poly: TOP_LEFT_ROUNDED_CORNER, + }, + top_right: SizedPoly { + width: Dimension::Cells(0.5), + height: Dimension::Cells(0.5), + poly: TOP_RIGHT_ROUNDED_CORNER, + }, + bottom_left: SizedPoly::none(), + bottom_right: SizedPoly::none(), + })) + .colors(ElementColors { + border: BorderColor::new(rgbcolor_to_window_color( + colors.active_tab.bg_color, + )), + bg: rgbcolor_to_window_color(colors.active_tab.bg_color).into(), + text: rgbcolor_to_window_color(colors.active_tab.fg_color).into(), + }), + TabBarItem::Tab { .. } => element + .item_type(UIItemType::TabBar(item.item.clone())) + .margin(BoxDimension { + left: Dimension::Cells(0.), + right: Dimension::Cells(0.), + top: Dimension::Cells(0.25), + bottom: Dimension::Cells(0.), + }) + .padding(BoxDimension { + left: Dimension::Cells(0.5), + right: Dimension::Cells(0.5), + top: Dimension::Cells(0.25), + bottom: Dimension::Cells(0.5), + }) + .border(BoxDimension::new(Dimension::Pixels(1.))) + .border_corners(Some(Corners { + top_left: SizedPoly { + width: Dimension::Cells(0.5), + height: Dimension::Cells(0.5), + poly: TOP_LEFT_ROUNDED_CORNER, + }, + top_right: SizedPoly { + width: Dimension::Cells(0.5), + height: Dimension::Cells(0.5), + poly: TOP_RIGHT_ROUNDED_CORNER, + }, + bottom_left: SizedPoly { + width: Dimension::Cells(0.), + height: Dimension::Cells(0.33), + poly: &[], + }, + bottom_right: SizedPoly { + width: Dimension::Cells(0.), + height: Dimension::Cells(0.33), + poly: &[], + }, + })) + .colors({ + let bg = rgbcolor_to_window_color(colors.inactive_tab.bg_color); + let edge = rgbcolor_to_window_color(colors.inactive_tab_edge); + ElementColors { + border: BorderColor { + left: bg, + right: edge, + top: bg, + bottom: bg, + }, + bg: bg.into(), + text: rgbcolor_to_window_color(colors.inactive_tab.fg_color) + .into(), + } + }) + .hover_colors(Some(ElementColors { + border: BorderColor::new(rgbcolor_to_window_color( + colors.inactive_tab_hover.bg_color, + )), + bg: rgbcolor_to_window_color(colors.inactive_tab_hover.bg_color) + .into(), + text: rgbcolor_to_window_color(colors.inactive_tab_hover.fg_color) + .into(), + })), + } + }) + .collect(), + ); + + let tabs = Element::new(&font, content) + .display(DisplayType::Block) + .item_type(UIItemType::TabBar(TabBarItem::None)) + .padding(BoxDimension { + left: Dimension::Cells(0.5), + right: Dimension::Cells(0.), + top: Dimension::Cells(0.), + bottom: Dimension::Cells(0.), + }) + .colors(ElementColors { + border: BorderColor::default(), + bg: rgbcolor_to_window_color(if self.focused.is_some() { + self.config.window_frame.active_titlebar_bg + } else { + self.config.window_frame.inactive_titlebar_bg + }) + .into(), + text: rgbcolor_to_window_color(if self.focused.is_some() { + self.config.window_frame.active_titlebar_fg + } else { + self.config.window_frame.inactive_titlebar_fg + }) + .into(), + }); + let metrics = RenderMetrics::with_font_metrics(&font.metrics()); + + let computed = self.compute_element( + &LayoutContext { + height: DimensionContext { + dpi: self.dimensions.dpi as f32, + pixel_max: self.terminal_size.pixel_height as f32, + pixel_cell: metrics.cell_size.height as f32, + }, + width: DimensionContext { + dpi: self.dimensions.dpi as f32, + pixel_max: self.terminal_size.pixel_width as f32, + pixel_cell: metrics.cell_size.width as f32, + }, + bounds: euclid::rect( + 0., + 0., + self.dimensions.pixel_width as f32, + self.dimensions.pixel_height as f32, + ), + metrics: &metrics, + gl_state: self.render_state.as_ref().unwrap(), + }, + &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); + } + + 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]]; @@ -858,60 +680,7 @@ impl super::TermWindow { vb[1].map(&mut vb_mut1), vb[2].map(&mut vb_mut2), ]; - - // Overall window titlebar background - self.filled_rectangle( - &mut layers[0], - Rect::new( - Point::new(0, 0), - Size::new( - self.dimensions.pixel_width as isize, - metrics.cell_height.get() as isize * 2, - ), - ), - rgbcolor_to_window_color(if self.focused.is_some() { - self.config.window_frame.active_titlebar_bg - } else { - self.config.window_frame.inactive_titlebar_bg - }), - )?; - - if let Some(status_item) = items - .iter() - .find(|item| matches!(item.item, TabBarItem::None)) - { - let (_x, item) = self.paint_one_tab( - 0., - self.dimensions.pixel_width, - palette, - status_item, - &colors, - &font, - &metrics, - &mut layers, - )?; - ui_items.push(item); - } - - let mut x = metrics.cell_width.get() as f32 * 0.25; - for item in items.iter() { - if matches!(item.item, TabBarItem::None) { - // Already handled this one - continue; - } - let (new_x, item) = self.paint_one_tab( - x, - max_tab_width, - palette, - item, - &colors, - &font, - &metrics, - &mut layers, - )?; - x = new_x; - ui_items.push(item); - } + self.render_element(&computed, &mut layers[1], None)?; Ok(ui_items) } @@ -1144,12 +913,11 @@ impl super::TermWindow { ); self.filled_rectangle( &mut layers[0], - Rect::new( - Point::new(0, 0), - Size::new( - self.dimensions.pixel_width as isize, - self.dimensions.pixel_height as isize, - ), + euclid::rect( + 0., + 0., + self.dimensions.pixel_width as f32, + self.dimensions.pixel_height as f32, ), background, )?; @@ -1164,63 +932,61 @@ impl super::TermWindow { // We want to fill out to the edges of the splits let (x, width_delta) = if pos.left == 0 { - (0, padding_left as isize + (cell_width / 2.0) as isize) + (0., padding_left + (cell_width / 2.0)) } else { ( - padding_left as isize - (cell_width / 2.0) as isize - + (pos.left as f32 * cell_width) as isize, - cell_width as isize, + padding_left - (cell_width / 2.0) + (pos.left as f32 * cell_width), + cell_width, ) }; let (y, height_delta) = if pos.top == 0 { ( - (top_pixel_y - padding_top) as isize, - padding_top as isize + (cell_height / 2.0) as isize, + (top_pixel_y - padding_top), + padding_top + (cell_height / 2.0), ) } else { ( - top_pixel_y as isize + (pos.top as f32 * cell_height) as isize - - (cell_height / 2.0) as isize, - cell_height as isize, + top_pixel_y + (pos.top as f32 * cell_height) - (cell_height / 2.0), + cell_height, ) }; let mut quad = self.filled_rectangle( &mut layers[0], - Rect::new( - Point::new(x, y), - Size::new( - (pos.width as f32 * cell_width) as isize - + width_delta - + if pos.left + pos.width >= self.terminal_size.cols as usize { - // And all the way to the right edge if we're right-most - crate::termwindow::resize::effective_right_padding( - &self.config, - DimensionContext { - dpi: self.dimensions.dpi as f32, - pixel_max: self.terminal_size.pixel_width as f32, - pixel_cell: cell_width, - }, - ) as isize - } else { - 0 - }, - (pos.height as f32 * cell_height) as isize - + height_delta - + if pos.top + pos.height >= self.terminal_size.rows as usize { - // And all the way to the bottom if we're bottom-most - self.config.window_padding.bottom.evaluate_as_pixels( - DimensionContext { - dpi: self.dimensions.dpi as f32, - pixel_max: self.terminal_size.pixel_height as f32, - pixel_cell: cell_height, - }, - ) as isize - } else { - 0 - }, - ), + euclid::rect( + x, + y, + (pos.width as f32 * cell_width) + + width_delta + + if pos.left + pos.width >= self.terminal_size.cols as usize { + // And all the way to the right edge if we're right-most + crate::termwindow::resize::effective_right_padding( + &self.config, + DimensionContext { + dpi: self.dimensions.dpi as f32, + pixel_max: self.terminal_size.pixel_width as f32, + pixel_cell: cell_width, + }, + ) as f32 + } else { + 0. + }, + (pos.height as f32 * cell_height) + + height_delta as f32 + + if pos.top + pos.height >= self.terminal_size.rows as usize { + // And all the way to the bottom if we're bottom-most + self.config + .window_padding + .bottom + .evaluate_as_pixels(DimensionContext { + dpi: self.dimensions.dpi as f32, + pixel_max: self.terminal_size.pixel_height as f32, + pixel_cell: cell_height, + }) + } else { + 0. + }, ), rgbcolor_alpha_to_window_color( palette.background, @@ -1275,15 +1041,11 @@ impl super::TermWindow { let mut quad = self.filled_rectangle( &mut layers[0], - Rect::new( - Point::new( - ((pos.left as f32 * cell_width) + padding_left) as isize, - (top_pixel_y + (pos.top as f32 * cell_height) + padding_top) as isize, - ), - Size::new( - (pos.width as f32 * cell_width) as isize, - (pos.height as f32 * cell_height) as isize, - ), + euclid::rect( + (pos.left as f32 * cell_width) + padding_left, + top_pixel_y + (pos.top as f32 * cell_height) + padding_top, + pos.width as f32 * cell_width, + pos.height as f32 * cell_height, ), background, )?; @@ -1345,12 +1107,11 @@ impl super::TermWindow { self.filled_rectangle( &mut layers[2], - Rect::new( - Point::new( - self.dimensions.pixel_width as isize - padding as isize, - thumb_top as isize, - ), - Size::new(padding as isize, thumb_size as isize), + euclid::rect( + self.dimensions.pixel_width as f32 - padding, + thumb_top, + padding, + thumb_size, ), color, )?; @@ -1567,15 +1328,11 @@ impl super::TermWindow { if split.direction == SplitDirection::Horizontal { self.filled_rectangle( &mut quads, - Rect::new( - Point::new( - pos_x as isize + (cell_width / 2.0) as isize, - pos_y as isize - (cell_height / 2.0) as isize, - ), - Size::new( - self.render_metrics.underline_height, - (1 + split.size as isize) * cell_height as isize, - ), + euclid::rect( + pos_x + (cell_width / 2.0), + pos_y - (cell_height / 2.0), + self.render_metrics.underline_height as f32, + (1. + split.size as f32) * cell_height, ), foreground, )?; @@ -1591,15 +1348,11 @@ impl super::TermWindow { } else { self.filled_rectangle( &mut quads, - Rect::new( - Point::new( - pos_x as isize - (cell_width / 2.0) as isize, - pos_y as isize + (cell_height / 2.0) as isize, - ), - Size::new( - (1 + split.size as isize) * cell_width as isize, - self.render_metrics.underline_height, - ), + euclid::rect( + pos_x - (cell_width / 2.0), + pos_y + (cell_height / 2.0), + (1.0 + split.size as f32) * cell_width, + self.render_metrics.underline_height as f32, ), foreground, )?; @@ -1840,10 +1593,10 @@ impl super::TermWindow { }; let bounding_rect = euclid::rect( - params.left_pixel_x as isize, - params.top_pixel_y as isize, - params.pixel_width as isize, - cell_height as isize, + params.left_pixel_x, + params.top_pixel_y, + params.pixel_width, + cell_height, ); // Make a pass to compute background colors. @@ -1879,24 +1632,20 @@ impl super::TermWindow { }; if !bg_is_default { - let rect = Rect::new( - Point::new( - (params.left_pixel_x - + if params.use_pixel_positioning { - item.x_pos - } else { - cluster.first_cell_idx as f32 * cell_width - }) as isize, - params.top_pixel_y as isize, - ), - Size::new( - if params.use_pixel_positioning { - item.pixel_width as isize + let rect = euclid::rect( + params.left_pixel_x + + if params.use_pixel_positioning { + item.x_pos } else { - cluster_width as isize * cell_width as isize + cluster.first_cell_idx as f32 * cell_width }, - cell_height as isize, - ), + params.top_pixel_y, + if params.use_pixel_positioning { + item.pixel_width + } else { + cluster_width as f32 * cell_width + }, + cell_height, ); if let Some(rect) = rect.intersection(&bounding_rect) { let mut quad = self.filled_rectangle(&mut layers[0], rect, bg_color)?; @@ -1909,17 +1658,11 @@ impl super::TermWindow { if !params.selection.is_empty() { let mut quad = self.filled_rectangle( &mut layers[0], - Rect::new( - Point::new( - (params.left_pixel_x + (params.selection.start as f32 * cell_width)) - as isize, - params.top_pixel_y as isize, - ), - Size::new( - ((params.selection.end - params.selection.start) as f32 * cell_width) - as isize, - cell_height as isize, - ), + euclid::rect( + params.left_pixel_x + (params.selection.start as f32 * cell_width), + params.top_pixel_y, + (params.selection.end - params.selection.start) as f32 * cell_width, + cell_height, ), params.selection_bg, )?; @@ -2262,15 +2005,11 @@ impl super::TermWindow { if params.line.is_reverse() { let mut quad = self.filled_rectangle( &mut layers[0], - Rect::new( - Point::new( - (params.left_pixel_x + (last_cell_idx as f32 * cell_width)) as isize, - params.top_pixel_y as isize, - ), - Size::new( - ((num_cols - last_cell_idx) as f32 * cell_width) as isize, - cell_height as isize, - ), + euclid::rect( + params.left_pixel_x + (last_cell_idx as f32 * cell_width), + params.top_pixel_y, + (num_cols - last_cell_idx) as f32 * cell_width, + cell_height, ), params.foreground, )?; @@ -2323,13 +2062,11 @@ impl super::TermWindow { // Avoid poking a transparent hole underneath the cursor let mut quad = self.filled_rectangle( &mut layers[2], - Rect::new( - Point::new( - (params.left_pixel_x + (params.cursor.x as f32 * cell_width)) - as isize, - params.top_pixel_y as isize, - ), - Size::new(cell_width as isize, cell_height as isize), + euclid::rect( + params.left_pixel_x + (params.cursor.x as f32 * cell_width), + params.top_pixel_y, + cell_width, + cell_height, ), bg_color, )?; @@ -2791,11 +2528,11 @@ impl super::TermWindow { } } -fn rgbcolor_to_window_color(color: RgbColor) -> LinearRgba { +pub fn rgbcolor_to_window_color(color: RgbColor) -> LinearRgba { rgbcolor_alpha_to_window_color(color, 1.0) } -fn rgbcolor_alpha_to_window_color(color: RgbColor, alpha: f32) -> LinearRgba { +pub fn rgbcolor_alpha_to_window_color(color: RgbColor, alpha: f32) -> LinearRgba { let (red, green, blue, _) = color.to_linear_tuple_rgba(); LinearRgba::with_components(red, green, blue, alpha) } diff --git a/wezterm-gui/src/utilsprites.rs b/wezterm-gui/src/utilsprites.rs index f948c0a2d..e475038bf 100644 --- a/wezterm-gui/src/utilsprites.rs +++ b/wezterm-gui/src/utilsprites.rs @@ -43,6 +43,24 @@ impl RenderMetrics { } } + pub fn scale_line_height(&self, line_height: f64) -> Self { + let size = euclid::size2( + self.cell_size.width, + (self.cell_size.height as f64 * line_height) as isize, + ); + + let adjust = (((self.descender_row as f64 * line_height) - self.descender_row as f64) / 2.0) + as isize; + Self { + descender: self.descender - PixelLength::new(adjust as f64), + descender_row: self.descender_row - adjust, + descender_plus_two: self.descender_plus_two - adjust, + underline_height: self.underline_height, + strike_row: self.strike_row, + cell_size: size, + } + } + pub fn new(fonts: &Rc) -> anyhow::Result { let metrics = fonts .default_font_metrics() diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 3e8903f9f..92827aecb 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -5,6 +5,7 @@ use std::convert::TryFrom; pub struct PixelUnit; pub struct ScreenPixelUnit; pub type Point = euclid::Point2D; +pub type PointF = euclid::Point2D; pub type ScreenPoint = euclid::Point2D; /// Which key is pressed. Not all of these are probable to appear diff --git a/window/src/lib.rs b/window/src/lib.rs index 7103dd7cc..c74be8ee1 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -51,7 +51,9 @@ pub struct Dimensions { } pub type Rect = euclid::Rect; +pub type RectF = euclid::Rect; pub type Size = euclid::Size2D; +pub type SizeF = euclid::Size2D; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum MouseCursor {