summaryrefslogtreecommitdiff
path: root/src/library/layout.rs
blob: 2d0e3ac5d4781dcd101802dab528b62f2e5163d5 (plain) (blame)
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
use crate::length::ScaleLength;
use super::*;

function! {
    /// `align`: Aligns content along the layouting axes.
    #[derive(Debug, Clone, PartialEq)]
    pub struct AlignFunc {
        body: Option<SyntaxModel>,
        map: PosAxisMap<AlignmentValue>,
    }

    parse(header, body, ctx, f) {
        AlignFunc {
            body: body!(opt: body, ctx, f),
            map: PosAxisMap::parse::<AxisKey>(&mut f.diagnostics, &mut header.args),
        }
    }

    layout(self, ctx, f) {
        ctx.base = ctx.spaces[0].dimensions;

        let map = self.map.dedup(&mut f.diagnostics, ctx.axes, |alignment| {
            alignment.axis().map(|s| s.to_generic(ctx.axes))
        });

        for &axis in &[Primary, Secondary] {
            if let Some(Spanned { v: alignment, span }) = map.get_spanned(axis) {
                if let Some(generic) = alignment.to_generic(ctx.axes, axis) {
                    *ctx.alignment.get_mut(axis) = generic;
                } else {
                    error!(
                        @f, span,
                        "invalid alignment `{}` for {} axis", alignment, axis,
                    );
                }
            }
        }

        match &self.body {
            Some(body) => {
                let layouted = layout(body, ctx).await;
                f.extend(layouted.feedback);
                vec![AddMultiple(layouted.output)]
            }
            None => vec![SetAlignment(ctx.alignment)],
        }
    }
}

function! {
    /// `direction`: Sets the directions of the layouting axes.
    #[derive(Debug, Clone, PartialEq)]
    pub struct DirectionFunc {
        name_span: Span,
        body: Option<SyntaxModel>,
        map: PosAxisMap<Direction>,
    }

    parse(header, body, ctx, f) {
        DirectionFunc {
            name_span: header.name.span,
            body: body!(opt: body, ctx, f),
            map: PosAxisMap::parse::<AxisKey>(&mut f.diagnostics, &mut header.args),
        }
    }

    layout(self, ctx, f) {
        ctx.base = ctx.spaces[0].dimensions;

        let map = self.map.dedup(&mut f.diagnostics, ctx.axes, |direction| {
            Some(direction.axis().to_generic(ctx.axes))
        });

        let mut axes = ctx.axes;

        map.with(Primary, |&dir| axes.primary = dir);
        map.with(Secondary, |&dir| axes.secondary = dir);

        if axes.primary.axis() == axes.secondary.axis() {
            error!(
                @f, self.name_span,
                "invalid aligned primary and secondary axes: `{}`, `{}`",
                ctx.axes.primary, ctx.axes.secondary,
            );
        } else {
            ctx.axes = axes;
        }

        match &self.body {
            Some(body) => {
                let layouted = layout(body, ctx).await;
                f.extend(layouted.feedback);
                vec![AddMultiple(layouted.output)]
            }
            None => vec![SetAxes(ctx.axes)],
        }
    }
}

function! {
    /// `box`: Layouts content into a box.
    #[derive(Debug, Clone, PartialEq)]
    pub struct BoxFunc {
        body: SyntaxModel,
        extents: AxisMap<ScaleLength>,
        debug: Option<bool>,
    }

    parse(header, body, ctx, f) {
        BoxFunc {
            body: body!(opt: body, ctx, f).unwrap_or(SyntaxModel::new()),
            extents: AxisMap::parse::<ExtentKey>(&mut f.diagnostics, &mut header.args.key),
            debug: header.args.key.get::<bool>(&mut f.diagnostics, "debug"),
        }
    }

    layout(self, ctx, f) {
        ctx.repeat = false;
        ctx.spaces.truncate(1);

        if let Some(debug) = self.debug {
            ctx.debug = debug;
        }

        let map = self.extents.dedup(&mut f.diagnostics, ctx.axes);
        for &axis in &[Horizontal, Vertical] {
            if let Some(scale) = map.get(axis) {
                let length = scale.scaled(ctx.base.get(axis));
                *ctx.base.get_mut(axis) = length;
                *ctx.spaces[0].dimensions.get_mut(axis) = length;
                *ctx.spaces[0].expansion.get_mut(axis) = true;
            }
        }

        let layouted = layout(&self.body, ctx).await;
        let layout = layouted.output.into_iter().next().unwrap();
        f.extend(layouted.feedback);

        vec![Add(layout)]
    }
}