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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
//! Lit tiles: a coloured shape with its fill lit from above, a rim that
//! bevels its edge and a soft shadow under it. One painter for every icon
//! that sits on one -- the circles heading pane tabs, the rounded squares
//! heading settings rows -- so they are lit alike; a circle is a tile whose
//! corner radius is half its side.
//!
//! Nothing here is a texture of its own: the fill and rim are gradient quads
//! over the atlas's shared corner sprites, and the shadow is the atlas's
//! blurred silhouette for the tile's radius, so a page of tiles costs a few
//! quads each and no memory per tile.
use crate::quad::TripleLayerQuadAllocator;
use crate::termwindow::ui::icons::BrandIcon;
use crate::ui::draw::DrawContext;
use window::color::{LinearRgba, SrgbaTuple};
/// How a tile is lit. Every amount is how far the tile's colour is blended
/// toward white (`lighten`) or black (`darken`), in sRGB, as a designer's
/// tool would.
pub(crate) struct TileStyle {
pub fill_top_lighten: f32,
pub fill_bottom_darken: f32,
pub rim_top_lighten: f32,
pub rim_bottom_darken: f32,
/// The rim's width against the tile's side.
pub rim_per_side: f32,
/// The shadow's colour, as the tile's darkened this far (1 is black).
pub shadow_darken: f32,
/// Its peak opacity on dark chrome and on light, where the same shadow
/// shows far more.
pub shadow_alpha_dark: f32,
pub shadow_alpha_light: f32,
/// Its softness and how far it drops, in design pixels.
pub shadow_sigma: f32,
pub shadow_drop: f32,
}
/// Linear light to sRGB, one channel. Not `LinearRgba::to_srgb`: that one
/// switches to the power curve at 0.04045, the cut-off on the sRGB side
/// rather than the linear one (0.0031308), so a channel whose sRGB byte is
/// below about 57 comes back far too light.
fn to_srgb(linear: f32) -> f32 {
if linear <= 0.0031308 {
linear * 12.92
} else {
linear.powf(1.0 / 2.4) * 1.055 - 0.055
}
}
/// `color` taken `amount` of the way to `target`, blended in sRGB.
fn blend(color: LinearRgba, target: f32, amount: f32) -> LinearRgba {
let toward = |linear: f32| {
let channel = to_srgb(linear);
channel + (target - channel) * amount
};
SrgbaTuple(toward(color.0), toward(color.1), toward(color.2), color.3).to_linear()
}
pub(crate) fn lighten(color: LinearRgba, amount: f32) -> LinearRgba {
blend(color, 1.0, amount)
}
pub(crate) fn darken(color: LinearRgba, amount: f32) -> LinearRgba {
blend(color, 0.0, amount)
}
/// A tile `side` pixels square at `(x, y)` with corners of `radius`,
/// coloured `color` and lit by `style`. `dark` is the chrome's appearance.
#[allow(clippy::too_many_arguments)]
pub(crate) fn draw_tile(
ctx: &DrawContext,
layers: &mut TripleLayerQuadAllocator,
layer: usize,
x: f32,
y: f32,
side: f32,
radius: f32,
color: LinearRgba,
dark: bool,
style: &TileStyle,
) -> anyhow::Result<()> {
let bounds = euclid::rect(x, y, side, side);
let shadow_alpha = if dark {
style.shadow_alpha_dark
} else {
style.shadow_alpha_light
};
ctx.draw_shadow(
layers,
layer,
bounds,
radius,
ctx.px(style.shadow_sigma),
ctx.px(style.shadow_drop),
darken(color, style.shadow_darken).mul_alpha(shadow_alpha),
)?;
ctx.draw_rounded_rect_vertical_gradient(
layers,
layer,
bounds,
radius,
lighten(color, style.rim_top_lighten),
darken(color, style.rim_bottom_darken),
)?;
let rim = (side * style.rim_per_side).round().max(1.0);
let inner = (side - rim * 2.0).max(0.0);
ctx.draw_rounded_rect_vertical_gradient(
layers,
layer,
euclid::rect(x + rim, y + rim, inner, inner),
(radius - rim).max(0.0),
lighten(color, style.fill_top_lighten),
darken(color, style.fill_bottom_darken),
)
}
/// How opaque white (`toward` 1) or black (`toward` 0) laid over `ground`
/// has to be to turn it into `target`. Layers blend in linear light, where
/// a little white goes a long way on a dark colour, so this is worked out
/// from the colours rather than taken from an amount.
fn cover(ground: LinearRgba, target: LinearRgba, toward: f32) -> f32 {
let channel = |ground: f32, target: f32| {
let span = toward - ground;
if span.abs() < 1e-4 {
0.0
} else {
((target - ground) / span).clamp(0.0, 1.0)
}
};
(channel(ground.0, target.0) + channel(ground.1, target.1) + channel(ground.2, target.2))
/ 3.0
}
/// An app's icon lit as [`draw_tile`] lights a coloured square: a shadow
/// and rim in the icon's own ground, the icon inside the rim, and the
/// fill's light and shade laid over it, as strong as they would be on that
/// ground. Icons without a square of their own are drawn as they are.
#[allow(clippy::too_many_arguments)]
pub(crate) fn draw_brand_tile(
ctx: &DrawContext,
layers: &mut TripleLayerQuadAllocator,
icon: BrandIcon,
x: f32,
y: f32,
side: f32,
dark: bool,
style: &TileStyle,
) -> anyhow::Result<()> {
let Some((r, g, b)) = icon.app_icon_ground() else {
return ctx.draw_brand_icon(layers, icon, x, y, side);
};
// On the top layer, where the icon is, so the light lands over it.
let (x, y, side) = (x.round(), y.round(), side.round());
let color = LinearRgba::with_srgba(r, g, b, 0xFF);
let radius = side * 0.27;
let shadow_alpha = if dark {
style.shadow_alpha_dark
} else {
style.shadow_alpha_light
};
let bounds = euclid::rect(x, y, side, side);
ctx.draw_shadow(
layers,
2,
bounds,
radius,
ctx.px(style.shadow_sigma),
ctx.px(style.shadow_drop),
darken(color, style.shadow_darken).mul_alpha(shadow_alpha),
)?;
ctx.draw_rounded_rect_vertical_gradient(
layers,
2,
bounds,
radius,
lighten(color, style.rim_top_lighten),
darken(color, style.rim_bottom_darken),
)?;
let rim = (side * style.rim_per_side).round().max(1.0);
let inner = (side - rim * 2.0).max(0.0);
ctx.draw_brand_icon(layers, icon, x + rim, y + rim, inner)?;
let fill = euclid::rect(x + rim, y + rim, inner, inner);
let fill_radius = (radius - rim).max(0.0);
let white = LinearRgba::with_components(1.0, 1.0, 1.0, 1.0);
let light = cover(color, lighten(color, style.fill_top_lighten), 1.0);
ctx.draw_rounded_rect_vertical_gradient(
layers,
2,
fill,
fill_radius,
white.mul_alpha(light),
white.mul_alpha(0.0),
)?;
let black = LinearRgba::with_components(0.0, 0.0, 0.0, 1.0);
let shade = cover(color, darken(color, style.fill_bottom_darken), 0.0);
ctx.draw_rounded_rect_vertical_gradient(
layers,
2,
fill,
fill_radius,
black.mul_alpha(0.0),
black.mul_alpha(shade),
)
}
#[cfg(test)]
mod tests {
use super::*;
fn srgb_bytes(color: LinearRgba) -> (u8, u8, u8) {
let byte = |linear: f32| (to_srgb(linear) * 255.0).round() as u8;
(byte(color.0), byte(color.1), byte(color.2))
}
#[test]
fn an_icons_light_is_as_strong_as_a_tiles() {
let over = |ground: LinearRgba, toward: f32, alpha: f32| {
let mix = |g: f32| g + (toward - g) * alpha;
LinearRgba::with_components(mix(ground.0), mix(ground.1), mix(ground.2), 1.0)
};
for byte in [0x22, 0x80, 0xD9] {
let ground = LinearRgba::with_srgba(byte, byte, byte, 0xFF);
let lit = lighten(ground, 0.16);
let shaded = darken(ground, 0.10);
assert_eq!(srgb_bytes(over(ground, 1.0, cover(ground, lit, 1.0))), srgb_bytes(lit));
assert_eq!(
srgb_bytes(over(ground, 0.0, cover(ground, shaded, 0.0))),
srgb_bytes(shaded)
);
}
// On a dark ground the white is far fainter than the amount.
let dark = LinearRgba::with_srgba(0x22, 0x22, 0x22, 0xFF);
assert!(cover(dark, lighten(dark, 0.16), 1.0) < 0.06);
}
#[test]
fn blending_happens_in_srgb() {
let grey = LinearRgba::with_srgba(0x80, 0x80, 0x80, 0xFF);
// Halfway to white in sRGB is 0xC0, not what a linear blend gives.
assert_eq!(srgb_bytes(lighten(grey, 0.5)), (0xC0, 0xC0, 0xC0));
assert_eq!(srgb_bytes(darken(grey, 0.5)), (0x40, 0x40, 0x40));
assert_eq!(srgb_bytes(lighten(grey, 0.0)), (0x80, 0x80, 0x80));
}
#[test]
fn dark_channels_keep_their_value() {
// 0x37 is in the range the shared conversion gets wrong: it came
// back as 0x7E and turned a blue icon lavender.
let blue = LinearRgba::with_srgba(0x37, 0x76, 0xAB, 0xFF);
assert_eq!(srgb_bytes(lighten(blue, 0.0)), (0x37, 0x76, 0xAB));
let dim = LinearRgba::with_srgba(0x31, 0x31, 0x31, 0xFF);
assert_eq!(srgb_bytes(lighten(dim, 0.5)), (0x98, 0x98, 0x98));
let dimmer = LinearRgba::with_srgba(0x32, 0x32, 0x32, 0xFF);
assert_eq!(srgb_bytes(darken(dimmer, 0.5)), (0x19, 0x19, 0x19));
}
}