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

@ -4,9 +4,9 @@ use anyhow::Context as _;
use editor::items::entry_git_aware_label_color;
use file_icons::FileIcons;
use gpui::{
canvas, div, fill, img, opaque_grey, point, size, AnyElement, AppContext, Bounds, EventEmitter,
FocusHandle, FocusableView, InteractiveElement, IntoElement, Model, ObjectFit, ParentElement,
Render, Styled, Task, View, ViewContext, VisualContext, WeakView, WindowContext,
canvas, div, fill, img, opaque_grey, point, size, AnyElement, App, Bounds, Context, Entity,
EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, ObjectFit,
ParentElement, Render, Styled, Task, WeakEntity, Window,
};
use persistence::IMAGE_VIEWER;
use project::{image_store::ImageItemEvent, ImageItem, Project, ProjectPath};
@ -22,16 +22,17 @@ use workspace::{
const IMAGE_VIEWER_KIND: &str = "ImageView";
pub struct ImageView {
image_item: Model<ImageItem>,
project: Model<Project>,
image_item: Entity<ImageItem>,
project: Entity<Project>,
focus_handle: FocusHandle,
}
impl ImageView {
pub fn new(
image_item: Model<ImageItem>,
project: Model<Project>,
cx: &mut ViewContext<Self>,
image_item: Entity<ImageItem>,
project: Entity<Project>,
cx: &mut Context<Self>,
) -> Self {
cx.subscribe(&image_item, Self::on_image_event).detach();
Self {
@ -43,9 +44,9 @@ impl ImageView {
fn on_image_event(
&mut self,
_: Model<ImageItem>,
_: Entity<ImageItem>,
event: &ImageItemEvent,
cx: &mut ViewContext<Self>,
cx: &mut Context<Self>,
) {
match event {
ImageItemEvent::FileHandleChanged | ImageItemEvent::Reloaded => {
@ -77,23 +78,23 @@ impl Item for ImageView {
fn for_each_project_item(
&self,
cx: &AppContext,
cx: &App,
f: &mut dyn FnMut(gpui::EntityId, &dyn project::ProjectItem),
) {
f(self.image_item.entity_id(), self.image_item.read(cx))
}
fn is_singleton(&self, _cx: &AppContext) -> bool {
fn is_singleton(&self, _cx: &App) -> bool {
true
}
fn tab_tooltip_text(&self, cx: &AppContext) -> Option<SharedString> {
fn tab_tooltip_text(&self, cx: &App) -> Option<SharedString> {
let abs_path = self.image_item.read(cx).file.as_local()?.abs_path(cx);
let file_path = abs_path.compact().to_string_lossy().to_string();
Some(file_path.into())
}
fn tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement {
fn tab_content(&self, params: TabContentParams, _: &Window, cx: &App) -> AnyElement {
let project_path = self.image_item.read(cx).project_path(cx);
let label_color = if ItemSettings::get_global(cx).git_status {
@ -129,7 +130,7 @@ impl Item for ImageView {
.into_any_element()
}
fn tab_icon(&self, cx: &WindowContext) -> Option<Icon> {
fn tab_icon(&self, _: &Window, cx: &App) -> Option<Icon> {
let path = self.image_item.read(cx).path();
ItemSettings::get_global(cx)
.file_icons
@ -138,11 +139,11 @@ impl Item for ImageView {
.map(Icon::from_path)
}
fn breadcrumb_location(&self, _: &AppContext) -> ToolbarItemLocation {
fn breadcrumb_location(&self, _: &App) -> ToolbarItemLocation {
ToolbarItemLocation::PrimaryLeft
}
fn breadcrumbs(&self, _theme: &Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
fn breadcrumbs(&self, _theme: &Theme, cx: &App) -> Option<Vec<BreadcrumbText>> {
let text = breadcrumbs_text_for_image(self.project.read(cx), self.image_item.read(cx), cx);
Some(vec![BreadcrumbText {
text,
@ -154,12 +155,13 @@ impl Item for ImageView {
fn clone_on_split(
&self,
_workspace_id: Option<WorkspaceId>,
cx: &mut ViewContext<Self>,
) -> Option<View<Self>>
_: &mut Window,
cx: &mut Context<Self>,
) -> Option<Entity<Self>>
where
Self: Sized,
{
Some(cx.new_view(|cx| Self {
Some(cx.new(|cx| Self {
image_item: self.image_item.clone(),
project: self.project.clone(),
focus_handle: cx.focus_handle(),
@ -167,7 +169,7 @@ impl Item for ImageView {
}
}
fn breadcrumbs_text_for_image(project: &Project, image: &ImageItem, cx: &AppContext) -> String {
fn breadcrumbs_text_for_image(project: &Project, image: &ImageItem, cx: &App) -> String {
let path = image.file.file_name(cx);
if project.visible_worktrees(cx).count() <= 1 {
return path.to_string_lossy().to_string();
@ -190,13 +192,14 @@ impl SerializableItem for ImageView {
}
fn deserialize(
project: Model<Project>,
_workspace: WeakView<Workspace>,
project: Entity<Project>,
_workspace: WeakEntity<Workspace>,
workspace_id: WorkspaceId,
item_id: ItemId,
cx: &mut WindowContext,
) -> Task<gpui::Result<View<Self>>> {
cx.spawn(|mut cx| async move {
window: &mut Window,
cx: &mut App,
) -> Task<gpui::Result<Entity<Self>>> {
window.spawn(cx, |mut cx| async move {
let image_path = IMAGE_VIEWER
.get_image_path(item_id, workspace_id)?
.ok_or_else(|| anyhow::anyhow!("No image path found"))?;
@ -218,16 +221,19 @@ impl SerializableItem for ImageView {
.update(&mut cx, |project, cx| project.open_image(project_path, cx))?
.await?;
cx.update(|cx| Ok(cx.new_view(|cx| ImageView::new(image_item, project, cx))))?
cx.update(|_, cx| Ok(cx.new(|cx| ImageView::new(image_item, project, cx))))?
})
}
fn cleanup(
workspace_id: WorkspaceId,
alive_items: Vec<ItemId>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> Task<gpui::Result<()>> {
cx.spawn(|_| IMAGE_VIEWER.delete_unloaded_items(workspace_id, alive_items))
window.spawn(cx, |_| {
IMAGE_VIEWER.delete_unloaded_items(workspace_id, alive_items)
})
}
fn serialize(
@ -235,7 +241,8 @@ impl SerializableItem for ImageView {
workspace: &mut Workspace,
item_id: ItemId,
_closing: bool,
cx: &mut ViewContext<Self>,
_window: &mut Window,
cx: &mut Context<Self>,
) -> Option<Task<gpui::Result<()>>> {
let workspace_id = workspace.database_id()?;
let image_path = self.image_item.read(cx).file.as_local()?.abs_path(cx);
@ -255,16 +262,19 @@ impl SerializableItem for ImageView {
}
impl EventEmitter<()> for ImageView {}
impl FocusableView for ImageView {
fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
impl Focusable for ImageView {
fn focus_handle(&self, _cx: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl Render for ImageView {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let image = self.image_item.read(cx).image.clone();
let checkered_background = |bounds: Bounds<Pixels>, _, cx: &mut WindowContext| {
let checkered_background = |bounds: Bounds<Pixels>,
_,
window: &mut Window,
_cx: &mut App| {
let square_size = 32.0;
let start_y = bounds.origin.y.0;
@ -289,7 +299,7 @@ impl Render for ImageView {
opaque_grey(0.7, 0.4)
};
cx.paint_quad(fill(rect, color));
window.paint_quad(fill(rect, color));
color_swapper = !color_swapper;
x += square_size;
}
@ -299,7 +309,7 @@ impl Render for ImageView {
}
};
let checkered_background = canvas(|_, _| (), checkered_background)
let checkered_background = canvas(|_, _, _| (), checkered_background)
.border_2()
.border_color(cx.theme().styles.colors.border)
.size_full()
@ -334,9 +344,10 @@ impl ProjectItem for ImageView {
type Item = ImageItem;
fn for_project_item(
project: Model<Project>,
item: Model<Self::Item>,
cx: &mut ViewContext<Self>,
project: Entity<Project>,
item: Entity<Self::Item>,
_: &mut Window,
cx: &mut Context<Self>,
) -> Self
where
Self: Sized,
@ -345,7 +356,7 @@ impl ProjectItem for ImageView {
}
}
pub fn init(cx: &mut AppContext) {
pub fn init(cx: &mut App) {
workspace::register_project_item::<ImageView>(cx);
workspace::register_serializable_item::<ImageView>(cx)
}