1use gpui::{div, Context, Element, Entity, Render, Subscription, WeakEntity, Window};
2use itertools::Itertools;
3use workspace::{item::ItemHandle, ui::prelude::*, StatusItemView};
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);
19 cx.notify();
20 })
21 .detach();
22
23 let handle = cx.entity().clone();
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().clone();
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) {
54 self.pending_keys = window.pending_input_keystrokes().map(|keystrokes| {
55 keystrokes
56 .iter()
57 .map(|keystroke| format!("{}", keystroke))
58 .join(" ")
59 });
60 }
61
62 fn vim(&self) -> Option<Entity<Vim>> {
63 self.vim.as_ref().and_then(|vim| vim.upgrade())
64 }
65
66 fn current_operators_description(&self, vim: Entity<Vim>, cx: &mut Context<Self>) -> String {
67 let recording = Vim::globals(cx)
68 .recording_register
69 .map(|reg| format!("recording @{reg} "))
70 .into_iter();
71
72 let vim = vim.read(cx);
73 recording
74 .chain(
75 cx.global::<VimGlobals>()
76 .pre_count
77 .map(|count| format!("{}", count)),
78 )
79 .chain(vim.selected_register.map(|reg| format!("\"{reg}")))
80 .chain(
81 vim.operator_stack
82 .iter()
83 .map(|item| item.status().to_string()),
84 )
85 .chain(
86 cx.global::<VimGlobals>()
87 .post_count
88 .map(|count| format!("{}", count)),
89 )
90 .collect::<Vec<_>>()
91 .join("")
92 }
93}
94
95impl Render for ModeIndicator {
96 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
97 let vim = self.vim();
98 let Some(vim) = vim else {
99 return div().into_any();
100 };
101
102 let vim_readable = vim.read(cx);
103 let label = if let Some(label) = vim_readable.status_label.clone() {
104 label
105 } else {
106 let mode = if vim_readable.temp_mode {
107 format!("(insert) {}", vim_readable.mode)
108 } else {
109 vim_readable.mode.to_string()
110 };
111
112 let current_operators_description = self.current_operators_description(vim.clone(), cx);
113 let pending = self
114 .pending_keys
115 .as_ref()
116 .unwrap_or(¤t_operators_description);
117 format!("{} -- {} --", pending, mode).into()
118 };
119
120 Label::new(label)
121 .size(LabelSize::Small)
122 .line_height_style(LineHeightStyle::UiLabel)
123 .into_any_element()
124 }
125}
126
127impl StatusItemView for ModeIndicator {
128 fn set_active_pane_item(
129 &mut self,
130 _active_pane_item: Option<&dyn ItemHandle>,
131 _window: &mut Window,
132 _cx: &mut Context<Self>,
133 ) {
134 }
135}