1use gpui::{Context, Element, Entity, Render, Subscription, WeakEntity, Window, div};
2use ui::text_for_keystrokes;
3use workspace::{StatusItemView, item::ItemHandle, ui::prelude::*};
4
5use crate::{Vim, VimEvent, VimGlobals};
6
7/// The ModeIndicator displays the current mode in the status bar.
8pub struct ModeIndicator {
9 vim: Option<WeakEntity<Vim>>,
10 pending_keys: Option<String>,
11 vim_subscription: Option<Subscription>,
12}
13
14impl ModeIndicator {
15 /// Construct a new mode indicator in this window.
16 pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
17 cx.observe_pending_input(window, |this: &mut Self, window, cx| {
18 this.update_pending_keys(window, cx);
19 cx.notify();
20 })
21 .detach();
22
23 let handle = cx.entity();
24 let window_handle = window.window_handle();
25 cx.observe_new::<Vim>(move |_, window, cx| {
26 let Some(window) = window else {
27 return;
28 };
29 if window.window_handle() != window_handle {
30 return;
31 }
32 let vim = cx.entity();
33 handle.update(cx, |_, cx| {
34 cx.subscribe(&vim, |mode_indicator, vim, event, cx| match event {
35 VimEvent::Focused => {
36 mode_indicator.vim_subscription =
37 Some(cx.observe(&vim, |_, _, cx| cx.notify()));
38 mode_indicator.vim = Some(vim.downgrade());
39 }
40 })
41 .detach()
42 })
43 })
44 .detach();
45
46 Self {
47 vim: None,
48 pending_keys: None,
49 vim_subscription: None,
50 }
51 }
52
53 fn update_pending_keys(&mut self, window: &mut Window, cx: &App) {
54 self.pending_keys = window
55 .pending_input_keystrokes()
56 .map(|keystrokes| text_for_keystrokes(keystrokes, cx));
57 }
58
59 fn vim(&self) -> Option<Entity<Vim>> {
60 self.vim.as_ref().and_then(|vim| vim.upgrade())
61 }
62
63 fn current_operators_description(&self, vim: Entity<Vim>, cx: &mut Context<Self>) -> String {
64 let recording = Vim::globals(cx)
65 .recording_register
66 .map(|reg| format!("recording @{reg} "))
67 .into_iter();
68
69 let vim = vim.read(cx);
70 recording
71 .chain(
72 cx.global::<VimGlobals>()
73 .pre_count
74 .map(|count| format!("{}", count)),
75 )
76 .chain(vim.selected_register.map(|reg| format!("\"{reg}")))
77 .chain(
78 vim.operator_stack
79 .iter()
80 .map(|item| item.status().to_string()),
81 )
82 .chain(
83 cx.global::<VimGlobals>()
84 .post_count
85 .map(|count| format!("{}", count)),
86 )
87 .collect::<Vec<_>>()
88 .join("")
89 }
90}
91
92impl Render for ModeIndicator {
93 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
94 let vim = self.vim();
95 let Some(vim) = vim else {
96 return div().into_any();
97 };
98
99 let vim_readable = vim.read(cx);
100 let label = if let Some(label) = vim_readable.status_label.clone() {
101 label
102 } else {
103 let mode = if vim_readable.temp_mode {
104 format!("(insert) {}", vim_readable.mode)
105 } else {
106 vim_readable.mode.to_string()
107 };
108
109 let current_operators_description = self.current_operators_description(vim.clone(), cx);
110 let pending = self
111 .pending_keys
112 .as_ref()
113 .unwrap_or(¤t_operators_description);
114 format!("{} -- {} --", pending, mode).into()
115 };
116
117 Label::new(label)
118 .size(LabelSize::Small)
119 .line_height_style(LineHeightStyle::UiLabel)
120 .into_any_element()
121 }
122}
123
124impl StatusItemView for ModeIndicator {
125 fn set_active_pane_item(
126 &mut self,
127 _active_pane_item: Option<&dyn ItemHandle>,
128 _window: &mut Window,
129 _cx: &mut Context<Self>,
130 ) {
131 }
132}