shuyi_ui/widgets/name_entry/
glyph.rs1use super::{GridView, TypingView};
8use crate::frame::color::{EngineColor, Palette};
9use crate::frame::glyph_buffer::{BoxStyle, Cell, GlyphBuffer, Rect};
10use crate::prelude::*;
11
12pub const TYPING_HEIGHT: i32 = 7;
15
16fn centred_in(buf: &GlyphBuffer, rect: Rect, text: &str) -> i32 {
18 rect.x + (rect.w - buf.text_cells(text)) / 2
19}
20
21fn paint_box(palette: &Palette, buf: &mut GlyphBuffer, rect: Rect, title: &str) {
23 let c = |e| palette.engine(e);
24 let bg = c(EngineColor::Panel);
25 buf.fill_rect(rect, Cell::new(' ', c(EngineColor::Text), bg));
26 buf.draw_box(rect, BoxStyle::Double, c(EngineColor::Border), bg);
27 let title = format!(" {title} ");
28 buf.print(rect.x + 2, rect.y, &title, c(EngineColor::Highlight), bg);
29}
30
31pub fn paint_typing(palette: &Palette, buf: &mut GlyphBuffer, rect: Rect, typing: &TypingView) {
34 let c = |e| palette.engine(e);
35 let bg = c(EngineColor::Panel);
36 paint_box(palette, buf, rect, &typing.title);
37 let shown = format!("{}_", typing.typed);
38 let x = centred_in(buf, rect, &shown);
39 buf.print(x, rect.y + 2, &shown, c(EngineColor::Text), bg);
40 let hint = &typing.hint;
41 let x = centred_in(buf, rect, hint);
42 buf.print(x, rect.y + 4, hint, c(EngineColor::TextDim), bg);
43}
44
45pub fn grid_height(grid: &GridView) -> i32 {
48 4 + 2 * i32::try_from(grid.rows.len()).unwrap_or(0)
49}
50
51pub fn paint_grid(palette: &Palette, buf: &mut GlyphBuffer, rect: Rect, grid: &GridView) {
55 let c = |e| palette.engine(e);
56 let bg = c(EngineColor::Panel);
57 let rect = Rect::new(rect.x, rect.y, rect.w, grid_height(grid));
58 paint_box(palette, buf, rect, &grid.title);
59 let shown = format!("{}_", grid.spelled);
60 let x = centred_in(buf, rect, &shown);
61 buf.print(x, rect.y + 2, &shown, c(EngineColor::Text), bg);
62 for (r, labels) in grid.rows.iter().enumerate() {
63 let y = rect.y + 4 + i32::try_from(r * 2).unwrap_or(0);
64 let widths: Vec<i32> = labels.iter().map(|l| buf.text_cells(l) + 2).collect();
66 let total: i32 = widths.iter().sum();
67 let mut x = rect.x + (rect.w - total) / 2;
68 for (col, (label, w)) in labels.iter().zip(&widths).enumerate() {
69 let (fg, cell_bg) = if grid.focus == (r, col) {
70 (bg, c(EngineColor::Focus))
71 } else {
72 (c(EngineColor::Text), bg)
73 };
74 buf.print(x, y, &format!(" {label} "), fg, cell_bg);
75 x += w;
76 }
77 }
78}