use winit::dpi::PhysicalSize;
use crate::gpu::{MODE_SOLID, Quad};
use crate::layout::Rect;
use crate::overlay::{Menu, Palette};
use crate::settings::Settings;
use crate::render::{Ctx, NO_CLIP, WHITE, glass, mix, quad, rounded, shadow, text, with_alpha};
pub(crate) fn card(ctx: &Ctx, r: Rect, radius: f32, out: &mut Vec<Quad>) {
let s = ctx.scale;
let pal = ctx.palette;
out.push(shadow(r, radius, 22.0 * s, 8.0 * s, 0.55, ctx.alpha < 1.0));
let bg = mix(pal.surface, pal.foreground, 0.035);
if ctx.glass {
out.push(glass(r, with_alpha(bg, (ctx.alpha + 0.12).min(1.0)), with_alpha(WHITE, 0.6), 0.55, radius, s));
} else {
let border = mix(pal.border, pal.foreground, 0.06);
out.push(rounded(r, bg, radius, s.max(1.0), border, NO_CLIP));
}
}
pub fn draw_menu(ctx: &mut Ctx, menu: &mut Menu, win: 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 pad = (6.0 * s).round();
let row_h = (ch * 1.7).round();
let longest = menu.items.iter().map(|i| i.label.chars().count()).max().unwrap_or(4) as f32;
let w = ((longest + 5.0) * cw).max(190.0 * s);
let h = row_h * menu.items.len() as f32;
let (ww, wh) = (win.width as f32, win.height as f32);
let x = menu.at.0.min(ww - w - pad - 6.0 * s).max(pad + 6.0 * s).round();
let y = menu.at.1.min(wh - h - pad - 6.0 * s).max(pad + 6.0 * s).round();
menu.rect = Rect::new(x, y, w, h);
menu.row_h = row_h;
card(ctx, menu.rect.inset(-pad), 10.0 * s, out);
let fg = pal.foreground;
let muted = mix(pal.foreground, pal.surface, 0.55);
for (i, item) in menu.items.iter().enumerate() {
let ry = y + i as f32 * row_h;
if i == menu.selected && item.enabled {
out.push(rounded(Rect::new(x, ry, w, row_h), with_alpha(pal.accent, 0.16), 7.0 * s, 0.0, [0.0; 4], NO_CLIP));
}
let color = if item.enabled { fg } else { muted };
let ty = (ry + (row_h - ch) * 0.5).round();
text(ctx, out, x + cw, ty, &item.label, color, 0, w - 2.0 * cw, NO_CLIP);
}
}
pub fn draw_palette(ctx: &mut Ctx, pal_state: &mut Palette, win: 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 (ww, wh) = (win.width as f32, win.height as f32);
out.push(quad(Rect::new(0.0, 0.0, ww, wh), with_alpha(pal.background, 0.5), NO_CLIP, MODE_SOLID));
let pad = (16.0 * s).round();
let field_h = (ch * 1.9).round();
let row_h = (ch * 1.7).round();
let footer_h = ch + 6.0 * s;
let w = (660.0 * s).min(ww - 40.0 * s).max(cw * 24.0).min(ww);
let top = (wh * 0.12).round();
let fixed = pad * 2.0 + field_h + 10.0 * s + footer_h;
let capacity = (((wh - top - 24.0 * s - fixed) / row_h).floor().max(1.0) as usize).min(10);
pal_state.visible = capacity;
let shown = pal_state.shown.len().min(capacity).max(1);
let h = fixed + shown as f32 * row_h;
let x = ((ww - w) * 0.5).round();
let card_rect = Rect::new(x, top, w, h);
pal_state.card = card_rect;
pal_state.row_h = row_h;
card(ctx, card_rect, 14.0 * s, out);
let inner_w = w - 2.0 * pad;
let fg = pal.foreground;
let muted = mix(pal.foreground, pal.surface, 0.55);
let field = Rect::new(x + pad, top + pad, inner_w, field_h);
out.push(rounded(field, with_alpha(pal.surface, ctx.alpha), 9.0 * s, s.max(1.0), pal.border, NO_CLIP));
let ty = (field.y + (field_h - ch) * 0.5).round();
text(ctx, out, field.x + cw, ty, "\u{203a}", pal.accent, crate::glyphs::STYLE_BOLD, cw * 2.0, NO_CLIP);
let qx = field.x + cw * 3.0;
let qw = if pal_state.query.is_empty() {
let hint = with_alpha(mix(pal.foreground, pal.surface, 0.7), 1.0);
text(ctx, out, qx, ty, "Type a command, or a name to save the layout as", hint, 0, field.w - cw * 4.0, NO_CLIP);
0.0
} else {
text(ctx, out, qx, ty, &pal_state.query, fg, 0, field.w - cw * 5.0, NO_CLIP)
};
let caret_x = if pal_state.query.is_empty() { qx - 3.0 * s } else { qx + qw + 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, ctx.cursor_alpha.max(0.25)), NO_CLIP, MODE_SOLID));
let list = Rect::new(x + pad * 0.5, field.bottom() + 10.0 * s, w - pad, shown as f32 * row_h);
pal_state.list = list;
let clip = [list.x, list.y, list.right(), list.bottom()];
if pal_state.shown.is_empty() {
text(ctx, out, list.x + cw, (list.y + (row_h - ch) * 0.5).round(), "No matching commands", muted, 0, list.w, clip);
}
let first = pal_state.scroll;
for (n, entry) in pal_state.shown.iter().enumerate().skip(first).take(capacity) {
let ry = list.y + (n - first) as f32 * row_h;
let selected = n == pal_state.selected;
if selected {
out.push(rounded(Rect::new(list.x, ry, list.w, row_h), with_alpha(pal.accent, 0.16), 8.0 * s, 0.0, [0.0; 4], clip));
}
let ty = (ry + (row_h - ch) * 0.5).round();
let hint_w = entry.hint.chars().count() as f32 * cw;
if !entry.hint.is_empty() {
text(ctx, out, list.right() - cw - hint_w, ty, &entry.hint, muted, 0, hint_w + 1.0, clip);
}
let color = if selected { fg } else { mix(pal.foreground, pal.surface, 0.18) };
text(ctx, out, list.x + cw, ty, &entry.label, color, 0, (list.w - hint_w - cw * 4.0).max(0.0), clip);
}
let hint = "\u{2191}\u{2193} select enter run esc close";
let hw = (hint.chars().count() as f32 * cw).min(inner_w);
let fy = list.bottom() + 6.0 * s;
text(ctx, out, x + (w - hw) * 0.5, fy, hint, mix(pal.foreground, pal.surface, 0.68), 0, inner_w, NO_CLIP);
}
fn field(ctx: &mut Ctx, out: &mut Vec<Quad>, r: Rect, content: &str, placeholder: &str, focused: bool, caret: bool) {
let s = ctx.scale;
let pal = ctx.palette;
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let border = if focused { pal.accent } else { pal.border };
out.push(rounded(r, with_alpha(pal.surface, ctx.alpha.max(0.7)), 7.0 * s, s.max(1.0), border, NO_CLIP));
let ty = (r.y + (r.h - ch) * 0.5).round();
let max_w = (r.w - cw * 2.0).max(0.0);
let chars: Vec<char> = content.chars().collect();
let room = (max_w / cw).floor() as usize;
let shown: String =
if caret { chars[chars.len().saturating_sub(room.saturating_sub(1))..].iter().collect() } else { content.to_string() };
let used = if shown.is_empty() && !caret {
text(ctx, out, r.x + cw, ty, placeholder, mix(pal.foreground, pal.surface, 0.65), 0, max_w, NO_CLIP)
} else {
text(ctx, out, r.x + cw, ty, &shown, pal.foreground, 0, max_w, NO_CLIP)
};
if caret {
let c = Rect::new(r.x + cw + used + 1.0, ty + ch * 0.12, (1.5 * s).round().max(1.0), ch * 0.76);
out.push(quad(c, with_alpha(pal.accent, ctx.cursor_alpha.max(0.25)), NO_CLIP, MODE_SOLID));
}
}
fn button(ctx: &mut Ctx, out: &mut Vec<Quad>, r: Rect, label: &str) {
let s = ctx.scale;
let pal = ctx.palette;
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let bg = with_alpha(mix(pal.surface, pal.foreground, 0.08), ctx.alpha.max(0.7));
out.push(rounded(r, bg, 7.0 * s, s.max(1.0), pal.border, NO_CLIP));
let w = label.chars().count() as f32 * cw;
text(ctx, out, (r.x + (r.w - w) * 0.5).round(), (r.y + (r.h - ch) * 0.5).round(), label, pal.foreground, 0, r.w, NO_CLIP);
}
pub fn draw_settings(ctx: &mut Ctx, set: &mut Settings, win: PhysicalSize<u32>, out: &mut Vec<Quad>) {
use crate::config::parse_hex;
use crate::settings::{Kind, Page};
let s = ctx.scale;
let pal = ctx.palette;
let (cw, ch) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h);
let (ww, wh) = (win.width as f32, win.height as f32);
out.push(quad(Rect::new(0.0, 0.0, ww, wh), with_alpha(pal.background, 0.55), NO_CLIP, MODE_SOLID));
let pad = (18.0 * s).round();
let row_h = (ch * 1.95).round();
let w = (900.0 * s).min(ww - 40.0 * s).max(cw * 50.0).min(ww);
let h = (640.0 * s).min(wh - 40.0 * s).max(row_h * 7.0).min(wh);
let rect = Rect::new(((ww - w) * 0.5).round(), ((wh - h) * 0.5).round(), w, h);
set.card = rect;
card(ctx, rect, 14.0 * s, out);
let fg = pal.foreground;
let muted = mix(pal.foreground, pal.surface, 0.55);
let tab_h = (ch * 1.8).round();
let mut tx = rect.x + pad;
let ty = rect.y + pad;
for (i, page) in Page::ALL.iter().enumerate() {
let label = page.label();
let tw = (label.chars().count() as f32 + 4.0) * cw;
let r = Rect::new(tx, ty, tw, tab_h);
set.tabs[i] = r;
let active = *page == set.page;
if active {
out.push(rounded(r, with_alpha(pal.accent, 0.2), 9.0 * s, 0.0, [0.0; 4], NO_CLIP));
}
let color = if active { fg } else { muted };
let style = if active { crate::glyphs::STYLE_BOLD } else { 0 };
text(ctx, out, r.x + 2.0 * cw, (r.y + (tab_h - ch) * 0.5).round(), label, color, style, tw, NO_CLIP);
tx += tw + 6.0 * s;
}
let esc = "esc to close";
let ew = esc.chars().count() as f32 * cw;
text(ctx, out, rect.right() - pad - ew, (ty + (tab_h - ch) * 0.5).round(), esc, muted, 0, ew + 1.0, NO_CLIP);
let footer_h = (ch * 2.4).round();
let list_top = ty + tab_h + 12.0 * s;
let list_h = (rect.bottom() - pad - footer_h - list_top).max(row_h);
let visible = ((list_h / row_h).floor() as usize).max(1);
set.visible = visible;
set.row_h = row_h;
let rows = set.rows();
set.scroll = set.scroll.min(rows.len().saturating_sub(visible));
if set.selected < set.scroll {
set.scroll = set.selected;
} else if set.selected >= set.scroll + visible {
set.scroll = set.selected + 1 - visible;
}
let list = Rect::new(rect.x + pad * 0.5, list_top, rect.w - pad, visible as f32 * row_h);
set.list = list;
set.controls.clear();
let clip = [list.x, list.y, list.right(), list.bottom()];
let x0 = list.x + list.w * 0.5;
let col_w = list.right() - cw - x0;
let control_h = (ch * 1.5).round();
for (n, row) in rows.iter().enumerate().skip(set.scroll).take(visible) {
let ry = list.y + (n - set.scroll) as f32 * row_h;
let selected = n == set.selected;
if selected {
let band = Rect::new(list.x, ry, list.w, row_h);
out.push(rounded(band, with_alpha(pal.accent, 0.12), 8.0 * s, 0.0, [0.0; 4], clip));
}
let ty = (ry + (row_h - ch) * 0.5).round();
let mut lx = list.x + cw;
let label_color = if selected { fg } else { mix(fg, pal.surface, 0.15) };
lx += text(ctx, out, lx, ty, &row.label, label_color, 0, x0 - lx - cw, clip);
if row.restart {
text(ctx, out, lx + cw, ty, "restart", muted, 0, x0 - lx - cw * 2.0, clip);
}
let cy = (ry + (row_h - control_h) * 0.5).round();
let editing = selected && set.edit.is_some();
let capturing = selected && set.capturing;
let none = Rect::default();
match &row.kind {
Kind::Toggle => {
let on = set.toggled(row);
let pill = Rect::new(x0, (ry + (row_h - ch * 1.1) * 0.5).round(), cw * 4.4, (ch * 1.1).round());
let bg = if on { pal.accent } else { mix(pal.border, pal.foreground, 0.15) };
out.push(rounded(pill, with_alpha(bg, 0.9), pill.h * 0.5, 0.0, [0.0; 4], clip));
let knob = pill.h - 6.0 * s;
let kx = if on { pill.right() - knob - 3.0 * s } else { pill.x + 3.0 * s };
let k = Rect::new(kx, pill.y + 3.0 * s, knob, knob);
out.push(rounded(k, with_alpha(WHITE, 0.95), knob * 0.5, 0.0, [0.0; 4], clip));
set.controls.push((n, [none, none, pill]));
}
Kind::Number { .. } => {
let bw = cw * 3.0;
let minus = Rect::new(x0, cy, bw, control_h);
let value = Rect::new(minus.right() + 5.0 * s, cy, cw * 11.0, control_h);
let plus = Rect::new(value.right() + 5.0 * s, cy, bw, control_h);
button(ctx, out, minus, "-");
let shown = set.edit.as_deref().filter(|_| editing).map_or_else(|| set.display(row), str::to_string);
field(ctx, out, value, &shown, "", editing, editing);
button(ctx, out, plus, "+");
set.controls.push((n, [minus, plus, value]));
}
Kind::Text | Kind::Color => {
let swatch = if matches!(row.kind, Kind::Color) { control_h } else { 0.0 };
let width = if swatch > 0.0 { cw * 11.0 } else { col_w.min(cw * 38.0) };
if swatch > 0.0 {
let color = parse_hex(&set.text(row)).unwrap_or([0.0; 4]);
let sw = Rect::new(x0, cy, swatch, control_h);
out.push(rounded(sw, color, 6.0 * s, s.max(1.0), pal.border, clip));
}
let r = Rect::new(x0 + swatch + if swatch > 0.0 { 6.0 * s } else { 0.0 }, cy, width, control_h);
let shown = set.edit.as_deref().filter(|_| editing).map_or_else(|| set.text(row), str::to_string);
field(ctx, out, r, &shown, "empty (default)", editing, editing);
set.controls.push((n, [none, none, Rect::new(x0, cy, r.right() - x0, control_h)]));
}
Kind::Preset(i) => {
let mut x = x0;
if let Some(t) = crate::themes::preset(*i) {
for color in [&t.background, &t.surface, &t.foreground, &t.accent, &t.ansi[1], &t.ansi[2], &t.ansi[4]] {
let c = parse_hex(color).unwrap_or([0.0; 4]);
let sw = Rect::new(x, cy, control_h, control_h);
out.push(rounded(sw, c, 6.0 * s, s.max(1.0), pal.border, clip));
x += control_h + 4.0 * s;
}
}
if set.preset_active(*i) {
text(ctx, out, x + cw, ty, "in use", pal.accent, 0, list.right() - x - cw, clip);
}
set.controls.push((n, [none, none, Rect::new(x0, cy, x - x0, control_h)]));
}
Kind::Shortcut(_) | Kind::Hotkey => {
let r = Rect::new(x0, cy, col_w.min(cw * 30.0), control_h);
let shown = if capturing { "Press the new keys...".to_string() } else { set.display(row) };
let border = if capturing { pal.accent } else { pal.border };
out.push(rounded(r, with_alpha(pal.surface, ctx.alpha.max(0.7)), 7.0 * s, s.max(1.0), border, clip));
let color = if capturing { pal.accent } else if shown == "none" { muted } else { fg };
text(ctx, out, r.x + cw, (r.y + (r.h - ch) * 0.5).round(), &shown, color, 0, r.w - cw * 2.0, clip);
if set.is_custom(row) && !capturing {
let tag = "custom";
let tw = tag.chars().count() as f32 * cw;
text(ctx, out, r.right() + cw, ty, tag, muted, 0, tw + 1.0, clip);
}
set.controls.push((n, [none, none, r]));
}
}
}
if rows.len() > visible {
let track = Rect::new(list.right() - 4.0 * s, list.y, 3.0 * s, list.h);
let thumb_h = (visible as f32 / rows.len() as f32 * track.h).max(18.0 * s);
let thumb_y = track.y + set.scroll as f32 / (rows.len() - visible).max(1) as f32 * (track.h - thumb_h);
let thumb = Rect::new(track.x, thumb_y, track.w, thumb_h);
out.push(rounded(thumb, with_alpha(pal.foreground, 0.28), track.w * 0.5, 0.0, [0.0; 4], NO_CLIP));
}
let inner_w = rect.w - 2.0 * pad;
let fy = list.bottom() + 8.0 * s;
let line = if !set.message.is_empty() {
(set.message.clone(), pal.accent)
} else {
(rows.get(set.selected).map(|r| r.help.to_string()).unwrap_or_default(), muted)
};
text(ctx, out, rect.x + pad, fy, &line.0, line.1, 0, inner_w, NO_CLIP);
let hint = if set.capturing {
"press the new keys esc cancel"
} else if set.edit.is_some() {
"type a value enter apply esc cancel"
} else {
"\u{2191}\u{2193} select \u{2190}\u{2192} change enter edit del reset tab next page"
};
let hw = (hint.chars().count() as f32 * cw).min(inner_w);
text(ctx, out, rect.x + (rect.w - hw) * 0.5, fy + ch * 1.15, hint, mix(pal.foreground, pal.surface, 0.68), 0, inner_w, NO_CLIP);
}