use crate::Palette;
use egui::Ui;
use makeover_layout::{CellPart, Sort};
pub type Sizing<'a> = makeover_layout::Sizing<'a, f32>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TableStyle {
pub header_height: f32,
pub row_height: f32,
pub code_row_height: f32,
pub edge_padding: f32,
pub ascending: &'static str,
pub descending: &'static str,
pub resizable: bool,
}
impl Default for TableStyle {
fn default() -> Self {
Self {
header_height: 32.0,
row_height: 45.0,
code_row_height: 20.0,
edge_padding: 12.0,
ascending: Sort::Ascending.glyph(),
descending: Sort::Descending.glyph(),
resizable: false,
}
}
}
#[derive(Default)]
pub struct Body<'a> {
pub rows: usize,
pub selected: Option<&'a dyn Fn(usize) -> bool>,
pub scroll_to: Option<usize>,
}
impl std::fmt::Debug for Body<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Body")
.field("rows", &self.rows)
.field("selected", &self.selected.is_some())
.field("scroll_to", &self.scroll_to)
.finish()
}
}
#[must_use]
pub const fn part_color(part: Option<CellPart>, palette: &Palette) -> egui::Color32 {
match part {
Some(CellPart::Tokens) => palette.content_muted,
Some(CellPart::Actions | CellPart::Link) => palette.action,
_ => palette.content,
}
}
pub fn cell<R>(
ui: &mut Ui,
part: Option<CellPart>,
palette: &Palette,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> R {
let restore = ui.visuals().override_text_color;
ui.visuals_mut().override_text_color = Some(part_color(part, palette));
let out = add_contents(ui);
ui.visuals_mut().override_text_color = restore;
out
}
#[cfg(test)]
mod tests {
use super::*;
use egui::Color32;
fn palette() -> Palette {
Palette {
page: Color32::from_rgb(1, 1, 1),
raised: Color32::from_rgb(2, 2, 2),
overlay: Color32::from_rgb(3, 3, 3),
well: Color32::from_rgb(4, 4, 4),
sunken: Color32::from_rgb(5, 5, 5),
bevel_light: Color32::WHITE,
bevel_dark: Color32::BLACK,
elevation: Color32::from_black_alpha(46),
content: Color32::from_rgb(6, 6, 6),
content_secondary: Color32::from_rgb(56, 56, 56),
content_muted: Color32::from_rgb(7, 7, 7),
action: Color32::from_rgb(8, 8, 8),
content_on_action: Color32::from_rgb(250, 250, 250),
danger: Color32::from_rgb(9, 9, 9),
success: Color32::from_rgb(10, 10, 10),
warning: Color32::from_rgb(11, 11, 11),
info: Color32::from_rgb(12, 12, 12),
border: Color32::from_rgb(200, 200, 200),
info_surface: Color32::from_rgb(201, 201, 201),
success_surface: Color32::from_rgb(202, 202, 202),
warning_surface: Color32::from_rgb(203, 203, 203),
danger_surface: Color32::from_rgb(204, 204, 204),
row_hover: Color32::from_rgb(206, 206, 206),
row_rule: Color32::from_rgb(207, 207, 207),
row_selected: Color32::from_rgb(208, 208, 208),
}
}
#[test]
fn the_parts_a_cell_can_be_are_coloured_apart() {
let p = palette();
assert_eq!(part_color(Some(CellPart::Value), &p), p.content);
assert_eq!(part_color(Some(CellPart::Tokens), &p), p.content_muted);
assert_eq!(part_color(Some(CellPart::Actions), &p), p.action);
assert_eq!(part_color(Some(CellPart::Link), &p), p.action);
assert_ne!(part_color(Some(CellPart::Link), &p), p.content);
assert_eq!(part_color(None, &p), p.content);
}
#[test]
fn the_carets_match_the_terminal_renderers() {
let style = TableStyle::default();
assert_eq!(style.ascending, Sort::Ascending.glyph());
assert_eq!(style.descending, Sort::Descending.glyph());
assert_eq!(style.ascending.trim(), style.ascending);
}
#[test]
fn resizing_is_off_because_the_description_has_no_word_for_it() {
assert!(!TableStyle::default().resizable);
}
#[test]
fn a_record_row_is_the_models_45_and_a_code_row_is_one_line() {
let style = TableStyle::default();
assert!((style.row_height - 45.0).abs() < f32::EPSILON);
assert!(style.code_row_height < style.row_height);
}
#[test]
fn a_body_claims_nothing_until_it_is_asked_to() {
let body = Body::default();
assert_eq!(body.rows, 0);
assert!(body.selected.is_none());
assert!(body.scroll_to.is_none());
}
#[test]
fn a_selection_is_asked_per_row_and_not_collected() {
let selected = |index: usize| index.is_multiple_of(2);
let body = Body {
rows: 4,
selected: Some(&selected),
scroll_to: None,
};
let f = body.selected.expect("a predicate was supplied");
assert_eq!(
(0..body.rows).map(f).collect::<Vec<_>>(),
vec![true, false, true, false]
);
}
}