Skip to content

Commit

Permalink
Eliminate GPUI View, ViewContext, and WindowContext types (zed-indust…
Browse files Browse the repository at this point in the history
…ries#22632)

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 <[email protected]>
Co-authored-by: Cole Miller <[email protected]>
Co-authored-by: Mikayla <[email protected]>
Co-authored-by: Joseph <[email protected]>
Co-authored-by: max <[email protected]>
Co-authored-by: Michael Sloan <[email protected]>
Co-authored-by: Mikayla Maki <[email protected]>
Co-authored-by: Mikayla <[email protected]>
Co-authored-by: joão <[email protected]>
  • Loading branch information
10 people authored Jan 26, 2025
1 parent 21b4a0d commit 6fca1d2
Show file tree
Hide file tree
Showing 648 changed files with 36,251 additions and 28,211 deletions.
125 changes: 72 additions & 53 deletions crates/activity_indicator/src/activity_indicator.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/anthropic/src/anthropic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod supported_countries;

use std::{pin::Pin, str::FromStr};

use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, Context as _, Result};
use chrono::{DateTime, Utc};
use futures::{io::BufReader, stream::BoxStream, AsyncBufReadExt, AsyncReadExt, Stream, StreamExt};
use http_client::http::{HeaderMap, HeaderValue};
Expand Down
6 changes: 3 additions & 3 deletions crates/assets/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This crate was essentially pulled out verbatim from main `zed` crate to avoid having to run RustEmbed macro whenever zed has to be rebuilt. It saves a second or two on an incremental build.
use anyhow::anyhow;

use gpui::{AppContext, AssetSource, Result, SharedString};
use gpui::{App, AssetSource, Result, SharedString};
use rust_embed::RustEmbed;

