summaryrefslogtreecommitdiff
path: root/src/library/boxed.rs
blob: 5c727bb69ab8f57a47b94eaa76a229371e66492b (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
use crate::length::ScaleLength;
use super::*;

/// `box`: Layouts its contents into a box.
///
/// # Keyword arguments
/// - `width`: The width of the box (length of relative to parent's width).
/// - `height`: The height of the box (length of relative to parent's height).
pub async fn boxed(_: Span, mut args: TableValue, mut ctx: LayoutContext<'_>) -> Pass<Value> {
    let mut f = Feedback::new();

    let content = args.take::<SyntaxTree>().unwrap_or(SyntaxTree::new());

    ctx.base = ctx.spaces[0].size;
    ctx.spaces.truncate(1);
    ctx.repeat = false;

    if let Some(w) = args.take_key::<ScaleLength>("width", &mut f) {
        let length = w.raw_scaled(ctx.base.x);
        ctx.base.x = length;
        ctx.spaces[0].size.x = length;
        ctx.spaces[0].expansion.horizontal = true;
    }

    if let Some(h) = args.take_key::<ScaleLength>("height", &mut f) {
        let length = h.raw_scaled(ctx.base.y);
        ctx.base.y = length;
        ctx.spaces[0].size.y = length;
        ctx.spaces[0].expansion.vertical = true;
    }

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

    args.unexpected(&mut f);
    Pass::commands(vec![Add(layout)], f)
}