mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-24 02:46:43 +00:00
Add a ViewContext::defer
This takes a closure that will be enqueued as an effect to ensure there are no entities on the stack.
This commit is contained in:
parent
b1931fbf5d
commit
8b04c5d3ac
1 changed files with 16 additions and 0 deletions
|
@ -1060,6 +1060,10 @@ impl MutableAppContext {
|
|||
}
|
||||
}
|
||||
|
||||
fn defer(&mut self, callback: Box<dyn FnOnce(&mut MutableAppContext)>) {
|
||||
self.pending_effects.push_back(Effect::Deferred(callback))
|
||||
}
|
||||
|
||||
pub(crate) fn notify_model(&mut self, model_id: usize) {
|
||||
if self.pending_notifications.insert(model_id) {
|
||||
self.pending_effects
|
||||
|
@ -1443,6 +1447,7 @@ impl MutableAppContext {
|
|||
Effect::ViewNotification { window_id, view_id } => {
|
||||
self.notify_view_observers(window_id, view_id)
|
||||
}
|
||||
Effect::Deferred(callback) => callback(self),
|
||||
Effect::Release { entity_id } => self.notify_release_observers(entity_id),
|
||||
Effect::Focus { window_id, view_id } => {
|
||||
self.focus(window_id, view_id);
|
||||
|
@ -1876,6 +1881,7 @@ pub enum Effect {
|
|||
window_id: usize,
|
||||
view_id: usize,
|
||||
},
|
||||
Deferred(Box<dyn FnOnce(&mut MutableAppContext)>),
|
||||
Release {
|
||||
entity_id: usize,
|
||||
},
|
||||
|
@ -1905,6 +1911,7 @@ impl Debug for Effect {
|
|||
.field("window_id", window_id)
|
||||
.field("view_id", view_id)
|
||||
.finish(),
|
||||
Effect::Deferred(_) => f.debug_struct("Effect::Deferred").finish(),
|
||||
Effect::Release { entity_id } => f
|
||||
.debug_struct("Effect::Release")
|
||||
.field("entity_id", entity_id)
|
||||
|
@ -2410,6 +2417,15 @@ impl<'a, T: View> ViewContext<'a, T> {
|
|||
self.app.notify_view(self.window_id, self.view_id);
|
||||
}
|
||||
|
||||
pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut T, &mut ViewContext<T>)) {
|
||||
let handle = self.handle();
|
||||
self.app.defer(Box::new(move |cx| {
|
||||
handle.update(cx, |view, cx| {
|
||||
callback(view, cx);
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn propagate_action(&mut self) {
|
||||
self.halt_action_dispatch = false;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue