In UniformList, guard against misbehavior of append_items

Nathan Sobo and Max Brunsfeld created

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>

Change summary

crates/gpui/src/elements/uniform_list.rs | 58 ++++++++++++++-----------
1 file changed, 33 insertions(+), 25 deletions(-)

Detailed changes

crates/gpui/src/elements/uniform_list.rs 🔗

@@ -44,7 +44,7 @@ pub struct LayoutState {
 pub struct UniformList {
     state: UniformListState,
     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_bottom: f32,
     get_width_from_item: Option<usize>,
@@ -70,9 +70,6 @@ impl UniformList {
                     cx.render(&handle, |view, cx| {
                         append_items(view, range, items, cx);
                     });
-                    true
-                } else {
-                    false
                 }
             }),
             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 {
-            return (
-                constraint.min,
-                LayoutState {
-                    item_height: 0.,
-                    scroll_max: 0.,
-                    items: Default::default(),
-                },
-            );
+            return no_items;
         }
 
         let mut items = Vec::new();
         let mut size = constraint.max;
         let mut item_size;
         let sample_item_ix;
-        let mut sample_item;
+        let sample_item;
         if let Some(sample_ix) = self.get_width_from_item {
             (self.append_items)(sample_ix..sample_ix + 1, &mut items, cx);
             sample_item_ix = sample_ix;
-            sample_item = items.pop().unwrap();
-            item_size = sample_item.layout(constraint, cx);
-            size.set_x(item_size.x());
+
+            if let Some(mut item) = items.pop() {
+                item_size = item.layout(constraint, cx);
+                size.set_x(item_size.x());
+                sample_item = item;
+            } else {
+                return no_items;
+            }
         } else {
             (self.append_items)(0..1, &mut items, cx);
             sample_item_ix = 0;
-            sample_item = items.pop().unwrap();
-            item_size = sample_item.layout(
-                SizeConstraint::new(
-                    vec2f(constraint.max.x(), 0.0),
-                    vec2f(constraint.max.x(), f32::INFINITY),
-                ),
-                cx,
-            );
-            item_size.set_x(size.x());
+            if let Some(mut item) = items.pop() {
+                item_size = item.layout(
+                    SizeConstraint::new(
+                        vec2f(constraint.max.x(), 0.0),
+                        vec2f(constraint.max.x(), f32::INFINITY),
+                    ),
+                    cx,
+                );
+                item_size.set_x(size.x());
+                sample_item = item
+            } else {
+                return no_items;
+            }
         }
 
         let item_constraint = SizeConstraint {