#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum Direction {
#[default]
Row,
Column,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum Justify {
#[default]
Start,
Center,
End,
SpaceBetween,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum Align {
Start,
Center,
End,
#[default]
Stretch,
}
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum Dimension {
#[default]
Auto,
Cells(i32),
Percent(f32),
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Edges {
pub left: i32,
pub right: i32,
pub top: i32,
pub bottom: i32,
}
impl Edges {
pub const ZERO: Edges = Edges {
left: 0,
right: 0,
top: 0,
bottom: 0,
};
pub const fn all(n: i32) -> Edges {
Edges {
left: n,
right: n,
top: n,
bottom: n,
}
}
pub const fn hv(horizontal: i32, vertical: i32) -> Edges {
Edges {
left: horizontal,
right: horizontal,
top: vertical,
bottom: vertical,
}
}
pub const fn horizontal(self) -> i32 {
self.left + self.right
}
pub const fn vertical(self) -> i32 {
self.top + self.bottom
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum Position {
#[default]
Flow,
Absolute,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum Overflow {
#[default]
Visible,
Clip,
Scroll,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Track {
Cells(i32),
Percent(f32),
Auto,
Fr(f32),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Display {
#[default]
Flex,
Grid { cols: Vec<Track>, rows: Vec<Track> },
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Inset {
pub left: Option<i32>,
pub right: Option<i32>,
pub top: Option<i32>,
pub bottom: Option<i32>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Style {
pub display: Display,
pub direction: Direction,
pub justify: Justify,
pub align_items: Align,
pub align_self: Option<Align>,
pub wrap: bool,
pub gap: i32,
pub cross_gap: i32,
pub col_span: i32,
pub row_span: i32,
pub padding: Edges,
pub margin: Edges,
pub width: Dimension,
pub height: Dimension,
pub min_width: Option<i32>,
pub max_width: Option<i32>,
pub min_height: Option<i32>,
pub max_height: Option<i32>,
pub grow: f32,
pub shrink: f32,
pub basis: Dimension,
pub position: Position,
pub inset: Inset,
pub overflow: Overflow,
}
impl Default for Style {
fn default() -> Self {
Style {
display: Display::Flex,
direction: Direction::Row,
justify: Justify::Start,
align_items: Align::Stretch,
align_self: None,
wrap: false,
gap: 0,
cross_gap: 0,
col_span: 1,
row_span: 1,
padding: Edges::ZERO,
margin: Edges::ZERO,
width: Dimension::Auto,
height: Dimension::Auto,
min_width: None,
max_width: None,
min_height: None,
max_height: None,
grow: 0.0,
shrink: 1.0,
basis: Dimension::Auto,
position: Position::Flow,
inset: Inset::default(),
overflow: Overflow::Visible,
}
}
}
impl Style {
pub fn row() -> Style {
Style {
direction: Direction::Row,
..Style::default()
}
}
pub fn fill() -> Style {
Style {
width: Dimension::Percent(1.0),
height: Dimension::Percent(1.0),
..Style::default()
}
}
pub fn line(rows: i32) -> Style {
Style {
width: Dimension::Percent(1.0),
height: Dimension::Cells(rows.max(0)),
..Style::default()
}
}
pub fn column() -> Style {
Style {
direction: Direction::Column,
..Style::default()
}
}
pub fn justify(mut self, j: Justify) -> Style {
self.justify = j;
self
}
pub fn align_items(mut self, a: Align) -> Style {
self.align_items = a;
self
}
pub fn align_self(mut self, a: Align) -> Style {
self.align_self = Some(a);
self
}
pub fn gap(mut self, g: i32) -> Style {
self.gap = g;
self
}
pub fn padding(mut self, p: Edges) -> Style {
self.padding = p;
self
}
pub fn margin(mut self, m: Edges) -> Style {
self.margin = m;
self
}
pub fn width(mut self, w: Dimension) -> Style {
self.width = w;
self
}
pub fn height(mut self, h: Dimension) -> Style {
self.height = h;
self
}
pub fn w(self, cells: i32) -> Style {
self.width(Dimension::Cells(cells))
}
pub fn h(self, cells: i32) -> Style {
self.height(Dimension::Cells(cells))
}
pub fn min_w(mut self, cells: i32) -> Style {
self.min_width = Some(cells);
self
}
pub fn max_w(mut self, cells: i32) -> Style {
self.max_width = Some(cells);
self
}
pub fn min_h(mut self, cells: i32) -> Style {
self.min_height = Some(cells);
self
}
pub fn max_h(mut self, cells: i32) -> Style {
self.max_height = Some(cells);
self
}
pub fn grow(mut self, g: f32) -> Style {
self.grow = g;
self
}
pub fn shrink(mut self, s: f32) -> Style {
self.shrink = s;
self
}
pub fn basis(mut self, b: Dimension) -> Style {
self.basis = b;
self
}
pub fn absolute(mut self, inset: Inset) -> Style {
self.position = Position::Absolute;
self.inset = inset;
self
}
pub fn clip(mut self) -> Style {
self.overflow = Overflow::Clip;
self
}
pub fn scroll(mut self) -> Style {
self.overflow = Overflow::Scroll;
self
}
pub fn clips_children(&self) -> bool {
matches!(self.overflow, Overflow::Clip | Overflow::Scroll)
}
pub fn grid(mut self, cols: Vec<Track>, rows: Vec<Track>) -> Style {
self.display = Display::Grid { cols, rows };
self
}
pub fn wrap(mut self) -> Style {
self.wrap = true;
self
}
pub fn cross_gap(mut self, gap: i32) -> Style {
self.cross_gap = gap;
self
}
pub fn col_span(mut self, n: i32) -> Style {
self.col_span = n.max(1);
self
}
pub fn row_span(mut self, n: i32) -> Style {
self.row_span = n.max(1);
self
}
}