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, LayoutContext, SceneBuilder, SizeConstraint, Subscription,
12 View, 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 self.left_items.push(Box::new(item));
97 cx.notify();
98 }
99
100 pub fn add_right_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
101 where
102 T: 'static + StatusItemView,
103 {
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(
129 &self,
130 active_pane_item: Option<&dyn ItemHandle>,
131 cx: &mut WindowContext,
132 ) {
133 self.update(cx, |this, cx| {
134 this.set_active_pane_item(active_pane_item, cx)
135 });
136 }
137}
138
139impl From<&dyn StatusItemViewHandle> for AnyViewHandle {
140 fn from(val: &dyn StatusItemViewHandle) -> Self {
141 val.as_any().clone()
142 }
143}
144
145struct StatusBarElement {
146 left: AnyElement<StatusBar>,
147 right: AnyElement<StatusBar>,
148}
149
150impl Element<StatusBar> for StatusBarElement {
151 type LayoutState = ();
152 type PaintState = ();
153
154 fn layout(
155 &mut self,
156 mut constraint: SizeConstraint,
157 view: &mut StatusBar,
158 cx: &mut LayoutContext<StatusBar>,
159 ) -> (Vector2F, Self::LayoutState) {
160 let max_width = constraint.max.x();
161 constraint.min = vec2f(0., constraint.min.y());
162
163 let right_size = self.right.layout(constraint, view, cx);
164 let constraint = SizeConstraint::new(
165 vec2f(0., constraint.min.y()),
166 vec2f(max_width - right_size.x(), constraint.max.y()),
167 );
168
169 self.left.layout(constraint, view, cx);
170
171 (vec2f(max_width, right_size.y()), ())
172 }
173
174 fn paint(
175 &mut self,
176 scene: &mut SceneBuilder,
177 bounds: RectF,
178 visible_bounds: RectF,
179 _: &mut Self::LayoutState,
180 view: &mut StatusBar,
181 cx: &mut ViewContext<StatusBar>,
182 ) -> Self::PaintState {
183 let origin_y = bounds.upper_right().y();
184 let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
185
186 let left_origin = vec2f(bounds.lower_left().x(), origin_y);
187 self.left
188 .paint(scene, left_origin, visible_bounds, view, cx);
189
190 let right_origin = vec2f(bounds.upper_right().x() - self.right.size().x(), origin_y);
191 self.right
192 .paint(scene, right_origin, visible_bounds, view, cx);
193 }
194
195 fn rect_for_text_range(
196 &self,
197 _: Range<usize>,
198 _: RectF,
199 _: RectF,
200 _: &Self::LayoutState,
201 _: &Self::PaintState,
202 _: &StatusBar,
203 _: &ViewContext<StatusBar>,
204 ) -> Option<RectF> {
205 None
206 }
207
208 fn debug(
209 &self,
210 bounds: RectF,
211 _: &Self::LayoutState,
212 _: &Self::PaintState,
213 _: &StatusBar,
214 _: &ViewContext<StatusBar>,
215 ) -> serde_json::Value {
216 json!({
217 "type": "StatusBarElement",
218 "bounds": bounds.to_json()
219 })
220 }
221}