Ensure focus_in and focus_out fire on window activation (#3993)

Also:
- Rename cx.on_blur to cx.on_focus_lost
- Fix a bug where notify calls in focus handlers were ignored
- Fix a bug where vim would get stuck in the wrong mode when switching
  windows

Release Notes:

- (preview only) vim: fix switching between multiple windows
This commit is contained in:
Conrad Irwin 2024-01-10 10:00:07 -07:00 committed by GitHub
commit 5dff458014
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 26 deletions

View file

@ -69,7 +69,7 @@ fn released(entity_id: EntityId, cx: &mut AppContext) {
mod test {
use crate::{test::VimTestContext, Vim};
use editor::Editor;
use gpui::{Context, Entity};
use gpui::{Context, Entity, VisualTestContext};
use language::Buffer;
// regression test for blur called with a different active editor
@ -82,11 +82,13 @@ mod test {
let editor2 = cx
.update(|cx| {
window2.update(cx, |_, cx| {
cx.activate_window();
cx.focus_self();
cx.view().clone()
})
})
.unwrap();
cx.run_until_parked();
cx.update(|cx| {
let vim = Vim::read(cx);
@ -101,4 +103,42 @@ mod test {
editor1.handle_blur(cx);
});
}
// regression test for focus_in/focus_out being called on window activation
#[gpui::test]
async fn test_focus_across_windows(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
let mut cx1 = VisualTestContext::from_window(cx.window, &cx);
let editor1 = cx.editor.clone();
dbg!(editor1.entity_id());
let buffer = cx.new_model(|_| Buffer::new(0, 0, "a = 1\nb = 2\n"));
let (editor2, cx2) = cx.add_window_view(|cx| Editor::for_buffer(buffer, None, cx));
editor2.update(cx2, |_, cx| {
cx.focus_self();
cx.activate_window();
});
cx.run_until_parked();
cx1.update(|cx| {
assert_eq!(
Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
editor2.entity_id(),
)
});
cx1.update(|cx| {
cx.activate_window();
});
cx.run_until_parked();
cx.update(|cx| {
assert_eq!(
Vim::read(cx).active_editor.as_ref().unwrap().entity_id(),
editor1.entity_id(),
)
});
}
}