diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-03-20 20:19:30 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-03-20 20:19:30 +0100 |
| commit | 898728f260923a91444eb23b522d0abf01a4299b (patch) | |
| tree | 64fdd3f78e16e6428e765a8e2d99c3cd910bd9df /src/layout/fixed.rs | |
| parent | 6cb9fe9064a037224b6560b69b441b72e787fa94 (diff) | |
Square, circle and ellipse 🔵
Diffstat (limited to 'src/layout/fixed.rs')
| -rw-r--r-- | src/layout/fixed.rs | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/layout/fixed.rs b/src/layout/fixed.rs index e3365668..22c45ef1 100644 --- a/src/layout/fixed.rs +++ b/src/layout/fixed.rs @@ -7,6 +7,10 @@ pub struct FixedNode { pub width: Option<Linear>, /// The fixed height, if any. pub height: Option<Linear>, + /// The fixed aspect ratio between width and height, if any. + /// + /// The resulting frame will satisfy `width = aspect * height`. + pub aspect: Option<f64>, /// The child node whose size to fix. pub child: Node, } @@ -14,18 +18,26 @@ pub struct FixedNode { impl Layout for FixedNode { fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Fragment { let Areas { current, full, .. } = areas; - let size = Size::new( + + let full = Size::new( self.width.map(|w| w.resolve(full.width)).unwrap_or(current.width), self.height.map(|h| h.resolve(full.height)).unwrap_or(current.height), ); + let mut size = full; + if let Some(aspect) = self.aspect { + // Shrink the size to ensure that the aspect ratio can be satisfied. + let width = size.width.min(aspect * size.height); + size = Size::new(width, width / aspect); + } + let fill_if = |cond| if cond { Expand::Fill } else { Expand::Fit }; let expand = Spec::new( fill_if(self.width.is_some()), fill_if(self.height.is_some()), ); - let areas = Areas::once(size, expand); + let areas = Areas::once(size, full, expand).with_aspect(self.aspect); self.child.layout(ctx, &areas) } } |
