mode_indicator.rs

 1use gpui::{elements::Label, AnyElement, Element, Entity, View, ViewContext};
 2use workspace::{item::ItemHandle, StatusItemView};
 3
 4use crate::state::Mode;
 5
 6pub struct ModeIndicator {
 7    pub mode: Mode,
 8}
 9
10impl ModeIndicator {
11    pub fn new(mode: Mode) -> Self {
12        Self { mode }
13    }
14
15    pub fn set_mode(&mut self, mode: Mode, cx: &mut ViewContext<Self>) {
16        if mode != self.mode {
17            self.mode = mode;
18            cx.notify();
19        }
20    }
21}
22
23impl Entity for ModeIndicator {
24    type Event = ();
25}
26
27impl View for ModeIndicator {
28    fn ui_name() -> &'static str {
29        "ModeIndicatorView"
30    }
31
32    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
33        let theme = &theme::current(cx).workspace.status_bar;
34        // we always choose text to be 12 monospace characters
35        // so that as the mode indicator changes, the rest of the
36        // UI stays still.
37        let text = match self.mode {
38            Mode::Normal => "-- NORMAL --",
39            Mode::Insert => "-- INSERT --",
40            Mode::Visual { line: false } => "-- VISUAL --",
41            Mode::Visual { line: true } => "VISUAL LINE ",
42        };
43        Label::new(text, theme.vim_mode_indicator.text.clone())
44            .contained()
45            .with_style(theme.vim_mode_indicator.container)
46            .into_any()
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}