editor_events.rs

 1use crate::{Vim, VimEvent};
 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    if let Some(previously_active_editor) = Vim::read(cx).active_editor.clone() {
13        previously_active_editor.window().update(cx, |cx| {
14            Vim::update(cx, |vim, cx| {
15                vim.update_active_editor(cx, |previously_active_editor, cx| {
16                    vim.unhook_vim_settings(previously_active_editor, cx)
17                });
18            });
19        });
20    }
21
22    editor.window().update(cx, |cx| {
23        Vim::update(cx, |vim, cx| {
24            vim.set_active_editor(editor.clone(), cx);
25            if vim.enabled {
26                cx.emit_global(VimEvent::ModeChanged {
27                    mode: vim.state().mode,
28                });
29            }
30        });
31    });
32}
33
34fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut AppContext) {
35    editor.window().update(cx, |cx| {
36        Vim::update(cx, |vim, cx| {
37            if let Some(previous_editor) = vim.active_editor.clone() {
38                if previous_editor == editor.clone() {
39                    vim.active_editor = None;
40                }
41            }
42
43            editor.update(cx, |editor, cx| vim.unhook_vim_settings(editor, cx))
44        });
45    });
46}
47
48fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) {
49    editor.window().update(cx, |cx| {
50        cx.update_default_global(|vim: &mut Vim, _| {
51            if let Some(previous_editor) = vim.active_editor.clone() {
52                if previous_editor == editor.clone() {
53                    vim.active_editor = None;
54                }
55            }
56            vim.editor_states.remove(&editor.id())
57        });
58    });
59}