From e870e5d8a51cede29d3e4b40adefd660d90d0029 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Sun, 1 Aug 2021 16:25:16 +0200 Subject: [PATCH] feat(layout): add private option to control last chunk expansion --- src/layout.rs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/layout.rs b/src/layout.rs index c9eb6ec..b1df157 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -63,6 +63,9 @@ pub struct Layout { direction: Direction, margin: Margin, constraints: Vec, + /// Whether the last chunk of the computed layout should be expanded to fill the available + /// space. + expand_to_fill: bool, } thread_local! { @@ -78,6 +81,7 @@ impl Default for Layout { vertical: 0, }, constraints: Vec::new(), + expand_to_fill: true, } } } @@ -114,6 +118,11 @@ impl Layout { self } + pub(crate) fn expand_to_fill(mut self, expand_to_fill: bool) -> Layout { + self.expand_to_fill = expand_to_fill; + self + } + /// Wrapper function around the cassowary-rs solver to be able to split a given /// area into smaller ones based on the preferred widths or heights and the direction. /// @@ -222,11 +231,13 @@ fn split(area: Rect, layout: &Layout) -> Vec { Direction::Vertical => first.top() | EQ(REQUIRED) | f64::from(dest_area.top()), }); } - if let Some(last) = elements.last() { - ccs.push(match layout.direction { - Direction::Horizontal => last.right() | EQ(REQUIRED) | f64::from(dest_area.right()), - Direction::Vertical => last.bottom() | EQ(REQUIRED) | f64::from(dest_area.bottom()), - }); + if layout.expand_to_fill { + if let Some(last) = elements.last() { + ccs.push(match layout.direction { + Direction::Horizontal => last.right() | EQ(REQUIRED) | f64::from(dest_area.right()), + Direction::Vertical => last.bottom() | EQ(REQUIRED) | f64::from(dest_area.bottom()), + }); + } } match layout.direction { Direction::Horizontal => { @@ -299,14 +310,16 @@ fn split(area: Rect, layout: &Layout) -> Vec { } } - // Fix imprecision by extending the last item a bit if necessary - if let Some(last) = results.last_mut() { - match layout.direction { - Direction::Vertical => { - last.height = dest_area.bottom() - last.y; - } - Direction::Horizontal => { - last.width = dest_area.right() - last.x; + if layout.expand_to_fill { + // Fix imprecision by extending the last item a bit if necessary + if let Some(last) = results.last_mut() { + match layout.direction { + Direction::Vertical => { + last.height = dest_area.bottom() - last.y; + } + Direction::Horizontal => { + last.width = dest_area.right() - last.x; + } } } }