rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Small drawing building blocks: quads, colors, text and rounded boxes.

use super::*;

pub(crate) fn quad(rect: Rect, color: Rgba, clip: [f32; 4], mode: f32) -> Quad {
    Quad {
        rect: [rect.x, rect.y, rect.w, rect.h],
        uv: [0.0; 4],
        color,
        clip,
        params: [mode, 0.0, 0.0, 0.0],
        color2: [0.0; 4],
    }
}

pub(crate) fn with_alpha(c: Rgba, a: f32) -> Rgba {
    [c[0], c[1], c[2], c[3] * a]
}

pub(crate) fn mix(a: Rgba, b: Rgba, t: f32) -> Rgba {
    [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t]
}

/// Soft drop shadow under a card. Cut out under see-through cards.
pub(crate) fn shadow(card: Rect, radius: f32, blur: f32, drop: f32, alpha: f32, hole: bool) -> Quad {
    let e = blur * 1.5;
    Quad {
        rect: [card.x - e, card.y - e + drop, card.w + 2.0 * e, card.h + 2.0 * e],
        uv: [0.0; 4],
        color: [0.0, 0.0, 0.0, alpha],
        clip: NO_CLIP,
        params: [MODE_SHADOW, radius, blur, e],
        color2: [if hole { 1.0 } else { 0.0 }, drop, 0.0, 0.0],
    }
}

/// Rim-lit glass card. `rim` is the edge light color; its alpha is the strength.
/// `outline` is the strength of the dark hairline around it.
pub(crate) fn glass(card: Rect, tint: Rgba, rim: Rgba, outline: f32, radius: f32, s: f32) -> Quad {
    Quad {
        rect: [card.x, card.y, card.w, card.h],
        uv: [0.0; 4],
        color: tint,
        clip: NO_CLIP,
        params: [MODE_GLASS, radius, (1.4 * s).max(1.0), outline],
        color2: rim,
    }
}

pub(super) fn resolve(color: Color, colors: &Colors, pal: &Palette) -> Rgba {
    let rgb = |c: alacritty_terminal::vte::ansi::Rgb| [c.r as f32 / 255.0, c.g as f32 / 255.0, c.b as f32 / 255.0, 1.0];
    match color {
        Color::Spec(c) => rgb(c),
        Color::Indexed(i) => colors[i as usize].map(rgb).unwrap_or_else(|| pal.indexed(i)),
        Color::Named(n) => {
            if let Some(c) = colors[n as usize] {
                return rgb(c);
            }
            let idx = n as usize;
            match n {
                NamedColor::Foreground | NamedColor::BrightForeground => pal.foreground,
                NamedColor::Background => pal.surface,
                NamedColor::Cursor => pal.cursor,
                NamedColor::DimForeground => mix(pal.foreground, pal.surface, 0.35),
                _ if idx < 16 => pal.ansi[idx],
                // Dim variants of the 8 base colors.
                _ => {
                    let base = (idx - NamedColor::DimBlack as usize).min(7);
                    mix(pal.ansi[base], pal.surface, 0.35)
                }
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub(super) fn hollow(q: &mut Vec<Quad>, x: f32, y: f32, w: f32, h: f32, t: f32, c: Rgba, clip: [f32; 4]) {
    q.push(quad(Rect::new(x, y, w, t), c, clip, MODE_SOLID));
    q.push(quad(Rect::new(x, y + h - t, w, t), c, clip, MODE_SOLID));
    q.push(quad(Rect::new(x, y + t, t, h - 2.0 * t), c, clip, MODE_SOLID));
    q.push(quad(Rect::new(x + w - t, y + t, t, h - 2.0 * t), c, clip, MODE_SOLID));
}

/// Re-tint neutral grays (Claude's borders, hints and panels) into the theme,
/// so its UI blends into the pane instead of sitting on top of it.
pub(super) fn soften_gray(c: Rgba, pal: &Palette, is_bg: bool) -> Rgba {
    let max = c[0].max(c[1]).max(c[2]);
    let min = c[0].min(c[1]).min(c[2]);
    if max - min > 0.06 {
        return c;
    }
    let l = (c[0] + c[1] + c[2]) / 3.0;
    if is_bg {
        mix(pal.surface, pal.foreground, (l * 0.3).min(0.12))
    } else if l < 0.75 {
        mix(pal.surface, pal.foreground, l.clamp(0.3, 0.9))
    } else {
        c
    }
}

/// Draw a run of text on the cell grid; returns the width used.
#[allow(clippy::too_many_arguments)]
pub(crate) fn text(
    ctx: &mut Ctx,
    out: &mut Vec<Quad>,
    x: f32,
    y: f32,
    s: &str,
    color: Rgba,
    style: u8,
    max_w: f32,
    clip: [f32; 4],
) -> f32 {
    let cw = ctx.glyphs.cell_w;
    let max_chars = (max_w / cw).floor().max(0.0) as usize;
    let mut chars: Vec<char> = s.chars().collect();
    if chars.len() > max_chars {
        chars.truncate(max_chars.saturating_sub(1));
        chars.push('…');
    }
    for (i, c) in chars.iter().enumerate() {
        if *c == ' ' {
            continue;
        }
        if let Some(g) = ctx.glyphs.get(GlyphKey { c: *c, zw: None, style }) {
            let (mode, col) = if g.color { (MODE_COLOR, [1.0, 1.0, 1.0, color[3]]) } else { (MODE_MASK, color) };
            out.push(Quad {
                rect: [(x + i as f32 * cw + g.left).round(), (y + g.top).round(), g.uv[2], g.uv[3]],
                uv: g.uv,
                color: col,
                clip,
                params: [mode, 0.0, 0.0, 0.0],
                color2: [0.0; 4],
            });
        }
    }
    chars.len() as f32 * cw
}

pub(crate) fn rounded(r: Rect, color: Rgba, radius: f32, border: f32, border_color: Rgba, clip: [f32; 4]) -> Quad {
    Quad {
        rect: [r.x, r.y, r.w, r.h],
        uv: [0.0; 4],
        color,
        clip,
        params: [MODE_ROUNDED, radius, border, 0.0],
        color2: border_color,
    }
}