diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index 805f7b24f0394d8afa1ed9e2580275cd39bef1bd..9ffb23943b6c9b8de7ab88cb46b3b31a37f69c8b 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -359,7 +359,6 @@ impl MainThread { let id = cx.windows.insert(None); let handle = WindowHandle::new(id); let mut window = Window::new(handle.into(), options, cx); - let display_id = window.display_id; let root_view = build_root_view(&mut WindowContext::mutable(cx, &mut window)); window.root_view.replace(root_view.into_any()); cx.windows.get_mut(id).unwrap().replace(window); diff --git a/crates/gpui3/src/app/async_context.rs b/crates/gpui3/src/app/async_context.rs index 92a26456e442d093a12f2d990d5eae6ffc07b817..65f4e666b318fd3b44d270f352aee7f1dc3882af 100644 --- a/crates/gpui3/src/app/async_context.rs +++ b/crates/gpui3/src/app/async_context.rs @@ -60,9 +60,13 @@ pub struct AsyncWindowContext { } impl AsyncWindowContext { - pub fn new(app: AsyncAppContext, window: AnyWindowHandle) -> Self { + pub(crate) fn new(app: AsyncAppContext, window: AnyWindowHandle) -> Self { Self { app, window } } + + pub fn update(&self, update: impl FnOnce(&mut WindowContext) -> R) -> Result { + self.app.update_window(self.window, update) + } } impl Context for AsyncWindowContext { diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index a4ff0cfa8307e69e93ec0a36de6fdb2b721a189d..7e3c5d3ce58be10a56db5a05867ff887824d2bbf 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -143,6 +143,14 @@ impl<'a, 'w> WindowContext<'a, 'w> { AsyncWindowContext::new(self.app.to_async(), self.window.handle) } + pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) { + let cx = self.to_async(); + let display_id = self.window.display_id; + self.display_linker.on_next_frame(display_id, move |_, _| { + cx.update(f).ok(); + }); + } + pub fn spawn( &mut self, f: impl FnOnce(AnyWindowHandle, AsyncWindowContext) -> Fut + Send + 'static, @@ -581,6 +589,15 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> { self.entities.weak_handle(self.entity_id) } + pub fn on_next_frame(&mut self, f: impl FnOnce(&mut S, &mut ViewContext) + Send + 'static) { + let mut cx = self.to_async(); + let entity = self.handle(); + let display_id = self.window.display_id; + self.display_linker.on_next_frame(display_id, move |_, _| { + entity.update(&mut cx, f).ok(); + }); + } + pub fn observe( &mut self, handle: &Handle,