use super::*;
#[derive(Clone, Copy)]
pub(super) struct Colors {
pub(super) fg: Rgba,
pub(super) soft: Rgba,
pub(super) muted: Rgba,
pub(super) accent: Rgba,
pub(super) code: Rgba,
pub(super) panel: Rgba,
pub(super) raised: Rgba,
pub(super) line: Rgba,
pub(super) red: Rgba,
pub(super) green: Rgba,
pub(super) yellow: Rgba,
pub(super) cyan: Rgba,
}
impl Colors {
pub(super) fn new(ctx: &Ctx) -> Colors {
let p = ctx.palette;
let panel = mix(p.surface, p.foreground, 0.025);
let solid = |c: Rgba| [c[0], c[1], c[2], 1.0];
Colors {
fg: p.foreground,
soft: mix(p.foreground, p.surface, 0.2),
muted: mix(p.foreground, p.surface, 0.5),
accent: p.accent,
code: mix(p.accent, p.foreground, 0.35),
panel: solid(panel),
raised: solid(mix(panel, p.foreground, 0.06)),
line: solid(mix(panel, p.foreground, 0.14)),
red: p.ansi[1],
green: p.ansi[2],
yellow: p.ansi[3],
cyan: p.ansi[6],
}
}
pub(super) fn level(&self, level: Level) -> Rgba {
match level {
Level::Risk => self.red,
Level::Warn => self.yellow,
Level::Note => self.cyan,
}
}
pub(super) fn state(&self, state: State) -> (&'static str, Rgba) {
match state {
State::Untested => ("\u{25cb}", self.muted),
State::Done(Verdict::Passed) => ("\u{2713}", self.green),
State::Done(Verdict::Failed) => ("\u{2717}", self.red),
State::Done(Verdict::Skipped) => ("\u{2013}", self.muted),
State::Stale(_) => ("\u{21bb}", self.yellow),
}
}
}
pub(super) struct Button {
pub(super) label: String,
pub(super) key: &'static str,
pub(super) click: Click,
pub(super) tone: Option<Rgba>,
pub(super) on: bool,
}
pub(super) fn button(label: &str, click: Click) -> Button {
Button { label: label.to_string(), key: "", click, tone: None, on: false }
}
pub(super) fn button_chars(b: &Button) -> usize {
b.label.chars().count() + if b.key.is_empty() { 0 } else { b.key.chars().count() + 1 }
}
impl Button {
pub(super) fn key(mut self, key: &'static str) -> Button {
self.key = key;
self
}
pub(super) fn tone(mut self, color: Rgba, on: bool) -> Button {
self.tone = Some(color);
self.on = on;
self
}
}
pub(super) type Hints = Vec<(&'static str, &'static str)>;
pub(super) fn hint_width((key, does): (&str, &str), cw: f32) -> f32 {
(key.chars().count() as f32 + does.chars().count() as f32 + 1.8) * cw
}
pub(super) fn hint_rows(hints: &[(&str, &str)], width: f32, cw: f32) -> usize {
let (mut rows, mut used) = (1, 0.0);
for &hint in hints {
let w = hint_width(hint, cw);
if used > 0.0 && used + w > width {
rows += 1;
used = 0.0;
}
used += w + 2.0 * cw;
}
rows
}
pub(super) fn hint_row_height(ch: f32) -> f32 {
(ch * 1.45).round()
}
pub(super) struct Flow<'a, 'c> {
pub(super) ctx: &'a mut Ctx<'c>,
pub(super) out: &'a mut Vec<Quad>,
pub(super) hits: &'a mut Vec<(Rect, Click)>,
pub(super) colors: Colors,
pub(super) mouse: (f32, f32),
pub(super) area: Rect,
pub(super) x: f32,
pub(super) w: f32,
pub(super) y: f32,
pub(super) cw: f32,
pub(super) ch: f32,
pub(super) s: f32,
}
impl<'a, 'c> Flow<'a, 'c> {
pub(super) fn new(
ctx: &'a mut Ctx<'c>,
out: &'a mut Vec<Quad>,
hits: &'a mut Vec<(Rect, Click)>,
mouse: (f32, f32),
area: Rect,
scroll: f32,
) -> Self {
let colors = Colors::new(ctx);
let (cw, ch, s) = (ctx.glyphs.cell_w, ctx.glyphs.cell_h, ctx.scale);
Flow { ctx, out, hits, colors, mouse, area, x: area.x, w: area.w, y: area.y - scroll.round(), cw, ch, s }
}
pub(super) fn clip(&self) -> [f32; 4] {
[self.area.x, self.area.y, self.area.right(), self.area.bottom()]
}
pub(super) fn shows(&self, y: f32, h: f32) -> bool {
y + h >= self.area.y && y <= self.area.bottom()
}
pub(super) fn line_height(&self) -> f32 {
(self.ch * 1.25).round()
}
pub(super) fn cols(&self, width: f32) -> usize {
(width / self.cw).floor().max(4.0) as usize
}
pub(super) fn gap(&mut self, lines: f32) {
self.y += (self.ch * lines).round();
}
pub(super) fn fill(&mut self, r: Rect, color: Rgba, radius: f32) {
if self.shows(r.y, r.h) {
let clip = self.clip();
self.out.push(rounded(r, color, radius * self.s, 0.0, [0.0; 4], clip));
}
}
pub(super) fn framed(&mut self, r: Rect, color: Rgba, border: Rgba, radius: f32) {
if self.shows(r.y, r.h) {
let clip = self.clip();
self.out.push(rounded(r, color, radius * self.s, self.s.max(1.0), border, clip));
}
}
pub(super) fn put(&mut self, x: f32, y: f32, content: &str, color: Rgba, style: u8, max_w: f32) -> f32 {
if !self.shows(y, self.ch) || max_w < self.cw {
return 0.0;
}
let content: String = content.chars().map(|c| if c.is_control() { ' ' } else { c }).collect();
let clip = self.clip();
text(self.ctx, self.out, x.round(), y.round(), &content, color, style, max_w, clip)
}
pub(super) fn para(&mut self, content: &str, color: Rgba, style: u8, indent: f32) {
let width = self.w - indent;
let lines = wrap(content, self.cols(width));
let (lh, code) = (self.line_height(), self.colors.code);
for line in lines {
let mut x = self.x + indent;
for (piece, is_code) in line {
let chars = piece.chars().count() as f32;
let color = if is_code { code } else { color };
self.put(x, self.y, &piece, color, style, (chars + 0.5) * self.cw);
x += chars * self.cw;
}
self.y += lh;
}
}
pub(super) fn hit(&mut self, r: Rect, click: Click) {
let (top, bottom) = (r.y.max(self.area.y), r.bottom().min(self.area.bottom()));
if bottom > top {
self.hits.push((Rect::new(r.x, top, r.w, bottom - top), click));
}
}
pub(super) fn hovered(&self, r: Rect) -> bool {
r.contains(self.mouse.0, self.mouse.1) && self.area.contains(self.mouse.0, self.mouse.1)
}
pub(super) fn button_width(&self, b: &Button) -> f32 {
(button_chars(b) as f32 + 2.0) * self.cw
}
pub(super) fn keys(&mut self, hints: &[(&str, &str)]) {
let c = self.colors;
let row = hint_row_height(self.ch);
let mut x = self.x;
for &(key, does) in hints {
let w = hint_width((key, does), self.cw);
if x > self.x && x + w > self.x + self.w {
x = self.x;
self.y += row;
}
let chars = key.chars().count() as f32;
let cap = Rect::new(x, self.y, ((chars + 1.0) * self.cw).round(), (self.ch * 1.2).round());
self.framed(cap, c.raised, c.line, 4.0);
let ty = cap.y + (cap.h - self.ch) * 0.5;
self.put(cap.x + self.cw * 0.5, ty, key, c.soft, 0, chars * self.cw + 1.0);
let after = cap.right() + self.cw * 0.7;
self.put(after, ty, does, c.muted, 0, (self.x + self.w - after).max(0.0));
x += w + 2.0 * self.cw;
}
self.y += row;
}
pub(super) fn button_at(&mut self, r: Rect, b: Button) {
let c = self.colors;
let tone = b.tone.unwrap_or(c.fg);
let hover = self.hovered(r);
let fill = if b.on {
mix(c.panel, tone, 0.34)
} else if hover {
mix(c.raised, c.fg, 0.08)
} else {
c.raised
};
let border = if b.on || (hover && b.tone.is_some()) { tone } else { c.line };
self.framed(r, fill, border, 6.0);
let width = button_chars(&b) as f32 * self.cw;
let color = if b.tone.is_some() && !b.on { mix(tone, c.fg, 0.25) } else { c.fg };
let (x, y) = (r.x + (r.w - width) * 0.5, r.y + (r.h - self.ch) * 0.5);
let used = b.label.chars().count() as f32 * self.cw;
self.put(x, y, &b.label, color, 0, used + 1.0);
if !b.key.is_empty() {
let key_w = b.key.chars().count() as f32 * self.cw;
self.put(x + used + self.cw, y, b.key, c.muted, 0, key_w + 1.0);
}
self.hit(r, b.click);
}
pub(super) fn buttons(&mut self, buttons: Vec<Button>) {
let (h, gap) = ((self.ch * 1.6).round(), (6.0 * self.s).round());
let mut x = self.x;
for b in buttons {
let w = self.button_width(&b).min(self.w);
if x > self.x && x + w > self.x + self.w {
x = self.x;
self.y += h + gap;
}
self.button_at(Rect::new(x, self.y, w, h), b);
x += w + gap;
}
self.y += h;
}
pub(super) fn field(&mut self, content: &str, placeholder: &str, focused: bool, click: Option<Click>) {
let c = self.colors;
let r = Rect::new(self.x, self.y, self.w, (self.ch * 1.8).round());
self.framed(r, c.raised, if focused { c.accent } else { c.line }, 7.0);
let ty = r.y + (r.h - self.ch) * 0.5;
let room = self.cols(r.w - 2.0 * self.cw).saturating_sub(1);
let chars: Vec<char> = content.chars().collect();
let shown: String = chars[chars.len().saturating_sub(room)..].iter().collect();
if shown.is_empty() {
self.put(r.x + self.cw, ty, placeholder, c.muted, 0, r.w - 2.0 * self.cw);
} else {
self.put(r.x + self.cw, ty, &shown, c.fg, 0, r.w - self.cw);
}
if focused && self.shows(r.y, r.h) {
let cx = r.x + self.cw + shown.chars().count() as f32 * self.cw + 1.0;
let caret = Rect::new(cx, ty + self.ch * 0.12, (1.5 * self.s).round().max(1.0), self.ch * 0.76);
let clip = self.clip();
self.out.push(quad(caret, with_alpha(c.accent, self.ctx.cursor_alpha.max(0.25)), clip, MODE_SOLID));
}
if let Some(click) = click {
self.hit(r, click);
}
self.y = r.bottom();
}
pub(super) fn text_box(&mut self, content: &str, focused: bool, click: Click) {
let c = self.colors;
let cols = self.cols(self.w - 2.0 * self.cw).saturating_sub(1).max(1);
let mut lines: Vec<String> = Vec::new();
for line in content.split('\n') {
let chars: Vec<char> = line.chars().collect();
if chars.is_empty() {
lines.push(String::new());
}
lines.extend(chars.chunks(cols).map(|part| part.iter().collect::<String>()));
}
let lh = self.line_height();
let height = (lines.len().max(5) as f32 * lh + self.ch * 0.6).round();
let r = Rect::new(self.x, self.y, self.w, height);
self.framed(r, c.raised, if focused { c.accent } else { c.line }, 7.0);
let top = r.y + self.ch * 0.3;
for (i, line) in lines.iter().enumerate() {
self.put(r.x + self.cw, top + i as f32 * lh, line, c.fg, 0, r.w - self.cw);
}
if focused {
let last = lines.last().map_or(0, |l| l.chars().count());
let cx = r.x + self.cw * (last as f32 + 1.0) + 1.0;
let cy = top + (lines.len() - 1) as f32 * lh;
if self.shows(cy, self.ch) {
let caret = Rect::new(cx, cy + self.ch * 0.12, (1.5 * self.s).round().max(1.0), self.ch * 0.76);
let clip = self.clip();
self.out.push(quad(caret, with_alpha(c.accent, self.ctx.cursor_alpha.max(0.25)), clip, MODE_SOLID));
}
}
self.hit(r, click);
self.y = r.bottom();
}
pub(super) fn rule(&mut self) {
let r = Rect::new(self.x, self.y, self.w, self.s.max(1.0).round());
let line = self.colors.line;
self.fill(r, line, 0.0);
self.y = r.bottom();
}
pub(super) fn callout(&mut self, content: &str, color: Rgba) {
let pad = self.cw;
let height = wrap(content, self.cols(self.w - 2.5 * pad)).len() as f32 * self.line_height() + self.ch * 0.5;
let r = Rect::new(self.x, self.y, self.w, height.round());
let tint = mix(self.colors.panel, color, 0.12);
self.fill(r, tint, 6.0);
self.fill(Rect::new(r.x, r.y, (3.0 * self.s).round(), r.h), color, 1.0);
self.y += (self.ch * 0.25).round();
let (x, w) = (self.x, self.w);
self.x += 1.5 * pad;
self.w -= 2.5 * pad;
let fg = self.colors.fg;
self.para(content, fg, 0, 0.0);
(self.x, self.w) = (x, w);
self.y = r.bottom();
}
pub(super) fn place(&mut self, f: &Finding) {
let Some(span) = &f.span else { return };
let c = self.colors;
let from_description = span.file == "PR description" || span.file == "commit messages";
let place = format!("{}:{}", span.file, span.line);
let width = (place.chars().count() as f32 * self.cw).min(self.w);
let r = Rect::new(self.x, self.y, width, self.line_height());
let color = if from_description {
c.muted
} else if self.hovered(r) {
c.accent
} else {
c.code
};
self.put(self.x, self.y, &place, color, 0, self.w);
if !from_description {
self.hit(r, Click::File(span.file.clone(), span.line));
}
self.y += self.line_height();
let (shown, col, len) = snippet(span, self.cols(self.w - 2.0 * self.cw));
let r = Rect::new(self.x, self.y, self.w, (self.ch * 1.7).round());
self.fill(r, c.raised, 6.0);
let ty = r.y + self.ch * 0.25;
self.put(r.x + self.cw, ty, &shown, c.fg, 0, r.w - self.cw);
let under = Rect::new(
r.x + self.cw * (col as f32 + 1.0),
ty + self.ch * 1.08,
len as f32 * self.cw,
(2.0 * self.s).round(),
);
let color = c.level(f.level);
self.fill(under, color, 1.0);
self.y = r.bottom() + (self.ch * 0.15).round();
self.para(&span.label, color, 0, self.cw);
}
}