Add ViewHandle::defer

It's like update, but happens after the current effect instead of synchronously. Also, it doesn't allow the callback to return a value because there would be nothing to do with it.
This commit is contained in:
Nathan Sobo 2022-01-22 13:21:59 -07:00
parent 8b04c5d3ac
commit b755b2d602

View file

@ -2938,6 +2938,17 @@ impl<T: View> ViewHandle<T> {
})
}
pub fn defer<C, F>(&self, cx: &mut C, update: F)
where
C: AsMut<MutableAppContext>,
F: 'static + FnOnce(&mut T, &mut ViewContext<T>),
{
let this = self.clone();
cx.as_mut().defer(Box::new(move |cx| {
this.update(cx, |view, cx| update(view, cx));
}));
}
pub fn is_focused(&self, cx: &AppContext) -> bool {
cx.focused_view_id(self.window_id)
.map_or(false, |focused_id| focused_id == self.view_id)