use crate::{
impl_into_node,
layout::{
Alignment,
grid::{GridSpec, GridTrack},
},
node::{Element, SpacingValue},
};
impl_into_node!(Grid);
impl Grid {
pub fn new() -> Self {
Self::from_node(crate::node::Node::grid(GridSpec::default(), vec![]))
}
pub fn with_columns(count: usize) -> Self {
Self::new().columns(vec![GridTrack::Fr(1.0); count])
}
pub fn child(mut self, child: impl Into<Element>) -> Self {
self.update_node(|node| node.push_child_element(child.into()));
self
}
pub fn with_children(mut self, children: impl IntoIterator<Item = impl Into<Element>>) -> Self {
self.update_node(|node| node.extend_child_elements(children.into_iter().map(Into::into)));
self
}
pub fn columns(mut self, columns: impl IntoIterator<Item = impl Into<GridTrack>>) -> Self {
let columns = columns.into_iter().map(Into::into).collect();
self.update_node(|node| node.update_grid(|spec| spec.columns = columns));
self
}
pub fn rows(mut self, rows: impl IntoIterator<Item = impl Into<GridTrack>>) -> Self {
let rows = rows.into_iter().map(Into::into).collect();
self.update_node(|node| node.update_grid(|spec| spec.rows = rows));
self
}
pub fn gap(mut self, gap: impl Into<SpacingValue>) -> Self {
let gap = gap.into();
self.update_node(|node| {
node.update_grid(|spec| {
spec.column_gap = gap;
spec.row_gap = gap;
})
});
self
}
pub fn column_gap(mut self, gap: impl Into<SpacingValue>) -> Self {
let gap = gap.into();
self.update_node(|node| node.update_grid(|spec| spec.column_gap = gap));
self
}
pub fn row_gap(mut self, gap: impl Into<SpacingValue>) -> Self {
let gap = gap.into();
self.update_node(|node| node.update_grid(|spec| spec.row_gap = gap));
self
}
pub fn align_items(mut self, align: Alignment) -> Self {
self.update_node(|node| node.update_grid(|spec| spec.align = align));
self
}
pub fn justify_items(mut self, justify: Alignment) -> Self {
self.update_node(|node| node.update_grid(|spec| spec.justify = justify));
self
}
}
impl Default for Grid {
fn default() -> Self {
Self::new()
}
}