1use crate::{ItemViewHandle, Pane, Settings};
2use gpui::{
3 elements::*, AnyViewHandle, ElementBox, Entity, MutableAppContext, RenderContext, Subscription,
4 View, ViewContext, ViewHandle,
5};
6
7pub trait StatusItemView: View {
8 fn set_active_pane_item(
9 &mut self,
10 active_pane_item: Option<&dyn crate::ItemViewHandle>,
11 cx: &mut ViewContext<Self>,
12 );
13}
14
15trait StatusItemViewHandle {
16 fn to_any(&self) -> AnyViewHandle;
17 fn set_active_pane_item(
18 &self,
19 active_pane_item: Option<&dyn ItemViewHandle>,
20 cx: &mut MutableAppContext,
21 );
22}
23
24pub struct StatusBar {
25 left_items: Vec<Box<dyn StatusItemViewHandle>>,
26 right_items: Vec<Box<dyn StatusItemViewHandle>>,
27 active_pane: ViewHandle<Pane>,
28 _observe_active_pane: Subscription,
29}
30
31impl Entity for StatusBar {
32 type Event = ();
33}
34
35impl View for StatusBar {
36 fn ui_name() -> &'static str {
37 "StatusBar"
38 }
39
40 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
41 let theme = &cx.app_state::<Settings>().theme.workspace.status_bar;
42 Flex::row()
43 .with_children(self.left_items.iter().map(|i| {
44 ChildView::new(i.as_ref())
45 .aligned()
46 .contained()
47 .with_margin_right(theme.item_spacing)
48 .boxed()
49 }))
50 .with_child(Empty::new().flexible(1., true).boxed())
51 .with_children(self.right_items.iter().map(|i| {
52 ChildView::new(i.as_ref())
53 .aligned()
54 .contained()
55 .with_margin_left(theme.item_spacing)
56 .boxed()
57 }))
58 .contained()
59 .with_style(theme.container)
60 .constrained()
61 .with_height(theme.height)
62 .boxed()
63 }
64}
65
66impl StatusBar {
67 pub fn new(active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) -> Self {
68 let mut this = Self {
69 left_items: Default::default(),
70 right_items: Default::default(),
71 active_pane: active_pane.clone(),
72 _observe_active_pane: cx
73 .observe(active_pane, |this, _, cx| this.update_active_pane_item(cx)),
74 };
75 this.update_active_pane_item(cx);
76 this
77 }
78
79 pub fn add_left_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
80 where
81 T: 'static + StatusItemView,
82 {
83 self.left_items.push(Box::new(item));
84 cx.notify();
85 }
86
87 pub fn add_right_item<T>(&mut self, item: ViewHandle<T>, cx: &mut ViewContext<Self>)
88 where
89 T: 'static + StatusItemView,
90 {
91 self.right_items.push(Box::new(item));
92 cx.notify();
93 }
94
95 pub fn set_active_pane(&mut self, active_pane: &ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
96 self.active_pane = active_pane.clone();
97 self._observe_active_pane =
98 cx.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx));
99 self.update_active_pane_item(cx);
100 }
101
102 fn update_active_pane_item(&mut self, cx: &mut ViewContext<Self>) {
103 let active_pane_item = self.active_pane.read(cx).active_item();
104 for item in self.left_items.iter().chain(&self.right_items) {
105 item.set_active_pane_item(active_pane_item.as_deref(), cx);
106 }
107 }
108}
109
110impl<T: StatusItemView> StatusItemViewHandle for ViewHandle<T> {
111 fn to_any(&self) -> AnyViewHandle {
112 self.into()
113 }
114
115 fn set_active_pane_item(
116 &self,
117 active_pane_item: Option<&dyn ItemViewHandle>,
118 cx: &mut MutableAppContext,
119 ) {
120 self.update(cx, |this, cx| {
121 this.set_active_pane_item(active_pane_item, cx)
122 });
123 }
124}
125
126impl Into<AnyViewHandle> for &dyn StatusItemViewHandle {
127 fn into(self) -> AnyViewHandle {
128 self.to_any()
129 }
130}