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,7 +1,7 @@
use crate::ItemHandle;
use gpui::{
AnyView, Entity, EntityId, EventEmitter, ParentElement as _, Render, Styled, View, ViewContext,
WindowContext,
AnyView, App, Context, Entity, EntityId, EventEmitter, ParentElement as _, Render, Styled,
Window,
};
use ui::prelude::*;
use ui::{h_flex, v_flex};
@ -14,10 +14,17 @@ pub trait ToolbarItemView: Render + EventEmitter<ToolbarItemEvent> {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn crate::ItemHandle>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) -> ToolbarItemLocation;
fn pane_focus_update(&mut self, _pane_focused: bool, _cx: &mut ViewContext<Self>) {}
fn pane_focus_update(
&mut self,
_pane_focused: bool,
_window: &mut Window,
_cx: &mut Context<Self>,
) {
}
}
trait ToolbarItemViewHandle: Send {
@ -26,9 +33,10 @@ trait ToolbarItemViewHandle: Send {
fn set_active_pane_item(
&self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> ToolbarItemLocation;
fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext);
fn focus_changed(&mut self, pane_focused: bool, window: &mut Window, cx: &mut App);
}
#[derive(Copy, Clone, Debug, PartialEq)]
@ -85,7 +93,7 @@ impl Toolbar {
}
impl Render for Toolbar {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
if !self.has_any_visible_items() {
return div();
}
@ -157,16 +165,16 @@ impl Toolbar {
}
}
pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext<Self>) {
pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut Context<Self>) {
self.can_navigate = can_navigate;
cx.notify();
}
pub fn add_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
pub fn add_item<T>(&mut self, item: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
where
T: 'static + ToolbarItemView,
{
let location = item.set_active_pane_item(self.active_item.as_deref(), cx);
let location = item.set_active_pane_item(self.active_item.as_deref(), window, cx);
cx.subscribe(&item, |this, item, event, cx| {
if let Some((_, current_location)) = this
.items
@ -188,7 +196,12 @@ impl Toolbar {
cx.notify();
}
pub fn set_active_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
pub fn set_active_item(
&mut self,
item: Option<&dyn ItemHandle>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.active_item = item.map(|item| item.boxed_clone());
self.hidden = self
.active_item
@ -197,7 +210,7 @@ impl Toolbar {
.unwrap_or(false);
for (toolbar_item, current_location) in self.items.iter_mut() {
let new_location = toolbar_item.set_active_pane_item(item, cx);
let new_location = toolbar_item.set_active_pane_item(item, window, cx);
if new_location != *current_location {
*current_location = new_location;
cx.notify();
@ -205,13 +218,13 @@ impl Toolbar {
}
}
pub fn focus_changed(&mut self, focused: bool, cx: &mut ViewContext<Self>) {
pub fn focus_changed(&mut self, focused: bool, window: &mut Window, cx: &mut Context<Self>) {
for (toolbar_item, _) in self.items.iter_mut() {
toolbar_item.focus_changed(focused, cx);
toolbar_item.focus_changed(focused, window, cx);
}
}
pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<View<T>> {
pub fn item_of_type<T: ToolbarItemView>(&self) -> Option<Entity<T>> {
self.items
.iter()
.find_map(|(item, _)| item.to_any().downcast().ok())
@ -222,7 +235,7 @@ impl Toolbar {
}
}
impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
impl<T: ToolbarItemView> ToolbarItemViewHandle for Entity<T> {
fn id(&self) -> EntityId {
self.entity_id()
}
@ -234,16 +247,17 @@ impl<T: ToolbarItemView> ToolbarItemViewHandle for View<T> {
fn set_active_pane_item(
&self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> ToolbarItemLocation {
self.update(cx, |this, cx| {
this.set_active_pane_item(active_pane_item, cx)
this.set_active_pane_item(active_pane_item, window, cx)
})
}
fn focus_changed(&mut self, pane_focused: bool, cx: &mut WindowContext) {
fn focus_changed(&mut self, pane_focused: bool, window: &mut Window, cx: &mut App) {
self.update(cx, |this, cx| {
this.pane_focus_update(pane_focused, cx);
this.pane_focus_update(pane_focused, window, cx);
cx.notify();
});
}