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