layoutcss_parser/components/
ledge.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use indoc::formatdoc;
use crate::harmonic::get_harmonic;
use std::collections::HashSet;
const LEDGE_STYLE: &str = r#"
  ledge-l{
    display:flex;
    flex-wrap: wrap;
  }
  ledge-l > * {
    min-width: 0;
  }
"#;

const LEDGE_NO_WRAP_STYLE: &str = r#"
  ledge-l[layout~="nowrap"]  {
      flex-wrap:nowrap;
  }
"#;

const LEDGE_TWIN_WIDTH_STYLE: &str = r#"
  ledge-l[layout~="twin-width"] > * {
      flex-grow:1;
      flex-basis:0;
      min-width: 0;
  }
"#;

fn ledge_direction_style(value: &str) -> String {
    formatdoc!(
        r#"
        ledge-l[layout~="direction:{value}"]{{
            flex-direction: {value};
        }}
        "#,
    )
}

fn ledge_justify_style(value: &str) -> String {
    formatdoc!(
        r#"
        ledge-l[layout~="justify:{value}"]{{
            justify-content: {value};
        }}
        "#,
    )
}

fn ledge_align_style(value: &str) -> String {
    formatdoc!(
        r#"
        ledge-l[layout~="align:{value}"]{{
            align-items: {value};
        }}
        "#,
    )
}
fn ledge_gap_style(value: &str, harmonic: String) -> String {
    formatdoc!(
        r#"
        ledge-l[layout~="gap:{value}"]{{
            gap: {harmonic};
        }}
        "#,
    )
}

fn ledge_gap_x_style(value: &str, harmonic: String) -> String {
    formatdoc!(
        r#"
        ledge-l[layout~="gap-x:{value}"]{{
            column-gap: {harmonic};
        }}
        "#,
    )
}

fn ledge_gap_y_style(value: &str, harmonic: String) -> String {
    formatdoc!(
        r#"
        ledge-l[layout~="gap-y:{value}"]{{
            row-gap: {harmonic};
        }}
        "#,
    )
}

pub fn ledge_css(
    nowrap: bool,
    twin_width: bool,
    direction: Option<&str>,
    justify: Option<&str>,
    align: Option<&str>,
    gap: Option<&str>,
    gap_x: Option<&str>,
    gap_y: Option<&str>,
    harmonic_ratio: f64,
    set: &mut HashSet<String>,
) {
    set.insert(LEDGE_STYLE.to_string());

    if nowrap {
        set.insert(LEDGE_NO_WRAP_STYLE.to_string());
    }
    if twin_width {
        set.insert(LEDGE_TWIN_WIDTH_STYLE.to_string());
    }
    if let Some(value) = direction {
        set.insert(ledge_direction_style(value));
    }
    if let Some(value) = justify {
        set.insert(ledge_justify_style(value));
    }
    if let Some(value) = align {
        set.insert(ledge_align_style(value));
    }
    if let Some(value) = gap {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        set.insert(ledge_gap_style(value, harmonic_value));
    }
    if let Some(ref value) = gap_x {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        set.insert(ledge_gap_x_style(value, harmonic_value));
    }
    if let Some(ref value) = gap_y {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        set.insert(ledge_gap_y_style(value, harmonic_value));
    }
}