rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Builds the quad list for a frame: pane cards, cell backgrounds, glyphs, cursor.

pub mod boxdraw;
pub mod glyphs;
pub mod gpu;

use std::time::{Duration, Instant};

use alacritty_terminal::grid::Dimensions;
use alacritty_terminal::term::TermMode;
use alacritty_terminal::term::cell::Flags;
use alacritty_terminal::term::color::Colors;
use alacritty_terminal::vte::ansi::{Color, CursorShape, NamedColor};

use crate::agent::claude::{self, Usage};
use crate::app::{Hit, State, title_buttons, titlebar_height};
use crate::config::{Config, Palette, Rgba};
use crate::render::glyphs::{GlyphKey, Glyphs, STYLE_BOLD, STYLE_ITALIC};
use crate::render::gpu::{
    MODE_COLOR, MODE_GLASS, MODE_MASK, MODE_ROUNDED, MODE_SHADOW, MODE_SOLID, MODE_UNDERCURL, Quad,
};
use crate::terminal::layout::Rect;
use crate::terminal::pane::Pane;
use crate::terminal::search::Search;
use crate::ui::overlay::Overlay;
use crate::ui::picker::Picker;

mod chrome;
mod pane;
mod picker;
mod primitives;

use chrome::*;
pub use pane::claude_bar_height;
use pane::*;
use picker::*;
pub(crate) use primitives::*;

pub(crate) const NO_CLIP: [f32; 4] = [-1e9, -1e9, 1e9, 1e9];
const CLAUDE: Rgba = [0.851, 0.467, 0.341, 1.0];
const BLINK_PERIOD: f32 = 1.1;
const BLINK_IDLE_STOP: f32 = 12.0;

/// Smooth blink: returns alpha and how long until the next frame is needed
/// (`None` means "mid-fade, redraw every frame").
fn blink(t: f32) -> (f32, Option<f32>) {
    use std::f32::consts::TAU;
    let theta = (t / BLINK_PERIOD).fract() * TAU;
    let alpha = (1.5 * theta.cos() + 0.5).clamp(0.0, 1.0);
    let (a1, a2) = ((1.0f32 / 3.0).acos(), (-1.0f32 / 3.0).acos());
    let windows = [(a1, a2), (TAU - a2, TAU - a1)];
    if windows.iter().any(|(s, e)| theta > *s && theta < *e) {
        return (alpha, None);
    }
    let next = [a1, TAU - a2, a1 + TAU].into_iter().find(|s| *s > theta).unwrap_or(a1 + TAU);
    (alpha, Some((next - theta) / TAU * BLINK_PERIOD))
}

pub(crate) struct Ctx<'a> {
    pub(crate) glyphs: &'a mut Glyphs,
    pub(crate) palette: &'a Palette,
    pub(crate) config: &'a Config,
    pub(crate) scale: f32,
    pub(crate) now: Instant,
    pub(crate) multi: f32,
    pub(crate) focused: bool,
    pub(crate) cursor_alpha: f32,
    pub(crate) usage: Usage,
    /// Glass look (rim-lit cards).
    pub(crate) glass: bool,
    /// Alpha for surfaces: below 1 when the window is see-through.
    pub(crate) alpha: f32,
    pub(crate) bg: Vec<Quad>,
    pub(crate) fg: Vec<Quad>,
}

/// Returns how long the event loop may sleep before the cursor blink needs another frame.
pub fn build_scene(st: &mut State, config: &Config, palette: &Palette) -> Option<Duration> {
    let now = Instant::now();
    let since_input = (now - st.blink_epoch).as_secs_f32();
    let (cursor_alpha, blink_wait) = if config.cursor_blink && st.window_focused && since_input < BLINK_IDLE_STOP {
        let (a, wait) = blink(since_input);
        let stop_in = BLINK_IDLE_STOP - since_input;
        (a, Some(wait.map_or(0.0, |w| w.min(stop_in))))
    } else {
        (1.0, None)
    };
    let blink_wait = blink_wait.map(|w| Duration::from_secs_f32(w.max(0.0)));

    let now = st.now;
    let mut out = std::mem::take(&mut st.quads);
    out.clear();
    let mut ctx = Ctx {
        glyphs: &mut st.glyphs,
        palette,
        config,
        scale: st.scale,
        now,
        multi: st.multi,
        focused: false,
        cursor_alpha,
        usage: st.usage,
        glass: config.glass,
        alpha: surface_alpha(config, st.gpu.transparent),
        bg: Vec::new(),
        fg: Vec::new(),
    };

    let size = st.window.inner_size();
    let chrome = Chrome {
        title: &st.window_title,
        hover: st.hover,
        pressed: st.pressed,
        maximized: st.window.is_maximized(),
        active: st.window_focused,
        width: size.width as f32,
    };
    draw_titlebar(&mut ctx, &chrome, &mut out);

    for p in st.closing.iter_mut() {
        ctx.focused = false;
        draw_pane(&mut ctx, p, &mut out);
    }
    let mut again = false;
    for p in st.panes.iter_mut() {
        ctx.focused = p.id == st.focused && st.window_focused;
        if let Some(view) = p.review.as_mut() {
            view.focused = p.id == st.focused;
            if !view.focused {
                view.blur();
            }
        }
        again |= draw_pane(&mut ctx, p, &mut out);
    }
    if again {
        st.window.request_redraw();
    }
    if let Some(picker) = st.picker.as_mut()
        && let Some(rect) = st.panes.iter().find(|p| p.id == picker.pane).map(|p| p.rect)
    {
        draw_picker(&mut ctx, picker, &rect, &mut out);
    }
    if st.drop_hover
        && let Some(rect) = st.panes.iter().find(|p| p.id == st.focused).map(|p| p.rect)
    {
        draw_drop_hint(&mut ctx, &rect, &mut out);
    }
    if let Some(overlay) = st.overlay.as_mut() {
        let size = st.window.inner_size();
        match overlay {
            Overlay::Menu(menu) => crate::ui::overlay_draw::draw_menu(&mut ctx, menu, size, &mut out),
            Overlay::Palette(palette) => crate::ui::overlay_draw::draw_palette(&mut ctx, palette, size, &mut out),
            Overlay::Settings(settings) => crate::ui::overlay_draw::draw_settings(&mut ctx, settings, size, &mut out),
        }
    }
    if let Some((message, since)) = &st.toast {
        draw_toast(&mut ctx, message, since.elapsed().as_secs_f32(), st.window.inner_size(), &mut out);
    }
    st.quads = out;
    blink_wait
}

fn surface_alpha(config: &Config, transparent: bool) -> f32 {
    if config.glass && transparent { config.glass_opacity.clamp(0.2, 1.0) } else { 1.0 }
}

/// Window background. When see-through, it's a lighter tint over the blurred desktop.
pub fn clear_color(config: &Config, palette: &Palette, transparent: bool) -> Rgba {
    let a = surface_alpha(config, transparent);
    with_alpha(palette.background, if a < 1.0 { (a - 0.25).clamp(0.3, 1.0) } else { 1.0 })
}

pub(crate) const WHITE: Rgba = [1.0, 1.0, 1.0, 1.0];