In UniformList, guard against misbehavior of append_items

If for some reason the handle got dropped and we call it, we'll deal with it somewhat gracefully.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2022-05-26 18:30:28 -06:00
parent 8dd82fdce1
commit b6b16fc9c3

View file

@ -44,7 +44,7 @@ pub struct LayoutState {
pub struct UniformList { pub struct UniformList {
state: UniformListState, state: UniformListState,
item_count: usize, item_count: usize,
append_items: Box<dyn Fn(Range<usize>, &mut Vec<ElementBox>, &mut LayoutContext) -> bool>, append_items: Box<dyn Fn(Range<usize>, &mut Vec<ElementBox>, &mut LayoutContext)>,
padding_top: f32, padding_top: f32,
padding_bottom: f32, padding_bottom: f32,
get_width_from_item: Option<usize>, get_width_from_item: Option<usize>,
@ -70,9 +70,6 @@ impl UniformList {
cx.render(&handle, |view, cx| { cx.render(&handle, |view, cx| {
append_items(view, range, items, cx); append_items(view, range, items, cx);
}); });
true
} else {
false
} }
}), }),
padding_top: 0., padding_top: 0.,
@ -172,40 +169,51 @@ impl Element for UniformList {
); );
} }
let no_items = (
constraint.min,
LayoutState {
item_height: 0.,
scroll_max: 0.,
items: Default::default(),
},
);
if self.item_count == 0 { if self.item_count == 0 {
return ( return no_items;
constraint.min,
LayoutState {
item_height: 0.,
scroll_max: 0.,
items: Default::default(),
},
);
} }
let mut items = Vec::new(); let mut items = Vec::new();
let mut size = constraint.max; let mut size = constraint.max;
let mut item_size; let mut item_size;
let sample_item_ix; let sample_item_ix;
let mut sample_item; let sample_item;
if let Some(sample_ix) = self.get_width_from_item { if let Some(sample_ix) = self.get_width_from_item {
(self.append_items)(sample_ix..sample_ix + 1, &mut items, cx); (self.append_items)(sample_ix..sample_ix + 1, &mut items, cx);
sample_item_ix = sample_ix; sample_item_ix = sample_ix;
sample_item = items.pop().unwrap();
item_size = sample_item.layout(constraint, cx); if let Some(mut item) = items.pop() {
size.set_x(item_size.x()); item_size = item.layout(constraint, cx);
size.set_x(item_size.x());
sample_item = item;
} else {
return no_items;
}
} else { } else {
(self.append_items)(0..1, &mut items, cx); (self.append_items)(0..1, &mut items, cx);
sample_item_ix = 0; sample_item_ix = 0;
sample_item = items.pop().unwrap(); if let Some(mut item) = items.pop() {
item_size = sample_item.layout( item_size = item.layout(
SizeConstraint::new( SizeConstraint::new(
vec2f(constraint.max.x(), 0.0), vec2f(constraint.max.x(), 0.0),
vec2f(constraint.max.x(), f32::INFINITY), vec2f(constraint.max.x(), f32::INFINITY),
), ),
cx, cx,
); );
item_size.set_x(size.x()); item_size.set_x(size.x());
sample_item = item
} else {
return no_items;
}
} }
let item_constraint = SizeConstraint { let item_constraint = SizeConstraint {