layoutcss_parser/components/
icon.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
use indoc::formatdoc;
use std::collections::HashSet;

use crate::harmonic::get_harmonic;

const ICON_STYLE: &str = r#"
icon-l{
    display: inline-block;
    width: fit-content;
    vertical-align: middle;
}

icon-l > :nth-child(1) {
    height: 0.75em;
    width: auto;
}
"#;

fn icon_scale_style(value: &str, harmonic: f64) -> String {
    formatdoc!(
        r#"
        icon-l[layout*="scale:{value}"] >:nth-child(1){{
            height: {harmonic:.2}rem;
        }}
        "#,
    )
}

fn icon_align_style(value: &str) -> String {
    formatdoc!(
        r#"
        icon-l[layout~="align:{value}"]{{
            vertical-align: {value};
        }}
        "#,
    )
}

fn icon_group_style(
    value: &str,
    gap_dir_selector: &str,
    gap_dir: &str,
    opposite_dir: &str,
    harmonic: f64,
) -> String {
    formatdoc!(
        r#"
        icon-l{gap_dir_selector}[layout*="gap:{value}"] >:nth-child(1){{
            margin-inline-{gap_dir}: {harmonic:.2}rem;
            margin-inline-{opposite_dir}: initial;
        }}
        "#,
    )
}

pub fn icon_css(
    scale: Option<&str>,
    align: Option<&str>,
    gap_dir: Option<&str>,
    gap: Option<&str>,
    harmonic_ratio: f64,
    set: &mut HashSet<String>,
) {
    set.insert(ICON_STYLE.to_string());
    if let Some(value) = scale {
        let harmonic_value = get_harmonic(&value, harmonic_ratio);
        set.insert(icon_scale_style(value, harmonic_value));
    }
    if let Some(value) = align {
        set.insert(icon_align_style(value));
    }
    if let Some(value) = gap {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        let gap_dir = match gap_dir {
            Some("end") => "end",
            _ => "start",
        };
        let gap_dir_selector = if gap_dir == "end" {
            r#"[layout*="gap-dir:end"]"#
        } else {
            ""
        };
        let opposite_dir = if gap_dir == "end" { "start" } else { "end" };
        set.insert(icon_group_style(
            value,
            gap_dir_selector,
            gap_dir,
            opposite_dir,
            harmonic_value,
        ));
    }
}