summaryrefslogtreecommitdiff
path: root/src/layout/pad.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/pad.rs
parent42500d5ed85539c5ab04dd3544beaf802da29be9 (diff)
Switch back to custom geometry types, unified with layout primitives 🏞
Diffstat (limited to 'src/layout/pad.rs')
-rw-r--r--src/layout/pad.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/layout/pad.rs b/src/layout/pad.rs
new file mode 100644
index 00000000..2e1817b7
--- /dev/null
+++ b/src/layout/pad.rs
@@ -0,0 +1,53 @@
+use super::*;
+use crate::geom::Linear;
+
+/// A node that pads its child at the sides.
+#[derive(Debug, Clone, PartialEq)]
+pub struct Pad {
+ pub padding: Sides<Linear>,
+ pub child: LayoutNode,
+}
+
+#[async_trait(?Send)]
+impl Layout for Pad {
+ async fn layout(
+ &self,
+ ctx: &mut LayoutContext,
+ constraints: LayoutConstraints,
+ ) -> Vec<LayoutItem> {
+ self.child
+ .layout(ctx, LayoutConstraints {
+ spaces: constraints
+ .spaces
+ .into_iter()
+ .map(|space| LayoutSpace {
+ base: space.base - self.padding.eval(space.base).size(),
+ size: space.size - self.padding.eval(space.size).size(),
+ })
+ .collect(),
+ repeat: constraints.repeat,
+ })
+ .await
+ .into_iter()
+ .map(|item| match item {
+ LayoutItem::Box(boxed, align) => {
+ let padding = self.padding.eval(boxed.size);
+ let padded = boxed.size + padding.size();
+
+ let mut outer = BoxLayout::new(padded);
+ let start = Point::new(padding.left, padding.top);
+ outer.push_layout(start, boxed);
+
+ LayoutItem::Box(outer, align)
+ }
+ item => item,
+ })
+ .collect()
+ }
+}
+
+impl From<Pad> for LayoutNode {
+ fn from(pad: Pad) -> Self {
+ Self::dynamic(pad)
+ }
+}