gpui: Improve Global ergonomics (#11923)

This PR adds some ergonomic improvements when working with GPUI
`Global`s.

Two new traits have been added—`ReadGlobal` and `UpdateGlobal`—that
provide associated functions on any type that implements `Global` for
accessing and updating the global without needing to call the methods on
the `cx` directly (which generally involves qualifying the type).

I looked into adding `ObserveGlobal` as well, but this seems a bit
trickier to implement as the signatures of `cx.observe_global` vary
slightly between the different contexts.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-05-16 12:47:43 -04:00 committed by GitHub
parent 1b261608c6
commit c1e291bc96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 81 additions and 33 deletions

View file

@ -28,8 +28,8 @@ use gpui::{
AsyncWindowContext, AvailableSpace, ClipboardItem, Context, Entity, EventEmitter, FocusHandle,
FocusableView, FontStyle, FontWeight, HighlightStyle, InteractiveElement, IntoElement, Model,
ModelContext, ParentElement, Pixels, Render, SharedString, StatefulInteractiveElement, Styled,
Subscription, Task, TextStyle, UniformListScrollHandle, View, ViewContext, VisualContext,
WeakModel, WeakView, WhiteSpace, WindowContext,
Subscription, Task, TextStyle, UniformListScrollHandle, UpdateGlobal, View, ViewContext,
VisualContext, WeakModel, WeakView, WhiteSpace, WindowContext,
};
use language::{language_settings::SoftWrap, Buffer, LanguageRegistry, Point, ToOffset as _};
use multi_buffer::MultiBufferRow;
@ -240,7 +240,7 @@ impl AssistantPanel {
|| prev_settings_version != CompletionProvider::global(cx).settings_version()
{
self.authentication_prompt =
Some(cx.update_global::<CompletionProvider, _>(|provider, cx| {
Some(CompletionProvider::update_global(cx, |provider, cx| {
provider.authentication_prompt(cx)
}));
}
@ -1088,7 +1088,7 @@ impl AssistantPanel {
}
fn authenticate(&mut self, cx: &mut ViewContext<Self>) -> Task<Result<()>> {
cx.update_global::<CompletionProvider, _>(|provider, cx| provider.authenticate(cx))
CompletionProvider::update_global(cx, |provider, cx| provider.authenticate(cx))
}
fn render_signed_in(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {