From 0655a20ad119e2e9790afcc45039fd4ac0e7d432 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 16 Mar 2023 20:23:25 +0100 Subject: Make `Shrink` have priority over `Fill` in layout --- core/src/layout/flex.rs | 29 ++++++++++++----- core/src/layout/limits.rs | 83 +++++++++++++++++++---------------------------- core/src/layout/node.rs | 29 +++++++++++++++-- 3 files changed, 81 insertions(+), 60 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index c02b63d8..738dc81d 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -20,7 +20,7 @@ use crate::Element; use crate::layout::{Limits, Node}; use crate::widget; -use crate::{Alignment, Padding, Point, Size}; +use crate::{Alignment, Length, Padding, Point, Size}; /// The main axis of a flex layout. #[derive(Debug)] @@ -63,6 +63,8 @@ pub fn resolve( axis: Axis, renderer: &Renderer, limits: &Limits, + width: Length, + height: Length, padding: Padding, spacing: f32, align_items: Alignment, @@ -72,12 +74,12 @@ pub fn resolve( where Renderer: crate::Renderer, { - let limits = limits.pad(padding); + let limits = limits.width(width).height(height).shrink(padding); let total_spacing = spacing * items.len().saturating_sub(1) as f32; let max_cross = axis.cross(limits.max()); let mut fill_sum = 0; - let mut cross = axis.cross(limits.min()).max(axis.cross(limits.fill())); + let mut cross = 0.0f32; let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec = Vec::with_capacity(items.len()); @@ -109,7 +111,16 @@ where } } - let remaining = available.max(0.0); + let remaining = match axis { + Axis::Horizontal => match width { + Length::Shrink => 0.0, + _ => available.max(0.0), + }, + Axis::Vertical => match height { + Length::Shrink => 0.0, + _ => available.max(0.0), + }, + }; for (i, (child, tree)) in items.iter().zip(trees).enumerate() { let fill_factor = match axis { @@ -154,18 +165,18 @@ where let (x, y) = axis.pack(main, pad.1); - node.move_to(Point::new(x, y)); + node.move_to_mut(Point::new(x, y)); match axis { Axis::Horizontal => { - node.align( + node.align_mut( Alignment::Start, align_items, Size::new(0.0, cross), ); } Axis::Vertical => { - node.align( + node.align_mut( align_items, Alignment::Start, Size::new(cross, 0.0), @@ -179,7 +190,7 @@ where } let (width, height) = axis.pack(main - pad.0, cross); - let size = limits.resolve(Size::new(width, height)); + let size = limits.resolve(Size::new(width, height), width, height); - Node::with_children(size.pad(padding), nodes) + Node::with_children(size.expand(padding), nodes) } diff --git a/core/src/layout/limits.rs b/core/src/layout/limits.rs index 39a3d98b..eef4c4c9 100644 --- a/core/src/layout/limits.rs +++ b/core/src/layout/limits.rs @@ -1,12 +1,11 @@ #![allow(clippy::manual_clamp)] -use crate::{Length, Padding, Size}; +use crate::{Length, Size}; /// A set of size constraints for layouting. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Limits { min: Size, max: Size, - fill: Size, } impl Limits { @@ -14,16 +13,11 @@ impl Limits { pub const NONE: Limits = Limits { min: Size::ZERO, max: Size::INFINITY, - fill: Size::INFINITY, }; /// Creates new [`Limits`] with the given minimum and maximum [`Size`]. pub const fn new(min: Size, max: Size) -> Limits { - Limits { - min, - max, - fill: Size::INFINITY, - } + Limits { min, max } } /// Returns the minimum [`Size`] of the [`Limits`]. @@ -36,26 +30,15 @@ impl Limits { self.max } - /// Returns the fill [`Size`] of the [`Limits`]. - pub fn fill(&self) -> Size { - self.fill - } - /// Applies a width constraint to the current [`Limits`]. pub fn width(mut self, width: impl Into) -> Limits { match width.into() { - Length::Shrink => { - self.fill.width = self.min.width; - } - Length::Fill | Length::FillPortion(_) => { - self.fill.width = self.fill.width.min(self.max.width); - } + Length::Shrink | Length::Fill | Length::FillPortion(_) => {} Length::Fixed(amount) => { let new_width = amount.min(self.max.width).max(self.min.width); self.min.width = new_width; self.max.width = new_width; - self.fill.width = new_width; } } @@ -65,19 +48,13 @@ impl Limits { /// Applies a height constraint to the current [`Limits`]. pub fn height(mut self, height: impl Into) -> Limits { match height.into() { - Length::Shrink => { - self.fill.height = self.min.height; - } - Length::Fill | Length::FillPortion(_) => { - self.fill.height = self.fill.height.min(self.max.height); - } + Length::Shrink | Length::Fill | Length::FillPortion(_) => {} Length::Fixed(amount) => { let new_height = amount.min(self.max.height).max(self.min.height); self.min.height = new_height; self.max.height = new_height; - self.fill.height = new_height; } } @@ -112,13 +89,10 @@ impl Limits { self } - /// Shrinks the current [`Limits`] to account for the given padding. - pub fn pad(&self, padding: Padding) -> Limits { - self.shrink(Size::new(padding.horizontal(), padding.vertical())) - } - /// Shrinks the current [`Limits`] by the given [`Size`]. - pub fn shrink(&self, size: Size) -> Limits { + pub fn shrink(&self, size: impl Into) -> Limits { + let size = size.into(); + let min = Size::new( (self.min().width - size.width).max(0.0), (self.min().height - size.height).max(0.0), @@ -129,12 +103,7 @@ impl Limits { (self.max().height - size.height).max(0.0), ); - let fill = Size::new( - (self.fill.width - size.width).max(0.0), - (self.fill.height - size.height).max(0.0), - ); - - Limits { min, max, fill } + Limits { min, max } } /// Removes the minimum width constraint for the current [`Limits`]. @@ -142,22 +111,38 @@ impl Limits { Limits { min: Size::ZERO, max: self.max, - fill: self.fill, } } /// Computes the resulting [`Size`] that fits the [`Limits`] given the /// intrinsic size of some content. - pub fn resolve(&self, intrinsic_size: Size) -> Size { - Size::new( - intrinsic_size - .width - .min(self.max.width) - .max(self.fill.width), - intrinsic_size + pub fn resolve( + &self, + intrinsic_size: Size, + width: impl Into, + height: impl Into, + ) -> Size { + let width = match width.into() { + Length::Fill | Length::FillPortion(_) => self.max.width, + Length::Fixed(amount) => { + amount.min(self.max.width).max(self.min.width) + } + Length::Shrink => { + intrinsic_size.width.min(self.max.width).max(self.min.width) + } + }; + + let height = match height.into() { + Length::Fill | Length::FillPortion(_) => self.max.height, + Length::Fixed(amount) => { + amount.min(self.max.height).max(self.min.height) + } + Length::Shrink => intrinsic_size .height .min(self.max.height) - .max(self.fill.height), - ) + .max(self.min.height), + }; + + Size::new(width, height) } } diff --git a/core/src/layout/node.rs b/core/src/layout/node.rs index 2b44a7d5..00087431 100644 --- a/core/src/layout/node.rs +++ b/core/src/layout/node.rs @@ -1,4 +1,4 @@ -use crate::{Alignment, Point, Rectangle, Size, Vector}; +use crate::{Alignment, Padding, Point, Rectangle, Size, Vector}; /// The bounds of an element and its children. #[derive(Debug, Clone, Default)] @@ -26,6 +26,14 @@ impl Node { } } + /// Creates a new [`Node`] that wraps a single child with some [`Padding`]. + pub fn container(child: Self, padding: Padding) -> Self { + Self::with_children( + child.bounds.size().expand(padding), + vec![child.move_to(Point::new(padding.left, padding.top))], + ) + } + /// Returns the [`Size`] of the [`Node`]. pub fn size(&self) -> Size { Size::new(self.bounds.width, self.bounds.height) @@ -43,6 +51,17 @@ impl Node { /// Aligns the [`Node`] in the given space. pub fn align( + mut self, + horizontal_alignment: Alignment, + vertical_alignment: Alignment, + space: Size, + ) -> Self { + self.align_mut(horizontal_alignment, vertical_alignment, space); + self + } + + /// Mutable reference version of [`align`]. + pub fn align_mut( &mut self, horizontal_alignment: Alignment, vertical_alignment: Alignment, @@ -70,7 +89,13 @@ impl Node { } /// Moves the [`Node`] to the given position. - pub fn move_to(&mut self, position: Point) { + pub fn move_to(mut self, position: Point) -> Self { + self.move_to_mut(position); + self + } + + /// Mutable reference version of [`move_to`]. + pub fn move_to_mut(&mut self, position: Point) { self.bounds.x = position.x; self.bounds.y = position.y; } -- cgit From 89418c1244d14ac6020b31f3f1e19d15b4c0a272 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 23 Mar 2023 16:07:23 +0100 Subject: Determine cross-axis max length based on contents if `Shrink` --- core/src/layout/flex.rs | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 738dc81d..823fb9e6 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -47,7 +47,7 @@ impl Axis { } } - fn pack(&self, main: f32, cross: f32) -> (f32, f32) { + fn pack(&self, main: T, cross: T) -> (T, T) { match self { Axis::Horizontal => (main, cross), Axis::Vertical => (cross, main), @@ -78,7 +78,7 @@ where let total_spacing = spacing * items.len().saturating_sub(1) as f32; let max_cross = axis.cross(limits.max()); - let mut fill_sum = 0; + let mut fill_main_sum = 0; let mut cross = 0.0f32; let mut available = axis.main(limits.max()) - total_spacing; @@ -86,13 +86,12 @@ where nodes.resize(items.len(), Node::default()); for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { - let fill_factor = match axis { - Axis::Horizontal => child.as_widget().width(), - Axis::Vertical => child.as_widget().height(), - } - .fill_factor(); + let (fill_main_factor, fill_cross_factor) = axis.pack( + child.as_widget().width().fill_factor(), + child.as_widget().height().fill_factor(), + ); - if fill_factor == 0 { + if fill_main_factor == 0 && fill_cross_factor == 0 { let (max_width, max_height) = axis.pack(available, max_cross); let child_limits = @@ -107,7 +106,7 @@ where nodes[i] = layout; } else { - fill_sum += fill_factor; + fill_main_sum += fill_main_factor; } } @@ -122,15 +121,27 @@ where }, }; + let max_cross = match axis { + Axis::Horizontal => match height { + Length::Shrink => cross, + _ => max_cross, + }, + Axis::Vertical => match width { + Length::Shrink => cross, + _ => max_cross, + }, + }; + for (i, (child, tree)) in items.iter().zip(trees).enumerate() { - let fill_factor = match axis { - Axis::Horizontal => child.as_widget().width(), - Axis::Vertical => child.as_widget().height(), - } - .fill_factor(); + let (fill_main_factor, fill_cross_factor) = axis.pack( + child.as_widget().width().fill_factor(), + child.as_widget().height().fill_factor(), + ); + + if fill_main_factor != 0 || fill_cross_factor != 0 { + let max_main = + remaining * fill_main_factor as f32 / fill_main_sum as f32; - if fill_factor != 0 { - let max_main = remaining * fill_factor as f32 / fill_sum as f32; let min_main = if max_main.is_infinite() { 0.0 } else { @@ -140,7 +151,8 @@ where let (min_width, min_height) = axis.pack(min_main, axis.cross(limits.min())); - let (max_width, max_height) = axis.pack(max_main, max_cross); + let (max_width, max_height) = axis + .pack(max_main, max_cross * fill_cross_factor.max(1) as f32); let child_limits = Limits::new( Size::new(min_width, min_height), -- cgit From aa3c956516a23af86dfb9d96b769e5f26addbe60 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 24 Mar 2023 03:02:26 +0100 Subject: Fix available space provided to children with non-fill main axis but fill cross axis --- core/src/layout/flex.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 823fb9e6..5ae98b8c 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -139,10 +139,13 @@ where ); if fill_main_factor != 0 || fill_cross_factor != 0 { - let max_main = - remaining * fill_main_factor as f32 / fill_main_sum as f32; + let max_main = if fill_main_factor == 0 { + available.max(0.0) + } else { + remaining * fill_main_factor as f32 / fill_main_sum as f32 + }; - let min_main = if max_main.is_infinite() { + let min_main = if fill_main_factor == 0 || max_main.is_infinite() { 0.0 } else { max_main -- cgit From fd8f980b88df260ce49d46ed6c514f6e382c6494 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Mar 2023 14:40:03 +0200 Subject: Use `max_cross` if all elements are fluid in `layout::flex` --- core/src/layout/flex.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 5ae98b8c..9a4b2cbf 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -123,11 +123,11 @@ where let max_cross = match axis { Axis::Horizontal => match height { - Length::Shrink => cross, + Length::Shrink if cross > 0.0 => cross, _ => max_cross, }, Axis::Vertical => match width { - Length::Shrink => cross, + Length::Shrink if cross > 0.0 => cross, _ => max_cross, }, }; @@ -154,8 +154,7 @@ where let (min_width, min_height) = axis.pack(min_main, axis.cross(limits.min())); - let (max_width, max_height) = axis - .pack(max_main, max_cross * fill_cross_factor.max(1) as f32); + let (max_width, max_height) = axis.pack(max_main, max_cross); let child_limits = Limits::new( Size::new(min_width, min_height), -- cgit From 22226394f7b1a0e0205b9bb5b3ef9b85a3b406f5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jan 2024 17:24:43 +0100 Subject: Introduce `Widget::size_hint` and fix further layout inconsistencies --- core/src/layout/flex.rs | 72 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 19 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 9a4b2cbf..67cc7f2a 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -91,8 +91,46 @@ where child.as_widget().height().fill_factor(), ); - if fill_main_factor == 0 && fill_cross_factor == 0 { - let (max_width, max_height) = axis.pack(available, max_cross); + if fill_main_factor == 0 { + if fill_cross_factor == 0 { + 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.as_widget().layout(tree, renderer, &child_limits); + let size = layout.size(); + + available -= axis.main(size); + cross = cross.max(axis.cross(size)); + + nodes[i] = layout; + } + } else { + fill_main_sum += fill_main_factor; + } + } + + let intrinsic_cross = match axis { + Axis::Horizontal => match height { + Length::Shrink => cross, + _ => max_cross, + }, + Axis::Vertical => match width { + Length::Shrink => cross, + _ => max_cross, + }, + }; + + for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { + let (fill_main_factor, fill_cross_factor) = axis.pack( + child.as_widget().width().fill_factor(), + child.as_widget().height().fill_factor(), + ); + + if fill_main_factor == 0 && fill_cross_factor != 0 { + let (max_width, max_height) = axis.pack(available, intrinsic_cross); let child_limits = Limits::new(Size::ZERO, Size::new(max_width, max_height)); @@ -102,11 +140,8 @@ where let size = layout.size(); available -= axis.main(size); - cross = cross.max(axis.cross(size)); nodes[i] = layout; - } else { - fill_main_sum += fill_main_factor; } } @@ -121,24 +156,13 @@ where }, }; - let max_cross = match axis { - Axis::Horizontal => match height { - Length::Shrink if cross > 0.0 => cross, - _ => max_cross, - }, - Axis::Vertical => match width { - Length::Shrink if cross > 0.0 => cross, - _ => max_cross, - }, - }; - for (i, (child, tree)) in items.iter().zip(trees).enumerate() { let (fill_main_factor, fill_cross_factor) = axis.pack( child.as_widget().width().fill_factor(), child.as_widget().height().fill_factor(), ); - if fill_main_factor != 0 || fill_cross_factor != 0 { + if fill_main_factor != 0 { let max_main = if fill_main_factor == 0 { available.max(0.0) } else { @@ -151,6 +175,12 @@ where max_main }; + let max_cross = if fill_cross_factor == 0 { + max_cross + } else { + intrinsic_cross + }; + let (min_width, min_height) = axis.pack(min_main, axis.cross(limits.min())); @@ -203,8 +233,12 @@ where main += axis.main(size); } - let (width, height) = axis.pack(main - pad.0, cross); - let size = limits.resolve(Size::new(width, height), width, height); + let (intrinsic_width, intrinsic_height) = axis.pack(main - pad.0, cross); + let size = limits.resolve( + Size::new(intrinsic_width, intrinsic_height), + width, + height, + ); Node::with_children(size.expand(padding), nodes) } -- cgit From d278bfd21d0399009e652560afb9a4d185e92637 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jan 2024 17:46:33 +0100 Subject: Replace `width` and `height` with `Widget::size` --- core/src/layout/flex.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 67cc7f2a..036b31fd 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -86,10 +86,11 @@ where nodes.resize(items.len(), Node::default()); for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { - let (fill_main_factor, fill_cross_factor) = axis.pack( - child.as_widget().width().fill_factor(), - child.as_widget().height().fill_factor(), - ); + let (fill_main_factor, fill_cross_factor) = { + let size = child.as_widget().size(); + + axis.pack(size.width.fill_factor(), size.height.fill_factor()) + }; if fill_main_factor == 0 { if fill_cross_factor == 0 { @@ -124,10 +125,11 @@ where }; for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { - let (fill_main_factor, fill_cross_factor) = axis.pack( - child.as_widget().width().fill_factor(), - child.as_widget().height().fill_factor(), - ); + let (fill_main_factor, fill_cross_factor) = { + let size = child.as_widget().size(); + + axis.pack(size.width.fill_factor(), size.height.fill_factor()) + }; if fill_main_factor == 0 && fill_cross_factor != 0 { let (max_width, max_height) = axis.pack(available, intrinsic_cross); @@ -157,10 +159,11 @@ where }; for (i, (child, tree)) in items.iter().zip(trees).enumerate() { - let (fill_main_factor, fill_cross_factor) = axis.pack( - child.as_widget().width().fill_factor(), - child.as_widget().height().fill_factor(), - ); + let (fill_main_factor, fill_cross_factor) = { + let size = child.as_widget().size(); + + axis.pack(size.width.fill_factor(), size.height.fill_factor()) + }; if fill_main_factor != 0 { let max_main = if fill_main_factor == 0 { -- cgit From 4bdd8a62791cfa4864d3d4cf1d5b19c6f227d537 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Jan 2024 17:54:10 +0100 Subject: Fix `cross` axis calculation in `flex` layout --- core/src/layout/flex.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 036b31fd..2a12d57f 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -142,6 +142,7 @@ where let size = layout.size(); available -= axis.main(size); + cross = cross.max(axis.cross(layout.size())); nodes[i] = layout; } -- cgit From d62bb8193c1c43f565fcc5c52293d564c91e215d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 06:35:33 +0100 Subject: Introduce useful helpers in `layout` module --- core/src/layout/flex.rs | 2 +- core/src/layout/limits.rs | 7 ++++--- core/src/layout/node.rs | 10 +++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 2a12d57f..cf3e1340 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -239,9 +239,9 @@ where let (intrinsic_width, intrinsic_height) = axis.pack(main - pad.0, cross); let size = limits.resolve( - Size::new(intrinsic_width, intrinsic_height), width, height, + Size::new(intrinsic_width, intrinsic_height), ); Node::with_children(size.expand(padding), nodes) diff --git a/core/src/layout/limits.rs b/core/src/layout/limits.rs index eef4c4c9..7fbc7b9d 100644 --- a/core/src/layout/limits.rs +++ b/core/src/layout/limits.rs @@ -114,13 +114,14 @@ impl Limits { } } - /// Computes the resulting [`Size`] that fits the [`Limits`] given the - /// intrinsic size of some content. + /// Computes the resulting [`Size`] that fits the [`Limits`] given + /// some width and height requirements and the intrinsic size of + /// some content. pub fn resolve( &self, - intrinsic_size: Size, width: impl Into, height: impl Into, + intrinsic_size: Size, ) -> Size { let width = match width.into() { Length::Fill | Length::FillPortion(_) => self.max.width, diff --git a/core/src/layout/node.rs b/core/src/layout/node.rs index 00087431..40c71436 100644 --- a/core/src/layout/node.rs +++ b/core/src/layout/node.rs @@ -89,19 +89,23 @@ impl Node { } /// Moves the [`Node`] to the given position. - pub fn move_to(mut self, position: Point) -> Self { + pub fn move_to(mut self, position: impl Into) -> Self { self.move_to_mut(position); self } /// Mutable reference version of [`move_to`]. - pub fn move_to_mut(&mut self, position: Point) { + pub fn move_to_mut(&mut self, position: impl Into) { + let position = position.into(); + self.bounds.x = position.x; self.bounds.y = position.y; } /// Translates the [`Node`] by the given translation. - pub fn translate(self, translation: Vector) -> Self { + pub fn translate(self, translation: impl Into) -> Self { + let translation = translation.into(); + Self { bounds: self.bounds + translation, ..self -- cgit From 025064c9e028ea65cc0c6ff236d42e9861efdda9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 07:01:11 +0100 Subject: Fix broken doc links in `layout::Node` API --- core/src/layout/node.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/node.rs b/core/src/layout/node.rs index 40c71436..5743a9bd 100644 --- a/core/src/layout/node.rs +++ b/core/src/layout/node.rs @@ -60,7 +60,7 @@ impl Node { self } - /// Mutable reference version of [`align`]. + /// Mutable reference version of [`Self::align`]. pub fn align_mut( &mut self, horizontal_alignment: Alignment, @@ -94,7 +94,7 @@ impl Node { self } - /// Mutable reference version of [`move_to`]. + /// Mutable reference version of [`Self::move_to`]. pub fn move_to_mut(&mut self, position: impl Into) { let position = position.into(); -- cgit From 88f8c343fa7d69203ab98bb7abc85fe002014422 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jan 2024 07:15:57 +0100 Subject: Fix `cross` calculation in `layout::flex` --- core/src/layout/flex.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index cf3e1340..47cd7112 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -79,7 +79,17 @@ where let max_cross = axis.cross(limits.max()); let mut fill_main_sum = 0; - let mut cross = 0.0f32; + let mut cross = match axis { + Axis::Horizontal => match height { + Length::Shrink => 0.0, + _ => max_cross, + }, + Axis::Vertical => match width { + Length::Shrink => 0.0, + _ => max_cross, + }, + }; + let mut available = axis.main(limits.max()) - total_spacing; let mut nodes: Vec = Vec::with_capacity(items.len()); @@ -113,17 +123,6 @@ where } } - let intrinsic_cross = match axis { - Axis::Horizontal => match height { - Length::Shrink => cross, - _ => max_cross, - }, - Axis::Vertical => match width { - Length::Shrink => cross, - _ => max_cross, - }, - }; - for (i, (child, tree)) in items.iter().zip(trees.iter_mut()).enumerate() { let (fill_main_factor, fill_cross_factor) = { let size = child.as_widget().size(); @@ -132,7 +131,7 @@ where }; if fill_main_factor == 0 && fill_cross_factor != 0 { - let (max_width, max_height) = axis.pack(available, intrinsic_cross); + let (max_width, max_height) = axis.pack(available, cross); let child_limits = Limits::new(Size::ZERO, Size::new(max_width, max_height)); @@ -182,7 +181,7 @@ where let max_cross = if fill_cross_factor == 0 { max_cross } else { - intrinsic_cross + cross }; let (min_width, min_height) = -- cgit From fa53d9adbb0efbbe806a749476f83c04f756be75 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Jan 2024 06:11:36 +0100 Subject: Loosen cross axis constraint for main axis fills in `flex` layout --- core/src/layout/flex.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'core/src/layout') diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index 47cd7112..3358ef3d 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -166,13 +166,10 @@ where }; if fill_main_factor != 0 { - let max_main = if fill_main_factor == 0 { - available.max(0.0) - } else { - remaining * fill_main_factor as f32 / fill_main_sum as f32 - }; + let max_main = + remaining * fill_main_factor as f32 / fill_main_sum as f32; - let min_main = if fill_main_factor == 0 || max_main.is_infinite() { + let min_main = if max_main.is_infinite() { 0.0 } else { max_main @@ -184,9 +181,7 @@ where cross }; - let (min_width, min_height) = - axis.pack(min_main, axis.cross(limits.min())); - + let (min_width, min_height) = axis.pack(min_main, 0.0); let (max_width, max_height) = axis.pack(max_main, max_cross); let child_limits = Limits::new( -- cgit