editor_events.rs

 1use editor::{EditorBlurred, EditorCreated, EditorFocused, EditorMode, EditorReleased};
 2use gpui::MutableAppContext;
 3
 4use crate::{state::Mode, Vim};
 5
 6pub fn init(cx: &mut MutableAppContext) {
 7    cx.subscribe_global(editor_created).detach();
 8    cx.subscribe_global(editor_focused).detach();
 9    cx.subscribe_global(editor_blurred).detach();
10    cx.subscribe_global(editor_released).detach();
11}
12
13fn editor_created(EditorCreated(editor): &EditorCreated, cx: &mut MutableAppContext) {
14    cx.update_default_global(|vim: &mut Vim, cx| {
15        vim.editors.insert(editor.id(), editor.downgrade());
16        vim.sync_editor_options(cx);
17    })
18}
19
20fn editor_focused(EditorFocused(editor): &EditorFocused, cx: &mut MutableAppContext) {
21    Vim::update(cx, |state, cx| {
22        state.active_editor = Some(editor.downgrade());
23        if editor.read(cx).mode() != EditorMode::Full {
24            state.switch_mode(Mode::Insert, cx);
25        }
26    });
27}
28
29fn editor_blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut MutableAppContext) {
30    Vim::update(cx, |state, cx| {
31        if let Some(previous_editor) = state.active_editor.clone() {
32            if previous_editor == editor.clone() {
33                state.active_editor = None;
34            }
35        }
36        state.sync_editor_options(cx);
37    })
38}
39
40fn editor_released(EditorReleased(editor): &EditorReleased, cx: &mut MutableAppContext) {
41    cx.update_default_global(|vim: &mut Vim, _| {
42        vim.editors.remove(&editor.id());
43        if let Some(previous_editor) = vim.active_editor.clone() {
44            if previous_editor == editor.clone() {
45                vim.active_editor = None;
46            }
47        }
48    });
49}