status_bar.rs

  1use crate::{ItemHandle, MultiWorkspace, Pane, ToggleWorkspaceSidebar};
  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::{Divider, Indicator, Tooltip, 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    sidebar_has_notifications: bool,
 39    show_sidebar_toggle: bool,
 40}
 41
 42impl Render for StatusBar {
 43    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 44        h_flex()
 45            .w_full()
 46            .justify_between()
 47            .gap(DynamicSpacing::Base08.rems(cx))
 48            .p(DynamicSpacing::Base04.rems(cx))
 49            .bg(cx.theme().colors().status_bar_background)
 50            .map(|el| match window.window_decorations() {
 51                Decorations::Server => el,
 52                Decorations::Client { tiling, .. } => el
 53                    .when(!(tiling.bottom || tiling.right), |el| {
 54                        el.rounded_br(CLIENT_SIDE_DECORATION_ROUNDING)
 55                    })
 56                    .when(
 57                        !(tiling.bottom || tiling.left) && !self.workspace_sidebar_open,
 58                        |el| el.rounded_bl(CLIENT_SIDE_DECORATION_ROUNDING),
 59                    )
 60                    // This border is to avoid a transparent gap in the rounded corners
 61                    .mb(px(-1.))
 62                    .border_b(px(1.0))
 63                    .border_color(cx.theme().colors().status_bar_background),
 64            })
 65            .child(self.render_left_tools(cx))
 66            .child(self.render_right_tools())
 67    }
 68}
 69
 70impl StatusBar {
 71    fn render_left_tools(&self, cx: &mut Context<Self>) -> impl IntoElement {
 72        h_flex()
 73            .gap_1()
 74            .min_w_0()
 75            .overflow_x_hidden()
 76            .when(
 77                self.show_sidebar_toggle && !self.workspace_sidebar_open,
 78                |this| this.child(self.render_sidebar_toggle(cx)),
 79            )
 80            .children(self.left_items.iter().map(|item| item.to_any()))
 81    }
 82
 83    fn render_right_tools(&self) -> impl IntoElement {
 84        h_flex()
 85            .flex_shrink_0()
 86            .gap_1()
 87            .overflow_x_hidden()
 88            .children(self.right_items.iter().rev().map(|item| item.to_any()))
 89    }
 90
 91    fn render_sidebar_toggle(&self, cx: &mut Context<Self>) -> impl IntoElement {
 92        h_flex()
 93            .gap_0p5()
 94            .child(
 95                IconButton::new(
 96                    "toggle-workspace-sidebar",
 97                    IconName::ThreadsSidebarLeftClosed,
 98                )
 99                .icon_size(IconSize::Small)
100                .when(self.sidebar_has_notifications, |this| {
101                    this.indicator(Indicator::dot().color(Color::Accent))
102                        .indicator_border_color(Some(cx.theme().colors().status_bar_background))
103                })
104                .tooltip(move |_, cx| {
105                    Tooltip::for_action("Open Threads Sidebar", &ToggleWorkspaceSidebar, cx)
106                })
107                .on_click(move |_, window, cx| {
108                    if let Some(multi_workspace) = window.root::<MultiWorkspace>().flatten() {
109                        multi_workspace.update(cx, |multi_workspace, cx| {
110                            multi_workspace.toggle_sidebar(window, cx);
111                        });
112                    }
113                }),
114            )
115            .child(Divider::vertical().color(ui::DividerColor::Border))
116    }
117}
118
119impl StatusBar {
120    pub fn new(active_pane: &Entity<Pane>, window: &mut Window, cx: &mut Context<Self>) -> Self {
121        let mut this = Self {
122            left_items: Default::default(),
123            right_items: Default::default(),
124            active_pane: active_pane.clone(),
125            _observe_active_pane: cx.observe_in(active_pane, window, |this, _, window, cx| {
126                this.update_active_pane_item(window, cx)
127            }),
128            workspace_sidebar_open: false,
129            sidebar_has_notifications: false,
130            show_sidebar_toggle: false,
131        };
132        this.update_active_pane_item(window, cx);
133        this
134    }
135
136    pub fn set_workspace_sidebar_open(&mut self, open: bool, cx: &mut Context<Self>) {
137        self.workspace_sidebar_open = open;
138        cx.notify();
139    }
140
141    pub fn set_sidebar_has_notifications(&mut self, has: bool, cx: &mut Context<Self>) {
142        self.sidebar_has_notifications = has;
143        cx.notify();
144    }
145
146    pub fn set_show_sidebar_toggle(&mut self, show: bool, cx: &mut Context<Self>) {
147        self.show_sidebar_toggle = show;
148        cx.notify();
149    }
150
151    pub fn add_left_item<T>(&mut self, item: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
152    where
153        T: 'static + StatusItemView,
154    {
155        let active_pane_item = self.active_pane.read(cx).active_item();
156        item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
157
158        self.left_items.push(Box::new(item));
159        cx.notify();
160    }
161
162    pub fn item_of_type<T: StatusItemView>(&self) -> Option<Entity<T>> {
163        self.left_items
164            .iter()
165            .chain(self.right_items.iter())
166            .find_map(|item| item.to_any().downcast().log_err())
167    }
168
169    pub fn position_of_item<T>(&self) -> Option<usize>
170    where
171        T: StatusItemView,
172    {
173        for (index, item) in self.left_items.iter().enumerate() {
174            if item.item_type() == TypeId::of::<T>() {
175                return Some(index);
176            }
177        }
178        for (index, item) in self.right_items.iter().enumerate() {
179            if item.item_type() == TypeId::of::<T>() {
180                return Some(index + self.left_items.len());
181            }
182        }
183        None
184    }
185
186    pub fn insert_item_after<T>(
187        &mut self,
188        position: usize,
189        item: Entity<T>,
190        window: &mut Window,
191        cx: &mut Context<Self>,
192    ) where
193        T: 'static + StatusItemView,
194    {
195        let active_pane_item = self.active_pane.read(cx).active_item();
196        item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
197
198        if position < self.left_items.len() {
199            self.left_items.insert(position + 1, Box::new(item))
200        } else {
201            self.right_items
202                .insert(position + 1 - self.left_items.len(), Box::new(item))
203        }
204        cx.notify()
205    }
206
207    pub fn remove_item_at(&mut self, position: usize, cx: &mut Context<Self>) {
208        if position < self.left_items.len() {
209            self.left_items.remove(position);
210        } else {
211            self.right_items.remove(position - self.left_items.len());
212        }
213        cx.notify();
214    }
215
216    pub fn add_right_item<T>(
217        &mut self,
218        item: Entity<T>,
219        window: &mut Window,
220        cx: &mut Context<Self>,
221    ) where
222        T: 'static + StatusItemView,
223    {
224        let active_pane_item = self.active_pane.read(cx).active_item();
225        item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
226
227        self.right_items.push(Box::new(item));
228        cx.notify();
229    }
230
231    pub fn set_active_pane(
232        &mut self,
233        active_pane: &Entity<Pane>,
234        window: &mut Window,
235        cx: &mut Context<Self>,
236    ) {
237        self.active_pane = active_pane.clone();
238        self._observe_active_pane = cx.observe_in(active_pane, window, |this, _, window, cx| {
239            this.update_active_pane_item(window, cx)
240        });
241        self.update_active_pane_item(window, cx);
242    }
243
244    fn update_active_pane_item(&mut self, window: &mut Window, cx: &mut Context<Self>) {
245        let active_pane_item = self.active_pane.read(cx).active_item();
246        for item in self.left_items.iter().chain(&self.right_items) {
247            item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
248        }
249    }
250}
251
252impl<T: StatusItemView> StatusItemViewHandle for Entity<T> {
253    fn to_any(&self) -> AnyView {
254        self.clone().into()
255    }
256
257    fn set_active_pane_item(
258        &self,
259        active_pane_item: Option<&dyn ItemHandle>,
260        window: &mut Window,
261        cx: &mut App,
262    ) {
263        self.update(cx, |this, cx| {
264            this.set_active_pane_item(active_pane_item, window, cx)
265        });
266    }
267
268    fn item_type(&self) -> TypeId {
269        TypeId::of::<T>()
270    }
271}
272
273impl From<&dyn StatusItemViewHandle> for AnyView {
274    fn from(val: &dyn StatusItemViewHandle) -> Self {
275        val.to_any()
276    }
277}