Add support for adding/removing status items

This commit is contained in:
Conrad Irwin 2023-07-21 13:19:26 -06:00
parent 458916409c
commit d14a484a20

View file

@ -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 {