status_bar.rs

  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, Element, Entity, SceneBuilder, SizeConstraint, Subscription,
 12    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 ViewContext<Self>) -> Element<Self> {
 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: Element<StatusBar>,
143    right: Element<StatusBar>,
144}
145
146impl Drawable<StatusBar> for StatusBarElement {
147    type LayoutState = ();
148    type PaintState = ();
149
150    fn layout(
151        &mut self,
152        mut constraint: SizeConstraint,
153        view: &mut StatusBar,
154        cx: &mut ViewContext<StatusBar>,
155    ) -> (Vector2F, Self::LayoutState) {
156        let max_width = constraint.max.x();
157        constraint.min = vec2f(0., constraint.min.y());
158
159        let right_size = self.right.layout(constraint, view, cx);
160        let constraint = SizeConstraint::new(
161            vec2f(0., constraint.min.y()),
162            vec2f(max_width - right_size.x(), constraint.max.y()),
163        );
164
165        self.left.layout(constraint, view, cx);
166
167        (vec2f(max_width, right_size.y()), ())
168    }
169
170    fn paint(
171        &mut self,
172        scene: &mut SceneBuilder,
173        bounds: RectF,
174        visible_bounds: RectF,
175        _: &mut Self::LayoutState,
176        view: &mut StatusBar,
177        cx: &mut ViewContext<StatusBar>,
178    ) -> Self::PaintState {
179        let origin_y = bounds.upper_right().y();
180        let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();
181
182        let left_origin = vec2f(bounds.lower_left().x(), origin_y);
183        self.left
184            .paint(scene, left_origin, visible_bounds, view, cx);
185
186        let right_origin = vec2f(bounds.upper_right().x() - self.right.size().x(), origin_y);
187        self.right
188            .paint(scene, right_origin, visible_bounds, view, cx);
189    }
190
191    fn rect_for_text_range(
192        &self,
193        _: Range<usize>,
194        _: RectF,
195        _: RectF,
196        _: &Self::LayoutState,
197        _: &Self::PaintState,
198        _: &StatusBar,
199        _: &ViewContext<StatusBar>,
200    ) -> Option<RectF> {
201        None
202    }
203
204    fn debug(
205        &self,
206        bounds: RectF,
207        _: &Self::LayoutState,
208        _: &Self::PaintState,
209        _: &StatusBar,
210        _: &ViewContext<StatusBar>,
211    ) -> serde_json::Value {
212        json!({
213            "type": "StatusBarElement",
214            "bounds": bounds.to_json()
215        })
216    }
217}