Add chainable Element methods for common containers

Max Brunsfeld and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

gpui/src/elements.rs    | 28 +++++++++++++++++++++
zed/src/people_panel.rs | 56 ++++++++++++++++--------------------------
2 files changed, 50 insertions(+), 34 deletions(-)

Detailed changes

gpui/src/elements.rs 🔗

@@ -108,6 +108,34 @@ pub trait Element {
             element: Rc::new(RefCell::new(Lifecycle::Init { element: self })),
         })
     }
+
+    fn constrained(self) -> ConstrainedBox
+    where
+        Self: 'static + Sized,
+    {
+        ConstrainedBox::new(self.boxed())
+    }
+
+    fn aligned(self) -> Align
+    where
+        Self: 'static + Sized,
+    {
+        Align::new(self.boxed())
+    }
+
+    fn contained(self) -> Container
+    where
+        Self: 'static + Sized,
+    {
+        Container::new(self.boxed())
+    }
+
+    fn expanded(self, flex: f32) -> Expanded
+    where
+        Self: 'static + Sized,
+    {
+        Expanded::new(flex, self.boxed())
+    }
 }
 
 pub enum Lifecycle<T: Element> {

zed/src/people_panel.rs 🔗

@@ -96,21 +96,15 @@ impl PeoplePanel {
                             .map(|avatar| Image::new(avatar).with_style(theme.host_avatar).boxed()),
                     )
                     .with_child(
-                        ConstrainedBox::new(
-                            Align::new(
-                                Container::new(
-                                    Label::new(
-                                        collaborator.user.github_login.clone(),
-                                        theme.host_username.text.clone(),
-                                    )
-                                    .boxed(),
-                                )
-                                .with_style(theme.host_username.container)
-                                .boxed()
-                            )
-                            .left()
-                            .boxed()
+                        Label::new(
+                            collaborator.user.github_login.clone(),
+                            theme.host_username.text.clone(),
                         )
+                        .contained()
+                        .with_style(theme.host_username.container)
+                        .aligned()
+                        .left()
+                        .constrained()
                         .with_height(host_avatar_height)
                         .boxed()
                     )
@@ -192,32 +186,26 @@ impl PeoplePanel {
                                                 Container::new(
                                                     Flex::row()
                                                         .with_child(
-                                                            ConstrainedBox::new(
-                                                                Align::new(
-                                                                    Label::new(
-                                                                        worktree.root_name.clone(),
-                                                                        style.text.clone(),
-                                                                    )
-                                                                    .boxed(),
-                                                                )
-                                                                .left()
-                                                                .boxed()
+                                                            Label::new(
+                                                                worktree.root_name.clone(),
+                                                                style.text.clone(),
                                                             )
+                                                            .aligned()
+                                                            .left()
+                                                            .constrained()
                                                             .with_height(guest_avatar_height)
                                                             .boxed()
                                                         )
                                                         .with_children(worktree.guests.iter().filter_map(
                                                             |participant| {
                                                                 participant.avatar.clone().map(|avatar| {
-                                                                    Container::new(
-                                                                        Image::new(avatar)
-                                                                            .with_style(theme.guest_avatar)
-                                                                            .boxed(),
-                                                                    )
-                                                                    .with_margin_left(
-                                                                        theme.guest_avatar_spacing,
-                                                                    )
-                                                                    .boxed()
+                                                                    Image::new(avatar)
+                                                                        .with_style(theme.guest_avatar)
+                                                                        .contained()
+                                                                        .with_margin_left(
+                                                                            theme.guest_avatar_spacing,
+                                                                        )
+                                                                        .boxed()
                                                                 })
                                                             },
                                                         ))
@@ -236,7 +224,7 @@ impl PeoplePanel {
                                         });
                                 }
 
-                                Expanded::new(1.0, worktree_row.boxed()).boxed()
+                                worktree_row.expanded(1.0).boxed()
                             })
                             .boxed()
                     }),