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:
parent
1b261608c6
commit
c1e291bc96
8 changed files with 81 additions and 33 deletions
|
@ -12,7 +12,7 @@ use assistant_settings::{AnthropicModel, AssistantSettings, OpenAiModel, ZedDotD
|
|||
use client::{proto, Client};
|
||||
use command_palette_hooks::CommandPaletteFilter;
|
||||
pub(crate) use completion_provider::*;
|
||||
use gpui::{actions, AppContext, BorrowAppContext, Global, SharedString};
|
||||
use gpui::{actions, AppContext, Global, SharedString, UpdateGlobal};
|
||||
pub(crate) use saved_conversation::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use settings::{Settings, SettingsStore};
|
||||
|
@ -233,13 +233,13 @@ pub fn init(client: Arc<Client>, cx: &mut AppContext) {
|
|||
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
||||
filter.hide_namespace(Assistant::NAMESPACE);
|
||||
});
|
||||
cx.update_global(|assistant: &mut Assistant, cx: &mut AppContext| {
|
||||
Assistant::update_global(cx, |assistant, cx| {
|
||||
let settings = AssistantSettings::get_global(cx);
|
||||
|
||||
assistant.set_enabled(settings.enabled, cx);
|
||||
});
|
||||
cx.observe_global::<SettingsStore>(|cx| {
|
||||
cx.update_global(|assistant: &mut Assistant, cx: &mut AppContext| {
|
||||
Assistant::update_global(cx, |assistant, cx| {
|
||||
let settings = AssistantSettings::get_global(cx);
|
||||
|
||||
assistant.set_enabled(settings.enabled, cx);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -418,7 +418,7 @@ fn merge<T: Copy>(target: &mut T, value: Option<T>) {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use gpui::{AppContext, BorrowAppContext};
|
||||
use gpui::{AppContext, UpdateGlobal};
|
||||
use settings::SettingsStore;
|
||||
|
||||
use super::*;
|
||||
|
@ -440,7 +440,7 @@ mod tests {
|
|||
);
|
||||
|
||||
// Ensure backward-compatibility.
|
||||
cx.update_global::<SettingsStore, _>(|store, cx| {
|
||||
SettingsStore::update_global(cx, |store, cx| {
|
||||
store
|
||||
.set_user_settings(
|
||||
r#"{
|
||||
|
@ -460,7 +460,7 @@ mod tests {
|
|||
low_speed_timeout_in_seconds: None,
|
||||
}
|
||||
);
|
||||
cx.update_global::<SettingsStore, _>(|store, cx| {
|
||||
SettingsStore::update_global(cx, |store, cx| {
|
||||
store
|
||||
.set_user_settings(
|
||||
r#"{
|
||||
|
@ -482,7 +482,7 @@ mod tests {
|
|||
);
|
||||
|
||||
// The new version supports setting a custom model when using zed.dev.
|
||||
cx.update_global::<SettingsStore, _>(|store, cx| {
|
||||
SettingsStore::update_global(cx, |store, cx| {
|
||||
store
|
||||
.set_user_settings(
|
||||
r#"{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue