From aeb036f4dc2ede271d6c0710e91fae605ecdac84 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 29 Jan 2022 22:32:30 +0100 Subject: Fix that templates don't create a scope --- src/eval/mod.rs | 9 ++++--- src/library/mod.rs | 4 +-- src/library/place.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/library/placed.rs | 67 --------------------------------------------------- 4 files changed, 75 insertions(+), 72 deletions(-) create mode 100644 src/library/place.rs delete mode 100644 src/library/placed.rs (limited to 'src') diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 05fa7eba..2fa07d49 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -187,7 +187,7 @@ fn process_nodes( ctx: &mut EvalContext, nodes: &mut impl Iterator, ) -> TypResult { - let prev = mem::take(&mut ctx.styles); + let prev_styles = mem::take(&mut ctx.styles); let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default()); while let Some(piece) = nodes.next() { // Need to deal with wrap here. @@ -201,7 +201,7 @@ fn process_nodes( seq.push(Styled::new(node, ctx.styles.clone())); } - ctx.styles = prev; + ctx.styles = prev_styles; Ok(Node::Sequence(seq)) } @@ -461,7 +461,10 @@ impl Eval for TemplateExpr { type Output = Node; fn eval(&self, ctx: &mut EvalContext) -> TypResult { - self.body().eval(ctx) + ctx.scopes.enter(); + let node = self.body().eval(ctx)?; + ctx.scopes.exit(); + Ok(node) } } diff --git a/src/library/mod.rs b/src/library/mod.rs index 81c8fcdc..44f8f947 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -17,7 +17,7 @@ pub mod list; pub mod pad; pub mod page; pub mod par; -pub mod placed; +pub mod place; pub mod shape; pub mod spacing; pub mod stack; @@ -40,7 +40,7 @@ pub use list::*; pub use pad::*; pub use page::*; pub use par::*; -pub use placed::*; +pub use place::*; pub use shape::*; pub use spacing::*; pub use stack::*; diff --git a/src/library/place.rs b/src/library/place.rs new file mode 100644 index 00000000..cee687fa --- /dev/null +++ b/src/library/place.rs @@ -0,0 +1,67 @@ +//! Absolute placement of nodes. + +use super::prelude::*; +use super::AlignNode; + +/// Place content at an absolute position. +#[derive(Debug, Hash)] +pub struct PlaceNode(pub PackedNode); + +#[class] +impl PlaceNode { + fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult { + let aligns = args.find().unwrap_or(Spec::with_x(Some(Align::Left))); + let tx = args.named("dx")?.unwrap_or_default(); + let ty = args.named("dy")?.unwrap_or_default(); + let body: PackedNode = args.expect("body")?; + Ok(Node::block(Self( + body.moved(Point::new(tx, ty)).aligned(aligns), + ))) + } +} + +impl Layout for PlaceNode { + fn layout( + &self, + ctx: &mut LayoutContext, + regions: &Regions, + styles: StyleChain, + ) -> Vec>> { + let out_of_flow = self.out_of_flow(); + + // The pod is the base area of the region because for absolute + // placement we don't really care about the already used area (current). + let pod = { + let finite = regions.base.map(Length::is_finite); + let expand = finite & (regions.expand | out_of_flow); + Regions::one(regions.base, regions.base, expand) + }; + + let mut frames = self.0.layout(ctx, &pod, styles); + let Constrained { item: frame, cts } = &mut frames[0]; + + // If expansion is off, zero all sizes so that we don't take up any + // space in our parent. Otherwise, respect the expand settings. + let target = regions.expand.select(regions.current, Size::zero()); + Rc::make_mut(frame).resize(target, Align::LEFT_TOP); + + // Set base constraint because our pod size is base and exact + // constraints if we needed to expand or offset. + *cts = Constraints::new(regions.expand); + cts.base = regions.base.map(Some); + cts.exact = regions.current.filter(regions.expand | out_of_flow); + + frames + } +} + +impl PlaceNode { + /// Whether this node wants to be placed relative to its its parent's base + /// origin. instead of relative to the parent's current flow/cursor + /// position. + pub fn out_of_flow(&self) -> bool { + self.0 + .downcast::() + .map_or(false, |node| node.aligns.y.is_some()) + } +} diff --git a/src/library/placed.rs b/src/library/placed.rs deleted file mode 100644 index cee687fa..00000000 --- a/src/library/placed.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! Absolute placement of nodes. - -use super::prelude::*; -use super::AlignNode; - -/// Place content at an absolute position. -#[derive(Debug, Hash)] -pub struct PlaceNode(pub PackedNode); - -#[class] -impl PlaceNode { - fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult { - let aligns = args.find().unwrap_or(Spec::with_x(Some(Align::Left))); - let tx = args.named("dx")?.unwrap_or_default(); - let ty = args.named("dy")?.unwrap_or_default(); - let body: PackedNode = args.expect("body")?; - Ok(Node::block(Self( - body.moved(Point::new(tx, ty)).aligned(aligns), - ))) - } -} - -impl Layout for PlaceNode { - fn layout( - &self, - ctx: &mut LayoutContext, - regions: &Regions, - styles: StyleChain, - ) -> Vec>> { - let out_of_flow = self.out_of_flow(); - - // The pod is the base area of the region because for absolute - // placement we don't really care about the already used area (current). - let pod = { - let finite = regions.base.map(Length::is_finite); - let expand = finite & (regions.expand | out_of_flow); - Regions::one(regions.base, regions.base, expand) - }; - - let mut frames = self.0.layout(ctx, &pod, styles); - let Constrained { item: frame, cts } = &mut frames[0]; - - // If expansion is off, zero all sizes so that we don't take up any - // space in our parent. Otherwise, respect the expand settings. - let target = regions.expand.select(regions.current, Size::zero()); - Rc::make_mut(frame).resize(target, Align::LEFT_TOP); - - // Set base constraint because our pod size is base and exact - // constraints if we needed to expand or offset. - *cts = Constraints::new(regions.expand); - cts.base = regions.base.map(Some); - cts.exact = regions.current.filter(regions.expand | out_of_flow); - - frames - } -} - -impl PlaceNode { - /// Whether this node wants to be placed relative to its its parent's base - /// origin. instead of relative to the parent's current flow/cursor - /// position. - pub fn out_of_flow(&self) -> bool { - self.0 - .downcast::() - .map_or(false, |node| node.aligns.y.is_some()) - } -} -- cgit v1.2.3