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::app::{Hit, State, title_buttons, titlebar_height};
use crate::boxdraw;
use crate::claude::{self, Usage};
use crate::config::{Config, Palette, Rgba};
use crate::glyphs::{GlyphKey, Glyphs, STYLE_BOLD, STYLE_ITALIC};
use crate::gpu::{MODE_COLOR, MODE_GLASS, MODE_MASK, MODE_ROUNDED, MODE_SHADOW, MODE_SOLID, MODE_UNDERCURL, Quad};
use crate::layout::Rect;
use crate::overlay::Overlay;
use crate::pane::Pane;
use crate::picker::Picker;
use crate::search::Search;
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;
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]
}
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,
pub(crate) glass: bool,
pub(crate) alpha: f32,
pub(crate) bg: Vec<Quad>,
pub(crate) fg: Vec<Quad>,
}
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);
}
for p in st.panes.iter_mut() {
ctx.focused = p.id == st.focused && st.window_focused;
draw_pane(&mut ctx, p, &mut out);
}
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::overlay_draw::draw_menu(&mut ctx, menu, size, &mut out),
Overlay::Palette(palette) => crate::overlay_draw::draw_palette(&mut ctx, palette, size, &mut out),
Overlay::Settings(settings) => crate::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 }
}
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) 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],
}
}
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(crate) const WHITE: Rgba = [1.0, 1.0, 1.0, 1.0];
struct Chrome<'a> {
title: &'a str,
hover: Hit,
pressed: Hit,
maximized: bool,
active: bool,
width: f32,
}
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);
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],
});
}
}
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],
});
}
}
}
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],
_ => {
let base = (idx - NamedColor::DimBlack as usize).min(7);
mix(pal.ansi[base], pal.surface, 0.35)
}
}
}
}
}
fn draw_pane(ctx: &mut Ctx, pane: &mut Pane, out: &mut Vec<Quad>) {
let s = ctx.scale;
let pal = ctx.palette;
let o = pane.opacity;
let card = pane.rect;
if card.w < 1.0 || card.h < 1.0 || o < 0.004 {
return;
}
let radius = ctx.config.radius * s;
let pad = (ctx.config.padding * s).round();
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let focus = pane.focus;
let see_through = ctx.alpha < 1.0;
out.push(shadow(card, radius, 14.0 * s, 3.0 * s, if see_through { 0.24 } else { 0.45 } * o, see_through));
let (ring_color, ring) =
if pane.claude { (CLAUDE, focus * ctx.multi.max(0.55)) } else { (pal.accent, focus * ctx.multi) };
let surface = mix(pal.surface, pal.foreground, pane.bell * 0.03);
if ctx.glass {
let mut rim = mix(WHITE, ring_color, ring * 0.85);
rim = mix(rim, pal.ansi[3], pane.bell * 0.8);
rim[3] = (0.6 + 0.4 * ring + 0.4 * pane.bell).min(1.0) * o;
out.push(glass(card, with_alpha(surface, ctx.alpha * o), rim, 0.55 * o, radius, s));
} else {
let mut border = mix(pal.border, with_alpha(ring_color, 0.85), ring);
border = mix(border, pal.ansi[3], pane.bell * 0.8);
out.push(Quad {
rect: [card.x, card.y, card.w, card.h],
uv: [0.0; 4],
color: with_alpha(surface, o),
clip: NO_CLIP,
params: [MODE_ROUNDED, radius, (1.0 * s).max(1.0), 0.0],
color2: with_alpha(border, o),
});
}
let ox = (card.x + pad).round();
let oy = (card.y + pad).round();
let inner = card.inset((pad * 0.35).max(2.0));
let bar_h = if pane.claude { claude_bar_height(ch, s) } else { 0.0 };
let clip = [inner.x, inner.y, inner.right(), (inner.bottom() - bar_h).max(inner.y)];
let term = pane.term.lock();
let history = term.grid().history_size();
let content = term.renderable_content();
let colors = content.colors;
let mode = content.mode;
let offset = content.display_offset;
let selection = content.selection;
let cursor = content.cursor;
let rows = pane.grid.rows;
let cursor_row = cursor.point.line.0 + offset as i32;
let show_cursor = mode.contains(TermMode::SHOW_CURSOR)
&& cursor.shape != CursorShape::Hidden
&& cursor_row >= 0
&& (cursor_row as usize) < rows;
let cursor_px = (cursor.point.column.0 as f32 * cw, cursor_row as f32 * ch);
let block_cursor = show_cursor && ctx.focused && cursor.shape == CursorShape::Block;
let invert_cell = block_cursor && ctx.cursor_alpha > 0.5;
ctx.bg.clear();
ctx.fg.clear();
let fg_alpha = o;
let sel_color = with_alpha(pal.selection, o);
let line_w = (ctx.glyphs.font_px / 14.0).round().max(1.0);
let baseline = ctx.glyphs.baseline;
for indexed in content.display_iter {
let cell = indexed.cell;
let row = indexed.point.line.0 + offset as i32;
if row < 0 || row as usize >= rows {
continue;
}
let col = indexed.point.column.0;
let flags = cell.flags;
if flags.intersects(Flags::WIDE_CHAR_SPACER | Flags::LEADING_WIDE_CHAR_SPACER) {
continue;
}
let width = if flags.contains(Flags::WIDE_CHAR) { 2.0 } else { 1.0 };
let x = ox + col as f32 * cw;
let y = oy + row as f32 * ch;
let mut fg = resolve(cell.fg, colors, pal);
let bg_default = matches!(cell.bg, Color::Named(NamedColor::Background));
let mut bg = resolve(cell.bg, colors, pal);
let mut has_bg = !bg_default;
if flags.contains(Flags::INVERSE) {
std::mem::swap(&mut fg, &mut bg);
has_bg = true;
}
if pane.claude {
fg = soften_gray(fg, pal, false);
if has_bg && !bg_default {
bg = with_alpha(soften_gray(bg, pal, true), ctx.alpha);
}
}
if flags.contains(Flags::DIM) {
fg = mix(fg, bg, 0.4);
}
let selected = selection.is_some_and(|s| s.contains(indexed.point));
if selected {
bg = pal.selection;
has_bg = true;
}
if has_bg {
let c = if selected { sel_color } else { with_alpha(bg, o) };
ctx.bg.push(quad(Rect::new(x, y, cw * width, ch), c, clip, MODE_SOLID));
}
let under_cursor = invert_cell && cursor_row == row && cursor.point.column.0 == col;
if under_cursor {
fg = pal.surface;
}
let c = cell.c;
if c != ' ' && c != '\t' && !flags.contains(Flags::HIDDEN) {
let mut style = 0;
if flags.contains(Flags::BOLD) {
style |= STYLE_BOLD;
}
if flags.contains(Flags::ITALIC) {
style |= STYLE_ITALIC;
}
let zw = cell.zerowidth().and_then(|z| z.first().copied());
if let Some(g) = ctx.glyphs.get(GlyphKey { c, zw, style }) {
let (mode, color) = if g.color {
(MODE_COLOR, [1.0, 1.0, 1.0, fg_alpha])
} else {
(MODE_MASK, with_alpha(fg, fg_alpha))
};
ctx.fg.push(Quad {
rect: [x + g.left, y + g.top, g.uv[2], g.uv[3]],
uv: g.uv,
color,
clip,
params: [mode, 0.0, 0.0, 0.0],
color2: [0.0; 4],
});
}
}
if flags.intersects(Flags::ALL_UNDERLINES | Flags::STRIKEOUT) {
let uc = cell.underline_color().map(|c| resolve(c, colors, pal)).unwrap_or(fg);
let uc = with_alpha(uc, fg_alpha);
let uy = (y + baseline + line_w * 1.5).min(y + ch - line_w);
let w = cw * width;
if flags.contains(Flags::UNDERCURL) {
let h = (3.0 * s).round().max(3.0);
ctx.fg.push(Quad {
rect: [x, uy - h * 0.5, w, h],
uv: [0.0; 4],
color: uc,
clip,
params: [MODE_UNDERCURL, line_w, 0.0, 0.0],
color2: [0.0; 4],
});
} else if flags.contains(Flags::DOUBLE_UNDERLINE) {
ctx.fg.push(quad(Rect::new(x, uy - line_w, w, line_w), uc, clip, MODE_SOLID));
ctx.fg.push(quad(Rect::new(x, uy + line_w, w, line_w), uc, clip, MODE_SOLID));
} else if flags.intersects(Flags::DOTTED_UNDERLINE | Flags::DASHED_UNDERLINE) {
let seg = if flags.contains(Flags::DOTTED_UNDERLINE) { line_w * 2.0 } else { cw * 0.5 };
let mut sx = x;
while sx < x + w {
ctx.fg.push(quad(Rect::new(sx, uy, (seg * 0.6).min(x + w - sx), line_w), uc, clip, MODE_SOLID));
sx += seg;
}
} else if flags.contains(Flags::UNDERLINE) {
ctx.fg.push(quad(Rect::new(x, uy, w, line_w), uc, clip, MODE_SOLID));
}
if flags.contains(Flags::STRIKEOUT) {
let sy = (y + baseline - ctx.glyphs.font_px * 0.3).round();
ctx.fg.push(quad(Rect::new(x, sy, w, line_w), with_alpha(fg, fg_alpha), clip, MODE_SOLID));
}
}
}
if let Some(search) = &pane.search {
let others = with_alpha(pal.accent, 0.28 * o);
let current = with_alpha(pal.ansi[3], 0.55 * o);
for (i, m) in search.matches.iter().enumerate() {
let row = m.line + offset as i32;
if row < 0 || row as usize >= rows {
continue;
}
let r = Rect::new(ox + m.start as f32 * cw, oy + row as f32 * ch, (m.end - m.start) as f32 * cw, ch);
ctx.bg.push(quad(r, if i == search.current { current } else { others }, clip, MODE_SOLID));
}
}
out.append(&mut ctx.bg);
if show_cursor && ctx.focused {
let cx = ox + cursor_px.0;
let cy = oy + cursor_px.1;
let thin = (1.6 * s).round().max(1.0);
let ccol = pal.cursor;
let a = ctx.cursor_alpha * o;
match cursor.shape {
CursorShape::Block => out.push(quad(Rect::new(cx, cy, cw, ch), with_alpha(ccol, a), clip, MODE_SOLID)),
CursorShape::Beam => ctx.fg.push(quad(Rect::new(cx, cy, thin, ch), with_alpha(ccol, a), clip, MODE_SOLID)),
CursorShape::Underline => {
ctx.fg.push(quad(Rect::new(cx, cy + ch - thin, cw, thin), with_alpha(ccol, a), clip, MODE_SOLID))
}
_ => hollow(&mut ctx.fg, cx, cy, cw, ch, thin.min(1.0 * s).max(1.0), with_alpha(ccol, a), clip),
}
}
if let Some((line, c0, c1)) = pane.link {
let row = line + offset as i32;
if row >= 0 && (row as usize) < rows {
let top = oy + row as f32 * ch;
let uy = (top + baseline + line_w * 1.5).min(top + ch - line_w);
let r = Rect::new(ox + c0 as f32 * cw, uy, (c1 - c0) as f32 * cw, line_w);
ctx.fg.push(quad(r, with_alpha(pal.accent, o), clip, MODE_SOLID));
}
}
out.append(&mut ctx.fg);
if let Some(search) = &pane.search {
draw_search_bar(ctx, search, card, o, out);
}
if offset > 0 && history > 0 {
let track = Rect::new(card.right() - 6.0 * s, card.y + radius, 3.0 * s, card.h - 2.0 * radius);
let total = (history + rows) as f32;
let th = (rows as f32 / total * track.h).max(18.0 * s);
let ty = track.y + (1.0 - offset as f32 / history as f32) * (track.h - th);
out.push(Quad {
rect: [track.x, ty, track.w, th],
uv: [0.0; 4],
color: with_alpha(pal.foreground, 0.28 * o),
clip: NO_CLIP,
params: [MODE_ROUNDED, track.w * 0.5, 0.0, 0.0],
color2: [0.0; 4],
});
}
if pane.claude {
draw_claude_bar(ctx, pane, card, out);
}
let veil = (1.0 - focus) * 0.38 * ctx.multi * o;
if veil > 0.002 {
let v = card.inset(1.0 * s);
out.push(Quad {
rect: [v.x, v.y, v.w, v.h],
uv: [0.0; 4],
color: with_alpha(pal.surface, veil),
clip: NO_CLIP,
params: [MODE_ROUNDED, (radius - 1.0 * s).max(0.0), 0.0, 0.0],
color2: [0.0; 4],
});
}
}
fn draw_drop_hint(ctx: &mut Ctx, pane: &Rect, out: &mut Vec<Quad>) {
let s = ctx.scale;
let pal = ctx.palette;
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let radius = ctx.config.radius * s;
let frame = pane.inset(1.0 * s);
out.push(rounded(frame, with_alpha(pal.accent, 0.12), radius, (2.0 * s).max(2.0), with_alpha(pal.accent, 0.9), NO_CLIP));
let message = "Drop to type the path";
let w = (message.chars().count() as f32 + 4.0) * cw;
let pill = Rect::new((pane.x + (pane.w - w) * 0.5).round(), (pane.y + (pane.h - ch * 2.0) * 0.5).round(), w, (ch * 2.0).round());
out.push(rounded(pill, with_alpha(mix(pal.surface, pal.foreground, 0.07), 0.95), 10.0 * s, s.max(1.0), with_alpha(pal.accent, 0.8), NO_CLIP));
text(ctx, out, pill.x + 2.0 * cw, (pill.y + (pill.h - ch) * 0.5).round(), message, pal.foreground, 0, w, NO_CLIP);
}
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);
}
fn draw_search_bar(ctx: &mut Ctx, search: &Search, card: Rect, o: f32, out: &mut Vec<Quad>) {
let s = ctx.scale;
let pal = ctx.palette;
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let h = (ch * 1.9).round();
let w = (360.0 * s).min(card.w - 24.0 * s).max(cw * 16.0).min(card.w);
let bar = Rect::new((card.right() - w - 12.0 * s).round(), (card.y + 10.0 * s).round(), w, h);
let bg = mix(pal.surface, pal.foreground, 0.05);
out.push(shadow(bar, 10.0 * s, 14.0 * s, 5.0 * s, 0.45 * o, ctx.alpha < 1.0));
let border = with_alpha(mix(pal.border, pal.accent, 0.55), o);
out.push(rounded(bar, with_alpha(bg, (ctx.alpha + 0.2).min(1.0) * o), 10.0 * s, s.max(1.0), border, NO_CLIP));
let ty = (bar.y + (h - ch) * 0.5).round();
let tx = bar.x + cw;
text(ctx, out, tx, ty, "›", with_alpha(pal.accent, o), STYLE_BOLD, cw * 2.0, NO_CLIP);
let label = search.label();
let label_w = label.chars().count() as f32 * cw;
let label_color = if search.matches.is_empty() && !search.query.is_empty() {
with_alpha(pal.ansi[1], o)
} else {
with_alpha(mix(pal.foreground, bg, 0.55), o)
};
text(ctx, out, bar.right() - cw - label_w, ty, &label, label_color, 0, label_w + 1.0, NO_CLIP);
let qx = tx + cw * 2.0;
let room = (bar.right() - cw * 2.0 - label_w - qx).max(0.0);
let max_chars = (room / cw).floor() as usize;
let fg = with_alpha(pal.foreground, o);
let caret_x = if search.query.is_empty() {
let hint = with_alpha(mix(pal.foreground, bg, 0.7), o);
text(ctx, out, qx, ty, "find in scrollback", hint, 0, room, NO_CLIP);
qx - 3.0 * s
} else {
let chars: Vec<char> = search.query.chars().collect();
let shown: String = chars[chars.len().saturating_sub(max_chars)..].iter().collect();
qx + text(ctx, out, qx, ty, &shown, fg, 0, room + cw, NO_CLIP) + 1.0
};
let caret = Rect::new(caret_x, ty + ch * 0.12, (1.5 * s).round().max(1.0), ch * 0.76);
out.push(quad(caret, with_alpha(pal.accent, o * ctx.cursor_alpha.max(0.25)), NO_CLIP, MODE_SOLID));
}
#[allow(clippy::too_many_arguments)]
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));
}
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
}
}
#[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,
}
}
fn draw_picker(ctx: &mut Ctx, picker: &mut Picker, pane: &Rect, out: &mut Vec<Quad>) {
let s = ctx.scale;
let pal = ctx.palette;
let speed = ctx.config.animation_speed;
let d = |secs: f32| if speed <= 0.0 { 0.0 } else { secs / speed };
let now = ctx.now;
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let target_op = if picker.closing { 0.0 } else { 1.0 };
picker.opacity_anim.set(target_op, d(if picker.closing { 0.12 } else { 0.16 }), now);
picker.opacity = picker.opacity_anim.get(now);
let sel = picker.selected as f32;
picker.highlight_anim.set(sel, d(0.12), now);
picker.highlight = picker.highlight_anim.get(now);
let op = picker.opacity;
let rise = if picker.closing { 0.0 } else { (1.0 - op) * 6.0 * s };
let pad = (18.0 * s).round();
let row_h = (ch * 1.8).round();
let search_h = (ch * 1.9).round();
let rows = picker.rows();
let fixed = pad * 2.0 + ch + 12.0 * s + search_h + 10.0 * s + 10.0 * s + ch;
let room = ((pane.h - 48.0 * s - fixed) / row_h).floor().max(1.0) as usize;
let visible = rows.min(room).min(9);
let w = (pane.w - 40.0 * s).min(700.0 * s).max(cw * 24.0).min(pane.w);
let h = fixed + visible as f32 * row_h;
let x = (pane.x + (pane.w - w) * 0.5).round();
let y = (pane.y + (pane.h - h) * 0.42 + rise).round();
let card = Rect::new(x, y, w, h);
picker.card = card;
let cur = picker.scroll_anim.target();
let scroll_t = if sel < cur {
sel
} else if sel >= cur + visible as f32 {
sel - visible as f32 + 1.0
} else {
cur
};
picker.scroll_anim.set(scroll_t, d(0.14), now);
picker.scroll = picker.scroll_anim.get(now);
picker.animating =
picker.opacity_anim.active(now) || picker.highlight_anim.active(now) || picker.scroll_anim.active(now);
let radius = ctx.config.radius * s;
out.push(rounded(pane.inset(1.0 * s), with_alpha(pal.background, 0.62 * op), radius, 0.0, [0.0; 4], NO_CLIP));
let see_through = ctx.alpha < 1.0;
out.push(shadow(card, 14.0 * s, 22.0 * s, 8.0 * s, 0.55 * op, see_through));
let card_bg = mix(pal.surface, pal.foreground, 0.035);
if ctx.glass {
let tint = with_alpha(card_bg, (ctx.alpha + 0.12).min(1.0) * op);
out.push(glass(card, tint, with_alpha(WHITE, 0.6 * op), 0.55 * op, 14.0 * s, s));
} else {
let border = mix(pal.border, pal.foreground, 0.06);
out.push(rounded(card, with_alpha(card_bg, op), 14.0 * s, s.max(1.0), with_alpha(border, op), NO_CLIP));
}
let muted = with_alpha(mix(pal.foreground, card_bg, 0.55), op);
let fg = with_alpha(pal.foreground, op);
let inner_w = w - 2.0 * pad;
let mut hx = x + pad;
let hy = y + pad;
hx += text(ctx, out, hx, hy, "✻ claude", with_alpha(CLAUDE, op), STYLE_BOLD, inner_w, NO_CLIP);
hx += cw * 2.0;
let count = match picker.sessions.len() {
0 => "no sessions yet".to_string(),
1 => "1 session".to_string(),
n => format!("{n} sessions"),
};
let count_w = count.chars().count() as f32 * cw;
text(ctx, out, x + w - pad - count_w, hy, &count, muted, 0, count_w + 1.0, NO_CLIP);
let project = picker.project.clone();
text(ctx, out, hx, hy, &project, fg, 0, (x + w - pad - count_w - cw * 2.0 - hx).max(0.0), NO_CLIP);
let sy = hy + ch + 12.0 * s;
let field = Rect::new(x + pad, sy, inner_w, search_h);
out.push(rounded(
field,
with_alpha(pal.surface, ctx.alpha * op),
9.0 * s,
s.max(1.0),
with_alpha(pal.border, op),
NO_CLIP,
));
let ty = (sy + (search_h - ch) * 0.5).round();
let tx = field.x + cw;
text(ctx, out, tx, ty, "›", with_alpha(CLAUDE, op), STYLE_BOLD, cw * 2.0, NO_CLIP);
let qx = tx + cw * 2.0;
let placeholder = with_alpha(mix(pal.foreground, pal.surface, 0.7), op);
let qw = if picker.query.is_empty() {
text(ctx, out, qx, ty, "search sessions", placeholder, 0, field.w - cw * 4.0, NO_CLIP);
0.0
} else {
let q = picker.query.clone();
text(ctx, out, qx, ty, &q, fg, 0, field.w - cw * 5.0, NO_CLIP)
};
if !picker.closing {
let cx = if picker.query.is_empty() { qx - 3.0 * s } else { qx + qw + 1.0 };
let caret = Rect::new(cx, ty + ch * 0.12, (1.5 * s).round().max(1.0), ch * 0.76);
out.push(quad(caret, with_alpha(CLAUDE, op * ctx.cursor_alpha.max(0.25)), NO_CLIP, MODE_SOLID));
}
let ly = sy + search_h + 10.0 * s;
let list = Rect::new(x + pad * 0.5, ly, w - pad, visible as f32 * row_h);
picker.list = list;
picker.row_h = row_h;
let clip = [list.x, list.y, list.right(), list.bottom()];
let hl_y = ly + (picker.highlight - picker.scroll) * row_h;
out.push(rounded(
Rect::new(list.x, hl_y, list.w, row_h),
with_alpha(CLAUDE, 0.12 * op),
8.0 * s,
0.0,
[0.0; 4],
clip,
));
let bar = Rect::new(list.x + 4.0 * s, hl_y + row_h * 0.28, 3.0 * s, row_h * 0.44);
out.push(rounded(bar, with_alpha(CLAUDE, op), 1.5 * s, 0.0, [0.0; 4], clip));
let first = picker.scroll.floor().max(0.0) as usize;
let text_x = list.x + 16.0 * s;
let right = list.right() - 14.0 * s;
for row in first..(first + visible + 1).min(rows) {
let ry = ly + (row as f32 - picker.scroll) * row_h;
let ty = (ry + (row_h - ch) * 0.5).round();
let selected = row == picker.selected;
if row == 0 {
let c = if selected { with_alpha(CLAUDE, op) } else { with_alpha(mix(CLAUDE, card_bg, 0.25), op) };
text(ctx, out, text_x, ty, "+ New session", c, STYLE_BOLD, right - text_x, clip);
let hint = "fresh start";
let hw = hint.chars().count() as f32 * cw;
text(ctx, out, right - hw, ty, hint, muted, 0, hw + 1.0, clip);
continue;
}
let session = &picker.sessions[picker.filtered[row - 1]];
let meta = if session.branch.is_empty() {
claude::relative_time(session.modified)
} else {
format!("{} · {}", claude::relative_time(session.modified), session.branch)
};
let title = session.title.clone();
let mw = meta.chars().count() as f32 * cw;
text(ctx, out, right - mw, ty, &meta, muted, 0, mw + 1.0, clip);
let tc = if selected { fg } else { with_alpha(mix(pal.foreground, card_bg, 0.22), op) };
text(ctx, out, text_x, ty, &title, tc, 0, (right - mw - cw * 3.0 - text_x).max(0.0), clip);
}
let fy = list.bottom() + 10.0 * s;
let hint = "↑↓ select enter open type to filter esc cancel";
let hw = (hint.chars().count() as f32 * cw).min(inner_w);
let hc = with_alpha(mix(pal.foreground, card_bg, 0.68), op);
text(ctx, out, x + (w - hw) * 0.5, fy, hint, hc, 0, inner_w, NO_CLIP);
}
pub fn claude_bar_height(cell_h: f32, scale: f32) -> f32 {
(cell_h * 1.6 + 6.0 * scale).round()
}
enum Part {
Text(String, Rgba, u8),
Meter(f32),
Gap,
}
fn draw_claude_bar(ctx: &mut Ctx, pane: &Pane, card: Rect, out: &mut Vec<Quad>) {
let s = ctx.scale;
let pal = ctx.palette;
let o = pane.opacity;
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let pad = (ctx.config.padding * s).round();
let bar_h = claude_bar_height(ch, s);
let top = card.bottom() - bar_h;
let muted = with_alpha(mix(pal.foreground, pal.surface, 0.5), o);
let fg = with_alpha(mix(pal.foreground, pal.surface, 0.1), o);
let level = |pct: f32| {
let c = if pct < 60.0 {
pal.ansi[2]
} else if pct < 85.0 {
pal.ansi[3]
} else {
pal.ansi[1]
};
with_alpha(c, o)
};
let line = Rect::new(card.x + pad * 0.6, top, card.w - pad * 1.2, s.max(1.0));
out.push(quad(line, with_alpha(mix(pal.border, pal.foreground, 0.04), o), NO_CLIP, MODE_SOLID));
let status = pane.claude_status.clone().unwrap_or_default();
let usage = ctx.usage.fresh();
let five = status.five_hour.or(usage.five_hour);
let week = status.seven_day.or(usage.seven_day);
let model = if status.model.is_empty() { "claude".to_string() } else { status.model.to_lowercase() };
let window = |label: &str, w: Option<claude::Window>| -> Vec<Part> {
let mut parts = vec![Part::Text(format!("{label} "), muted, 0)];
match w {
Some(w) => {
parts.push(Part::Meter(w.used_pct));
parts.push(Part::Text(format!(" {:.0}%", w.used_pct), level(w.used_pct), 0));
parts.push(Part::Text(format!(" resets {}", claude::until(w.resets_at)), muted, 0));
}
None => parts.push(Part::Text("—".into(), muted, 0)),
}
parts
};
let context = match status.context_pct {
Some(p) => {
vec![Part::Text("context ".into(), muted, 0), Part::Meter(p), Part::Text(format!(" {p:.0}%"), level(p), 0)]
}
None => vec![Part::Text("context —".into(), muted, 0)],
};
let mut segments: Vec<Vec<Part>> =
vec![vec![Part::Text("✻ ".into(), with_alpha(CLAUDE, o), STYLE_BOLD), Part::Text(model, fg, 0)]];
if five.is_none() && week.is_none() {
segments.push(vec![Part::Text("plan usage appears after Claude's first reply".into(), muted, 0)]);
} else {
segments.push(window("5h", five));
segments.push(window("week", week));
}
segments.push(context);
if status.lines_added + status.lines_removed > 0 {
segments.push(vec![
Part::Text(format!("+{}", status.lines_added), with_alpha(pal.ansi[2], o), 0),
Part::Text(format!(" −{}", status.lines_removed), with_alpha(pal.ansi[1], o), 0),
]);
}
let meter_w = (cw * 5.0).round();
let part_w = |p: &Part| match p {
Part::Text(t, _, _) => t.chars().count() as f32 * cw,
Part::Meter(_) => meter_w,
Part::Gap => cw * 2.5,
};
let avail = card.w - 2.0 * pad;
let mut used = 0.0;
let mut parts: Vec<Part> = Vec::new();
for seg in segments {
let w: f32 = seg.iter().map(part_w).sum::<f32>() + if parts.is_empty() { 0.0 } else { cw * 2.5 };
if used + w > avail {
break;
}
used += w;
if !parts.is_empty() {
parts.push(Part::Gap);
}
parts.extend(seg);
}
let ty = (top + (bar_h - ch) * 0.5).round();
let mut x = card.x + pad;
for p in &parts {
match p {
Part::Text(t, c, style) => {
text(ctx, out, x, ty, t, *c, *style, part_w(p) + 1.0, NO_CLIP);
}
Part::Meter(pct) => {
let h = (4.0 * s).round().max(3.0);
let my = (ty + (ch - h) * 0.5).round();
let track = Rect::new(x, my, meter_w, h);
out.push(rounded(
track,
with_alpha(mix(pal.surface, pal.foreground, 0.12), o),
h * 0.5,
0.0,
[0.0; 4],
NO_CLIP,
));
let fw = (meter_w * (pct / 100.0).clamp(0.0, 1.0)).max(h);
out.push(rounded(Rect::new(x, my, fw, h), level(*pct), h * 0.5, 0.0, [0.0; 4], NO_CLIP));
}
Part::Gap => {}
}
x += part_w(p);
}
}