From 33213abe7dfcb8d8065faadd2f5b72ec4b718af1 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Mon, 2 May 2022 18:48:32 +0200 Subject: New page margins API --- src/library/layout/page.rs | 26 +++++--------------------- src/model/styles.rs | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/library/layout/page.rs b/src/library/layout/page.rs index 4307d2f9..801a137d 100644 --- a/src/library/layout/page.rs +++ b/src/library/layout/page.rs @@ -18,14 +18,9 @@ impl PageNode { /// Whether the page is flipped into landscape orientation. pub const FLIPPED: bool = false; - /// The left margin. - pub const LEFT: Smart> = Smart::Auto; - /// The right margin. - pub const RIGHT: Smart> = Smart::Auto; - /// The top margin. - pub const TOP: Smart> = Smart::Auto; - /// The bottom margin. - pub const BOTTOM: Smart> = Smart::Auto; + /// The page margin. + #[property(fold)] + pub const MARGINS: Sides>> = Sides::splat(Smart::Auto); /// How many columns the page has. pub const COLUMNS: NonZeroUsize = NonZeroUsize::new(1).unwrap(); @@ -54,13 +49,7 @@ impl PageNode { styles.set_opt(Self::WIDTH, args.named("width")?); styles.set_opt(Self::HEIGHT, args.named("height")?); - let all = args.named("margins")?; - let hor = args.named("horizontal")?; - let ver = args.named("vertical")?; - styles.set_opt(Self::LEFT, args.named("left")?.or(hor).or(all)); - styles.set_opt(Self::TOP, args.named("top")?.or(ver).or(all)); - styles.set_opt(Self::RIGHT, args.named("right")?.or(hor).or(all)); - styles.set_opt(Self::BOTTOM, args.named("bottom")?.or(ver).or(all)); + styles.set_opt(Self::MARGINS, args.named("margins")?); styles.set_opt(Self::FLIPPED, args.named("flipped")?); styles.set_opt(Self::FILL, args.named("fill")?); @@ -96,12 +85,7 @@ impl PageNode { // Determine the margins. let default = Relative::from(0.1190 * min); - let padding = Sides { - left: styles.get(Self::LEFT).unwrap_or(default), - right: styles.get(Self::RIGHT).unwrap_or(default), - top: styles.get(Self::TOP).unwrap_or(default), - bottom: styles.get(Self::BOTTOM).unwrap_or(default), - }; + let padding = styles.get(Self::MARGINS).map(|side| side.unwrap_or(default)); let mut child = self.0.clone(); diff --git a/src/model/styles.rs b/src/model/styles.rs index 00d1df0f..1fddfd0e 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use super::{Content, Show, ShowNode}; use crate::diag::{At, TypResult}; -use crate::eval::{Args, Func, Node, Smart, Value}; +use crate::eval::{Args, Func, Node, RawLength, Smart, Value}; use crate::geom::{Length, Numeric, Relative, Sides, Spec}; use crate::library::layout::PageNode; use crate::library::structure::{EnumNode, ListNode}; @@ -488,6 +488,19 @@ impl Fold for Sides>> { } } +impl Fold for Sides>> { + type Output = Sides>>; + + fn fold(self, outer: Self::Output) -> Self::Output { + Sides { + left: self.left.or(outer.left), + top: self.top.or(outer.top), + right: self.right.or(outer.right), + bottom: self.bottom.or(outer.bottom), + } + } +} + /// A scoped property barrier. /// /// Barriers interact with [scoped](StyleMap::scoped) styles: A scoped style -- cgit v1.2.3