elvis_core/value/
grid.rs

1use crate::value::Unit;
2
3/// config columns and rows in `Grid`
4#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
5pub enum GridAuto {
6    /// Auto Grid
7    Auto,
8    /// Fix items in every line
9    Fixed(Unit),
10    /// Inherit the style of parents
11    Inherit,
12    /// Use initial value
13    Initial,
14    /// Max content
15    MaxContent,
16    /// Min content
17    MinContent,
18    /// Use both max and min content
19    MinMax(Unit, Unit),
20    /// Use grid template
21    Plain(Vec<Unit>),
22    /// Unset the value
23    Unset,
24}
25
26impl Default for GridAuto {
27    fn default() -> GridAuto {
28        GridAuto::Unset
29    }
30}
31
32impl ToString for GridAuto {
33    fn to_string(&self) -> String {
34        match self {
35            GridAuto::Auto => "auto".to_string(),
36            GridAuto::MaxContent => "max-content".to_string(),
37            GridAuto::MinContent => "min-content".to_string(),
38            GridAuto::MinMax(c, r) => format!("minmax({}, {})", c.to_string(), r.to_string()),
39            GridAuto::Fixed(u) => u.to_string(),
40            GridAuto::Inherit => "inherit".to_string(),
41            GridAuto::Initial => "initial".to_string(),
42            GridAuto::Unset => "unset".to_string(),
43            GridAuto::Plain(x) => {
44                let mut s = "".to_string();
45                for i in x {
46                    s += &i.to_string();
47                    s += " ";
48                }
49
50                s
51            }
52        }
53    }
54}
55
56/// Manage `Grid` direction
57///
58/// ### `Row`
59///
60/// Items are placed by filling each row in turn, adding new rows as necessary.
61/// If neither row nor column is provided, row is assumed.
62///
63/// ### `Column`
64///
65/// Items are placed by filling each column in turn, adding new columns as necessary.
66///
67/// ### `Dense`
68///
69/// dense" packing algorithm attempts to fill in holes earlier in the grid, if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items.
70///
71/// If it is omitted, a "sparse" algorithm is used, where the placement algorithm only ever moves "forward" in the grid when placing items, never backtracking to fill holes. This ensures that all of the auto-placed items appear "in order", even if this leaves holes that could have been filled by later items.
72#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
73pub enum GridFlow {
74    /// Grid Column
75    Column,
76    /// Grid Row
77    Row,
78    /// Grid deesen
79    Dense,
80    /// Grid column desen
81    ColumnDense,
82    /// Grid row desen
83    RowDense,
84    /// Inherit the style of parent
85    Inherit,
86    /// Use initial grid
87    Initial,
88    /// Unset the flow
89    Unset,
90}
91
92impl Default for GridFlow {
93    fn default() -> GridFlow {
94        GridFlow::Unset
95    }
96}
97
98impl ToString for GridFlow {
99    fn to_string(&self) -> String {
100        match self {
101            GridFlow::Column => "column",
102            GridFlow::ColumnDense => "column dense",
103            GridFlow::Dense => "dense",
104            GridFlow::Inherit => "inherit",
105            GridFlow::Initial => "initial",
106            GridFlow::Row => "row",
107            GridFlow::RowDense => "row dense",
108            GridFlow::Unset => "unset",
109        }
110        .to_string()
111    }
112}
113
114/// template rule in `Grid` columns an rows
115#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
116pub enum GridTemplate {
117    /// Fit content
118    FitContent(Unit),
119    /// Inherit the style of parent
120    Inherit,
121    /// Use initial style
122    Initial,
123    /// Set min and max width
124    MinMax(Unit, Unit),
125    /// No template
126    None,
127    /// Customize grid
128    Plain(Vec<Unit>),
129    /// Repeat width
130    Repeat(i32, Unit),
131    /// Use sub grid
132    SubGrid,
133    /// Unset grid template
134    Unset,
135}
136
137impl Default for GridTemplate {
138    fn default() -> GridTemplate {
139        GridTemplate::Repeat(1, Unit::Fr(1.0))
140    }
141}
142
143impl ToString for GridTemplate {
144    fn to_string(&self) -> String {
145        match self {
146            GridTemplate::FitContent(u) => format!("fit-content({})", u.to_string()),
147            GridTemplate::Inherit => "inherit".to_string(),
148            GridTemplate::Initial => "initial".to_string(),
149            GridTemplate::MinMax(i, a) => format!("minmax({}, {})", i.to_string(), a.to_string()),
150            GridTemplate::None => "none".to_string(),
151            GridTemplate::Plain(x) => {
152                let mut s = "".to_string();
153                for i in x {
154                    s += &i.to_string();
155                    s += " ";
156                }
157
158                s
159            }
160            GridTemplate::Repeat(n, u) => format!("({}, {})", n, u.to_string()),
161            GridTemplate::SubGrid => "subgrid".to_string(),
162            GridTemplate::Unset => "unit".to_string(),
163        }
164    }
165}