diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-04 13:48:07 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-04 13:48:07 +0200 |
| commit | 2467cd6272c13b618ad53c5dadff5b8c8e7885bf (patch) | |
| tree | 6ad13ec06a04997564efc514b40daa3fb65233e2 /src/library/boxed.rs | |
| parent | ed4fdcb0ada909f1cc3d7436334e253f0ec14d55 (diff) | |
Refactor function parsing ♻
Diffstat (limited to 'src/library/boxed.rs')
| -rw-r--r-- | src/library/boxed.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/library/boxed.rs b/src/library/boxed.rs new file mode 100644 index 00000000..909115d5 --- /dev/null +++ b/src/library/boxed.rs @@ -0,0 +1,53 @@ +use crate::length::ScaleLength; +use super::*; + +/// `box`: Layouts its contents into a box. +/// +/// # Keyword arguments +/// - `width`: The width of the box (length of relative to parent's width). +/// - `height`: The height of the box (length of relative to parent's height). +pub fn boxed(call: FuncCall, state: &ParseState) -> Pass<SyntaxNode> { + let mut f = Feedback::new(); + let mut args = call.header.args; + let node = BoxNode { + body: parse_body_maybe(call.body, state, &mut f).unwrap_or(SyntaxTree::new()), + width: args.key.get::<ScaleLength>("width", &mut f), + height: args.key.get::<ScaleLength>("height", &mut f), + }; + drain_args(args, &mut f); + Pass::node(node, f) +} + +#[derive(Debug, Clone, PartialEq)] +struct BoxNode { + body: SyntaxTree, + width: Option<ScaleLength>, + height: Option<ScaleLength>, +} + +#[async_trait(?Send)] +impl Layout for BoxNode { + async fn layout<'a>(&'a self, mut ctx: LayoutContext<'_>) -> Pass<Commands<'a>> { + ctx.spaces.truncate(1); + ctx.repeat = false; + + self.width.with(|v| { + let length = v.raw_scaled(ctx.base.x); + ctx.base.x = length; + ctx.spaces[0].size.x = length; + ctx.spaces[0].expansion.horizontal = true; + }); + + self.height.with(|v| { + let length = v.raw_scaled(ctx.base.y); + ctx.base.y = length; + ctx.spaces[0].size.y = length; + ctx.spaces[0].expansion.vertical = true; + }); + + layout(&self.body, ctx).await.map(|out| { + let layout = out.into_iter().next().unwrap(); + vec![Add(layout)] + }) + } +} |
