layoutcss_parser/components/
switcher.rs

1use crate::harmonic::get_harmonic;
2use indoc::formatdoc;
3
4use std::collections::HashSet;
5const SWITCHER_STYLE: &str = r#"
6 switcher-l{
7    display: flex;
8    flex-wrap: wrap;
9  }
10
11  switcher-l > *:not(outsider-l[layout~="disinherit"]){
12      flex-grow: 1;
13  }
14"#;
15
16const SWITCHER_REVERSE_STYLE: &str = r#"
17  switcher-l[layout~="reverse"]{
18    flex-wrap: wrap-reverse;
19  }
20"#;
21
22fn switcher_threshold_style(value: &str) -> String {
23    formatdoc!(
24        r#"
25        switcher-l[layout~="threshold:{value}"] > *:not(outsider-l[layout~="disinherit"]) {{
26            flex-basis: calc(({value} - 100%) * 999);
27        }}
28        "#,
29    )
30}
31
32fn switcher_limit_style(value: &str) -> String {
33    formatdoc!(
34        r#"
35        switcher-l[layout~="limit:{value}"] > :nth-last-child(n+{value}):not(outsider-l[layout~="disinherit"]),
36        switcher-l[layout~="limit:{value}"] > :nth-last-child(n+{value}) ~ *:not(outsider-l[layout~="disinherit"]){{
37            flex-basis: 100%;
38        }}
39        "#,
40    )
41}
42
43fn switcher_gap_style(value: &str, harmonic: String) -> String {
44    formatdoc!(
45        r#"
46        switcher-l[layout~="gap:{value}"]{{
47            gap: {harmonic};
48        }}
49        "#,
50    )
51}
52
53fn switcher_gap_x_style(value: &str, harmonic: String) -> String {
54    formatdoc!(
55        r#"
56        switcher-l[layout~="gap-x:{value}"]{{
57            column-gap: {harmonic};
58        }}
59        "#,
60    )
61}
62
63fn switcher_gap_y_style(value: &str, harmonic: String) -> String {
64    formatdoc!(
65        r#"
66        switcher-l[layout~="gap-y:{value}"]{{
67            row-gap: {harmonic};
68        }}
69        "#,
70    )
71}
72
73pub fn switcher_css(
74    threshold: Option<&str>,
75    limit: Option<&str>,
76    reverse: bool,
77    gap: Option<&str>,
78    gap_x: Option<&str>,
79    gap_y: Option<&str>,
80    harmonic_ratio: f64,
81    set: &mut HashSet<String>,
82) {
83    set.insert(SWITCHER_STYLE.to_string());
84    if let Some(value) = threshold {
85        set.insert(switcher_threshold_style(value));
86    }
87    if let Some(value) = limit {
88        set.insert(switcher_limit_style(value));
89    }
90    if reverse {
91        set.insert(SWITCHER_REVERSE_STYLE.to_string());
92    }
93    if let Some(value) = gap {
94        let harmonic_value = get_harmonic(value, harmonic_ratio);
95        set.insert(switcher_gap_style(value, harmonic_value));
96    }
97    if let Some(ref value) = gap_x {
98        let harmonic_value = get_harmonic(value, harmonic_ratio);
99        set.insert(switcher_gap_x_style(value, harmonic_value));
100    }
101    if let Some(ref value) = gap_y {
102        let harmonic_value = get_harmonic(value, harmonic_ratio);
103        set.insert(switcher_gap_y_style(value, harmonic_value));
104    }
105}