From 28ca0edb067582c7f47a72a22e86528819ed9679 Mon Sep 17 00:00:00 2001 From: David Kleingeld Date: Mon, 8 Sep 2025 16:09:33 +0200 Subject: [PATCH] Adds AsyncApp::try_read_default_global Similar to try_read_global except it will initialize the global if it was not initialized. --- crates/gpui/src/app/async_context.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/gpui/src/app/async_context.rs b/crates/gpui/src/app/async_context.rs index f3b8c0ce77d98a1083d13983c4a2f06f1f543c16..cfe7a5a75c258d09194c7d77a117208161713c6f 100644 --- a/crates/gpui/src/app/async_context.rs +++ b/crates/gpui/src/app/async_context.rs @@ -218,6 +218,23 @@ impl AsyncApp { Some(read(app.try_global()?, &app)) } + /// Reads the global state of the specified type, passing it to the given callback. + /// A default value is assigned if a global of this type has not yet been assigned. + /// + /// # Errors + /// If the app has ben dropped this returns an error. + pub fn try_read_default_global( + &self, + read: impl FnOnce(&G, &App) -> R, + ) -> Result { + let app = self.app.upgrade().context("app was released")?; + let mut app = app.borrow_mut(); + app.update(|cx| { + cx.default_global::(); + }); + Ok(read(app.try_global().context("app was released")?, &app)) + } + /// A convenience method for [`App::update_global`](BorrowAppContext::update_global) /// for updating the global state of the specified type. pub fn update_global(