status_bar.rs

  1use crate::{ItemHandle, Pane};
  2use gpui::{
  3    div, AnyView, Div, IntoElement, ParentElement, Render, Styled, Subscription, View, ViewContext,
  4    WindowContext,
  5};
  6use std::any::TypeId;
  7use ui::{h_stack, prelude::*};
  8use util::ResultExt;
  9
 10pub trait StatusItemView: Render {
 11    fn set_active_pane_item(
 12        &mut self,
 13        active_pane_item: Option<&dyn crate::ItemHandle>,
 14        cx: &mut ViewContext<Self>,
 15    );
 16}
 17
 18trait StatusItemViewHandle: Send {
 19    fn to_any(&self) -> AnyView;
 20    fn set_active_pane_item(
 21        &self,
 22        active_pane_item: Option<&dyn ItemHandle>,
 23        cx: &mut WindowContext,
 24    );
 25    fn item_type(&self) -> TypeId;
 26}
 27
 28pub struct StatusBar {
 29    left_items: Vec<Box<dyn StatusItemViewHandle>>,
 30    right_items: Vec<Box<dyn StatusItemViewHandle>>,
 31    active_pane: View<Pane>,
 32    _observe_active_pane: Subscription,
 33}
 34
 35impl Render for StatusBar {
 36    type Element = Div;
 37
 38    fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
 39        div()
 40            .py_0p5()
 41            .px_1()
 42            .flex()
 43            .items_center()
 44            .justify_between()
 45            .w_full()
 46            .h_8()
 47            .bg(cx.theme().colors().status_bar_background)
 48            .child(self.render_left_tools(cx))
 49            .child(self.render_right_tools(cx))
 50    }
 51}
 52
 53impl StatusBar {
 54    fn render_left_tools(&self, _: &mut ViewContext<Self>) -> impl IntoElement {
 55        h_stack()
 56            .items_center()
 57            .gap_2()
 58            .children(self.left_items.iter().map(|item| item.to_any()))
 59    }
 60
 61    fn render_right_tools(&self, _: &mut ViewContext<Self>) -> impl IntoElement {
 62        h_stack()
 63            .items_center()
 64            .gap_2()
 65            .children(self.right_items.iter().rev().map(|item| item.to_any()))
 66    }
 67}
 68
 69impl StatusBar {
 70    pub fn new(active_pane: &View<Pane>, cx: &mut ViewContext<Self>) -> Self {
 71        let mut this = Self {
 72            left_items: Default::default(),
 73            right_items: Default::default(),
 74            active_pane: active_pane.clone(),
 75            _observe_active_pane: cx
 76                .observe(active_pane, |this, _, cx| this.update_active_pane_item(cx)),
 77        };
 78        this.update_active_pane_item(cx);
 79        this
 80    }
 81
 82    pub fn add_left_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
 83    where
 84        T: 'static + StatusItemView,
 85    {
 86        let active_pane_item = self.active_pane.read(cx).active_item();
 87        item.set_active_pane_item(active_pane_item.as_deref(), cx);
 88
 89        self.left_items.push(Box::new(item));
 90        cx.notify();
 91    }
 92
 93    pub fn item_of_type<T: StatusItemView>(&self) -> Option<View<T>> {
 94        self.left_items
 95            .iter()
 96            .chain(self.right_items.iter())
 97            .find_map(|item| item.to_any().clone().downcast().log_err())
 98    }
 99
100    pub fn position_of_item<T>(&self) -> Option<usize>
101    where
102        T: StatusItemView,
103    {
104        for (index, item) in self.left_items.iter().enumerate() {
105            if item.item_type() == TypeId::of::<T>() {
106                return Some(index);
107            }
108        }
109        for (index, item) in self.right_items.iter().enumerate() {
110            if item.item_type() == TypeId::of::<T>() {
111                return Some(index + self.left_items.len());
112            }
113        }
114        return None;
115    }
116
117    pub fn insert_item_after<T>(
118        &mut self,
119        position: usize,
120        item: View<T>,
121        cx: &mut ViewContext<Self>,
122    ) where
123        T: 'static + StatusItemView,
124    {
125        let active_pane_item = self.active_pane.read(cx).active_item();
126        item.set_active_pane_item(active_pane_item.as_deref(), cx);
127
128        if position < self.left_items.len() {
129            self.left_items.insert(position + 1, Box::new(item))
130        } else {
131            self.right_items
132                .insert(position + 1 - self.left_items.len(), Box::new(item))
133        }
134        cx.notify()
135    }
136
137    pub fn remove_item_at(&mut self, position: usize, cx: &mut ViewContext<Self>) {
138        if position < self.left_items.len() {
139            self.left_items.remove(position);
140        } else {
141            self.right_items.remove(position - self.left_items.len());
142        }
143        cx.notify();
144    }
145
146    pub fn add_right_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
147    where
148        T: 'static + StatusItemView,
149    {
150        let active_pane_item = self.active_pane.read(cx).active_item();
151        item.set_active_pane_item(active_pane_item.as_deref(), cx);
152
153        self.right_items.push(Box::new(item));
154        cx.notify();
155    }
156
157    pub fn set_active_pane(&mut self, active_pane: &View<Pane>, cx: &mut ViewContext<Self>) {
158        self.active_pane = active_pane.clone();
159        self._observe_active_pane =
160            cx.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx));
161        self.update_active_pane_item(cx);
162    }
163
164    fn update_active_pane_item(&mut self, cx: &mut ViewContext<Self>) {
165        let active_pane_item = self.active_pane.read(cx).active_item();
166        for item in self.left_items.iter().chain(&self.right_items) {
167            item.set_active_pane_item(active_pane_item.as_deref(), cx);
168        }
169    }
170}
171
172impl<T: StatusItemView> StatusItemViewHandle for View<T> {
173    fn to_any(&self) -> AnyView {
174        self.clone().into()
175    }
176
177    fn set_active_pane_item(
178        &self,
179        active_pane_item: Option<&dyn ItemHandle>,
180        cx: &mut WindowContext,
181    ) {
182        self.update(cx, |this, cx| {
183            this.set_active_pane_item(active_pane_item, cx)
184        });
185    }
186
187    fn item_type(&self) -> TypeId {
188        TypeId::of::<T>()
189    }
190}
191
192impl From<&dyn StatusItemViewHandle> for AnyView {
193    fn from(val: &dyn StatusItemViewHandle) -> Self {
194        val.to_any().clone()
195    }
196}