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