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

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

/// Place a node at an absolute position.
#[derive(Debug, Hash)]
pub struct PlaceNode(pub LayoutNode);

#[class]
impl PlaceNode {
    fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> {
        let aligns = args.find()?.unwrap_or(Spec::with_x(Some(Align::Left)));
        let tx = args.named("dx")?.unwrap_or_default();
        let ty = args.named("dy")?.unwrap_or_default();
        let body: LayoutNode = args.expect("body")?;
        Ok(Template::block(Self(
            body.moved(Point::new(tx, ty)).aligned(aligns),
        )))
    }
}

impl Layout for PlaceNode {
    fn layout(
        &self,
        vm: &mut Vm,
        regions: &Regions,
        styles: StyleChain,
    ) -> TypResult<Vec<Arc<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.
        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(vm, &pod, styles)?;

        // 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 frame = &mut frames[0];
        let target = regions.expand.select(regions.first, Size::zero());
        Arc::make_mut(frame).resize(target, Align::LEFT_TOP);

        Ok(frames)
    }
}

impl PlaceNode {
    /// 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())
    }
}