From 3ad578e24823bf4612c3573efb1ad481bb16134e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 21 Aug 2025 18:16:46 +0200 Subject: [PATCH] Skip third `flex` pass entirely if main axis is compressed --- core/src/layout/flex.rs | 90 +++++++++++++++++++------------------ examples/layout/src/main.rs | 8 ++-- 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/core/src/layout/flex.rs b/core/src/layout/flex.rs index bec80427..0afc6e1b 100644 --- a/core/src/layout/flex.rs +++ b/core/src/layout/flex.rs @@ -155,7 +155,9 @@ where axis.pack(size.width, size.height) }; - if main_size.fill_factor() == 0 && cross_size.fill_factor() != 0 { + if (main_compress || main_size.fill_factor() == 0) + && cross_size.fill_factor() != 0 + { if let Length::Fixed(main) = main_size { available -= main; continue; @@ -183,54 +185,57 @@ where let remaining = available.max(0.0); - // THIRD PASS + // THIRD PASS (conditional) // We lay out the elements that are fluid in the main axis. // We use the remaining space to evenly allocate space based on fill factors. - for (i, (child, tree)) in items.iter_mut().zip(trees.iter_mut()).enumerate() - { - let (fill_main_factor, fill_cross_factor) = { - let size = child.as_widget().size(); + if !main_compress { + for (i, (child, tree)) in + items.iter_mut().zip(trees.iter_mut()).enumerate() + { + let (fill_main_factor, fill_cross_factor) = { + let size = child.as_widget().size(); - axis.pack(size.width.fill_factor(), size.height.fill_factor()) - }; - - if !main_compress && fill_main_factor != 0 { - let max_main = - remaining * fill_main_factor as f32 / fill_main_sum as f32; - - let max_main = if max_main.is_nan() { - f32::INFINITY - } else { - max_main + axis.pack(size.width.fill_factor(), size.height.fill_factor()) }; - let min_main = if max_main.is_infinite() { - 0.0 - } else { - max_main - }; + if fill_main_factor != 0 { + let max_main = + remaining * fill_main_factor as f32 / fill_main_sum as f32; - let (min_width, min_height) = axis.pack(min_main, 0.0); - let (max_width, max_height) = axis.pack( - max_main, - if fill_cross_factor == 0 { - max_cross + let max_main = if max_main.is_nan() { + f32::INFINITY } else { - cross - }, - ); + max_main + }; - let child_limits = Limits::with_compression( - Size::new(min_width, min_height), - Size::new(max_width, max_height), - compression, - ); + let min_main = if max_main.is_infinite() { + 0.0 + } else { + max_main + }; - let layout = - child.as_widget_mut().layout(tree, renderer, &child_limits); - cross = cross.max(axis.cross(layout.size())); + let (min_width, min_height) = axis.pack(min_main, 0.0); + let (max_width, max_height) = axis.pack( + max_main, + if fill_cross_factor == 0 { + max_cross + } else { + cross + }, + ); - nodes[i] = layout; + let child_limits = Limits::with_compression( + Size::new(min_width, min_height), + Size::new(max_width, max_height), + compression, + ); + + let layout = + child.as_widget_mut().layout(tree, renderer, &child_limits); + cross = cross.max(axis.cross(layout.size())); + + nodes[i] = layout; + } } } @@ -253,11 +258,8 @@ where let (max_width, max_height) = axis.pack(main, cross); - let child_limits = Limits::with_compression( - Size::ZERO, - Size::new(max_width, max_height), - compression, - ); + let child_limits = + Limits::new(Size::ZERO, Size::new(max_width, max_height)); let layout = child.as_widget_mut().layout(tree, renderer, &child_limits); diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index f477c406..c15a0bfc 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -94,14 +94,14 @@ impl Layout { let controls = row([ (!self.example.is_first()).then_some( - button("← Previous") + button(text("← Previous").shaping(text::Shaping::Advanced)) .padding([5, 10]) .on_press(Message::Previous) .into(), ), Some(horizontal_space().into()), (!self.example.is_last()).then_some( - button("Next →") + button(text("Next →").shaping(text::Shaping::Advanced)) .padding([5, 10]) .on_press(Message::Next) .into(), @@ -294,7 +294,7 @@ fn quotes<'a>() -> Element<'a, Message> { fn quote<'a>( content: impl Into>, ) -> Element<'a, Message> { - row![vertical_rule(2), content.into()] + row![vertical_rule(1), content.into()] .spacing(10) .height(Shrink) .into() @@ -313,7 +313,7 @@ fn quotes<'a>() -> Element<'a, Message> { "This is another reply", ), horizontal_rule(1), - "A separator ↑", + text("A separator ↑").shaping(text::Shaping::Advanced), ] .width(Shrink) .spacing(10)