mode_indicator.rs

 1use gpui::{div, Element, Render, Subscription, ViewContext};
 2use workspace::{item::ItemHandle, ui::prelude::*, StatusItemView};
 3
 4use crate::{state::Mode, Vim};
 5
 6/// The ModeIndicator displays the current mode in the status bar.
 7pub struct ModeIndicator {
 8    pub(crate) mode: Option<Mode>,
 9    _subscription: Subscription,
10}
11
12impl ModeIndicator {
13    /// Construct a new mode indicator in this window.
14    pub fn new(cx: &mut ViewContext<Self>) -> Self {
15        let _subscription = cx.observe_global::<Vim>(|this, cx| this.update_mode(cx));
16        let mut this = Self {
17            mode: None,
18            _subscription,
19        };
20        this.update_mode(cx);
21        this
22    }
23
24    fn update_mode(&mut self, cx: &mut ViewContext<Self>) {
25        // Vim doesn't exist in some tests
26        let Some(vim) = cx.try_global::<Vim>() else {
27            return;
28        };
29
30        if vim.enabled {
31            self.mode = Some(vim.state().mode);
32        } else {
33            self.mode = None;
34        }
35    }
36}
37
38impl Render for ModeIndicator {
39    fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
40        let Some(mode) = self.mode.as_ref() else {
41            return div().into_any();
42        };
43
44        Label::new(format!("-- {} --", mode))
45            .size(LabelSize::Small)
46            .into_any_element()
47    }
48}
49
50impl StatusItemView for ModeIndicator {
51    fn set_active_pane_item(
52        &mut self,
53        _active_pane_item: Option<&dyn ItemHandle>,
54        _cx: &mut ViewContext<Self>,
55    ) {
56        // nothing to do.
57    }
58}