summaryrefslogtreecommitdiff
path: root/src/layout/fixed.rs
blob: 0d438879b5e64b446434e951f40dec3defd35e4b (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
use super::*;
use crate::geom::Linear;

/// A node that can fix its child's width and height.
#[derive(Debug, Clone, PartialEq)]
pub struct Fixed {
    pub width: Option<Linear>,
    pub height: Option<Linear>,
    pub child: LayoutNode,
}

#[async_trait(?Send)]
impl Layout for Fixed {
    async fn layout(
        &self,
        ctx: &mut LayoutContext,
        constraints: LayoutConstraints,
    ) -> Vec<LayoutItem> {
        let space = constraints.spaces[0];
        let size = Size::new(
            self.width
                .map(|w| w.eval(space.base.width))
                .unwrap_or(space.size.width),
            self.height
                .map(|h| h.eval(space.base.height))
                .unwrap_or(space.size.height),
        );

        self.child
            .layout(ctx, LayoutConstraints {
                spaces: vec![LayoutSpace { base: size, size }],
                repeat: false,
            })
            .await
    }
}

impl From<Fixed> for LayoutNode {
    fn from(fixed: Fixed) -> Self {
        Self::dynamic(fixed)
    }
}