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, Pane};
use gpui::{
AnyView, Decorations, IntoElement, ParentElement, Render, Styled, Subscription, View,
ViewContext, WindowContext,
AnyView, App, Context, Decorations, Entity, IntoElement, ParentElement, Render, Styled,
Subscription, Window,
};
use std::any::TypeId;
use theme::CLIENT_SIDE_DECORATION_ROUNDING;
@ -12,7 +12,8 @@ pub trait StatusItemView: Render {
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>,
);
}
@ -21,7 +22,8 @@ trait StatusItemViewHandle: Send {
fn set_active_pane_item(
&self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
);
fn item_type(&self) -> TypeId;
}
@ -29,12 +31,12 @@ trait StatusItemViewHandle: Send {
pub struct StatusBar {
left_items: Vec<Box<dyn StatusItemViewHandle>>,
right_items: Vec<Box<dyn StatusItemViewHandle>>,
active_pane: View<Pane>,
active_pane: Entity<Pane>,
_observe_active_pane: Subscription,
}
impl Render for StatusBar {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
h_flex()
.w_full()
.justify_between()
@ -42,7 +44,7 @@ impl Render for StatusBar {
.py(DynamicSpacing::Base04.rems(cx))
.px(DynamicSpacing::Base08.rems(cx))
.bg(cx.theme().colors().status_bar_background)
.map(|el| match cx.window_decorations() {
.map(|el| match window.window_decorations() {
Decorations::Server => el,
Decorations::Client { tiling, .. } => el
.when(!(tiling.bottom || tiling.right), |el| {
@ -62,14 +64,14 @@ impl Render for StatusBar {
}
impl StatusBar {
fn render_left_tools(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render_left_tools(&self, cx: &mut Context<Self>) -> impl IntoElement {
h_flex()
.gap(DynamicSpacing::Base04.rems(cx))
.overflow_x_hidden()
.children(self.left_items.iter().map(|item| item.to_any()))
}
fn render_right_tools(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render_right_tools(&self, cx: &mut Context<Self>) -> impl IntoElement {
h_flex()
.gap(DynamicSpacing::Base04.rems(cx))
.children(self.right_items.iter().rev().map(|item| item.to_any()))
@ -77,30 +79,31 @@ impl StatusBar {
}
impl StatusBar {
pub fn new(active_pane: &View<Pane>, cx: &mut ViewContext<Self>) -> Self {
pub fn new(active_pane: &Entity<Pane>, window: &mut Window, cx: &mut Context<Self>) -> Self {
let mut this = Self {
left_items: Default::default(),
right_items: Default::default(),
active_pane: active_pane.clone(),
_observe_active_pane: cx
.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx)),
_observe_active_pane: cx.observe_in(active_pane, window, |this, _, window, cx| {
this.update_active_pane_item(window, cx)
}),
};
this.update_active_pane_item(cx);
this.update_active_pane_item(window, cx);
this
}
pub fn add_left_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
pub fn add_left_item<T>(&mut self, item: Entity<T>, window: &mut Window, cx: &mut Context<Self>)
where
T: 'static + StatusItemView,
{
let active_pane_item = self.active_pane.read(cx).active_item();
item.set_active_pane_item(active_pane_item.as_deref(), cx);
item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
self.left_items.push(Box::new(item));
cx.notify();
}
pub fn item_of_type<T: StatusItemView>(&self) -> Option<View<T>> {
pub fn item_of_type<T: StatusItemView>(&self) -> Option<Entity<T>> {
self.left_items
.iter()
.chain(self.right_items.iter())
@ -127,13 +130,14 @@ impl StatusBar {
pub fn insert_item_after<T>(
&mut self,
position: usize,
item: View<T>,
cx: &mut ViewContext<Self>,
item: Entity<T>,
window: &mut Window,
cx: &mut Context<Self>,
) where
T: 'static + StatusItemView,
{
let active_pane_item = self.active_pane.read(cx).active_item();
item.set_active_pane_item(active_pane_item.as_deref(), cx);
item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
if position < self.left_items.len() {
self.left_items.insert(position + 1, Box::new(item))
@ -144,7 +148,7 @@ impl StatusBar {
cx.notify()
}
pub fn remove_item_at(&mut self, position: usize, cx: &mut ViewContext<Self>) {
pub fn remove_item_at(&mut self, position: usize, cx: &mut Context<Self>) {
if position < self.left_items.len() {
self.left_items.remove(position);
} else {
@ -153,33 +157,43 @@ impl StatusBar {
cx.notify();
}
pub fn add_right_item<T>(&mut self, item: View<T>, cx: &mut ViewContext<Self>)
where
pub fn add_right_item<T>(
&mut self,
item: Entity<T>,
window: &mut Window,
cx: &mut Context<Self>,
) where
T: 'static + StatusItemView,
{
let active_pane_item = self.active_pane.read(cx).active_item();
item.set_active_pane_item(active_pane_item.as_deref(), cx);
item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
self.right_items.push(Box::new(item));
cx.notify();
}
pub fn set_active_pane(&mut self, active_pane: &View<Pane>, cx: &mut ViewContext<Self>) {
pub fn set_active_pane(
&mut self,
active_pane: &Entity<Pane>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.active_pane = active_pane.clone();
self._observe_active_pane =
cx.observe(active_pane, |this, _, cx| this.update_active_pane_item(cx));
self.update_active_pane_item(cx);
self._observe_active_pane = cx.observe_in(active_pane, window, |this, _, window, cx| {
this.update_active_pane_item(window, cx)
});
self.update_active_pane_item(window, cx);
}
fn update_active_pane_item(&mut self, cx: &mut ViewContext<Self>) {
fn update_active_pane_item(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let active_pane_item = self.active_pane.read(cx).active_item();
for item in self.left_items.iter().chain(&self.right_items) {
item.set_active_pane_item(active_pane_item.as_deref(), cx);
item.set_active_pane_item(active_pane_item.as_deref(), window, cx);
}
}
}
impl<T: StatusItemView> StatusItemViewHandle for View<T> {
impl<T: StatusItemView> StatusItemViewHandle for Entity<T> {
fn to_any(&self) -> AnyView {
self.clone().into()
}
@ -187,10 +201,11 @@ impl<T: StatusItemView> StatusItemViewHandle for View<T> {
fn set_active_pane_item(
&self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) {
self.update(cx, |this, cx| {
this.set_active_pane_item(active_pane_item, cx)
this.set_active_pane_item(active_pane_item, window, cx)
});
}