summaryrefslogtreecommitdiff
path: root/src/layout/pad.rs
diff options
context:
space:
mode:
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)
+ }
+}