use crate::element::Element;
use crate::style::{Align, Color, Common};
#[derive(Clone, Copy, Debug)]
pub enum ColumnWidth {
Fixed(f32),
Flex(f32),
}
#[derive(Clone, Copy, Debug)]
pub struct TableColumn {
pub width: ColumnWidth,
pub align: Align,
}
impl TableColumn {
pub fn fixed(width: f32) -> Self {
TableColumn {
width: ColumnWidth::Fixed(width),
align: Align::Start,
}
}
pub fn flex(weight: f32) -> Self {
TableColumn {
width: ColumnWidth::Flex(weight),
align: Align::Start,
}
}
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
}
#[derive(Clone, Debug, Default)]
pub struct Table {
pub columns: Vec<TableColumn>,
pub header: Option<Vec<Element>>,
pub rows: Vec<Vec<Element>>,
pub striped: Option<Color>,
pub cell_padding: f32,
pub row_offset: usize,
pub common: Common,
}
impl Table {
pub fn new() -> Self {
Table {
cell_padding: 4.0,
..Default::default()
}
}
pub fn columns(mut self, columns: impl IntoIterator<Item = TableColumn>) -> Self {
self.columns = columns.into_iter().collect();
self
}
pub fn header(mut self, cells: impl IntoIterator<Item = impl Into<Element>>) -> Self {
self.header = Some(cells.into_iter().map(Into::into).collect());
self
}
pub fn rows(mut self, rows: impl IntoIterator<Item = impl IntoIterator<Item = impl Into<Element>>>) -> Self {
self.rows = rows.into_iter().map(|row| row.into_iter().map(Into::into).collect()).collect();
self
}
pub fn striped(mut self, color: Color) -> Self {
self.striped = Some(color);
self
}
pub fn cell_padding(mut self, padding: f32) -> Self {
self.cell_padding = padding;
self
}
pub fn width(mut self, width: f32) -> Self {
self.common.width = Some(width);
self
}
pub fn height(mut self, height: f32) -> Self {
self.common.height = Some(height);
self
}
pub fn flex(mut self, factor: f32) -> Self {
self.common.flex = Some(factor);
self
}
pub fn keep_with_next(mut self) -> Self {
self.common.keep_with_next = true;
self
}
}