diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-11-20 17:28:58 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-11-20 17:31:52 +0100 |
| commit | f24e9b44e0ceb19be6f4e16af2d22815e9ccf5b7 (patch) | |
| tree | 9e520ee1f8b802973aa5b568c11fafc42e6426a8 /src/layout/mod.rs | |
| parent | 1dafe2c2ea0828bb075fdbb0da663967f7b5b2b9 (diff) | |
Refined expansion model 🔛
Diffstat (limited to 'src/layout/mod.rs')
| -rw-r--r-- | src/layout/mod.rs | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 13b63568..2c725e1d 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -147,9 +147,9 @@ pub struct LayoutContext<'a, 'p> { /// The axes to flow on. pub axes: LayoutAxes, - /// Whether to shrink the spaces to fit the content or to keep - /// the original dimensions. - pub shrink_to_fit: bool, + /// Whether layouts should expand to the full dimensions of the space + /// they lie on or whether should tightly fit the content. + pub expand: bool, } /// A possibly stack-allocated vector of layout spaces. @@ -219,6 +219,14 @@ impl LayoutAxes { pub fn anchor(&self, space: Size2D) -> Size2D { Size2D::new(self.primary.anchor(space.x), self.secondary.anchor(space.y)) } + + /// This axes with `expand` set to the given value for both axes. + pub fn expanding(&self, expand: bool) -> LayoutAxes { + LayoutAxes { + primary: self.primary.expanding(expand), + secondary: self.secondary.expanding(expand), + } + } } /// An axis with an alignment. @@ -226,17 +234,13 @@ impl LayoutAxes { pub struct AlignedAxis { pub axis: Axis, pub alignment: Alignment, + pub expand: bool, } impl AlignedAxis { - /// Returns an aligned axis if the alignment is compatible with the axis. - pub fn new(axis: Axis, alignment: Alignment) -> AlignedAxis { - AlignedAxis { axis, alignment } - } - - /// The pair of axis and alignment. - pub fn pair(&self) -> (Axis, Alignment) { - (self.axis, self.alignment) + /// Creates an aligned axis from its three components. + pub fn new(axis: Axis, alignment: Alignment, expand: bool) -> AlignedAxis { + AlignedAxis { axis, alignment, expand } } /// The position of the anchor specified by this axis on the given line. @@ -248,6 +252,16 @@ impl AlignedAxis { (true, End) | (false, Origin) => line, } } + + /// This axis with `expand` set to the given value. + pub fn expanding(&self, expand: bool) -> AlignedAxis { + AlignedAxis { expand, ..*self } + } + + /// Whether this axis needs expansion. + pub fn needs_expansion(&self) -> bool { + self.expand || self.alignment != Alignment::Origin + } } /// Where to put content. |
