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 .overflow_x_hidden()
72 .children(self.left_items.iter().map(|item| item.to_any()))
73 }
74
75 fn render_right_tools(&self) -> impl IntoElement {
76 h_flex()
77 .gap_1()
78 .overflow_x_hidden()
79 .children(self.right_items.iter().rev().map(|item| item.to_any()))
80 }
81}
82
83impl StatusBar {
84 pub fn new(active_pane: &Entity<Pane>, window: &mut Window, cx: &mut Context<Self>) -> Self {
85 let mut this = Self {
86 left_items: Default::default(),
87 right_items: Default::default(),
88 active_pane: active_pane.clone(),
89 _observe_active_pane: cx.observe_in(active_pane, window, |this, _, window, cx| {
90 this.update_active_pane_item(window, cx)
91 }),
92 };
93 this.update_active_pane_item(window, cx);
94 this
95 }
96
97 pub fn add_left_item<T>(&mut self, item: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
98 where
99 T: 'static + StatusItemView,
100 {
101 let active_pane_item = self.active_pane.read(cx).active_item();
102 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
103
104 self.left_items.push(Box::new(item));
105 cx.notify();
106 }
107
108 pub fn item_of_type<T: StatusItemView>(&self) -> Option<Entity<T>> {
109 self.left_items
110 .iter()
111 .chain(self.right_items.iter())
112 .find_map(|item| item.to_any().downcast().log_err())
113 }
114
115 pub fn position_of_item<T>(&self) -> Option<usize>
116 where
117 T: StatusItemView,
118 {
119 for (index, item) in self.left_items.iter().enumerate() {
120 if item.item_type() == TypeId::of::<T>() {
121 return Some(index);
122 }
123 }
124 for (index, item) in self.right_items.iter().enumerate() {
125 if item.item_type() == TypeId::of::<T>() {
126 return Some(index + self.left_items.len());
127 }
128 }
129 None
130 }
131
132 pub fn insert_item_after<T>(
133 &mut self,
134 position: usize,
135 item: Entity<T>,
136 window: &mut Window,
137 cx: &mut Context<Self>,
138 ) where
139 T: 'static + StatusItemView,
140 {
141 let active_pane_item = self.active_pane.read(cx).active_item();
142 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
143
144 if position < self.left_items.len() {
145 self.left_items.insert(position + 1, Box::new(item))
146 } else {
147 self.right_items
148 .insert(position + 1 - self.left_items.len(), Box::new(item))
149 }
150 cx.notify()
151 }
152
153 pub fn remove_item_at(&mut self, position: usize, cx: &mut Context<Self>) {
154 if position < self.left_items.len() {
155 self.left_items.remove(position);
156 } else {
157 self.right_items.remove(position - self.left_items.len());
158 }
159 cx.notify();
160 }
161
162 pub fn add_right_item<T>(
163 &mut self,
164 item: Entity<T>,
165 window: &mut Window,
166 cx: &mut Context<Self>,
167 ) where
168 T: 'static + StatusItemView,
169 {
170 let active_pane_item = self.active_pane.read(cx).active_item();
171 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
172
173 self.right_items.push(Box::new(item));
174 cx.notify();
175 }
176
177 pub fn set_active_pane(
178 &mut self,
179 active_pane: &Entity<Pane>,
180 window: &mut Window,
181 cx: &mut Context<Self>,
182 ) {
183 self.active_pane = active_pane.clone();
184 self._observe_active_pane = cx.observe_in(active_pane, window, |this, _, window, cx| {
185 this.update_active_pane_item(window, cx)
186 });
187 self.update_active_pane_item(window, cx);
188 }
189
190 fn update_active_pane_item(&mut self, window: &mut Window, cx: &mut Context<Self>) {
191 let active_pane_item = self.active_pane.read(cx).active_item();
192 for item in self.left_items.iter().chain(&self.right_items) {
193 item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
194 }
195 }
196}
197
198impl<T: StatusItemView> StatusItemViewHandle for Entity<T> {
199 fn to_any(&self) -> AnyView {
200 self.clone().into()
201 }
202
203 fn set_active_pane_item(
204 &self,
205 active_pane_item: Option<&dyn ItemHandle>,
206 window: &mut Window,
207 cx: &mut App,
208 ) {
209 self.update(cx, |this, cx| {
210 this.set_active_pane_item(active_pane_item, window, cx)
211 });
212 }
213
214 fn item_type(&self) -> TypeId {
215 TypeId::of::<T>()
216 }
217}
218
219impl From<&dyn StatusItemViewHandle> for AnyView {
220 fn from(val: &dyn StatusItemViewHandle) -> Self {
221 val.to_any()
222 }
223}