diff options
Diffstat (limited to 'src/layout/fixed.rs')
| -rw-r--r-- | src/layout/fixed.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/layout/fixed.rs b/src/layout/fixed.rs new file mode 100644 index 00000000..0d438879 --- /dev/null +++ b/src/layout/fixed.rs @@ -0,0 +1,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) + } +} |
