layoutcss_parser/components/
stack.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
use indoc::formatdoc;
use crate::harmonic::get_harmonic;

use std::collections::HashSet;
const STACK_STYLE: &str = r#"
  stack-l{
    display: block;
  }

  stack-l > *{
    margin-block: 0;
  }
"#;

fn stack_gap_style(value: &str, harmonic: String) -> String {
    formatdoc!(
        r#"
        stack-l[layout~="gap:{value}"] > * + *:not(outsider-l[layout~="disinherit"]){{
            margin-block-start: {harmonic};
        }}
        "#,
    )
}

fn stack_recursive_style(harmonic: String) -> String {
    formatdoc!(
        r#"
        stack-l[layout~="recursive"] * + *:not(outsider-l[layout~="disinherit"]){{
            margin-block-start: {harmonic};
        }}
        "#,
    )
}

pub fn stack_css(
    gap: Option<&str>,
    recursive: bool,
    harmonic_ratio: f64,
    set: &mut HashSet<String>,
) {
    set.insert(STACK_STYLE.to_string());
    if let Some(value) = gap {
        let harmonic_value = get_harmonic(value, harmonic_ratio);
        set.insert(stack_gap_style(value, harmonic_value.clone()));
        if recursive {
            set.insert(stack_recursive_style(harmonic_value));
        }
    }
}