diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-03-10 17:42:47 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-03-10 17:42:47 +0100 |
| commit | 4e5f85aa4ac0d6b51323bb2a6e1fbd3f4f46babb (patch) | |
| tree | 87f157c2058800e510e5538b0815c947c14fe22d /src/library/pad.rs | |
| parent | b0446cbdd189a8cc3b6a7321579f4aa89415a8e0 (diff) | |
Pad function 🔲
Diffstat (limited to 'src/library/pad.rs')
| -rw-r--r-- | src/library/pad.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/library/pad.rs b/src/library/pad.rs new file mode 100644 index 00000000..f9e4386d --- /dev/null +++ b/src/library/pad.rs @@ -0,0 +1,38 @@ +use super::*; + +/// `pad`: Pad content at the sides. +/// +/// # Positional arguments +/// - Padding for all sides: `padding`, of type `linear` relative to sides. +/// - Body: of type `template`. +/// +/// # Named arguments +/// - Left padding: `left`, of type `linear` relative to parent width. +/// - Right padding: `right`, of type `linear` relative to parent width. +/// - Top padding: `top`, of type `linear` relative to parent height. +/// - Bottom padding: `bottom`, of type `linear` relative to parent height. +pub fn pad(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value { + let all = args.find(ctx); + let left = args.get(ctx, "left"); + let top = args.get(ctx, "top"); + let right = args.get(ctx, "right"); + let bottom = args.get(ctx, "bottom"); + let body = args.require::<ValueTemplate>(ctx, "body").unwrap_or_default(); + + let padding = Sides::new( + left.or(all).unwrap_or_default(), + top.or(all).unwrap_or_default(), + right.or(all).unwrap_or_default(), + bottom.or(all).unwrap_or_default(), + ); + + Value::template("pad", move |ctx| { + let snapshot = ctx.state.clone(); + + let expand = Spec::uniform(Expansion::Fit); + let child = ctx.exec_body(&body, expand); + ctx.push(NodePad { padding, child }); + + ctx.state = snapshot; + }) +} |
