diff options
-rw-r--r-- | core/src/length.rs | 10 | ||||
-rw-r--r-- | native/src/layout/flex.rs | 14 | ||||
-rw-r--r-- | native/src/layout/limits.rs | 4 | ||||
-rw-r--r-- | web/src/style.rs | 2 | ||||
-rw-r--r-- | web/src/widget/image.rs | 2 |
5 files changed, 23 insertions, 9 deletions
diff --git a/core/src/length.rs b/core/src/length.rs index 10873e89..06d8cf0a 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -4,6 +4,15 @@ pub enum Length { /// Fill all the remaining space Fill, + /// Fill a portion of the remaining space relative to other elements. + /// + /// Let's say we have two elements: one with `FillPortion(2)` and one with + /// `FillPortion(3)`. The first will get 2 portions of the available space, + /// while the second one would get 3. + /// + /// `Length::Fill` is equivalent to `Length::FillPortion(1)`. + FillPortion(u16), + /// Fill the least amount of space Shrink, @@ -22,6 +31,7 @@ impl Length { pub fn fill_factor(&self) -> u16 { match self { Length::Fill => 1, + Length::FillPortion(factor) => *factor, Length::Shrink => 0, Length::Units(_) => 0, } diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 0ed17d41..03b13e38 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -73,10 +73,12 @@ where Renderer: crate::Renderer, { let limits = limits.pad(padding); + let total_spacing = spacing * items.len().saturating_sub(1) as f32; + let max_cross = axis.cross(limits.max()); - let mut total_non_fill = spacing * items.len().saturating_sub(1) as f32; let mut fill_sum = 0; let mut cross = axis.cross(limits.min()); + let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec<Node> = Vec::with_capacity(items.len()); nodes.resize(items.len(), Node::default()); @@ -89,12 +91,15 @@ where .fill_factor(); if fill_factor == 0 { - let child_limits = Limits::new(Size::ZERO, limits.max()); + let (max_width, max_height) = axis.pack(available, max_cross); + + let child_limits = + Limits::new(Size::ZERO, Size::new(max_width, max_height)); let layout = child.layout(renderer, &child_limits); let size = layout.size(); - total_non_fill += axis.main(size); + available -= axis.main(size); cross = cross.max(axis.cross(size)); nodes[i] = layout; @@ -103,8 +108,7 @@ where } } - let available = axis.main(limits.max()); - let remaining = (available - total_non_fill).max(0.0); + let remaining = available.max(0.0); for (i, child) in items.iter().enumerate() { let fill_factor = match axis { diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index 2705a47d..a35f7ff7 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -52,7 +52,7 @@ impl Limits { Length::Shrink => { self.fill.width = self.min.width; } - Length::Fill => { + Length::Fill | Length::FillPortion(_) => { self.fill.width = self.fill.width.min(self.max.width); } Length::Units(units) => { @@ -76,7 +76,7 @@ impl Limits { Length::Shrink => { self.fill.height = self.min.height; } - Length::Fill => { + Length::Fill | Length::FillPortion(_) => { self.fill.height = self.fill.height.min(self.max.height); } Length::Units(units) => { diff --git a/web/src/style.rs b/web/src/style.rs index 2fb8602a..4f72b22c 100644 --- a/web/src/style.rs +++ b/web/src/style.rs @@ -139,7 +139,7 @@ pub fn length(length: Length) -> String { match length { Length::Shrink => String::from("auto"), Length::Units(px) => format!("{}px", px), - Length::Fill => String::from("100%"), + Length::Fill | Length::FillPortion(_) => String::from("100%"), } } diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index ed8b7ecf..413b663e 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -67,7 +67,7 @@ impl<Message> Widget<Message> for Image { match self.width { Length::Shrink => {} - Length::Fill => { + Length::Fill | Length::FillPortion(_) => { image = image.attr("width", "100%"); } Length::Units(px) => { |