layoutcss_parser/components/
sidebar.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use indoc::formatdoc;
use crate::harmonic::get_harmonic;

use std::collections::HashSet;
const SIDEBAR_STYLE: &str = r#"
  sidebar-l{
    display: flex;
    flex-wrap: wrap;
  }
"#;

const SIDEBAR_REVERSE_STYLE: &str = r#"
  sidebar-l[layout~=reverse]{
    flex-wrap: wrap-reverse;
  }
"#;

fn sidebar_shrink_style(reverse: bool) -> String {
    formatdoc!(
        r#"
        sidebar-l[layout~=shrink]{{
            align-items: flex-{};
        }}
        "#,
        if reverse { "end" } else { "start" }
    )
}

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

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

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

fn sidebar_group_style(
    // they are String instead of &str
    // because they have already been replaced
    side_selector: String,
    side_width_selector: String,
    content_min_selector: String,
    selector_one: &str,
    selector_two: &str,
    side_width: &str,
    content_min: &str,
) -> String {
    formatdoc!(
        r#"
        sidebar-l{side_selector}{side_width_selector}{content_min_selector} > {selector_one}:not(outsider-l[layout~="disinherit"]) {{
              flex-basis: {side_width};
              flex-grow: 1;
              min-inline-size: initial;
              min-width:0;
              min-height:0;
        }}

        sidebar-l{side_selector}{side_width_selector}{content_min_selector} > {selector_two}:not(outsider-l[layout~="disinherit"]) {{
                flex-basis: 0;
                flex-grow: 999;
                min-inline-size: {content_min};
        }}
        "#,
    )
}

pub fn sidebar_css(
    reverse: bool,
    shrink: bool,
    side: Option<&str>,
    side_width: Option<&str>,
    content_min: Option<&str>,
    gap: Option<&str>,
    gap_x: Option<&str>,
    gap_y: Option<&str>,
    harmonic_ratio: f64,
    set: &mut HashSet<String>,
) {
    set.insert(SIDEBAR_STYLE.to_string());
    if reverse {
        set.insert(SIDEBAR_REVERSE_STYLE.to_string());
    }
    if shrink {
        set.insert(sidebar_shrink_style(reverse));
    }
    if let Some(value) = gap {
        let harmonic_value = get_harmonic(&value, harmonic_ratio);
        set.insert(sidebar_gap_style(value, harmonic_value));
    }
    if let Some(ref value) = gap_x {
        let harmonic_value = get_harmonic(&value, harmonic_ratio);
        set.insert(sidebar_gap_x_style(value, harmonic_value));
    }
    if let Some(ref value) = gap_y {
        let harmonic_value = get_harmonic(&value, harmonic_ratio);
        set.insert(sidebar_gap_y_style(value, harmonic_value));
    }
    if side.is_some() || side_width.is_some() || content_min.is_some() {
        let side_width_selector = if let Some(ref val) = side_width {
            formatdoc!(r#"[layout*="side-width:{}"]"#, val)
        } else {
            "".to_string()
        };
        let content_min_selector = if let Some(ref val) = content_min {
            formatdoc!(r#"[layout*="content-min:{}"]"#, val)
        } else {
            "".to_string()
        };
        let side_selector = if let Some(ref val) = side {
            formatdoc!(r#"[layout*="side:{}"]"#, val)
        } else {
            "".to_string()
        };

        let selector_one = match side {
            None => ":first-child",
            Some(val) if val == "left" => ":first-child",
            _ => ":last-child",
        };
        let selector_two = if selector_one == ":first-child" {
            ":last-child"
        } else {
            ":first-child"
        };
        let content_min = if let Some(val) = content_min {
            val
        } else {
            "50%"
        };
        let side_width = if let Some(val) = side_width {
            val
        } else {
            "auto"
        };

        set.insert(sidebar_group_style(
            side_selector,
            side_width_selector,
            content_min_selector,
            selector_one,
            selector_two,
            side_width,
            content_min,
        ));
    }
}