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,6 +1,6 @@
use gpui::{
AnyElement, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Model,
Subscription, Task, View, WeakModel, WeakView,
AnyElement, App, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Subscription,
Task, WeakEntity,
};
use picker::{Picker, PickerDelegate};
use project::{
@ -11,7 +11,7 @@ use std::sync::Arc;
use ui::{prelude::*, ListItem, ListItemSpacing, PopoverMenu, PopoverMenuHandle, PopoverTrigger};
pub struct RepositorySelector {
picker: View<Picker<RepositorySelectorDelegate>>,
picker: Entity<Picker<RepositorySelectorDelegate>>,
/// The task used to update the picker's matches when there is a change to
/// the repository list.
update_matches_task: Option<Task<()>>,
@ -19,7 +19,7 @@ pub struct RepositorySelector {
}
impl RepositorySelector {
pub fn new(project: Model<Project>, cx: &mut ViewContext<Self>) -> Self {
pub fn new(project: Entity<Project>, window: &mut Window, cx: &mut Context<Self>) -> Self {
let git_state = project.read(cx).git_state().cloned();
let all_repositories = git_state
.as_ref()
@ -27,17 +27,18 @@ impl RepositorySelector {
let filtered_repositories = all_repositories.clone();
let delegate = RepositorySelectorDelegate {
project: project.downgrade(),
repository_selector: cx.view().downgrade(),
repository_selector: cx.model().downgrade(),
repository_entries: all_repositories,
filtered_repositories,
selected_index: 0,
};
let picker =
cx.new_view(|cx| Picker::uniform_list(delegate, cx).max_height(Some(rems(20.).into())));
let picker = cx.new(|cx| {
Picker::uniform_list(delegate, window, cx).max_height(Some(rems(20.).into()))
});
let _subscriptions = if let Some(git_state) = git_state {
vec![cx.subscribe(&git_state, Self::handle_project_git_event)]
vec![cx.subscribe_in(&git_state, window, Self::handle_project_git_event)]
} else {
Vec::new()
};
@ -51,15 +52,16 @@ impl RepositorySelector {
fn handle_project_git_event(
&mut self,
git_state: Model<GitState>,
git_state: &Entity<GitState>,
_event: &project::git::Event,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
// TODO handle events individually
let task = self.picker.update(cx, |this, cx| {
let query = this.query(cx);
this.delegate.repository_entries = git_state.read(cx).all_repositories();
this.delegate.update_matches(query, cx)
this.delegate.update_matches(query, window, cx)
});
self.update_matches_task = Some(task);
}
@ -67,14 +69,14 @@ impl RepositorySelector {
impl EventEmitter<DismissEvent> for RepositorySelector {}
impl FocusableView for RepositorySelector {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
impl Focusable for RepositorySelector {
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
impl Render for RepositorySelector {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
self.picker.clone()
}
}
@ -84,13 +86,13 @@ pub struct RepositorySelectorPopoverMenu<T>
where
T: PopoverTrigger,
{
repository_selector: View<RepositorySelector>,
repository_selector: Entity<RepositorySelector>,
trigger: T,
handle: Option<PopoverMenuHandle<RepositorySelector>>,
}
impl<T: PopoverTrigger> RepositorySelectorPopoverMenu<T> {
pub fn new(repository_selector: View<RepositorySelector>, trigger: T) -> Self {
pub fn new(repository_selector: Entity<RepositorySelector>, trigger: T) -> Self {
Self {
repository_selector,
trigger,
@ -105,11 +107,11 @@ impl<T: PopoverTrigger> RepositorySelectorPopoverMenu<T> {
}
impl<T: PopoverTrigger> RenderOnce for RepositorySelectorPopoverMenu<T> {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let repository_selector = self.repository_selector.clone();
PopoverMenu::new("repository-switcher")
.menu(move |_cx| Some(repository_selector.clone()))
.menu(move |_window, _cx| Some(repository_selector.clone()))
.trigger(self.trigger)
.attach(gpui::Corner::BottomLeft)
.when_some(self.handle.clone(), |menu, handle| menu.with_handle(handle))
@ -117,8 +119,8 @@ impl<T: PopoverTrigger> RenderOnce for RepositorySelectorPopoverMenu<T> {
}
pub struct RepositorySelectorDelegate {
project: WeakModel<Project>,
repository_selector: WeakView<RepositorySelector>,
project: WeakEntity<Project>,
repository_selector: WeakEntity<RepositorySelector>,
repository_entries: Vec<RepositoryHandle>,
filtered_repositories: Vec<RepositoryHandle>,
selected_index: usize,
@ -143,19 +145,29 @@ impl PickerDelegate for RepositorySelectorDelegate {
self.selected_index
}
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>) {
fn set_selected_index(
&mut self,
ix: usize,
_window: &mut Window,
cx: &mut Context<Picker<Self>>,
) {
self.selected_index = ix.min(self.filtered_repositories.len().saturating_sub(1));
cx.notify();
}
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
"Select a repository...".into()
}
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
fn update_matches(
&mut self,
query: String,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Task<()> {
let all_repositories = self.repository_entries.clone();
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
let filtered_repositories = cx
.background_executor()
.spawn(async move {
@ -173,30 +185,34 @@ impl PickerDelegate for RepositorySelectorDelegate {
})
.await;
this.update(&mut cx, |this, cx| {
this.update_in(&mut cx, |this, window, cx| {
this.delegate.filtered_repositories = filtered_repositories;
this.delegate.set_selected_index(0, cx);
this.delegate.set_selected_index(0, window, cx);
cx.notify();
})
.ok();
})
}
fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
fn confirm(&mut self, _secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
let Some(selected_repo) = self.filtered_repositories.get(self.selected_index) else {
return;
};
selected_repo.activate(cx);
self.dismissed(cx);
self.dismissed(window, cx);
}
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
fn dismissed(&mut self, _window: &mut Window, cx: &mut Context<Picker<Self>>) {
self.repository_selector
.update(cx, |_this, cx| cx.emit(DismissEvent))
.ok();
}
fn render_header(&self, _cx: &mut ViewContext<Picker<Self>>) -> Option<AnyElement> {
fn render_header(
&self,
_window: &mut Window,
_cx: &mut Context<Picker<Self>>,
) -> Option<AnyElement> {
// TODO: Implement header rendering if needed
None
}
@ -205,7 +221,8 @@ impl PickerDelegate for RepositorySelectorDelegate {
&self,
ix: usize,
selected: bool,
cx: &mut ViewContext<Picker<Self>>,
_window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Option<Self::ListItem> {
let project = self.project.upgrade()?;
let repo_info = self.filtered_repositories.get(ix)?;
@ -220,7 +237,11 @@ impl PickerDelegate for RepositorySelectorDelegate {
)
}
fn render_footer(&self, cx: &mut ViewContext<Picker<Self>>) -> Option<gpui::AnyElement> {
fn render_footer(
&self,
_window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Option<gpui::AnyElement> {
// TODO: Implement footer rendering if needed
Some(
div()