status_bar.rs

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