1use std::ops::Range;
2
3use crate::{ItemHandle, Pane};
4use gpui::{
5 elements::*,
6 geometry::{
7 rect::RectF,
8 vector::{vec2f, Vector2F},
9 },
10 json::{json, ToJson},
11 AnyElement, AnyViewHandle, Entity, SizeConstraint, Subscription, View, ViewContext, ViewHandle,
12 WindowContext,
13};
14
15pub trait StatusItemView: View {
16 fn set_active_pane_item(
17 &mut self,
18 active_pane_item: Option<&dyn crate::ItemHandle>,
19 cx: &mut ViewContext<Self>,
20 );
21}
22
23trait StatusItemViewHandle {
24 fn as_any(&self) -> &AnyViewHandle;
25 fn set_active_pane_item(
26 &self,
27 active_pane_item: Option<&dyn ItemHandle>,
28 cx: &mut WindowContext,
29 );
30 fn ui_name(&self) -> &'static str;
31}
32
33pub struct StatusBar {
34 left_items: Vec<Box<dyn StatusItemViewHandle>>,
35 right_items: Vec<Box<dyn StatusItemViewHandle>>,
36 active_pane: ViewHandle<Pane>,
37 _observe_active_pane: Subscription,
38}
39
40impl Entity for StatusBar {
41 type Event = ();
42}
43
44impl View for StatusBar {
45 fn ui_name() -> &'static str {
46 "StatusBar"
47 }
48
49 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
50 let theme = &theme::current(cx).workspace.status_bar;
51
52 StatusBarElement {
53 left: Flex::row()
54 .with_children(self.left_items.iter().map(|i| {
55 ChildView::new(i.as_any(), cx)
56 .aligned()
57 .contained()
58 .with_margin_right(theme.item_spacing)
59 }))
60 .into_any(),
61 right: Flex::row()
62 .with_children(self.right_items.iter().rev().map(|i| {
63 ChildView::new(i.as_any(), cx)
64 .aligned()
65 .contained()
66 .with_margin_left(theme.item_spacing)
67 }))
68 .into_any(),
69 }
70 .contained()
71 .with_style(theme.container)
72 .constrained()
73 .with_height(theme.height)
74 .into_any()
75 }
76}
77
78impl StatusBar {
79 pub fn new(active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) -> Self {
80 let mut this = Self {
81 left_items: Default::default(),
82 right_items: Default::default(),
83 active_pane: active_pane.clone(),
84 _observe_active_pane: cx
85 .observe(active_pane, |this, _, cx| this.update_active_pane_item(cx)),
86 };
87 this.update_active_pane_item(cx);
88 this
89 }
90
91 pub fn add_left_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
92 where
93 T: 'static + StatusItemView,
94 {
95 self.left_items.push(Box::new(item));
96 cx.notify();
97 }
98
99 pub fn item_of_type<T: StatusItemView>(&self) -> Option<ViewHandle<T>> {
100 self.left_items
101 .iter()
102 .chain(self.right_items.iter())
103 .find_map(|item| item.as_any().clone().downcast())
104 }
105
106 pub fn position_of_item<T>(&self) -> Option<usize>
107 where
108 T: StatusItemView,
109 {
110 for (index, item) in self.left_items.iter().enumerate() {
111 if item.as_ref().ui_name() == T::ui_name() {
112 return Some(index);
113 }
114 }
115 for (index, item) in self.right_items.iter().enumerate() {
116 if item.as_ref().ui_name() == T::ui_name() {
117 return Some(index + self.left_items.len());
118 }
119 }
120 return None;
121 }
122
123 pub fn insert_item_after<T>(
124 &mut self,
125 position: usize,
126 item: ViewHandle<T>,
127 cx: &mut ViewContext<Self>,
128 ) where
129 T: 'static + StatusItemView,
130 {
131 if position < self.left_items.len() {
132 self.left_items.insert(position + 1, Box::new(item))
133 } else {
134 self.right_items
135 .insert(position + 1 - self.left_items.len(), Box::new(item))
136 }
137 cx.notify()
138 }
139
140 pub fn remove_item_at(&mut self, position: usize, cx: &mut ViewContext<Self>) {
141 if position < self.left_items.len() {
142 self.left_items.remove(position);
143 } else {
144 self.right_items.remove(position - self.left_items.len());
145 }
146 cx.notify();
147 }
148
149 pub fn add_right_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
150 where
151 T: 'static + StatusItemView,
152 {
153 self.right_items.push(Box::new(item));
154 cx.notify();
155 }
156
157 pub fn set_active_pane(&mut self, active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
158 self.active_pane = active_pane.clone();
159 self._observe_active_pane =
160 cx.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx));
161 self.update_active_pane_item(cx);
162 }
163
164 fn update_active_pane_item(&mut self, cx: &mut ViewContext<Self>) {
165 let active_pane_item = self.active_pane.read(cx).active_item();
166 for item in self.left_items.iter().chain(&self.right_items) {
167 item.set_active_pane_item(active_pane_item.as_deref(), cx);
168 }
169 }
170}
171
172impl<T: StatusItemView> StatusItemViewHandle for ViewHandle<T> {
173 fn as_any(&self) -> &AnyViewHandle {
174 self
175 }
176
177 fn set_active_pane_item(
178 &self,
179 active_pane_item: Option<&dyn ItemHandle>,
180 cx: &mut WindowContext,
181 ) {
182 self.update(cx, |this, cx| {
183 this.set_active_pane_item(active_pane_item, cx)
184 });
185 }
186
187 fn ui_name(&self) -> &'static str {
188 T::ui_name()
189 }
190}
191
192impl From<&dyn StatusItemViewHandle> for AnyViewHandle {
193 fn from(val: &dyn StatusItemViewHandle) -> Self {
194 val.as_any().clone()
195 }
196}
197
198struct StatusBarElement {
199 left: AnyElement<StatusBar>,
200 right: AnyElement<StatusBar>,
201}
202
203impl Element<StatusBar> for StatusBarElement {
204 type LayoutState = ();
205 type PaintState = ();
206
207 fn layout(
208 &mut self,
209 mut constraint: SizeConstraint,
210 view: &mut StatusBar,
211 cx: &mut ViewContext<StatusBar>,
212 ) -> (Vector2F, Self::LayoutState) {
213 let max_width = constraint.max.x();
214 constraint.min = vec2f(0., constraint.min.y());
215
216 let right_size = self.right.layout(constraint, view, cx);
217 let constraint = SizeConstraint::new(
218 vec2f(0., constraint.min.y()),
219 vec2f(max_width - right_size.x(), constraint.max.y()),
220 );
221
222 self.left.layout(constraint, view, cx);
223
224 (vec2f(max_width, right_size.y()), ())
225 }
226
227 fn paint(
228 &mut self,
229 bounds: RectF,
230 visible_bounds: RectF,
231 _: &mut Self::LayoutState,
232 view: &mut StatusBar,
233 cx: &mut ViewContext<StatusBar>,
234 ) -> Self::PaintState {
235 let origin_y = bounds.upper_right().y();
236 let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
237
238 let left_origin = vec2f(bounds.lower_left().x(), origin_y);
239 self.left.paint(left_origin, visible_bounds, view, cx);
240
241 let right_origin = vec2f(bounds.upper_right().x() - self.right.size().x(), origin_y);
242 self.right.paint(right_origin, visible_bounds, view, cx);
243 }
244
245 fn rect_for_text_range(
246 &self,
247 _: Range<usize>,
248 _: RectF,
249 _: RectF,
250 _: &Self::LayoutState,
251 _: &Self::PaintState,
252 _: &StatusBar,
253 _: &ViewContext<StatusBar>,
254 ) -> Option<RectF> {
255 None
256 }
257
258 fn debug(
259 &self,
260 bounds: RectF,
261 _: &Self::LayoutState,
262 _: &Self::PaintState,
263 _: &StatusBar,
264 _: &ViewContext<StatusBar>,
265 ) -> serde_json::Value {
266 json!({
267 "type": "StatusBarElement",
268 "bounds": bounds.to_json()
269 })
270 }
271}