use crate::Palette;
use egui::Ui;
use makeover_geometry::{Compactness, Step, Surface};
use makeover_layout::{CellPart, Sort};
pub type Sizing<'a> = makeover_layout::Sizing<'a, f32>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TableStyle {
pub compactness: Compactness,
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::for_compactness(Surface::web(), Compactness::Comfortable)
}
}
impl TableStyle {
#[must_use]
pub fn for_compactness(surface: Surface, compactness: Compactness) -> Self {
Self {
compactness,
header_height: surface.resolve(Step::Vast.ratio()),
row_height: surface.resolve(compactness.row_block()),
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 {
let Some(part) = part else {
return palette.content;
};
match part.intent().as_bytes() {
b"content-secondary" => palette.content_secondary,
b"content-muted" => palette.content_muted,
_ => 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 crate::tests::palette;
#[test]
fn a_cell_part_takes_the_colour_its_intent_names() {
let p = palette();
for part in [
CellPart::Value,
CellPart::Tokens,
CellPart::Actions,
CellPart::Link,
] {
let want = match part.intent() {
"content" => p.content,
"content-secondary" => p.content_secondary,
"content-muted" => p.content_muted,
other => panic!("{part:?} names {other}, which this renderer has not learned"),
};
assert_eq!(part_color(Some(part), &p), want, "{part:?}");
}
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_compact_table_packs_rows_at_the_dense_step() {
let style = TableStyle::for_compactness(Surface::web(), Compactness::Compact);
assert_eq!(style.compactness, Compactness::Compact);
assert!((style.row_height - 24.0).abs() < f32::EPSILON);
}
#[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]
);
}
}