status_bar.rs

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