1use crate::{ItemHandle, Pane, ToggleWorkspaceSidebar};
2use gpui::{
3 Action, 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(|_, window, cx| {
108 window.dispatch_action(ToggleWorkspaceSidebar.boxed_clone(), cx);
109 }),
110 )
111 .child(Divider::vertical().color(ui::DividerColor::Border))
112 }
113}
114
115impl StatusBar {
116 pub fn new(active_pane: &Entity<Pane>, window: &mut Window, cx: &mut Context<Self>) -> Self {
117 let mut this = Self {
118 left_items: Default::default(),
119 right_items: Default::default(),
120 active_pane: active_pane.clone(),
121 _observe_active_pane: cx.observe_in(active_pane, window, |this, _, window, cx| {
122 this.update_active_pane_item(window, cx)
123 }),
124 workspace_sidebar_open: false,
125 sidebar_has_notifications: false,
126 show_sidebar_toggle: false,
127 };
128 this.update_active_pane_item(window, cx);
129 this
130 }
131
132 pub fn set_workspace_sidebar_open(&mut self, open: bool, cx: &mut Context<Self>) {
133 self.workspace_sidebar_open = open;
134 cx.notify();
135 }
136
137 pub fn set_sidebar_has_notifications(&mut self, has: bool, cx: &mut Context<Self>) {
138 self.sidebar_has_notifications = has;
139 cx.notify();
140 }
141
142 pub fn set_show_sidebar_toggle(&mut self, show: bool, cx: &mut Context<Self>) {
143 self.show_sidebar_toggle = show;
144 cx.notify();
145 }
146
147 pub fn add_left_item<T>(&mut self, item: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
148 where
149 T: 'static + StatusItemView,
150 {
151 let active_pane_item = self.active_pane.read(cx).active_item();
152 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
153
154 self.left_items.push(Box::new(item));
155 cx.notify();
156 }
157
158 pub fn item_of_type<T: StatusItemView>(&self) -> Option<Entity<T>> {
159 self.left_items
160 .iter()
161 .chain(self.right_items.iter())
162 .find_map(|item| item.to_any().downcast().log_err())
163 }
164
165 pub fn position_of_item<T>(&self) -> Option<usize>
166 where
167 T: StatusItemView,
168 {
169 for (index, item) in self.left_items.iter().enumerate() {
170 if item.item_type() == TypeId::of::<T>() {
171 return Some(index);
172 }
173 }
174 for (index, item) in self.right_items.iter().enumerate() {
175 if item.item_type() == TypeId::of::<T>() {
176 return Some(index + self.left_items.len());
177 }
178 }
179 None
180 }
181
182 pub fn insert_item_after<T>(
183 &mut self,
184 position: usize,
185 item: Entity<T>,
186 window: &mut Window,
187 cx: &mut Context<Self>,
188 ) where
189 T: 'static + StatusItemView,
190 {
191 let active_pane_item = self.active_pane.read(cx).active_item();
192 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
193
194 if position < self.left_items.len() {
195 self.left_items.insert(position + 1, Box::new(item))
196 } else {
197 self.right_items
198 .insert(position + 1 - self.left_items.len(), Box::new(item))
199 }
200 cx.notify()
201 }
202
203 pub fn remove_item_at(&mut self, position: usize, cx: &mut Context<Self>) {
204 if position < self.left_items.len() {
205 self.left_items.remove(position);
206 } else {
207 self.right_items.remove(position - self.left_items.len());
208 }
209 cx.notify();
210 }
211
212 pub fn add_right_item<T>(
213 &mut self,
214 item: Entity<T>,
215 window: &mut Window,
216 cx: &mut Context<Self>,
217 ) where
218 T: 'static + StatusItemView,
219 {
220 let active_pane_item = self.active_pane.read(cx).active_item();
221 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
222
223 self.right_items.push(Box::new(item));
224 cx.notify();
225 }
226
227 pub fn set_active_pane(
228 &mut self,
229 active_pane: &Entity<Pane>,
230 window: &mut Window,
231 cx: &mut Context<Self>,
232 ) {
233 self.active_pane = active_pane.clone();
234 self._observe_active_pane = cx.observe_in(active_pane, window, |this, _, window, cx| {
235 this.update_active_pane_item(window, cx)
236 });
237 self.update_active_pane_item(window, cx);
238 }
239
240 fn update_active_pane_item(&mut self, window: &mut Window, cx: &mut Context<Self>) {
241 let active_pane_item = self.active_pane.read(cx).active_item();
242 for item in self.left_items.iter().chain(&self.right_items) {
243 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
244 }
245 }
246}
247
248impl<T: StatusItemView> StatusItemViewHandle for Entity<T> {
249 fn to_any(&self) -> AnyView {
250 self.clone().into()
251 }
252
253 fn set_active_pane_item(
254 &self,
255 active_pane_item: Option<&dyn ItemHandle>,
256 window: &mut Window,
257 cx: &mut App,
258 ) {
259 self.update(cx, |this, cx| {
260 this.set_active_pane_item(active_pane_item, window, cx)
261 });
262 }
263
264 fn item_type(&self) -> TypeId {
265 TypeId::of::<T>()
266 }
267}
268
269impl From<&dyn StatusItemViewHandle> for AnyView {
270 fn from(val: &dyn StatusItemViewHandle) -> Self {
271 val.to_any()
272 }
273}