
There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
199 lines
7.1 KiB
Rust
199 lines
7.1 KiB
Rust
use std::sync::Arc;
|
|
|
|
use collections::HashMap;
|
|
use gpui::{canvas, AnyView, App, EventEmitter, FocusHandle, Focusable, Subscription};
|
|
use language_model::{LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry};
|
|
use ui::{prelude::*, ElevationIndex};
|
|
use workspace::Item;
|
|
|
|
pub struct ConfigurationView {
|
|
focus_handle: FocusHandle,
|
|
configuration_views: HashMap<LanguageModelProviderId, AnyView>,
|
|
_registry_subscription: Subscription,
|
|
}
|
|
|
|
impl ConfigurationView {
|
|
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
|
let focus_handle = cx.focus_handle();
|
|
|
|
let registry_subscription = cx.subscribe_in(
|
|
&LanguageModelRegistry::global(cx),
|
|
window,
|
|
|this, _, event: &language_model::Event, window, cx| match event {
|
|
language_model::Event::AddedProvider(provider_id) => {
|
|
let provider = LanguageModelRegistry::read_global(cx).provider(provider_id);
|
|
if let Some(provider) = provider {
|
|
this.add_configuration_view(&provider, window, cx);
|
|
}
|
|
}
|
|
language_model::Event::RemovedProvider(provider_id) => {
|
|
this.remove_configuration_view(provider_id);
|
|
}
|
|
_ => {}
|
|
},
|
|
);
|
|
|
|
let mut this = Self {
|
|
focus_handle,
|
|
configuration_views: HashMap::default(),
|
|
_registry_subscription: registry_subscription,
|
|
};
|
|
this.build_configuration_views(window, cx);
|
|
this
|
|
}
|
|
|
|
fn build_configuration_views(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
|
let providers = LanguageModelRegistry::read_global(cx).providers();
|
|
for provider in providers {
|
|
self.add_configuration_view(&provider, window, cx);
|
|
}
|
|
}
|
|
|
|
fn remove_configuration_view(&mut self, provider_id: &LanguageModelProviderId) {
|
|
self.configuration_views.remove(provider_id);
|
|
}
|
|
|
|
fn add_configuration_view(
|
|
&mut self,
|
|
provider: &Arc<dyn LanguageModelProvider>,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
let configuration_view = provider.configuration_view(window, cx);
|
|
self.configuration_views
|
|
.insert(provider.id(), configuration_view);
|
|
}
|
|
|
|
fn render_provider_view(
|
|
&mut self,
|
|
provider: &Arc<dyn LanguageModelProvider>,
|
|
cx: &mut Context<Self>,
|
|
) -> Div {
|
|
let provider_id = provider.id().0.clone();
|
|
let provider_name = provider.name().0.clone();
|
|
let configuration_view = self.configuration_views.get(&provider.id()).cloned();
|
|
|
|
let open_new_context = cx.listener({
|
|
let provider = provider.clone();
|
|
move |_, _, _window, cx| {
|
|
cx.emit(ConfigurationViewEvent::NewProviderContextEditor(
|
|
provider.clone(),
|
|
))
|
|
}
|
|
});
|
|
|
|
v_flex()
|
|
.gap_2()
|
|
.child(
|
|
h_flex()
|
|
.justify_between()
|
|
.child(Headline::new(provider_name.clone()).size(HeadlineSize::Small))
|
|
.when(provider.is_authenticated(cx), move |this| {
|
|
this.child(
|
|
h_flex().justify_end().child(
|
|
Button::new(
|
|
SharedString::from(format!("new-context-{provider_id}")),
|
|
"Open New Chat",
|
|
)
|
|
.icon_position(IconPosition::Start)
|
|
.icon(IconName::Plus)
|
|
.style(ButtonStyle::Filled)
|
|
.layer(ElevationIndex::ModalSurface)
|
|
.on_click(open_new_context),
|
|
),
|
|
)
|
|
}),
|
|
)
|
|
.child(
|
|
div()
|
|
.p(DynamicSpacing::Base08.rems(cx))
|
|
.bg(cx.theme().colors().surface_background)
|
|
.border_1()
|
|
.border_color(cx.theme().colors().border_variant)
|
|
.rounded_md()
|
|
.when(configuration_view.is_none(), |this| {
|
|
this.child(div().child(Label::new(format!(
|
|
"No configuration view for {}",
|
|
provider_name
|
|
))))
|
|
})
|
|
.when_some(configuration_view, |this, configuration_view| {
|
|
this.child(configuration_view)
|
|
}),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Render for ConfigurationView {
|
|
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let providers = LanguageModelRegistry::read_global(cx).providers();
|
|
let provider_views = providers
|
|
.into_iter()
|
|
.map(|provider| self.render_provider_view(&provider, cx))
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut element = v_flex()
|
|
.id("assistant-configuration-view")
|
|
.track_focus(&self.focus_handle(cx))
|
|
.bg(cx.theme().colors().editor_background)
|
|
.size_full()
|
|
.overflow_y_scroll()
|
|
.child(
|
|
v_flex()
|
|
.p(DynamicSpacing::Base16.rems(cx))
|
|
.border_b_1()
|
|
.border_color(cx.theme().colors().border)
|
|
.gap_1()
|
|
.child(Headline::new("Configure your Assistant").size(HeadlineSize::Medium))
|
|
.child(
|
|
Label::new(
|
|
"At least one LLM provider must be configured to use the Assistant.",
|
|
)
|
|
.color(Color::Muted),
|
|
),
|
|
)
|
|
.child(
|
|
v_flex()
|
|
.p(DynamicSpacing::Base16.rems(cx))
|
|
.mt_1()
|
|
.gap_6()
|
|
.flex_1()
|
|
.children(provider_views),
|
|
)
|
|
.into_any();
|
|
|
|
// We use a canvas here to get scrolling to work in the ConfigurationView. It's a workaround
|
|
// because we couldn't the element to take up the size of the parent.
|
|
canvas(
|
|
move |bounds, window, cx| {
|
|
element.prepaint_as_root(bounds.origin, bounds.size.into(), window, cx);
|
|
element
|
|
},
|
|
|_, mut element, window, cx| {
|
|
element.paint(window, cx);
|
|
},
|
|
)
|
|
.flex_1()
|
|
.w_full()
|
|
}
|
|
}
|
|
|
|
pub enum ConfigurationViewEvent {
|
|
NewProviderContextEditor(Arc<dyn LanguageModelProvider>),
|
|
}
|
|
|
|
impl EventEmitter<ConfigurationViewEvent> for ConfigurationView {}
|
|
|
|
impl Focusable for ConfigurationView {
|
|
fn focus_handle(&self, _: &App) -> FocusHandle {
|
|
self.focus_handle.clone()
|
|
}
|
|
}
|
|
|
|
impl Item for ConfigurationView {
|
|
type Event = ConfigurationViewEvent;
|
|
|
|
fn tab_content_text(&self, _window: &Window, _cx: &App) -> Option<SharedString> {
|
|
Some("Configuration".into())
|
|
}
|
|
}
|