summaryrefslogtreecommitdiff
path: root/src/layout/fixed.rs
blob: 732351682db766f51831dcd445c32726093b31e5 (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
use super::*;

/// A node that can fix its child's width and height.
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct FixedNode {
    /// The fixed width, if any.
    pub width: Option<Linear>,
    /// The fixed height, if any.
    pub height: Option<Linear>,
    /// The child node whose size to fix.
    pub child: AnyNode,
}

impl Layout for FixedNode {
    fn layout(
        &self,
        ctx: &mut LayoutContext,
        regions: &Regions,
    ) -> Vec<Constrained<Rc<Frame>>> {
        let Regions { current, base, .. } = regions;
        let mut constraints = Constraints::new(regions.expand);
        constraints.set_base_using_linears(Spec::new(self.width, self.height), &regions);

        let size = Size::new(
            self.width.map_or(current.width, |w| w.resolve(base.width)),
            self.height.map_or(current.height, |h| h.resolve(base.height)),
        );

        // If one dimension was not specified, the `current` size needs to remain static.
        if self.width.is_none() {
            constraints.exact.horizontal = Some(current.width);
        }
        if self.height.is_none() {
            constraints.exact.vertical = Some(current.height);
        }

        let expand = Spec::new(self.width.is_some(), self.height.is_some());
        let regions = Regions::one(size, expand);
        let mut frames = self.child.layout(ctx, &regions);

        if let Some(frame) = frames.first_mut() {
            frame.constraints = constraints;
        }

        frames
    }
}

impl From<FixedNode> for AnyNode {
    fn from(fixed: FixedNode) -> Self {
        Self::new(fixed)
    }
}