layoutcss_parser/components/
row.rs

1use indoc::formatdoc;
2use crate::harmonic::get_harmonic;
3use std::collections::HashSet;
4const ROW_STYLE: &str = r#"
5  row-l{
6    display:flex;
7    flex-wrap: wrap;
8  }
9  row-l > * {
10    min-width: 0;
11  }
12"#;
13
14const ROW_NO_WRAP_STYLE: &str = r#"
15  row-l[layout~="nowrap"]  {
16      flex-wrap:nowrap;
17  }
18"#;
19
20const ROW_TWIN_WIDTH_STYLE: &str = r#"
21  row-l[layout~="twin-width"] > * {
22      flex-grow:1;
23      flex-basis:0;
24      min-width: 0;
25  }
26"#;
27
28fn row_direction_style(value: &str) -> String {
29    formatdoc!(
30        r#"
31        row-l[layout~="direction:{value}"]{{
32            flex-direction: {value};
33        }}
34        "#,
35    )
36}
37
38fn row_justify_style(value: &str) -> String {
39    formatdoc!(
40        r#"
41        row-l[layout~="justify:{value}"]{{
42            justify-content: {value};
43        }}
44        "#,
45    )
46}
47
48fn row_align_style(value: &str) -> String {
49    formatdoc!(
50        r#"
51        row-l[layout~="align:{value}"]{{
52            align-items: {value};
53        }}
54        "#,
55    )
56}
57fn row_gap_style(value: &str, harmonic: String) -> String {
58    formatdoc!(
59        r#"
60        row-l[layout~="gap:{value}"]{{
61            gap: {harmonic};
62        }}
63        "#,
64    )
65}
66
67fn row_gap_x_style(value: &str, harmonic: String) -> String {
68    formatdoc!(
69        r#"
70        row-l[layout~="gap-x:{value}"]{{
71            column-gap: {harmonic};
72        }}
73        "#,
74    )
75}
76
77fn row_gap_y_style(value: &str, harmonic: String) -> String {
78    formatdoc!(
79        r#"
80        row-l[layout~="gap-y:{value}"]{{
81            row-gap: {harmonic};
82        }}
83        "#,
84    )
85}
86
87pub fn row_css(
88    nowrap: bool,
89    twin_width: bool,
90    direction: Option<&str>,
91    justify: Option<&str>,
92    align: Option<&str>,
93    gap: Option<&str>,
94    gap_x: Option<&str>,
95    gap_y: Option<&str>,
96    harmonic_ratio: f64,
97    set: &mut HashSet<String>,
98) {
99    set.insert(ROW_STYLE.to_string());
100
101    if nowrap {
102        set.insert(ROW_NO_WRAP_STYLE.to_string());
103    }
104    if twin_width {
105        set.insert(ROW_TWIN_WIDTH_STYLE.to_string());
106    }
107    if let Some(value) = direction {
108        set.insert(row_direction_style(value));
109    }
110    if let Some(value) = justify {
111        set.insert(row_justify_style(value));
112    }
113    if let Some(value) = align {
114        set.insert(row_align_style(value));
115    }
116    if let Some(value) = gap {
117        let harmonic_value = get_harmonic(value, harmonic_ratio);
118        set.insert(row_gap_style(value, harmonic_value));
119    }
120    if let Some(ref value) = gap_x {
121        let harmonic_value = get_harmonic(value, harmonic_ratio);
122        set.insert(row_gap_x_style(value, harmonic_value));
123    }
124    if let Some(ref value) = gap_y {
125        let harmonic_value = get_harmonic(value, harmonic_ratio);
126        set.insert(row_gap_y_style(value, harmonic_value));
127    }
128}