1use crate::Vim;
2use editor::{EditorBlurred, EditorFocused, EditorReleased};
3use gpui::AppContext;
4
5pub fn init(cx: &mut AppContext) {
6 cx.subscribe_global(focused).detach();
7 cx.subscribe_global(blurred).detach();
8 cx.subscribe_global(released).detach();
9}
10
11fn focused(EditorFocused(editor): &EditorFocused, cx: &mut AppContext) {
12 cx.update_window(editor.window_id(), |cx| {
13 Vim::update(cx, |vim, cx| {
14 vim.update_active_editor(cx, |previously_active_editor, cx| {
15 Vim::unhook_vim_settings(previously_active_editor, cx);
16 });
17 vim.set_active_editor(editor.clone(), cx);
18 });
19 });
20}
21
22fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut AppContext) {
23 cx.update_window(editor.window_id(), |cx| {
24 Vim::update(cx, |vim, cx| {
25 if let Some(previous_editor) = vim.active_editor.clone() {
26 if previous_editor == editor.clone() {
27 vim.active_editor = None;
28 }
29 }
30
31 cx.update_window(editor.window_id(), |cx| {
32 editor.update(cx, |editor, cx| Vim::unhook_vim_settings(editor, cx))
33 });
34 });
35 });
36}
37
38fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) {
39 cx.update_window(editor.window_id(), |cx| {
40 cx.update_default_global(|vim: &mut Vim, _| {
41 if let Some(previous_editor) = vim.active_editor.clone() {
42 if previous_editor == editor.clone() {
43 vim.active_editor = None;
44 }
45 }
46 });
47 });
48}