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 AnyViewHandle, AppContext, DebugContext, ElementBox, Entity, LayoutContext, MeasurementContext,
12 PaintContext, RenderContext, SizeConstraint, Subscription, View, ViewContext, ViewHandle,
13};
14use settings::Settings;
15
16pub trait StatusItemView: View {
17 fn set_active_pane_item(
18 &mut self,
19 active_pane_item: Option<&dyn crate::ItemHandle>,
20 cx: &mut ViewContext<Self>,
21 );
22}
23
24trait StatusItemViewHandle {
25 fn as_any(&self) -> &AnyViewHandle;
26 fn set_active_pane_item(&self, active_pane_item: Option<&dyn ItemHandle>, cx: &mut AppContext);
27}
28
29pub struct StatusBar {
30 left_items: Vec<Box<dyn StatusItemViewHandle>>,
31 right_items: Vec<Box<dyn StatusItemViewHandle>>,
32 active_pane: ViewHandle<Pane>,
33 _observe_active_pane: Subscription,
34}
35
36impl Entity for StatusBar {
37 type Event = ();
38}
39
40impl View for StatusBar {
41 fn ui_name() -> &'static str {
42 "StatusBar"
43 }
44
45 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
46 let theme = &cx.global::<Settings>().theme.workspace.status_bar;
47
48 StatusBarElement {
49 left: Flex::row()
50 .with_children(self.left_items.iter().map(|i| {
51 ChildView::new(i.as_any(), cx)
52 .aligned()
53 .contained()
54 .with_margin_right(theme.item_spacing)
55 .boxed()
56 }))
57 .boxed(),
58
59 right: Flex::row()
60 .with_children(self.right_items.iter().rev().map(|i| {
61 ChildView::new(i.as_any(), cx)
62 .aligned()
63 .contained()
64 .with_margin_left(theme.item_spacing)
65 .boxed()
66 }))
67 .boxed(),
68 }
69 .contained()
70 .with_style(theme.container)
71 .constrained()
72 .with_height(theme.height)
73 .boxed()
74 }
75}
76
77impl StatusBar {
78 pub fn new(active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) -> Self {
79 let mut this = Self {
80 left_items: Default::default(),
81 right_items: Default::default(),
82 active_pane: active_pane.clone(),
83 _observe_active_pane: cx
84 .observe(active_pane, |this, _, cx| this.update_active_pane_item(cx)),
85 };
86 this.update_active_pane_item(cx);
87 this
88 }
89
90 pub fn add_left_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
91 where
92 T: 'static + StatusItemView,
93 {
94 cx.reparent(item.as_any());
95 self.left_items.push(Box::new(item));
96 cx.notify();
97 }
98
99 pub fn add_right_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
100 where
101 T: 'static + StatusItemView,
102 {
103 cx.reparent(item.as_any());
104 self.right_items.push(Box::new(item));
105 cx.notify();
106 }
107
108 pub fn set_active_pane(&mut self, active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
109 self.active_pane = active_pane.clone();
110 self._observe_active_pane =
111 cx.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx));
112 self.update_active_pane_item(cx);
113 }
114
115 fn update_active_pane_item(&mut self, cx: &mut ViewContext<Self>) {
116 let active_pane_item = self.active_pane.read(cx).active_item();
117 for item in self.left_items.iter().chain(&self.right_items) {
118 item.set_active_pane_item(active_pane_item.as_deref(), cx);
119 }
120 }
121}
122
123impl<T: StatusItemView> StatusItemViewHandle for ViewHandle<T> {
124 fn as_any(&self) -> &AnyViewHandle {
125 self
126 }
127
128 fn set_active_pane_item(&self, active_pane_item: Option<&dyn ItemHandle>, cx: &mut AppContext) {
129 self.update(cx, |this, cx| {
130 this.set_active_pane_item(active_pane_item, cx)
131 });
132 }
133}
134
135impl From<&dyn StatusItemViewHandle> for AnyViewHandle {
136 fn from(val: &dyn StatusItemViewHandle) -> Self {
137 val.as_any().clone()
138 }
139}
140
141struct StatusBarElement {
142 left: ElementBox,
143 right: ElementBox,
144}
145
146impl Element for StatusBarElement {
147 type LayoutState = ();
148 type PaintState = ();
149
150 fn layout(
151 &mut self,
152 mut constraint: SizeConstraint,
153 cx: &mut LayoutContext,
154 ) -> (Vector2F, Self::LayoutState) {
155 let max_width = constraint.max.x();
156 constraint.min = vec2f(0., constraint.min.y());
157
158 let right_size = self.right.layout(constraint, cx);
159 let constraint = SizeConstraint::new(
160 vec2f(0., constraint.min.y()),
161 vec2f(max_width - right_size.x(), constraint.max.y()),
162 );
163
164 self.left.layout(constraint, cx);
165
166 (vec2f(max_width, right_size.y()), ())
167 }
168
169 fn paint(
170 &mut self,
171 bounds: RectF,
172 visible_bounds: RectF,
173 _: &mut Self::LayoutState,
174 cx: &mut PaintContext,
175 ) -> Self::PaintState {
176 let origin_y = bounds.upper_right().y();
177 let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
178
179 let left_origin = vec2f(bounds.lower_left().x(), origin_y);
180 self.left.paint(left_origin, visible_bounds, cx);
181
182 let right_origin = vec2f(bounds.upper_right().x() - self.right.size().x(), origin_y);
183 self.right.paint(right_origin, visible_bounds, cx);
184 }
185
186 fn rect_for_text_range(
187 &self,
188 _: Range<usize>,
189 _: RectF,
190 _: RectF,
191 _: &Self::LayoutState,
192 _: &Self::PaintState,
193 _: &MeasurementContext,
194 ) -> Option<RectF> {
195 None
196 }
197
198 fn debug(
199 &self,
200 bounds: RectF,
201 _: &Self::LayoutState,
202 _: &Self::PaintState,
203 _: &DebugContext,
204 ) -> serde_json::Value {
205 json!({
206 "type": "StatusBarElement",
207 "bounds": bounds.to_json()
208 })
209 }
210}