Fix blinking behavior in editor when receiving/losing focus

Co-Authored-By: Marshall <marshall@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-11-08 19:03:57 +01:00
parent 738b2ce6c5
commit 2fd8b1f489
4 changed files with 195 additions and 28 deletions

View file

@ -518,8 +518,9 @@ impl AppContext {
) {
window_handle
.update(self, |_, cx| {
// The window might change focus multiple times in an effect cycle.
// We only honor effects for the most recently focused handle.
if cx.window.focus == focused {
let mut listeners = mem::take(&mut cx.window.current_frame.focus_listeners);
let focused = focused
.map(|id| FocusHandle::for_id(id, &cx.window.focus_handles).unwrap());
let blurred = cx
@ -528,15 +529,24 @@ impl AppContext {
.take()
.unwrap()
.and_then(|id| FocusHandle::for_id(id, &cx.window.focus_handles));
if focused.is_some() || blurred.is_some() {
let event = FocusEvent { focused, blurred };
for listener in &listeners {
let focus_changed = focused.is_some() || blurred.is_some();
let event = FocusEvent { focused, blurred };
let mut listeners = mem::take(&mut cx.window.current_frame.focus_listeners);
if focus_changed {
for listener in &mut listeners {
listener(&event, cx);
}
}
listeners.extend(cx.window.current_frame.focus_listeners.drain(..));
cx.window.current_frame.focus_listeners = listeners;
if focus_changed {
cx.window
.focus_listeners
.clone()
.retain(&(), |listener| listener(&event, cx));
}
}
})
.ok();