diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-02-12 21:00:23 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-02-12 21:00:23 +0100 |
| commit | e70fbf5372dcf0c9011f0b7799dfa77194401c4f (patch) | |
| tree | 5d8fcc1840b2a9b8fd4ed8654d87f735a42d740f /library/src/layout | |
| parent | 70c8a089057f2c73ebeb5201fd59ee588f1c49d4 (diff) | |
Support `auto` as sizing for box
Diffstat (limited to 'library/src/layout')
| -rw-r--r-- | library/src/layout/container.rs | 49 |
1 files changed, 20 insertions, 29 deletions
diff --git a/library/src/layout/container.rs b/library/src/layout/container.rs index 7bb6a6e9..7d733a87 100644 --- a/library/src/layout/container.rs +++ b/library/src/layout/container.rs @@ -41,19 +41,21 @@ use crate::prelude::*; #[capable(Layout)] #[derive(Debug, Hash)] pub struct BoxNode { - /// How to size the content horizontally and vertically. - pub sizing: Axes<Option<Rel<Length>>>, /// The content to be sized. pub body: Content, + /// The box's width. + pub width: Smart<Rel<Length>>, + /// The box's height. + pub height: Smart<Rel<Length>>, } #[node] impl BoxNode { fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> { - let width = args.named("width")?; - let height = args.named("height")?; let body = args.eat::<Content>()?.unwrap_or_default(); - Ok(Self { sizing: Axes::new(width, height), body }.pack()) + let width = args.named("width")?.unwrap_or_default(); + let height = args.named("height")?.unwrap_or_default(); + Ok(Self { body, width, height }.pack()) } fn field(&self, name: &str) -> Option<Value> { @@ -71,31 +73,20 @@ impl Layout for BoxNode { styles: StyleChain, regions: Regions, ) -> SourceResult<Fragment> { - // The "pod" is the region into which the child will be layouted. - let pod = { - // Resolve the sizing to a concrete size. - let size = self - .sizing - .resolve(styles) - .zip(regions.base()) - .map(|(s, b)| s.map(|v| v.relative_to(b))) - .unwrap_or(regions.size); + // Resolve the sizing to a concrete size. + let sizing = Axes::new(self.width, self.height); + let size = sizing + .resolve(styles) + .zip(regions.base()) + .map(|(s, b)| s.map(|v| v.relative_to(b))) + .unwrap_or(regions.size); - // Select the appropriate base and expansion for the child depending - // on whether it is automatically or relatively sized. - let is_auto = self.sizing.as_ref().map(Option::is_none); - let expand = regions.expand | !is_auto; - Regions::one(size, expand) - }; - - // Layout the child. - let mut frame = self.body.layout(vt, styles, pod)?.into_frame(); - - // Ensure frame size matches regions size if expansion is on. - let target = regions.expand.select(regions.size, frame.size()); - frame.resize(target, Align::LEFT_TOP); - - Ok(Fragment::frame(frame)) + // Select the appropriate base and expansion for the child depending + // on whether it is automatically or relatively sized. + let is_auto = sizing.as_ref().map(Smart::is_auto); + let expand = regions.expand | !is_auto; + let pod = Regions::one(size, expand); + self.body.layout(vt, styles, pod) } } |
