This commit is contained in:
Marshall Bowers 2023-11-02 13:21:28 -04:00
parent 0e1d2fdf21
commit 1e7a216d55
2 changed files with 41 additions and 5 deletions

View file

@ -12,7 +12,10 @@ use crate::{
use anyhow::{anyhow, Result};
use collections::HashMap;
use derive_more::{Deref, DerefMut};
use futures::channel::oneshot;
use futures::{
channel::{mpsc, oneshot},
StreamExt,
};
use parking_lot::RwLock;
use slotmap::SlotMap;
use smallvec::SmallVec;
@ -411,6 +414,31 @@ impl<'a> WindowContext<'a> {
let f = Box::new(f);
let display_id = self.window.display_id;
self.next_frame_callbacks
.entry(display_id)
.or_default()
.push(f);
self.frame_consumers.entry(display_id).or_insert_with(|| {
let (tx, rx) = mpsc::unbounded::<()>();
self.spawn(|cx| async move {
while rx.next().await.is_some() {
let _ = cx.update(|_, cx| {
for callback in cx
.app
.next_frame_callbacks
.get_mut(&display_id)
.unwrap()
.drain(..)
{
callback(cx);
}
});
}
})
});
if let Some(callbacks) = self.next_frame_callbacks.get_mut(&display_id) {
callbacks.push(f);
// If there was already a callback, it means that we already scheduled a frame.