zed/crates/vim/src/editor_events.rs

60 lines
1.9 KiB
Rust
Raw Normal View History

use crate::{Vim, VimEvent};
2023-04-20 21:25:11 +00:00
use editor::{EditorBlurred, EditorFocused, EditorReleased};
use gpui::AppContext;
pub fn init(cx: &mut AppContext) {
cx.subscribe_global(focused).detach();
cx.subscribe_global(blurred).detach();
cx.subscribe_global(released).detach();
}
fn focused(EditorFocused(editor): &EditorFocused, cx: &mut AppContext) {
if let Some(previously_active_editor) = Vim::read(cx).active_editor.clone() {
previously_active_editor.window().update(cx, |cx| {
Vim::update(cx, |vim, cx| {
vim.update_active_editor(cx, |previously_active_editor, cx| {
2023-07-21 01:41:38 +00:00
vim.unhook_vim_settings(previously_active_editor, cx)
});
});
});
}
editor.window().update(cx, |cx| {
Vim::update(cx, |vim, cx| {
2023-04-20 21:25:11 +00:00
vim.set_active_editor(editor.clone(), cx);
2023-08-18 21:50:34 +00:00
if vim.enabled {
cx.emit_global(VimEvent::ModeChanged {
mode: vim.state().mode,
});
}
});
});
}
fn blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut AppContext) {
editor.window().update(cx, |cx| {
Vim::update(cx, |vim, cx| {
if let Some(previous_editor) = vim.active_editor.clone() {
if previous_editor == editor.clone() {
vim.active_editor = None;
}
}
2023-04-17 09:17:35 +00:00
2023-07-21 01:41:38 +00:00
editor.update(cx, |editor, cx| vim.unhook_vim_settings(editor, cx))
2023-04-17 09:17:35 +00:00
});
});
}
fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) {
editor.window().update(cx, |cx| {
cx.update_default_global(|vim: &mut Vim, _| {
if let Some(previous_editor) = vim.active_editor.clone() {
if previous_editor == editor.clone() {
vim.active_editor = None;
}
}
vim.editor_states.remove(&editor.id())
});
});
}