diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index d51f8a5a10..2e9ac0f267 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>( + &mut self, + callback: F, + ) -> Option { + 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::(); - 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(this: &mut C, update: F) -> U + where + C: DerefMut, + T: 'static + Default, + F: FnOnce(&mut T, &mut C) -> U, + { + let type_id = TypeId::of::(); + 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(&mut self, update: F) -> U where T: 'static, diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index 3f01c14ddb..b429c70390 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(&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(&mut self, handle: &H, mut callback: F) -> Subscription where E: Entity,