#[derive(RustEmbed)]
Expand Down Expand Up @@ -39,7 +39,7 @@ impl AssetSource for Assets {

impl Assets {
/// Populate the [`TextSystem`] of the given [`AppContext`] with all `.ttf` fonts in the `fonts` directory.
pub fn load_fonts(&self, cx: &AppContext) -> gpui::Result<()> {
pub fn load_fonts(&self, cx: &App) -> gpui::Result<()> {
let font_paths = self.list("fonts")?;
let mut embedded_fonts = Vec::new();
for font_path in font_paths {
Expand All @@ -55,7 +55,7 @@ impl Assets {
cx.text_system().add_fonts(embedded_fonts)
}

pub fn load_test_fonts(&self, cx: &AppContext) {
pub fn load_test_fonts(&self, cx: &App) {
cx.text_system()
.add_fonts(vec![self
.load("fonts/plex-mono/ZedPlexMono-Regular.ttf")
Expand Down
14 changes: 7 additions & 7 deletions crates/assistant/src/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use client::Client;
use command_palette_hooks::CommandPaletteFilter;
use feature_flags::FeatureFlagAppExt;
use fs::Fs;
use gpui::{actions, AppContext, Global, UpdateGlobal};
use gpui::{actions, App, Global, UpdateGlobal};
use language_model::{
LanguageModelId, LanguageModelProviderId, LanguageModelRegistry, LanguageModelResponseMessage,
};
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Global for Assistant {}
impl Assistant {
const NAMESPACE: &'static str = "assistant";

fn set_enabled(&mut self, enabled: bool, cx: &mut AppContext) {
fn set_enabled(&mut self, enabled: bool, cx: &mut App) {
if self.enabled == enabled {
return;
}
Expand All @@ -92,7 +92,7 @@ pub fn init(
fs: Arc<dyn Fs>,
client: Arc<Client>,
prompt_builder: Arc<PromptBuilder>,
cx: &mut AppContext,
cx: &mut App,
) {
cx.set_global(Assistant::default());
AssistantSettings::register(cx);
Expand Down Expand Up @@ -165,7 +165,7 @@ pub fn init(
.detach();
}

fn init_language_model_settings(cx: &mut AppContext) {
fn init_language_model_settings(cx: &mut App) {
update_active_language_model_from_settings(cx);

cx.observe_global::<SettingsStore>(update_active_language_model_from_settings)
Expand All @@ -184,7 +184,7 @@ fn init_language_model_settings(cx: &mut AppContext) {
.detach();
}

fn update_active_language_model_from_settings(cx: &mut AppContext) {
fn update_active_language_model_from_settings(cx: &mut App) {
let settings = AssistantSettings::get_global(cx);
let provider_name = LanguageModelProviderId::from(settings.default_model.provider.clone());
let model_id = LanguageModelId::from(settings.default_model.model.clone());
Expand All @@ -204,7 +204,7 @@ fn update_active_language_model_from_settings(cx: &mut AppContext) {
});
}

fn register_slash_commands(prompt_builder: Option<Arc<PromptBuilder>>, cx: &mut AppContext) {
fn register_slash_commands(prompt_builder: Option<Arc<PromptBuilder>>, cx: &mut App) {
let slash_command_registry = SlashCommandRegistry::global(cx);

slash_command_registry.register_command(assistant_slash_commands::FileSlashCommand, true);
Expand Down Expand Up @@ -278,7 +278,7 @@ fn register_slash_commands(prompt_builder: Option<Arc<PromptBuilder>>, cx: &mut
.detach();
}

fn update_slash_commands_from_settings(cx: &mut AppContext) {
fn update_slash_commands_from_settings(cx: &mut App) {
let slash_command_registry = SlashCommandRegistry::global(cx);
let settings = SlashCommandSettings::get_global(cx);

Expand Down
42 changes: 22 additions & 20 deletions crates/assistant/src/assistant_configuration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use collections::HashMap;
use gpui::{canvas, AnyView, AppContext, EventEmitter, FocusHandle, FocusableView, Subscription};
use gpui::{canvas, AnyView, App, EventEmitter, FocusHandle, Focusable, Subscription};
use language_model::{LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry};
use ui::{prelude::*, ElevationIndex};
use workspace::Item;
Expand All @@ -13,16 +13,17 @@ pub struct ConfigurationView {
}

impl ConfigurationView {
pub fn new(cx: &mut ViewContext<Self>) -> Self {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let focus_handle = cx.focus_handle();

let registry_subscription = cx.subscribe(
let registry_subscription = cx.subscribe_in(
&LanguageModelRegistry::global(cx),
|this, _, event: &language_model::Event, cx| match event {
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, cx);
this.add_configuration_view(&provider, window, cx);
}
}
language_model::Event::RemovedProvider(provider_id) => {
Expand All @@ -37,14 +38,14 @@ impl ConfigurationView {
configuration_views: HashMap::default(),
_registry_subscription: registry_subscription,
};
this.build_configuration_views(cx);
this.build_configuration_views(window, cx);
this
}

fn build_configuration_views(&mut self, cx: &mut ViewContext<Self>) {
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, cx);
self.add_configuration_view(&provider, window, cx);
}
}

Expand All @@ -55,25 +56,26 @@ impl ConfigurationView {
fn add_configuration_view(
&mut self,
provider: &Arc<dyn LanguageModelProvider>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let configuration_view = provider.configuration_view(cx);
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 ViewContext<Self>,
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 |_, _, cx| {
move |_, _, _window, cx| {
cx.emit(ConfigurationViewEvent::NewProviderContextEditor(
provider.clone(),
))
Expand Down Expand Up @@ -123,7 +125,7 @@ impl ConfigurationView {
}

impl Render for ConfigurationView {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
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()
Expand Down Expand Up @@ -163,12 +165,12 @@ impl Render for ConfigurationView {
// 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, cx| {
element.prepaint_as_root(bounds.origin, bounds.size.into(), cx);
move |bounds, window, cx| {
element.prepaint_as_root(bounds.origin, bounds.size.into(), window, cx);
element
},
|_, mut element, cx| {
element.paint(cx);
|_, mut element, window, cx| {
element.paint(window, cx);
},
)
.flex_1()
Expand All @@ -182,16 +184,16 @@ pub enum ConfigurationViewEvent {

impl EventEmitter<ConfigurationViewEvent> for ConfigurationView {}

impl FocusableView for ConfigurationView {
fn focus_handle(&self, _: &AppContext) -> FocusHandle {
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, _cx: &WindowContext) -> Option<SharedString> {
fn tab_content_text(&self, _window: &Window, _cx: &App) -> Option<SharedString> {
Some("Configuration".into())
}
}
Loading

0 comments on commit 6fca1d2

Please sign in to comment.