Eliminate GPUI View, ViewContext, and WindowContext types (#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 <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>
This commit is contained in:
Nathan Sobo 2025-01-25 20:02:45 -07:00 committed by GitHub
parent 21b4a0d50e
commit 6fca1d2b0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
648 changed files with 36248 additions and 28208 deletions

View file

@ -1,4 +1,4 @@
use gpui::{actions, AppContext, ClipboardItem, PromptLevel};
use gpui::{actions, App, ClipboardItem, PromptLevel};
use system_specs::SystemSpecs;
use util::ResultExt;
use workspace::Workspace;
@ -45,18 +45,23 @@ fn file_bug_report_url(specs: &SystemSpecs) -> String {
)
}
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(|workspace: &mut Workspace, cx| {
feedback_modal::FeedbackModal::register(workspace, cx);
pub fn init(cx: &mut App) {
cx.observe_new(|workspace: &mut Workspace, window, cx| {
let Some(window) = window else {
return;
};
feedback_modal::FeedbackModal::register(workspace, window, cx);
workspace
.register_action(|_, _: &CopySystemSpecsIntoClipboard, cx| {
let specs = SystemSpecs::new(cx);
.register_action(|_, _: &CopySystemSpecsIntoClipboard, window, cx| {
let specs = SystemSpecs::new(window, cx);
cx.spawn(|_, mut cx| async move {
cx.spawn_in(window, |_, mut cx| async move {
let specs = specs.await.to_string();
cx.update(|cx| cx.write_to_clipboard(ClipboardItem::new_string(specs.clone())))
.log_err();
cx.update(|_, cx| {
cx.write_to_clipboard(ClipboardItem::new_string(specs.clone()))
})
.log_err();
cx.prompt(
PromptLevel::Info,
@ -65,33 +70,32 @@ pub fn init(cx: &mut AppContext) {
&["OK"],
)
.await
.ok();
})
.detach();
})
.register_action(|_, _: &RequestFeature, cx| {
let specs = SystemSpecs::new(cx);
cx.spawn(|_, mut cx| async move {
.register_action(|_, _: &RequestFeature, window, cx| {
let specs = SystemSpecs::new(window, cx);
cx.spawn_in(window, |_, mut cx| async move {
let specs = specs.await;
cx.update(|cx| {
cx.update(|_, cx| {
cx.open_url(&request_feature_url(&specs));
})
.log_err();
})
.detach();
})
.register_action(move |_, _: &FileBugReport, cx| {
let specs = SystemSpecs::new(cx);
cx.spawn(|_, mut cx| async move {
.register_action(move |_, _: &FileBugReport, window, cx| {
let specs = SystemSpecs::new(window, cx);
cx.spawn_in(window, |_, mut cx| async move {
let specs = specs.await;
cx.update(|cx| {
cx.update(|_, cx| {
cx.open_url(&file_bug_report_url(&specs));
})
.log_err();
})
.detach();
})
.register_action(move |_, _: &OpenZedRepo, cx| {
.register_action(move |_, _: &OpenZedRepo, _, cx| {
cx.open_url(zed_repo_url());
});
})

View file

@ -11,8 +11,8 @@ use db::kvp::KEY_VALUE_STORE;
use editor::{Editor, EditorEvent};
use futures::AsyncReadExt;
use gpui::{
div, rems, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Model,
PromptLevel, Render, Task, View, ViewContext,
div, rems, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
PromptLevel, Render, Task, Window,
};
use http_client::HttpClient;
use language::Buffer;
@ -76,23 +76,27 @@ enum SubmissionState {
pub struct FeedbackModal {
system_specs: SystemSpecs,
feedback_editor: View<Editor>,
email_address_editor: View<Editor>,
feedback_editor: Entity<Editor>,
email_address_editor: Entity<Editor>,
submission_state: Option<SubmissionState>,
dismiss_modal: bool,
character_count: i32,
}
impl FocusableView for FeedbackModal {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
impl Focusable for FeedbackModal {
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.feedback_editor.focus_handle(cx)
}
}
impl EventEmitter<DismissEvent> for FeedbackModal {}
impl ModalView for FeedbackModal {
fn on_before_dismiss(&mut self, cx: &mut ViewContext<Self>) -> DismissDecision {
self.update_email_in_store(cx);
fn on_before_dismiss(
&mut self,
window: &mut Window,
cx: &mut Context<Self>,
) -> DismissDecision {
self.update_email_in_store(window, cx);
if self.dismiss_modal {
return DismissDecision::Dismiss(true);
@ -103,9 +107,15 @@ impl ModalView for FeedbackModal {
return DismissDecision::Dismiss(true);
}
let answer = cx.prompt(PromptLevel::Info, "Discard feedback?", None, &["Yes", "No"]);
let answer = window.prompt(
PromptLevel::Info,
"Discard feedback?",
None,
&["Yes", "No"],
cx,
);
cx.spawn(move |this, mut cx| async move {
cx.spawn_in(window, move |this, mut cx| async move {
if answer.await.ok() == Some(0) {
this.update(&mut cx, |this, cx| {
this.dismiss_modal = true;
@ -121,11 +131,11 @@ impl ModalView for FeedbackModal {
}
impl FeedbackModal {
pub fn register(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
let _handle = cx.view().downgrade();
workspace.register_action(move |workspace, _: &GiveFeedback, cx| {
pub fn register(workspace: &mut Workspace, _: &mut Window, cx: &mut Context<Workspace>) {
let _handle = cx.model().downgrade();
workspace.register_action(move |workspace, _: &GiveFeedback, window, cx| {
workspace
.with_local_workspace(cx, |workspace, cx| {
.with_local_workspace(window, cx, |workspace, window, cx| {
let markdown = workspace
.app_state()
.languages
@ -133,17 +143,17 @@ impl FeedbackModal {
let project = workspace.project().clone();
let system_specs = SystemSpecs::new(cx);
cx.spawn(|workspace, mut cx| async move {
let system_specs = SystemSpecs::new(window, cx);
cx.spawn_in(window, |workspace, mut cx| async move {
let markdown = markdown.await.log_err();
let buffer = project.update(&mut cx, |project, cx| {
project.create_local_buffer("", markdown, cx)
})?;
let system_specs = system_specs.await;
workspace.update(&mut cx, |workspace, cx| {
workspace.toggle_modal(cx, move |cx| {
FeedbackModal::new(system_specs, project, buffer, cx)
workspace.update_in(&mut cx, |workspace, window, cx| {
workspace.toggle_modal(window, cx, move |window, cx| {
FeedbackModal::new(system_specs, project, buffer, window, cx)
});
})?;
@ -157,30 +167,31 @@ impl FeedbackModal {
pub fn new(
system_specs: SystemSpecs,
project: Model<Project>,
buffer: Model<Buffer>,
cx: &mut ViewContext<Self>,
project: Entity<Project>,
buffer: Entity<Buffer>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let email_address_editor = cx.new_view(|cx| {
let mut editor = Editor::single_line(cx);
let email_address_editor = cx.new(|cx| {
let mut editor = Editor::single_line(window, cx);
editor.set_placeholder_text("Email address (optional)", cx);
if let Ok(Some(email_address)) = KEY_VALUE_STORE.read_kvp(DATABASE_KEY_NAME) {
editor.set_text(email_address, cx)
editor.set_text(email_address, window, cx)
}
editor
});
let feedback_editor = cx.new_view(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(project.clone()), cx);
let feedback_editor = cx.new(|cx| {
let mut editor = Editor::for_buffer(buffer, Some(project.clone()), window, cx);
editor.set_placeholder_text(
"You can use markdown to organize your feedback with code and links.",
cx,
);
editor.set_show_gutter(false, cx);
editor.set_show_indent_guides(false, cx);
editor.set_show_inline_completions(Some(false), cx);
editor.set_show_inline_completions(Some(false), window, cx);
editor.set_vertical_scroll_margin(5, cx);
editor.set_use_modal_editing(false);
editor
@ -211,19 +222,24 @@ impl FeedbackModal {
}
}
pub fn submit(&mut self, cx: &mut ViewContext<Self>) -> Task<anyhow::Result<()>> {
pub fn submit(
&mut self,
window: &mut Window,
cx: &mut Context<Self>,
) -> Task<anyhow::Result<()>> {
let feedback_text = self.feedback_editor.read(cx).text(cx).trim().to_string();
let email = self.email_address_editor.read(cx).text_option(cx);
let answer = cx.prompt(
let answer = window.prompt(
PromptLevel::Info,
"Ready to submit your feedback?",
None,
&["Yes, Submit!", "No"],
cx,
);
let client = Client::global(cx).clone();
let specs = self.system_specs.clone();
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
let answer = answer.await.ok();
if answer == Some(0) {
this.update(&mut cx, |this, cx| {
@ -248,14 +264,15 @@ impl FeedbackModal {
}
Err(error) => {
log::error!("{}", error);
this.update(&mut cx, |this, cx| {
let prompt = cx.prompt(
this.update_in(&mut cx, |this, window, cx| {
let prompt = window.prompt(
PromptLevel::Critical,
FEEDBACK_SUBMISSION_ERROR_TEXT,
None,
&["OK"],
cx,
);
cx.spawn(|_, _cx| async move {
cx.spawn_in(window, |_, _cx| async move {
prompt.await.ok();
})
.detach();
@ -317,7 +334,7 @@ impl FeedbackModal {
Ok(())
}
fn update_submission_state(&mut self, cx: &mut ViewContext<Self>) {
fn update_submission_state(&mut self, cx: &mut Context<Self>) {
if self.awaiting_submission() {
return;
}
@ -348,10 +365,10 @@ impl FeedbackModal {
}
}
fn update_email_in_store(&self, cx: &mut ViewContext<Self>) {
fn update_email_in_store(&self, window: &mut Window, cx: &mut Context<Self>) {
let email = self.email_address_editor.read(cx).text_option(cx);
cx.spawn(|_, _| async move {
cx.spawn_in(window, |_, _| async move {
match email {
Some(email) => {
KEY_VALUE_STORE
@ -400,13 +417,13 @@ impl FeedbackModal {
matches!(self.submission_state, Some(SubmissionState::CanSubmit))
}
fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
fn cancel(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
cx.emit(DismissEvent)
}
}
impl Render for FeedbackModal {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.update_submission_state(cx);
let submit_button_text = if self.awaiting_submission() {
@ -415,7 +432,8 @@ impl Render for FeedbackModal {
"Submit"
};
let open_zed_repo = cx.listener(|_, _, cx| cx.dispatch_action(Box::new(OpenZedRepo)));
let open_zed_repo =
cx.listener(|_, _, window, cx| window.dispatch_action(Box::new(OpenZedRepo), cx));
v_flex()
.elevation_3(cx)
@ -496,8 +514,8 @@ impl Render for FeedbackModal {
Button::new("cancel_feedback", "Cancel")
.style(ButtonStyle::Subtle)
.color(Color::Muted)
.on_click(cx.listener(move |_, _, cx| {
cx.spawn(|this, mut cx| async move {
.on_click(cx.listener(move |_, _, window, cx| {
cx.spawn_in(window, |this, mut cx| async move {
this.update(&mut cx, |_, cx| cx.emit(DismissEvent))
.ok();
})
@ -508,11 +526,11 @@ impl Render for FeedbackModal {
Button::new("submit_feedback", submit_button_text)
.color(Color::Accent)
.style(ButtonStyle::Filled)
.on_click(cx.listener(|this, _, cx| {
this.submit(cx).detach();
.on_click(cx.listener(|this, _, window, cx| {
this.submit(window, cx).detach();
}))
.tooltip(move |cx| {
Tooltip::text("Submit feedback to the Zed team.", cx)
.tooltip(move |_, cx| {
Tooltip::simple("Submit feedback to the Zed team.", cx)
})
.when(!self.can_submit(), |this| this.disabled(true)),
),

View file

@ -1,5 +1,5 @@
use client::telemetry;
use gpui::{Task, WindowContext};
use gpui::{App, Task, Window};
use human_bytes::human_bytes;
use release_channel::{AppCommitSha, AppVersion, ReleaseChannel};
use serde::Serialize;
@ -19,7 +19,7 @@ pub struct SystemSpecs {
}
impl SystemSpecs {
pub fn new(cx: &WindowContext) -> Task<Self> {
pub fn new(window: &mut Window, cx: &mut App) -> Task<Self> {
let app_version = AppVersion::global(cx).to_string();
let release_channel = ReleaseChannel::global(cx);
let os_name = telemetry::os_name();
@ -35,7 +35,7 @@ impl SystemSpecs {
_ => None,
};
let gpu_specs = if let Some(specs) = cx.gpu_specs() {
let gpu_specs = if let Some(specs) = window.gpu_specs() {
Some(format!(
"{} || {} || {}",
specs.device_name, specs.driver_name, specs.driver_info