status_bar.rs

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