diff --git a/src/eval/template.rs b/src/eval/template.rs index 82a069f93..0604cc05e 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -149,12 +149,15 @@ impl Template { Self(Rc::new(vec![TemplateNode::Decorated(deco, self)])) } - /// Build the flow node resulting from instantiating the template with the - /// given style. - pub fn to_flow(&self, style: &Style) -> FlowNode { - let mut builder = Builder::new(style, false); - builder.template(self); - builder.build_flow() + /// Pack the template into a layout node. + pub fn pack(&self, style: &Style) -> PackedNode { + if let [TemplateNode::Block(f) | TemplateNode::Inline(f)] = self.0.as_slice() { + f(style) + } else { + let mut builder = Builder::new(style, false); + builder.template(self); + builder.build_flow().pack() + } } /// Build the layout tree resulting from instantiating the template with the diff --git a/src/eval/walk.rs b/src/eval/walk.rs index 134f10c70..fe7f0e984 100644 --- a/src/eval/walk.rs +++ b/src/eval/walk.rs @@ -124,7 +124,7 @@ impl Walk for EnumNode { fn walk_item(ctx: &mut EvalContext, label: EcoString, body: Template) { ctx.template += Template::from_block(move |style| { - let label = ParNode { + let label = Layout::pack(ParNode { dir: style.par.dir, leading: style.leading(), children: vec![ParChild::Text( @@ -132,13 +132,13 @@ fn walk_item(ctx: &mut EvalContext, label: EcoString, body: Template) { style.aligns.inline, Rc::clone(&style.text), )], - }; + }); let spacing = style.text.size / 2.0; GridNode { tracks: Spec::new(vec![TrackSizing::Auto; 2], vec![]), gutter: Spec::new(vec![TrackSizing::Linear(spacing.into())], vec![]), - children: vec![label.pack(), body.to_flow(style).pack()], + children: vec![label, body.pack(style)], } }); } diff --git a/src/library/flow.rs b/src/library/flow.rs index c41fb62c6..25162d198 100644 --- a/src/library/flow.rs +++ b/src/library/flow.rs @@ -27,9 +27,7 @@ pub fn flow(_: &mut EvalContext, args: &mut Args) -> TypResult { .iter() .map(|child| match child { Child::Spacing(spacing) => FlowChild::Spacing(*spacing), - Child::Any(child) => { - FlowChild::Node(child.to_flow(style).pack(), style.aligns.block) - } + Child::Any(node) => FlowChild::Node(node.pack(style), style.aligns.block), }) .collect(); diff --git a/src/library/grid.rs b/src/library/grid.rs index 692ed210f..62c10e5a1 100644 --- a/src/library/grid.rs +++ b/src/library/grid.rs @@ -45,7 +45,7 @@ pub fn grid(_: &mut EvalContext, args: &mut Args) -> TypResult { GridNode { tracks: tracks.clone(), gutter: gutter.clone(), - children: children.iter().map(|child| child.to_flow(style).pack()).collect(), + children: children.iter().map(|child| child.pack(style)).collect(), } }))) } diff --git a/src/library/pad.rs b/src/library/pad.rs index 88f8c5622..1ec1b4a23 100644 --- a/src/library/pad.rs +++ b/src/library/pad.rs @@ -17,10 +17,7 @@ pub fn pad(_: &mut EvalContext, args: &mut Args) -> TypResult { ); Ok(Value::Template(Template::from_inline(move |style| { - PadNode { - padding, - child: body.to_flow(style).pack(), - } + PadNode { padding, child: body.pack(style) } }))) } diff --git a/src/library/shape.rs b/src/library/shape.rs index 112987ad1..ecf21dc97 100644 --- a/src/library/shape.rs +++ b/src/library/shape.rs @@ -56,21 +56,11 @@ pub fn circle(_: &mut EvalContext, args: &mut Args) -> TypResult { fn shape_impl( kind: ShapeKind, - mut width: Option, - mut height: Option, + width: Option, + height: Option, fill: Option, body: Option