summaryrefslogtreecommitdiff
path: root/src/library/boxed.rs
blob: 1cc4e02002971ab064cdf783479468313ad30c8e (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
use crate::func::prelude::*;

/// `box`: Layouts content into a box.
#[derive(Debug, PartialEq)]
pub struct Boxed {
    body: SyntaxTree,
    width: Option<Size>,
    height: Option<Size>,
}

function! {
    data: Boxed,

    parse(args, body, ctx) {
        let width = args.get_key_opt::<ArgSize>("width")?.map(|a| a.val);
        let height = args.get_key_opt::<ArgSize>("height")?.map(|a| a.val);
        args.done()?;

        let body = parse!(required: body, ctx);
        Ok(Boxed {
            body,
            width,
            height,
        })
    }

    layout(this, mut ctx) {
        if let Some(width) = this.width {
            ctx.spaces[0].dimensions.x = width;
        }
        if let Some(height) = this.height {
            ctx.spaces[0].dimensions.y = height;
        }

        Ok(commands![AddMultiple(layout_tree(&this.body, ctx)?)])
    }
}