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