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, SceneBuilder, SizeConstraint, Subscription, View,
12 ViewContext, ViewHandle, WindowContext,
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(
27 &self,
28 active_pane_item: Option<&dyn ItemHandle>,
29 cx: &mut WindowContext,
30 );
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 = &cx.global::<Settings>().theme.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
62 right: Flex::row()
63 .with_children(self.right_items.iter().rev().map(|i| {
64 ChildView::new(i.as_any(), cx)
65 .aligned()
66 .contained()
67 .with_margin_left(theme.item_spacing)
68 }))
69 .into_any(),
70 }
71 .contained()
72 .with_style(theme.container)
73 .constrained()
74 .with_height(theme.height)
75 .into_any()
76 }
77}
78
79impl StatusBar {
80 pub fn new(active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) -> Self {
81 let mut this = Self {
82 left_items: Default::default(),
83 right_items: Default::default(),
84 active_pane: active_pane.clone(),
85 _observe_active_pane: cx
86 .observe(active_pane, |this, _, cx| this.update_active_pane_item(cx)),
87 };
88 this.update_active_pane_item(cx);
89 this
90 }
91
92 pub fn add_left_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
93 where
94 T: 'static + StatusItemView,
95 {
96 cx.reparent(item.as_any());
97 self.left_items.push(Box::new(item));
98 cx.notify();
99 }
100
101 pub fn add_right_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
102 where
103 T: 'static + StatusItemView,
104 {
105 cx.reparent(item.as_any());
106 self.right_items.push(Box::new(item));
107 cx.notify();
108 }
109
110 pub fn set_active_pane(&mut self, active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
111 self.active_pane = active_pane.clone();
112 self._observe_active_pane =
113 cx.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx));
114 self.update_active_pane_item(cx);
115 }
116
117 fn update_active_pane_item(&mut self, cx: &mut ViewContext<Self>) {
118 let active_pane_item = self.active_pane.read(cx).active_item();
119 for item in self.left_items.iter().chain(&self.right_items) {
120 item.set_active_pane_item(active_pane_item.as_deref(), cx);
121 }
122 }
123}
124
125impl<T: StatusItemView> StatusItemViewHandle for ViewHandle<T> {
126 fn as_any(&self) -> &AnyViewHandle {
127 self
128 }
129
130 fn set_active_pane_item(
131 &self,
132 active_pane_item: Option<&dyn ItemHandle>,
133 cx: &mut WindowContext,
134 ) {
135 self.update(cx, |this, cx| {
136 this.set_active_pane_item(active_pane_item, cx)
137 });
138 }
139}
140
141impl From<&dyn StatusItemViewHandle> for AnyViewHandle {
142 fn from(val: &dyn StatusItemViewHandle) -> Self {
143 val.as_any().clone()
144 }
145}
146
147struct StatusBarElement {
148 left: AnyElement<StatusBar>,
149 right: AnyElement<StatusBar>,
150}
151
152impl Element<StatusBar> for StatusBarElement {
153 type LayoutState = ();
154 type PaintState = ();
155
156 fn layout(
157 &mut self,
158 mut constraint: SizeConstraint,
159 view: &mut StatusBar,
160 cx: &mut ViewContext<StatusBar>,
161 ) -> (Vector2F, Self::LayoutState) {
162 let max_width = constraint.max.x();
163 constraint.min = vec2f(0., constraint.min.y());
164
165 let right_size = self.right.layout(constraint, view, cx);
166 let constraint = SizeConstraint::new(
167 vec2f(0., constraint.min.y()),
168 vec2f(max_width - right_size.x(), constraint.max.y()),
169 );
170
171 self.left.layout(constraint, view, cx);
172
173 (vec2f(max_width, right_size.y()), ())
174 }
175
176 fn paint(
177 &mut self,
178 scene: &mut SceneBuilder,
179 bounds: RectF,
180 visible_bounds: RectF,
181 _: &mut Self::LayoutState,
182 view: &mut StatusBar,
183 cx: &mut ViewContext<StatusBar>,
184 ) -> Self::PaintState {
185 let origin_y = bounds.upper_right().y();
186 let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
187
188 let left_origin = vec2f(bounds.lower_left().x(), origin_y);
189 self.left
190 .paint(scene, left_origin, visible_bounds, view, cx);
191
192 let right_origin = vec2f(bounds.upper_right().x() - self.right.size().x(), origin_y);
193 self.right
194 .paint(scene, right_origin, visible_bounds, view, cx);
195 }
196
197 fn rect_for_text_range(
198 &self,
199 _: Range<usize>,
200 _: RectF,
201 _: RectF,
202 _: &Self::LayoutState,
203 _: &Self::PaintState,
204 _: &StatusBar,
205 _: &ViewContext<StatusBar>,
206 ) -> Option<RectF> {
207 None
208 }
209
210 fn debug(
211 &self,
212 bounds: RectF,
213 _: &Self::LayoutState,
214 _: &Self::PaintState,
215 _: &StatusBar,
216 _: &ViewContext<StatusBar>,
217 ) -> serde_json::Value {
218 json!({
219 "type": "StatusBarElement",
220 "bounds": bounds.to_json()
221 })
222 }
223}