use crate::{
components::{
Skeleton, SkeletonLength,
table::{TableSize, row_height},
},
density,
theme::color,
tokens::{ColorRole, REM_PX},
};
use gpui_kit::{App, IntoElement, ParentElement, RenderOnce, Styled, Window, div, px, rems};
pub const BAR: (f32, f32) = (4., 1.);
pub const CELL_PADDING: f32 = 1.;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TableSkeletonLook {
pub header: gpui_kit::Hsla,
pub row: gpui_kit::Hsla,
pub rule: gpui_kit::Hsla,
pub header_bar: gpui_kit::Hsla,
}
impl TableSkeletonLook {
pub fn current() -> Self {
Self {
header: color(ColorRole::LayerAccent01),
row: color(ColorRole::Layer01),
rule: color(ColorRole::BorderSubtle01),
header_bar: color(ColorRole::SkeletonElement),
}
}
}
#[derive(IntoElement)]
pub struct DataTableSkeleton {
columns: usize,
rows: usize,
header: bool,
}
impl DataTableSkeleton {
pub fn new(columns: usize, rows: usize) -> Self {
Self {
columns: columns.max(1),
rows,
header: true,
}
}
pub fn header(mut self, header: bool) -> Self {
self.header = header;
self
}
}
impl RenderOnce for DataTableSkeleton {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let look = TableSkeletonLook::current();
let height = px(row_height(TableSize::Lg, density::current()));
let bar = |head: bool| {
if head {
div()
.w(rems(BAR.0))
.h(rems(BAR.1))
.bg(look.header_bar)
.into_any_element()
} else {
Skeleton::block()
.width(SkeletonLength::Rem(BAR.0))
.height(SkeletonLength::Px(BAR.1 * REM_PX))
.into_any_element()
}
};
let row = |head: bool| {
div()
.flex()
.flex_row()
.flex_none()
.w_full()
.h(height)
.bg(if head { look.header } else { look.row })
.border_b_1()
.border_color(look.rule)
.children((0..self.columns).map(|_| {
div()
.flex()
.flex_1()
.min_w_0()
.items_center()
.pl(rems(CELL_PADDING))
.overflow_hidden()
.child(bar(head))
}))
};
div()
.flex()
.flex_col()
.w_full()
.overflow_hidden()
.children(self.header.then(|| row(true)))
.children((0..self.rows).map(|_| row(false)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn geometry_matches_the_css() {
assert_eq!(BAR.0 * REM_PX, 64.);
assert_eq!(BAR.1 * REM_PX, 16.);
assert_eq!(CELL_PADDING * REM_PX, 16.);
assert_eq!(
row_height(TableSize::Lg, density::Density::Comfortable),
48.
);
}
#[test]
fn colors_come_from_the_css_roles() {
let look = TableSkeletonLook::current();
assert_eq!(look.header, color(ColorRole::LayerAccent01));
assert_eq!(look.row, color(ColorRole::Layer01));
assert_eq!(look.rule, color(ColorRole::BorderSubtle01));
assert_eq!(look.header_bar, color(ColorRole::SkeletonElement));
}
}