summaryrefslogtreecommitdiff
path: root/src/layout/fixed.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-10 22:19:36 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-10 22:19:36 +0200
commit92c01da36016e94ff20163806ddcbcf7e33d4031 (patch)
tree1a900b3c11edcc93e9153fada3ce92310db5768b /src/layout/fixed.rs
parent42500d5ed85539c5ab04dd3544beaf802da29be9 (diff)
Switch back to custom geometry types, unified with layout primitives 🏞
Diffstat (limited to 'src/layout/fixed.rs')
-rw-r--r--src/layout/fixed.rs42
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)
+ }
+}