summaryrefslogtreecommitdiff
path: root/src/library/placed.rs
blob: 8a4f46af8f7bb018be31fb38f672005898dd59ed (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
//! Absolute placement of nodes.

use super::prelude::*;
use super::AlignNode;

/// `place`: Place content at an absolute position.
pub fn place(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
    let aligns = args.find().unwrap_or(Spec::new(Some(Align::Left), None));
    let tx = args.named("dx")?.unwrap_or_default();
    let ty = args.named("dy")?.unwrap_or_default();
    let body: PackedNode = args.expect("body")?;
    Ok(Value::block(PlacedNode(
        body.moved(Point::new(tx, ty)).aligned(aligns),
    )))
}

/// A node that places its child absolutely.
#[derive(Debug, Hash)]
pub struct PlacedNode(pub PackedNode);

impl PlacedNode {
    /// Whether this node wants to be placed relative to its its parent's base
    /// origin. instead of relative to the parent's current flow/cursor
    /// position.
    pub fn out_of_flow(&self) -> bool {
        self.0
            .downcast::<AlignNode>()
            .map_or(false, |node| node.aligns.y.is_some())
    }
}

impl Layout for PlacedNode {
    fn layout(
        &self,
        ctx: &mut LayoutContext,
        regions: &Regions,
        styles: StyleChain,
    ) -> Vec<Constrained<Rc<Frame>>> {
        let out_of_flow = self.out_of_flow();

        // The pod is the base area of the region because for absolute
        // placement we don't really care about the already used area (current).
        let pod = {
            let finite = regions.base.map(Length::is_finite);
            let expand = finite & (regions.expand | out_of_flow);
            Regions::one(regions.base, regions.base, expand)
        };

        let mut frames = self.0.layout(ctx, &pod, styles);
        let Constrained { item: frame, cts } = &mut frames[0];

        // If expansion is off, zero all sizes so that we don't take up any
        // space in our parent. Otherwise, respect the expand settings.
        let target = regions.expand.select(regions.current, Size::zero());
        Rc::make_mut(frame).resize(target, Align::LEFT_TOP);

        // Set base constraint because our pod size is base and exact
        // constraints if we needed to expand or offset.
        *cts = Constraints::new(regions.expand);
        cts.base = regions.base.map(Some);
        cts.exact = regions.current.filter(regions.expand | out_of_flow);

        frames
    }
}