From 92c01da36016e94ff20163806ddcbcf7e33d4031 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 10 Oct 2020 22:19:36 +0200 Subject: =?UTF-8?q?Switch=20back=20to=20custom=20geometry=20types,=20unifi?= =?UTF-8?q?ed=20with=20layout=20primitives=20=F0=9F=8F=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/fixed.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/layout/fixed.rs (limited to 'src/layout/fixed.rs') 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, + pub height: Option, + pub child: LayoutNode, +} + +#[async_trait(?Send)] +impl Layout for Fixed { + async fn layout( + &self, + ctx: &mut LayoutContext, + constraints: LayoutConstraints, + ) -> Vec { + 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 for LayoutNode { + fn from(fixed: Fixed) -> Self { + Self::dynamic(fixed) + } +} -- cgit v1.2.3