Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add `WindowContext::update_default_global`
Antonio Scandurrad9bb37c64953
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rsindex d51f8a5a10d..2e9ac0f267d 100644--- a/crates/gpui/src/app.rs+++ b/crates/gpui/src/app.rs@@ -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(&self,options: PathPromptOptions,@@ -1210,19 +1219,28 @@ impl AppContext {T: 'static + Default,F: FnOnce(&mut T, &mut AppContext) -> U,{- self.update(|this| {- 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+ self.update(|mut this| {+ Self::update_default_global_internal(&mut this, |global, cx| update(global, cx))})}+ 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) -> UwhereT: 'static,diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rsindex 3f01c14ddb7..b429c70390f 100644--- a/crates/gpui/src/app/window.rs+++ b/crates/gpui/src/app/window.rs@@ -274,6 +274,14 @@ impl<'a: 'b, 'b> WindowContext<'a, 'b> {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) -> SubscriptionwhereE: Entity,