Add support for adding/removing status items

Conrad Irwin created

Change summary

crates/workspace/src/status_bar.rs | 55 ++++++++++++++++++++++++++++++-
1 file changed, 53 insertions(+), 2 deletions(-)

Detailed changes

crates/workspace/src/status_bar.rs 🔗

@@ -1,4 +1,4 @@
-use std::ops::Range;
+use std::{any::TypeId, ops::Range};
 
 use crate::{ItemHandle, Pane};
 use gpui::{
@@ -27,6 +27,7 @@ trait StatusItemViewHandle {
         active_pane_item: Option<&dyn ItemHandle>,
         cx: &mut WindowContext,
     );
+    fn ui_name(&self) -> &'static str;
 }
 
 pub struct StatusBar {
@@ -57,7 +58,6 @@ impl View for StatusBar {
                         .with_margin_right(theme.item_spacing)
                 }))
                 .into_any(),
-
             right: Flex::row()
                 .with_children(self.right_items.iter().rev().map(|i| {
                     ChildView::new(i.as_any(), cx)
@@ -96,6 +96,53 @@ impl StatusBar {
         cx.notify();
     }
 
+    pub fn position_of_item<T>(&mut self) -> Option<usize>
+    where
+        T: StatusItemView,
+    {
+        self.position_of_named_item(T::ui_name())
+    }
+
+    pub fn position_of_named_item(&mut self, name: &str) -> Option<usize> {
+        for (index, item) in self.left_items.iter().enumerate() {
+            if item.as_ref().ui_name() == name {
+                return Some(index);
+            }
+        }
+        for (index, item) in self.right_items.iter().enumerate() {
+            if item.as_ref().ui_name() == name {
+                return Some(index + self.left_items.len());
+            }
+        }
+        return None;
+    }
+
+    pub fn insert_item_after<T>(
+        &mut self,
+        position: usize,
+        item: ViewHandle<T>,
+        cx: &mut ViewContext<Self>,
+    ) where
+        T: 'static + StatusItemView,
+    {
+        if position < self.left_items.len() {
+            self.left_items.insert(position, Box::new(item))
+        } else {
+            self.right_items
+                .insert(position - self.left_items.len(), Box::new(item))
+        }
+        cx.notify()
+    }
+
+    pub fn remove_item_at(&mut self, position: usize, cx: &mut ViewContext<Self>) {
+        if position < self.left_items.len() {
+            self.left_items.remove(position);
+        } else {
+            self.right_items.remove(position - self.left_items.len());
+        }
+        cx.notify();
+    }
+
     pub fn add_right_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
     where
         T: 'static + StatusItemView,
@@ -133,6 +180,10 @@ impl<T: StatusItemView> StatusItemViewHandle for ViewHandle<T> {
             this.set_active_pane_item(active_pane_item, cx)
         });
     }
+
+    fn ui_name(&self) -> &'static str {
+        T::ui_name()
+    }
 }
 
 impl From<&dyn StatusItemViewHandle> for AnyViewHandle {