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.

feat(gui): draw elevated surfaces with a blurred nine-slice shadow

RoversXa68787a7f03f
diff --git a/wezterm-gui/src/glyphcache.rs b/wezterm-gui/src/glyphcache.rs
index 850dbe72..6e8d1c1f 100644
--- a/wezterm-gui/src/glyphcache.rs
+++ b/wezterm-gui/src/glyphcache.rs
@@ -641,6 +641,9 @@ pub struct GlyphCache {
pub cursor_glyphs: HashMap<(Option<CursorShape>, u8), Sprite>,
pub color: HashMap<(RgbColor, NotNan<f32>), Sprite>,
pub rotated_svg_icons: HashMap<SizedRotatedSvgIconKey, Sprite>,
+ /// Blurred rounded-rectangle silhouettes, one per corner radius and
+ /// blur, drawn as nine slices under elevated surfaces.
+ pub shadows: HashMap<crate::ui::shadow::ShadowKey, Sprite>,
min_frame_duration: Duration,
}
@@ -717,6 +720,7 @@ impl GlyphCache {
app_icons: HashMap::new(),
material_icons: HashMap::new(),
rotated_svg_icons: HashMap::new(),
+ shadows: HashMap::new(),
cursor_glyphs: HashMap::new(),
color: HashMap::new(),
min_frame_duration: Duration::from_millis(1000 / fonts.config().max_fps as u64),
@@ -753,6 +757,7 @@ impl GlyphCache {
app_icons: HashMap::new(),
material_icons: HashMap::new(),
rotated_svg_icons: HashMap::new(),
+ shadows: HashMap::new(),
cursor_glyphs: HashMap::new(),
color: HashMap::new(),
min_frame_duration: Duration::from_millis(1000 / fonts.config().max_fps as u64),
@@ -1414,6 +1419,19 @@ impl GlyphCache {
Ok(sprite)
}
+ pub fn cached_shadow(
+ &mut self,
+ key: crate::ui::shadow::ShadowKey,
+ ) -> anyhow::Result<Sprite> {
+ if let Some(sprite) = self.shadows.get(&key) {
+ return Ok(sprite.clone());
+ }
+ let image = crate::ui::shadow::rasterize_shadow(key);
+ let sprite = self.atlas.allocate(&image)?;
+ self.shadows.insert(key, sprite.clone());
+ Ok(sprite)
+ }
+
pub fn cached_brand_icon(&mut self, icon: BrandIcon, size: usize) -> anyhow::Result<Sprite> {
let size = size.max(1).min(u16::MAX as usize);
let key = SizedBrandIconKey {
diff --git a/wezterm-gui/src/ui/draw.rs b/wezterm-gui/src/ui/draw.rs
index 6c0b6aaa..70cab771 100644
--- a/wezterm-gui/src/ui/draw.rs
+++ b/wezterm-gui/src/ui/draw.rs
@@ -178,6 +178,79 @@ impl<'a> DrawContext<'a> {
/// Standard elevated surface used by overview cards. Two restrained
/// layers provide depth without a hard concentric halo.
+ /// A soft shadow under a rounded rectangle: the blurred silhouette from
+ /// the atlas, drawn as nine slices. `sigma` is the blur's standard
+ /// deviation and `offset_y` how far the shadow is dropped, both in
+ /// pixels; the colour's alpha is the shadow's peak opacity.
+ #[allow(clippy::too_many_arguments)]
+ pub(crate) fn draw_shadow(
+ &self,
+ layers: &mut TripleLayerQuadAllocator<'_>,
+ layer_num: usize,
+ rect: RectF,
+ radius: f32,
+ sigma: f32,
+ offset_y: f32,
+ color: LinearRgba,
+ ) -> anyhow::Result<()> {
+ let Some(snapped) = pixel_snap_rounded_rect(
+ rect.origin.x,
+ rect.origin.y + offset_y,
+ rect.size.width,
+ rect.size.height,
+ radius,
+ ) else {
+ return Ok(());
+ };
+ let sigma = sigma.round().clamp(0.0, 64.0);
+ if color.3 <= 0.0 || (sigma <= 0.0 && snapped.radius <= 0.0) {
+ return Ok(());
+ }
+ let key = crate::ui::shadow::ShadowKey {
+ radius: snapped.radius.round().clamp(0.0, 128.0) as u16,
+ sigma: sigma as u16,
+ };
+ let layout = key.layout();
+ let coords = self
+ .render_state
+ .glyph_cache
+ .borrow_mut()
+ .cached_shadow(key)?
+ .texture_coords();
+ let side = layout.side as f32;
+ let u = |px: f32| coords.min_x() + coords.size.width * (px / side);
+ let v = |px: f32| coords.min_y() + coords.size.height * (px / side);
+ let left_offset = self.dimensions.pixel_width as f32 / 2.0;
+ let top_offset = self.dimensions.pixel_height as f32 / 2.0;
+ for slice in crate::ui::shadow::shadow_slices(
+ snapped.x,
+ snapped.y,
+ snapped.width,
+ snapped.height,
+ layout,
+ ) {
+ let mut quad = layers.allocate(layer_num)?;
+ quad.set_position(
+ slice.screen_x.0 - left_offset,
+ slice.screen_y.0 - top_offset,
+ slice.screen_x.1 - left_offset,
+ slice.screen_y.1 - top_offset,
+ );
+ quad.set_texture_discrete(
+ u(slice.sprite_x.0),
+ u(slice.sprite_x.1),
+ v(slice.sprite_y.0),
+ v(slice.sprite_y.1),
+ );
+ quad.set_fg_color(color);
+ quad.set_alt_color_and_mix_value(color, 0.0);
+ quad.set_hsv(None);
+ quad.set_has_color(false);
+ quad.set_grayscale();
+ }
+ Ok(())
+ }
+
pub(crate) fn draw_elevated_surface(
&self,
layers: &mut TripleLayerQuadAllocator<'_>,
@@ -188,21 +261,26 @@ impl<'a> DrawContext<'a> {
shadow: LinearRgba,
radius: f32,
) -> anyhow::Result<()> {
- for (spread, offset_y, alpha) in [
- (self.px(6.0), self.px(5.0), 0.14),
- (self.px(2.0), self.px(3.0), 0.24),
- ] {
- self.draw_rounded_rect(
- layers,
- layer_num,
- rect.origin.x - spread,
- rect.origin.y - spread + offset_y,
- rect.size.width + spread * 2.0,
- rect.size.height + spread * 2.0,
- color_with_alpha(shadow, shadow.3 * alpha),
- radius + spread,
- )?;
- }
+ // Two shadows, as a lit surface casts: a wide, faint ambient one
+ // and a tight, darker one close under the edge.
+ self.draw_shadow(
+ layers,
+ layer_num,
+ rect,
+ radius,
+ self.px(9.0),
+ self.px(6.0),
+ color_with_alpha(shadow, shadow.3 * 0.42),
+ )?;
+ self.draw_shadow(
+ layers,
+ layer_num,
+ rect,
+ radius,
+ self.px(2.0),
+ self.px(1.5),
+ color_with_alpha(shadow, shadow.3 * 0.28),
+ )?;
self.draw_rounded_frame(
layers,
layer_num,
diff --git a/wezterm-gui/src/ui/mod.rs b/wezterm-gui/src/ui/mod.rs
index 100781a9..8b4e2e1f 100644
--- a/wezterm-gui/src/ui/mod.rs
+++ b/wezterm-gui/src/ui/mod.rs
@@ -7,6 +7,7 @@ pub(crate) mod geometry;
pub(crate) mod icons;
pub(crate) mod keys;
pub(crate) mod primitives;
+pub(crate) mod shadow;
pub(crate) mod state;
pub(crate) mod tokens;
pub(crate) mod widgets;
diff --git a/wezterm-gui/src/ui/shadow.rs b/wezterm-gui/src/ui/shadow.rs
new file mode 100644
index 00000000..3103509c
--- /dev/null
+++ b/wezterm-gui/src/ui/shadow.rs
@@ -0,0 +1,263 @@
+//! Soft drop shadows for rounded surfaces.
+//!
+//! The quad renderer has no blur, so a shadow used to be two slightly larger
+//! rounded rectangles stacked under the surface: two hard-edged steps that
+//! read as a halo. A real shadow is a Gaussian-blurred silhouette, and that
+//! is a picture -- so it is rasterized once per (corner radius, blur) pair
+//! into the glyph atlas, like the rounded corners are, and drawn as nine
+//! slices: four corners, four stretched edges and a stretched middle. The
+//! sprite is the shadow of the smallest surface with those corners; because
+//! the blur is uniform, every larger surface's shadow is that picture with
+//! its edges pulled apart.
+
+use ::window::bitmaps::{BitmapImage, Image};
+
+/// Width of the stretchable band between the corner slices, in sprite
+/// pixels. Two, so that a sample taken from its middle never reaches a
+/// corner texel however the edges are stretched.
+const MIDDLE: usize = 2;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub(crate) struct ShadowKey {
+ /// Corner radius of the surface, in pixels.
+ pub radius: u16,
+ /// Standard deviation of the blur, in pixels.
+ pub sigma: u16,
+}
+
+/// How the sprite for a key is laid out. Everything is in sprite pixels.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(crate) struct ShadowLayout {
+ /// How far the shadow reaches beyond the surface's edge.
+ pub extent: usize,
+ /// Side of a corner slice: the reach plus the corner radius.
+ pub corner: usize,
+ /// Side of the (square) sprite.
+ pub side: usize,
+}
+
+impl ShadowKey {
+ pub(crate) fn layout(self) -> ShadowLayout {
+ let extent = (self.sigma as usize) * 3;
+ let radius = self.radius as usize;
+ let corner = extent + radius;
+ // The surface in the sprite must be wide enough that opposite edges
+ // do not blur into each other: at least the blur's full reach either
+ // way, or the corner slices would carry the far edge's falloff.
+ let inner = (2 * radius).max(2 * extent) + MIDDLE;
+ ShadowLayout {
+ extent,
+ corner,
+ side: inner + 2 * extent,
+ }
+ }
+}
+
+/// The blurred silhouette of the smallest rounded rectangle with the key's
+/// corners, in the alpha channel; the colour channels are left black, as
+/// the grayscale quad path only reads alpha.
+pub(crate) fn rasterize_shadow(key: ShadowKey) -> Image {
+ let layout = key.layout();
+ let side = layout.side;
+ let extent = layout.extent as f32;
+ let radius = key.radius as f32;
+ let inner = (side - 2 * layout.extent) as f32;
+
+ // Coverage of the rounded rectangle at each pixel centre, anti-aliased
+ // by the signed distance to its edge.
+ let half = inner / 2.0;
+ let centre = extent + half;
+ let mut coverage = vec![0f32; side * side];
+ for y in 0..side {
+ for x in 0..side {
+ let px = (x as f32 + 0.5 - centre).abs() - (half - radius);
+ let py = (y as f32 + 0.5 - centre).abs() - (half - radius);
+ let outside = (px.max(0.0).powi(2) + py.max(0.0).powi(2)).sqrt();
+ let inside = px.max(py).min(0.0);
+ let distance = outside + inside - radius;
+ coverage[y * side + x] = (0.5 - distance).clamp(0.0, 1.0);
+ }
+ }
+
+ // Separable Gaussian blur, kernel out to three sigma either side.
+ let sigma = key.sigma.max(1) as f32;
+ let reach = layout.extent;
+ let kernel: Vec<f32> = (0..=2 * reach)
+ .map(|i| {
+ let d = i as f32 - reach as f32;
+ (-(d * d) / (2.0 * sigma * sigma)).exp()
+ })
+ .collect();
+ let norm: f32 = kernel.iter().sum();
+ let blur_axis = |src: &[f32], horizontal: bool| -> Vec<f32> {
+ let mut out = vec![0f32; side * side];
+ for y in 0..side {
+ for x in 0..side {
+ let mut acc = 0.0;
+ for (i, weight) in kernel.iter().enumerate() {
+ let offset = i as isize - reach as isize;
+ let (sx, sy) = if horizontal {
+ (x as isize + offset, y as isize)
+ } else {
+ (x as isize, y as isize + offset)
+ };
+ if sx >= 0 && sy >= 0 && (sx as usize) < side && (sy as usize) < side {
+ acc += weight * src[sy as usize * side + sx as usize];
+ }
+ }
+ out[y * side + x] = acc / norm;
+ }
+ }
+ out
+ };
+ let blurred = if key.sigma == 0 {
+ coverage
+ } else {
+ blur_axis(&blur_axis(&coverage, true), false)
+ };
+
+ let mut image = Image::new(side, side);
+ let pixels = image.pixel_data_slice_mut();
+ for (i, alpha) in blurred.iter().enumerate() {
+ pixels[i * 4 + 3] = (alpha.clamp(0.0, 1.0) * 255.0).round() as u8;
+ }
+ image
+}
+
+/// One slice of a nine-slice shadow: where it lands on screen, and which
+/// part of the sprite it shows, both as (min, max) along each axis. Sprite
+/// coordinates are in sprite pixels.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub(crate) struct ShadowSlice {
+ pub screen_x: (f32, f32),
+ pub screen_y: (f32, f32),
+ pub sprite_x: (f32, f32),
+ pub sprite_y: (f32, f32),
+}
+
+/// The nine slices that draw the shadow of a `width` x `height` surface
+/// whose top-left corner sits at `(x, y)`, with the shadow's own corner
+/// radius already baked into `layout`. Corners keep their size; edges and
+/// middle stretch. A surface narrower or shorter than two corners gets its
+/// corner slices shrunk to fit, which is the best a fixed sprite can do.
+pub(crate) fn shadow_slices(
+ x: f32,
+ y: f32,
+ width: f32,
+ height: f32,
+ layout: ShadowLayout,
+) -> Vec<ShadowSlice> {
+ let extent = layout.extent as f32;
+ let corner = layout.corner as f32;
+ let side = layout.side as f32;
+ let middle_start = corner;
+ let middle_end = side - corner;
+
+ // Screen breakpoints along one axis for a run of `length` pixels.
+ let axis = |start: f32, length: f32| -> [f32; 4] {
+ let outer = length + extent * 2.0;
+ let corner = corner.min(outer / 2.0);
+ [
+ start - extent,
+ start - extent + corner,
+ start + length + extent - corner,
+ start + length + extent,
+ ]
+ };
+ let xs = axis(x, width);
+ let ys = axis(y, height);
+ let sprite = [0.0, middle_start, middle_end, side];
+
+ let mut slices = Vec::with_capacity(9);
+ for row in 0..3 {
+ for col in 0..3 {
+ let screen_x = (xs[col], xs[col + 1]);
+ let screen_y = (ys[row], ys[row + 1]);
+ if screen_x.1 <= screen_x.0 || screen_y.1 <= screen_y.0 {
+ continue;
+ }
+ slices.push(ShadowSlice {
+ screen_x,
+ screen_y,
+ sprite_x: (sprite[col], sprite[col + 1]),
+ sprite_y: (sprite[row], sprite[row + 1]),
+ });
+ }
+ }
+ slices
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn alpha_at(image: &Image, x: usize, y: usize) -> u8 {
+ let (width, _) = image.image_dimensions();
+ image.pixel_data_slice()[(y * width + x) * 4 + 3]
+ }
+
+ #[test]
+ fn the_shadow_is_solid_under_the_surface_and_gone_at_its_reach() {
+ let key = ShadowKey {
+ radius: 8,
+ sigma: 4,
+ };
+ let layout = key.layout();
+ let image = rasterize_shadow(key);
+ let centre = layout.side / 2;
+ assert!(alpha_at(&image, centre, centre) >= 250);
+ assert_eq!(alpha_at(&image, 0, 0), 0);
+ assert_eq!(alpha_at(&image, centre, 0), 0);
+ // Symmetric in both axes.
+ assert_eq!(
+ alpha_at(&image, layout.extent, centre),
+ alpha_at(&image, layout.side - 1 - layout.extent, centre)
+ );
+ assert_eq!(
+ alpha_at(&image, centre, layout.extent),
+ alpha_at(&image, layout.extent, centre)
+ );
+ // Soft: the edge of the surface is partway through the falloff.
+ let edge = alpha_at(&image, layout.extent, centre);
+ assert!(edge > 60 && edge < 200, "edge alpha {edge}");
+ }
+
+ #[test]
+ fn nine_slices_tile_the_shadow_without_gaps_or_overlap() {
+ let layout = ShadowKey {
+ radius: 8,
+ sigma: 4,
+ }
+ .layout();
+ let slices = shadow_slices(100.0, 200.0, 300.0, 120.0, layout);
+ assert_eq!(slices.len(), 9);
+ let extent = layout.extent as f32;
+ let left = slices.iter().map(|s| s.screen_x.0).fold(f32::MAX, f32::min);
+ let right = slices.iter().map(|s| s.screen_x.1).fold(f32::MIN, f32::max);
+ let top = slices.iter().map(|s| s.screen_y.0).fold(f32::MAX, f32::min);
+ let bottom = slices.iter().map(|s| s.screen_y.1).fold(f32::MIN, f32::max);
+ assert_eq!((left, right), (100.0 - extent, 400.0 + extent));
+ assert_eq!((top, bottom), (200.0 - extent, 320.0 + extent));
+ let area: f32 = slices
+ .iter()
+ .map(|s| (s.screen_x.1 - s.screen_x.0) * (s.screen_y.1 - s.screen_y.0))
+ .sum();
+ assert_eq!(area, (right - left) * (bottom - top));
+ // The corners are never stretched.
+ assert_eq!(slices[0].screen_x.1 - slices[0].screen_x.0, layout.corner as f32);
+ assert_eq!(slices[0].sprite_x, (0.0, layout.corner as f32));
+ }
+
+ #[test]
+ fn a_surface_smaller_than_two_corners_shrinks_its_corner_slices() {
+ let layout = ShadowKey {
+ radius: 20,
+ sigma: 2,
+ }
+ .layout();
+ let slices = shadow_slices(0.0, 0.0, 10.0, 10.0, layout);
+ // Six slices: the middle band has no width to draw.
+ assert!(slices.iter().all(|s| s.screen_x.1 > s.screen_x.0));
+ assert!(slices.len() < 9);
+ }
+}