rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! The window's title bar and the toast message.

use super::*;

pub(super) struct Chrome<'a> {
    pub(super) title: &'a str,
    pub(super) hover: Hit,
    pub(super) pressed: Hit,
    pub(super) maximized: bool,
    pub(super) active: bool,
    pub(super) width: f32,
}

pub(super) fn draw_titlebar(ctx: &mut Ctx, c: &Chrome, out: &mut Vec<Quad>) {
    let s = ctx.scale;
    let pal = ctx.palette;
    let th = titlebar_height(s);
    let buttons = title_buttons(s, c.width);
    let dim = if c.active { 0.55 } else { 0.35 };
    let text_color = mix(pal.foreground, pal.background, 1.0 - dim);

    // Title, centered, truncated to stay clear of the buttons.
    let cw = ctx.glyphs.cell_w;
    let reserved = buttons[0].1.w * 3.0 + 24.0 * s;
    let max_chars = ((c.width - 2.0 * reserved) / cw).max(0.0) as usize;
    let mut chars: Vec<char> = c.title.chars().collect();
    if chars.len() > max_chars {
        chars.truncate(max_chars.saturating_sub(1));
        chars.push('…');
    }
    let tx = ((c.width - chars.len() as f32 * cw) * 0.5).round();
    let ty = ((th - ctx.glyphs.cell_h) * 0.5).round();
    for (i, ch) in chars.into_iter().enumerate() {
        if ch == ' ' {
            continue;
        }
        if let Some(g) = ctx.glyphs.get(GlyphKey { c: ch, zw: None, style: 0 }) {
            let (mode, color) = if g.color { (MODE_COLOR, [1.0, 1.0, 1.0, dim]) } else { (MODE_MASK, text_color) };
            out.push(Quad {
                rect: [tx + i as f32 * cw + g.left, ty + g.top, g.uv[2], g.uv[3]],
                uv: g.uv,
                color,
                clip: NO_CLIP,
                params: [mode, 0.0, 0.0, 0.0],
                color2: [0.0; 4],
            });
        }
    }

    // Window buttons.
    let ui = (s * 16.0).round().clamp(1.0, 255.0) as u8;
    for (hit, r) in buttons {
        let hovered = c.hover == hit;
        let pressed = c.pressed == hit && hovered;
        if hovered {
            let bg = match (hit, pressed) {
                (Hit::Close, false) => [0.86, 0.24, 0.27, 1.0],
                (Hit::Close, true) => [0.70, 0.18, 0.21, 1.0],
                (_, false) => with_alpha(pal.foreground, 0.07),
                (_, true) => with_alpha(pal.foreground, 0.12),
            };
            out.push(quad(r, bg, NO_CLIP, MODE_SOLID));
        }
        let icon = match hit {
            Hit::Min => boxdraw::ICON_MIN,
            Hit::Max if c.maximized => boxdraw::ICON_RESTORE,
            Hit::Max => boxdraw::ICON_MAX,
            _ => boxdraw::ICON_CLOSE,
        };
        let color = if hovered && hit == Hit::Close {
            [1.0, 1.0, 1.0, 1.0]
        } else if hovered {
            pal.foreground
        } else {
            text_color
        };
        if let Some(g) = ctx.glyphs.get(GlyphKey { c: icon, zw: None, style: ui }) {
            let x = (r.x + (r.w - g.uv[2]) * 0.5).round();
            let y = (r.y + (r.h - g.uv[3]) * 0.5).round();
            out.push(Quad {
                rect: [x, y, g.uv[2], g.uv[3]],
                uv: g.uv,
                color,
                clip: NO_CLIP,
                params: [MODE_MASK, 0.0, 0.0, 0.0],
                color2: [0.0; 4],
            });
        }
    }
}

/// A short message, centred near the bottom of the window. It fades in and out.
pub(super) fn draw_toast(
    ctx: &mut Ctx,
    message: &str,
    age: f32,
    size: winit::dpi::PhysicalSize<u32>,
    out: &mut Vec<Quad>,
) {
    let s = ctx.scale;
    let pal = ctx.palette;
    let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
    let total = crate::app::TOAST_SECS.as_secs_f32();
    let alpha = (age / 0.12).min(1.0).min(((total - age) / 0.35).clamp(0.0, 1.0));
    if alpha <= 0.0 {
        return;
    }
    let chars = message.chars().count() as f32;
    let w = ((chars + 4.0) * cw).min(size.width as f32 - 24.0 * s);
    let h = (ch * 1.9).round();
    let bar = Rect::new(((size.width as f32 - w) * 0.5).round(), (size.height as f32 - h - 28.0 * s).round(), w, h);
    let bg = mix(pal.surface, pal.foreground, 0.07);
    out.push(shadow(bar, 10.0 * s, 14.0 * s, 5.0 * s, 0.5 * alpha, ctx.alpha < 1.0));
    let border = with_alpha(mix(pal.border, pal.accent, 0.5), alpha);
    out.push(rounded(bar, with_alpha(bg, (ctx.alpha + 0.25).min(1.0) * alpha), 10.0 * s, s.max(1.0), border, NO_CLIP));
    let ty = (bar.y + (h - ch) * 0.5).round();
    text(ctx, out, bar.x + 2.0 * cw, ty, message, with_alpha(pal.foreground, alpha), 0, w - 3.0 * cw, NO_CLIP);
}