Skip to content

Conversation

not-my-profile
Copy link
Contributor

While working on implementing a devtools widget for Masonry I found a bug with the Flex widget (see the test I added). I found a fix but it feels more like a workaround than a clean solution because I think we should be able to use WidgetState.children_changed but I couldn't make it work (let mut any_changed = bc_changed || ctx.widget_state.children_changed; doesn't work for some reason?).

Without the fix the render result is:

image

@not-my-profile not-my-profile changed the title fix(masonry): layout not running after removing child fix(masonry): flex layout not running after removing child May 20, 2025
Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

This is really good. I still have a few more thoughts.

Overall, the flex implementation is not written in a way which convinces me of its correctness, but this should only possibly make things more correct.

I think that cases where a spacer's height changes won't be handled properly, but that's a pre-existing issue. It suggests that a more fundamental fix is needed, i.e. storing the value of remaining, and setting any_changed based on that changing instead. I don't think that needs to happen in this PR.

@@ -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.

Comment on lines +1670 to +1689
#[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");
}
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.

@@ -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

Copy link
Contributor

@PoignardAzur PoignardAzur left a comment

Choose a reason for hiding this comment

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

I think the root cause is that layout caching really shouldn't happen in widget implementations (e.g. in Flex::layout()) and should happen at the Masonry level instead.

On the medium term, I expect we'll fix that in the coming weeks with the layout refactor.

On the short term, I'm fine merging this PR, ideally with a comment explaining what I just said.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants