Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 29 additions & 2 deletions masonry/src/widgets/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Flex {
fill_major_axis: bool,
children: Vec<Child>,
old_bc: BoxConstraints,
child_removed: bool, // TODO: why can't we use WidgetState.children_changed instead?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we can't use children_changed here is two reasons:

  1. It will always be handled "before" we get to layout. Indeed, that's the reason that it doesn't have a getter - at this point in the timeline, it doesn't have a meaningful value.
  2. It doesn't handle spacers being removed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is, we should remove this comment.

gap: Option<f64>,
}

Expand Down Expand Up @@ -128,6 +129,7 @@ impl Flex {
main_alignment: MainAxisAlignment::Start,
fill_major_axis: false,
old_bc: BoxConstraints::tight(Size::ZERO),
child_removed: false,
gap: None,
}
}
Expand Down Expand Up @@ -579,6 +581,7 @@ impl Flex {
if let Child::Fixed { widget, .. } | Child::Flex { widget, .. } = child {
this.ctx.remove_child(widget);
}
this.widget.child_removed = true;
this.ctx.request_layout();
}

Expand Down Expand Up @@ -1009,7 +1012,8 @@ impl Widget for Flex {
// indicates that the box constrains for the following children have changed. Therefore they
// have to calculate layout again.
let bc_changed = self.old_bc != *bc;
let mut any_changed = bc_changed;
let mut any_changed = bc_changed || self.child_removed;
Copy link
Member

@DJMcNab DJMcNab May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effectively, any_changed is being used a proxy for if the amount of "available flex space" has changed since last run, right?

This all feels very fragile.

I wonder if just expressing that "directly" would make this better? This doesn't need to happen in this PR

self.child_removed = false;
self.old_bc = *bc;

let gap = self.gap.unwrap_or(axis_default_spacer(self.direction));
Expand Down Expand Up @@ -1289,10 +1293,12 @@ impl Widget for Flex {
// --- MARK: TESTS ---
#[cfg(test)]
mod tests {
use vello::peniko::color::palette;

use super::*;
use crate::assert_render_snapshot;
use crate::testing::TestHarness;
use crate::widgets::Label;
use crate::widgets::{Label, SizedBox};

#[test]
#[allow(clippy::cognitive_complexity)]
Expand Down Expand Up @@ -1661,6 +1667,27 @@ mod tests {
assert!(image_1 == image_2);
}

#[test]
fn remove_child_after_expanded_child() {
let widget = Flex::column()
.with_flex_child(
SizedBox::new(Label::new("child 1"))
.expand()
.background(palette::css::PURPLE),
1.,
)
.with_flex_child(Label::new("child 2"), 1.0);

let window_size = Size::new(200.0, 150.0);
let mut harness = TestHarness::create_with_size(widget, window_size);

harness.edit_root_widget(|mut flex| {
let mut flex = flex.downcast::<Flex>();
Flex::remove_child(&mut flex, 1);
});
assert_render_snapshot!(harness, "flex_remove_child_after_expanded_child");
}
Comment on lines +1670 to +1689
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a similar test where the removed item is a spacer? I believe that this PR should also fix that, but I'd like to be sure.


#[test]
fn get_flex_child() {
let widget = Flex::column()
Expand Down
Loading