-
Notifications
You must be signed in to change notification settings - Fork 155
fix(masonry): flex layout not running after removing child #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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? | ||
gap: Option<f64>, | ||
} | ||
|
||
|
@@ -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, | ||
} | ||
} | ||
|
@@ -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(); | ||
} | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Effectively, 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)); | ||
|
@@ -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)] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
There was a problem hiding this comment.
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:There was a problem hiding this comment.
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.