diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-10-16 11:39:26 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-10-23 20:22:47 +0200 |
| commit | 6690bc23545bfe7275ad92de9e6bd11b7345caf4 (patch) | |
| tree | e116a23f2f04b3053160aae09088830fdb21460f /src/layout/spacing.rs | |
| parent | 1e74f7c407e42174b631cb7477f3c88252da7e25 (diff) | |
Revise block node contract
Frames produced by block nodes are now always treated as exactly one per
given region and a frame must not be larger than its respective region.
Any overflow must be handled internally. This means that stack and grid
don't need to search for fitting regions anymore, since the child has
already does that for them. This commit further moves stack spacing into
a new `SpacingNode`.
Diffstat (limited to 'src/layout/spacing.rs')
| -rw-r--r-- | src/layout/spacing.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/layout/spacing.rs b/src/layout/spacing.rs new file mode 100644 index 00000000..68fab3e6 --- /dev/null +++ b/src/layout/spacing.rs @@ -0,0 +1,50 @@ +use super::*; + +/// Spacing between other nodes. +#[derive(Debug)] +#[cfg_attr(feature = "layout-cache", derive(Hash))] +pub struct SpacingNode { + /// Which axis to space on. + pub axis: SpecAxis, + /// How much spacing to add. + pub amount: Linear, +} + +impl Layout for SpacingNode { + fn layout( + &self, + _: &mut LayoutContext, + regions: &Regions, + ) -> Vec<Constrained<Rc<Frame>>> { + let base = regions.base.get(self.axis); + let resolved = self.amount.resolve(base); + let limit = regions.current.get(self.axis); + + // Generate constraints. + let mut cts = Constraints::new(regions.expand); + if self.amount.is_relative() { + cts.base.set(self.axis, Some(base)); + } + + // If the spacing fits into the region, any larger region would also do. + // If it was limited though, any change it region size might lead to + // different results. + if resolved < limit { + cts.min.set(self.axis, Some(resolved)); + } else { + cts.exact.set(self.axis, Some(limit)); + } + + // Create frame with limited spacing size along spacing axis and zero + // extent along the other axis. + let mut size = Size::zero(); + size.set(self.axis, resolved.min(limit)); + vec![Frame::new(size, size.h).constrain(cts)] + } +} + +impl From<SpacingNode> for LayoutNode { + fn from(spacing: SpacingNode) -> Self { + Self::new(spacing) + } +} |
