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