gui: add rounded corners to fancy tab buttons

This commit is contained in:
Wez Furlong committed 2021-10-05 09:12:13 -07:00
1 parent 6d26a84cf1
commit f3159b6689
2 files changed
+206 -59

No files matched your search

+62 -37
View File
@@ -1,11 +1,13 @@
use crate::glyphcache::GlyphCache;
use crate::utilsprites::RenderMetrics;
use ::window::bitmaps::atlas::Sprite;
use ::window::color::{LinearRgba, SrgbaPixel};
use std::ops::Range;
use termwiz::surface::CursorShape;
use tiny_skia::{FillRule, Paint, Path, PathBuilder, PixmapMut, Stroke, Transform};
use wezterm_font::units::{IntPixelLength, PixelLength};
use window::bitmaps::Texture2d;
use window::{BitmapImage, Image, Point, Rect};
use window::{BitmapImage, Image, Point, Rect, Size};
bitflags::bitflags! {
pub struct Quadrant: u8{
@@ -145,15 +147,21 @@ pub enum BlockKey {
Braille(u8),
Poly(&'static [Poly]),
PolyWithCustomMetrics {
polys: &'static [Poly],
underline_height: IntPixelLength,
cell_size: Size,
},
}
/// Filled polygon used to describe the more complex shapes in
/// <https://unicode.org/charts/PDF/U1FB00.pdf>
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Poly {
path: &'static [PolyCommand],
intensity: BlockAlpha,
style: PolyStyle,
pub path: &'static [PolyCommand],
pub intensity: BlockAlpha,
pub style: PolyStyle,
}
pub type BlockPoint = (BlockCoord, BlockCoord);
@@ -3627,7 +3635,7 @@ impl BlockKey {
}
impl<T: Texture2d> GlyphCache<T> {
fn draw_polys(&mut self, polys: &[Poly], buffer: &mut Image) {
fn draw_polys(&mut self, metrics: &RenderMetrics, polys: &[Poly], buffer: &mut Image) {
let (width, height) = buffer.image_dimensions();
let mut pixmap =
PixmapMut::from_bytes(buffer.pixel_data_slice_mut(), width as u32, height as u32)
@@ -3646,15 +3654,10 @@ impl<T: Texture2d> GlyphCache<T> {
paint.force_hq_pipeline = true;
let mut pb = PathBuilder::new();
for item in path.iter() {
item.to_skia(width, height, self.metrics.underline_height as f32, &mut pb);
item.to_skia(width, height, metrics.underline_height as f32, &mut pb);
}
let path = pb.finish().expect("poly path to be valid");
style.apply(
self.metrics.underline_height as f32,
&paint,
&path,
&mut pixmap,
);
style.apply(metrics.underline_height as f32, &paint, &path, &mut pixmap);
}
}
@@ -3677,7 +3680,9 @@ impl<T: Texture2d> GlyphCache<T> {
buffer.clear_rect(cell_rect, SrgbaPixel::rgba(0xff, 0xff, 0xff, 0xff));
}
Some(CursorShape::BlinkingBlock | CursorShape::SteadyBlock) => {
let metrics = self.metrics.clone();
self.draw_polys(
&metrics,
&[Poly {
path: &[
PolyCommand::MoveTo(BlockCoord::Zero, BlockCoord::Zero),
@@ -3693,7 +3698,9 @@ impl<T: Texture2d> GlyphCache<T> {
);
}
Some(CursorShape::BlinkingBar | CursorShape::SteadyBar) => {
let metrics = self.metrics.clone();
self.draw_polys(
&metrics,
&[Poly {
path: &[
PolyCommand::MoveTo(BlockCoord::Zero, BlockCoord::Zero),
@@ -3706,7 +3713,9 @@ impl<T: Texture2d> GlyphCache<T> {
);
}
Some(CursorShape::BlinkingUnderline | CursorShape::SteadyUnderline) => {
let metrics = self.metrics.clone();
self.draw_polys(
&metrics,
&[Poly {
path: &[
PolyCommand::MoveTo(BlockCoord::Zero, BlockCoord::One),
@@ -3726,37 +3735,53 @@ impl<T: Texture2d> GlyphCache<T> {
}
pub fn block_sprite(&mut self, block: BlockKey) -> anyhow::Result<Sprite<T>> {
let metrics = match &block {
BlockKey::PolyWithCustomMetrics {
underline_height,
cell_size,
..
} => RenderMetrics {
descender: PixelLength::new(0.),
descender_row: 0,
descender_plus_two: 0,
underline_height: *underline_height,
strike_row: 0,
cell_size: cell_size.clone(),
},
_ => self.metrics.clone(),
};
let mut buffer = Image::new(
self.metrics.cell_size.width as usize,
self.metrics.cell_size.height as usize,
metrics.cell_size.width as usize,
metrics.cell_size.height as usize,
);
let black = SrgbaPixel::rgba(0, 0, 0, 0);
let cell_rect = Rect::new(Point::new(0, 0), self.metrics.cell_size);
let cell_rect = Rect::new(Point::new(0, 0), metrics.cell_size);
buffer.clear_rect(cell_rect, black);
match block {
BlockKey::Upper(num) => {
let lower = self.metrics.cell_size.height as f32 * (num as f32) / 8.;
let width = self.metrics.cell_size.width as usize;
let lower = metrics.cell_size.height as f32 * (num as f32) / 8.;
let width = metrics.cell_size.width as usize;
fill_rect(&mut buffer, 0..width, 0..scale(lower));
}
BlockKey::Lower(num) => {
let upper = self.metrics.cell_size.height as f32 * ((8 - num) as f32) / 8.;
let width = self.metrics.cell_size.width as usize;
let height = self.metrics.cell_size.height as usize;
let upper = metrics.cell_size.height as f32 * ((8 - num) as f32) / 8.;
let width = metrics.cell_size.width as usize;
let height = metrics.cell_size.height as usize;
fill_rect(&mut buffer, 0..width, scale(upper)..height);
}
BlockKey::Left(num) => {
let width = self.metrics.cell_size.width as f32 * (num as f32) / 8.;
let height = self.metrics.cell_size.height as usize;
let width = metrics.cell_size.width as f32 * (num as f32) / 8.;
let height = metrics.cell_size.height as usize;
fill_rect(&mut buffer, 0..scale(width), 0..height);
}
BlockKey::Right(num) => {
let left = self.metrics.cell_size.width as f32 * ((8 - num) as f32) / 8.;
let width = self.metrics.cell_size.width as usize;
let height = self.metrics.cell_size.height as usize;
let left = metrics.cell_size.width as f32 * ((8 - num) as f32) / 8.;
let width = metrics.cell_size.width as usize;
let height = metrics.cell_size.height as usize;
fill_rect(&mut buffer, scale(left)..width, 0..height);
}
BlockKey::Full(alpha) => {
@@ -3766,10 +3791,10 @@ impl<T: Texture2d> GlyphCache<T> {
buffer.clear_rect(cell_rect, fill.srgba_pixel());
}
BlockKey::Quadrants(quads) => {
let y_half = self.metrics.cell_size.height as f32 / 2.;
let x_half = self.metrics.cell_size.width as f32 / 2.;
let width = self.metrics.cell_size.width as usize;
let height = self.metrics.cell_size.height as usize;
let y_half = metrics.cell_size.height as f32 / 2.;
let x_half = metrics.cell_size.width as f32 / 2.;
let width = metrics.cell_size.width as usize;
let height = metrics.cell_size.height as usize;
if quads.contains(Quadrant::UPPER_LEFT) {
fill_rect(&mut buffer, 0..scale(x_half), 0..scale(y_half));
}
@@ -3784,10 +3809,10 @@ impl<T: Texture2d> GlyphCache<T> {
}
}
BlockKey::Sextants(s) => {
let y_third = self.metrics.cell_size.height as f32 / 3.;
let x_half = self.metrics.cell_size.width as f32 / 2.;
let width = self.metrics.cell_size.width as usize;
let height = self.metrics.cell_size.height as usize;
let y_third = metrics.cell_size.height as f32 / 3.;
let x_half = metrics.cell_size.width as f32 / 2.;
let width = metrics.cell_size.width as usize;
let height = metrics.cell_size.height as usize;
if s.contains(Sextant::ONE) {
fill_rect(&mut buffer, 0..scale(x_half), 0..scale(y_third));
@@ -3831,8 +3856,8 @@ impl<T: Texture2d> GlyphCache<T> {
//
// NOTE: for simplicity & performance reasons, a dot is a square not a circle.
let dot_area_width = self.metrics.cell_size.width as f32 / 2.;
let dot_area_height = self.metrics.cell_size.height as f32 / 4.;
let dot_area_width = metrics.cell_size.width as f32 / 2.;
let dot_area_height = metrics.cell_size.height as f32 / 4.;
let square_length = dot_area_width / 2.;
let topleft_offset_x = dot_area_width / 2. - square_length / 2.;
let topleft_offset_y = dot_area_height / 2. - square_length / 2.;
@@ -3880,8 +3905,8 @@ impl<T: Texture2d> GlyphCache<T> {
pixmap.fill_path(&path, &paint, FillRule::Winding, identity, None);
}
}
BlockKey::Poly(polys) => {
self.draw_polys(polys, &mut buffer);
BlockKey::Poly(polys) | BlockKey::PolyWithCustomMetrics { polys, .. } => {
self.draw_polys(&metrics, polys, &mut buffer);
}
}
+144 -22
View File
@@ -1,4 +1,5 @@
use crate::customglyph::BlockKey;
use crate::customglyph::*;
use crate::glium::texture::SrgbTexture2d;
use crate::glyphcache::{CachedGlyph, GlyphCache};
use crate::quad::Quad;
@@ -28,6 +29,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use termwiz::cell::{unicode_column_width, Blink};
use termwiz::cellcluster::CellCluster;
use termwiz::surface::{CursorShape, CursorVisibility};
use wezterm_font::units::IntPixelLength;
use wezterm_font::{ClearShapeCache, FontMetrics, GlyphInfo, LoadedFont};
use wezterm_term::color::{ColorAttribute, ColorPalette, RgbColor};
use wezterm_term::{CellAttributes, Line, StableRowIndex};
@@ -327,6 +329,111 @@ impl super::TermWindow {
Ok(quad)
}
fn poly_quad<'a>(
&self,
layer: &'a mut MappedQuads,
point: Point,
polys: &'static [Poly],
underline_height: IntPixelLength,
cell_size: Size,
color: LinearRgba,
) -> anyhow::Result<Quad<'a>> {
let left_offset = self.dimensions.pixel_width as f32 / 2.;
let top_offset = self.dimensions.pixel_height as f32 / 2.;
let gl_state = self.render_state.as_ref().unwrap();
let sprite = gl_state
.glyph_cache
.borrow_mut()
.cached_block(BlockKey::PolyWithCustomMetrics {
polys,
underline_height,
cell_size,
})?
.texture_coords();
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,
);
quad.set_texture_adjust(0., 0., 0., 0.);
quad.set_texture(sprite);
quad.set_fg_color(color);
quad.set_hsv(None);
quad.set_has_color(false);
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,
mut pos_x: f32,
@@ -408,6 +515,7 @@ impl super::TermWindow {
None => false,
};
let is_tab;
let (bg_color, fg_color, is_status) = match item.item {
TabBarItem::Tab { active, .. } => {
let c = if active {
@@ -417,6 +525,7 @@ impl super::TermWindow {
} else {
&colors.inactive_tab
};
is_tab = true;
(c.bg_color, c.fg_color, false)
}
TabBarItem::NewTabButton => {
@@ -425,9 +534,13 @@ impl super::TermWindow {
} else {
&colors.new_tab
};
is_tab = false;
(c.bg_color, c.fg_color, false)
}
TabBarItem::None => (colors.background, colors.inactive_tab.fg_color, true),
TabBarItem::None => {
is_tab = false;
(colors.background, colors.inactive_tab.fg_color, true)
}
};
let bg_start;
@@ -448,17 +561,26 @@ impl super::TermWindow {
};
if !is_empty {
self.filled_rectangle(
&mut layers[0],
Rect::new(
Point::new(bg_start as isize, top_y as isize),
Size::new(
width as isize,
(metrics.cell_height.get() as f32 * 1.5).ceil() as isize,
),
let rect = Rect::new(
Point::new(bg_start as isize, top_y as isize),
Size::new(
width as isize,
(metrics.cell_height.get() as f32 * 1.5).ceil() as isize,
),
rgbcolor_to_window_color(bg_color),
)?;
);
let color = rgbcolor_to_window_color(bg_color);
if is_tab {
self.tab_background(
&mut layers[0],
metrics.cell_width.get() as isize / 3,
metrics.cell_height.get() as isize / 3,
rect,
color,
)?;
} else {
self.filled_rectangle(&mut layers[0], rect, color)?;
}
let glyph_color = rgbcolor_to_window_color(fg_color);
self.render_screen_line_opengl(
@@ -544,6 +666,17 @@ impl super::TermWindow {
}),
)?;
let mut x = 0.;
for item in items.iter() {
let (new_x, item) =
self.paint_one_tab(x, palette, item, &colors, &font, &metrics, &mut layers)?;
x = new_x;
match item.item_type {
UIItemType::TabBar(TabBarItem::None) => ui_items.insert(0, item),
_ => ui_items.push(item),
}
}
// Dividing line that is logically part of the active tab
self.filled_rectangle(
&mut layers[0],
@@ -560,17 +693,6 @@ impl super::TermWindow {
rgbcolor_to_window_color(colors.active_tab.bg_color),
)?;
let mut x = 0.;
for item in items.iter() {
let (new_x, item) =
self.paint_one_tab(x, palette, item, &colors, &font, &metrics, &mut layers)?;
x = new_x;
match item.item_type {
UIItemType::TabBar(TabBarItem::None) => ui_items.insert(0, item),
_ => ui_items.push(item),
}
}
Ok(ui_items)
}