summaryrefslogtreecommitdiff
path: root/src/library/direction.rs
blob: 39ac2ccd1e54dbe31a2e21988d4f06d46ccf602c (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
use crate::func::prelude::*;
use super::maps::PosAxisMap;


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

    parse(args, body, ctx) {
        DirectionFunc {
            body: parse!(optional: body, ctx),
            map: PosAxisMap::new(&mut args)?,
        }
    }

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

        let map = self.map.dedup(ctx.axes, |direction| {
            Some(direction.axis().to_generic(ctx.axes))
        })?;

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

        if ctx.axes.primary.axis() == ctx.axes.secondary.axis() {
            error!(
                "invalid aligned primary and secondary axes: `{}`, `{}`",
                format!("{:?}", ctx.axes.primary).to_lowercase(),
                format!("{:?}", ctx.axes.secondary).to_lowercase(),
            );
        }

        match &self.body {
            Some(body) => vec![AddMultiple(layout(&body, ctx).await?)],
            None => vec![Command::SetAxes(ctx.axes)],
        }
    }
}