diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index 5d47fffb6e12abf7d687887be77629bb93d17a6d..e8c45f0191c7b52badaecc096dceb0e312d696b9 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -376,6 +376,15 @@ impl<'a, 'w> WindowContext<'a, 'w> { self.notify(); } + /// Schedules the given function to be run at the end of the current effect cycle, allowing entities + /// that are currently on the stack to be returned to the app. + pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static + Send) { + let window = self.window.handle; + self.app.defer(move |cx| { + cx.update_window(window, f).ok(); + }); + } + pub fn subscribe( &mut self, entity: &E, @@ -1595,6 +1604,15 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> { self.window_cx.on_next_frame(move |cx| view.update(cx, f)); } + /// Schedules the given function to be run at the end of the current effect cycle, allowing entities + /// that are currently on the stack to be returned to the app. + pub fn defer(&mut self, f: impl FnOnce(&mut V, &mut ViewContext) + 'static + Send) { + let view = self.view(); + self.window_cx.defer(move |cx| { + view.update(cx, f).ok(); + }); + } + pub fn observe( &mut self, entity: &E,