Add WindowContext::update_default_global
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
1d487e19f9
commit
d9bb37c649
2 changed files with 36 additions and 10 deletions
|
@ -873,6 +873,15 @@ impl AppContext {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_active_window<T, F: FnOnce(&mut WindowContext) -> T>(
|
||||||
|
&mut self,
|
||||||
|
callback: F,
|
||||||
|
) -> Option<T> {
|
||||||
|
self.platform
|
||||||
|
.main_window_id()
|
||||||
|
.and_then(|id| self.update_window(id, callback))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn prompt_for_paths(
|
pub fn prompt_for_paths(
|
||||||
&self,
|
&self,
|
||||||
options: PathPromptOptions,
|
options: PathPromptOptions,
|
||||||
|
@ -1210,19 +1219,28 @@ impl AppContext {
|
||||||
T: 'static + Default,
|
T: 'static + Default,
|
||||||
F: FnOnce(&mut T, &mut AppContext) -> U,
|
F: FnOnce(&mut T, &mut AppContext) -> U,
|
||||||
{
|
{
|
||||||
self.update(|this| {
|
self.update(|mut this| {
|
||||||
let type_id = TypeId::of::<T>();
|
Self::update_default_global_internal(&mut this, |global, cx| update(global, cx))
|
||||||
let mut state = this
|
|
||||||
.globals
|
|
||||||
.remove(&type_id)
|
|
||||||
.unwrap_or_else(|| Box::new(T::default()));
|
|
||||||
let result = update(state.downcast_mut().unwrap(), this);
|
|
||||||
this.globals.insert(type_id, state);
|
|
||||||
this.notify_global(type_id);
|
|
||||||
result
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_default_global_internal<C, T, F, U>(this: &mut C, update: F) -> U
|
||||||
|
where
|
||||||
|
C: DerefMut<Target = AppContext>,
|
||||||
|
T: 'static + Default,
|
||||||
|
F: FnOnce(&mut T, &mut C) -> U,
|
||||||
|
{
|
||||||
|
let type_id = TypeId::of::<T>();
|
||||||
|
let mut state = this
|
||||||
|
.globals
|
||||||
|
.remove(&type_id)
|
||||||
|
.unwrap_or_else(|| Box::new(T::default()));
|
||||||
|
let result = update(state.downcast_mut().unwrap(), this);
|
||||||
|
this.globals.insert(type_id, state);
|
||||||
|
this.notify_global(type_id);
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update_global<T, F, U>(&mut self, update: F) -> U
|
pub fn update_global<T, F, U>(&mut self, update: F) -> U
|
||||||
where
|
where
|
||||||
T: 'static,
|
T: 'static,
|
||||||
|
|
|
@ -274,6 +274,14 @@ impl<'a: 'b, 'b> WindowContext<'a, 'b> {
|
||||||
AppContext::update_global_internal(self, |global, cx| update(global, cx))
|
AppContext::update_global_internal(self, |global, cx| update(global, cx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_default_global<T, F, U>(&mut self, update: F) -> U
|
||||||
|
where
|
||||||
|
T: 'static + Default,
|
||||||
|
F: FnOnce(&mut T, &mut Self) -> U,
|
||||||
|
{
|
||||||
|
AppContext::update_default_global_internal(self, |global, cx| update(global, cx))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
pub fn subscribe<E, H, F>(&mut self, handle: &H, mut callback: F) -> Subscription
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: Entity,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue