From 8469bad7487e111c8e5a0ec542f0232a0ebb4bdc Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Thu, 4 Feb 2021 21:30:18 +0100 Subject: =?UTF-8?q?Add=20rectangle=20function=20=F0=9F=8E=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/insert.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/library/insert.rs') diff --git a/src/library/insert.rs b/src/library/insert.rs index 58e8a11c..169fad97 100644 --- a/src/library/insert.rs +++ b/src/library/insert.rs @@ -4,6 +4,43 @@ use crate::env::{ImageResource, ResourceId}; use crate::layout::*; use crate::prelude::*; +/// `rect`: Layout content into a rectangle that also might have a fill. +/// +/// # Named arguments +/// - Width of the box: `width`, of type `linear` relative to parent width. +/// - Height of the box: `height`, of type `linear` relative to parent height. +pub fn rect(ctx: &mut EvalContext, args: &mut Args) -> Value { + let snapshot = ctx.state.clone(); + + let width = args.get(ctx, "width"); + let height = args.get(ctx, "height"); + let color = args.get(ctx, "color"); + + let dirs = ctx.state.dirs; + let align = ctx.state.align; + + ctx.start_content_group(); + + if let Some(body) = args.find::(ctx) { + body.eval(ctx); + } + + let children = ctx.end_content_group(); + + let fill_if = |c| if c { Expansion::Fill } else { Expansion::Fit }; + let expand = Spec::new(fill_if(width.is_some()), fill_if(height.is_some())); + + ctx.push(NodeRect { + width, + height, + color, + child: NodeStack { dirs, align, expand, children }.into(), + }); + + ctx.state = snapshot; + Value::None +} + /// `image`: Insert an image. /// /// Supports PNG and JPEG files. -- cgit v1.2.3