Add `WindowContext::update_default_global`

Antonio Scandurra and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

crates/gpui/src/app.rs        | 38 +++++++++++++++++++++++++++---------
crates/gpui/src/app/window.rs |  8 +++++++
2 files changed, 36 insertions(+), 10 deletions(-)

Detailed changes

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) -> U
     where
         T: 'static,

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) -> Subscription
     where
         E: Entity,