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 @@
#![allow(missing_docs)]
use gpui::{Action, AnyView, FocusHandle, IntoElement, Render, VisualContext};
use gpui::{Action, AnyView, AppContext as _, FocusHandle, IntoElement, Render};
use settings::Settings;
use theme::ThemeSettings;
@ -14,8 +14,8 @@ pub struct Tooltip {
}
impl Tooltip {
pub fn text(title: impl Into<SharedString>, cx: &mut WindowContext) -> AnyView {
cx.new_view(|_cx| Self {
pub fn simple(title: impl Into<SharedString>, cx: &mut App) -> AnyView {
cx.new(|_| Self {
title: title.into(),
meta: None,
key_binding: None,
@ -23,15 +23,28 @@ impl Tooltip {
.into()
}
pub fn text(title: impl Into<SharedString>) -> impl Fn(&mut Window, &mut App) -> AnyView {
let title = title.into();
move |_, cx| {
cx.new(|_| Self {
title: title.clone(),
meta: None,
key_binding: None,
})
.into()
}
}
pub fn for_action(
title: impl Into<SharedString>,
action: &dyn Action,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> AnyView {
cx.new_view(|cx| Self {
cx.new(|_| Self {
title: title.into(),
meta: None,
key_binding: KeyBinding::for_action(action, cx),
key_binding: KeyBinding::for_action(action, window),
})
.into()
}
@ -40,12 +53,13 @@ impl Tooltip {
title: impl Into<SharedString>,
action: &dyn Action,
focus_handle: &FocusHandle,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> AnyView {
cx.new_view(|cx| Self {
cx.new(|_| Self {
title: title.into(),
meta: None,
key_binding: KeyBinding::for_action_in(action, focus_handle, cx),
key_binding: KeyBinding::for_action_in(action, focus_handle, window),
})
.into()
}
@ -54,12 +68,13 @@ impl Tooltip {
title: impl Into<SharedString>,
action: Option<&dyn Action>,
meta: impl Into<SharedString>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> AnyView {
cx.new_view(|cx| Self {
cx.new(|_| Self {
title: title.into(),
meta: Some(meta.into()),
key_binding: action.and_then(|action| KeyBinding::for_action(action, cx)),
key_binding: action.and_then(|action| KeyBinding::for_action(action, window)),
})
.into()
}
@ -69,13 +84,14 @@ impl Tooltip {
action: Option<&dyn Action>,
meta: impl Into<SharedString>,
focus_handle: &FocusHandle,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> AnyView {
cx.new_view(|cx| Self {
cx.new(|_| Self {
title: title.into(),
meta: Some(meta.into()),
key_binding: action
.and_then(|action| KeyBinding::for_action_in(action, focus_handle, cx)),
.and_then(|action| KeyBinding::for_action_in(action, focus_handle, window)),
})
.into()
}
@ -100,8 +116,8 @@ impl Tooltip {
}
impl Render for Tooltip {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
tooltip_container(cx, |el, _| {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
tooltip_container(window, cx, |el, _, _| {
el.child(
h_flex()
.gap_4()
@ -118,8 +134,9 @@ impl Render for Tooltip {
}
pub fn tooltip_container<V>(
cx: &mut ViewContext<V>,
f: impl FnOnce(Div, &mut ViewContext<V>) -> Div,
window: &mut Window,
cx: &mut Context<V>,
f: impl FnOnce(Div, &mut Window, &mut Context<V>) -> Div,
) -> impl IntoElement {
let ui_font = ThemeSettings::get_global(cx).ui_font.clone();
@ -132,7 +149,7 @@ pub fn tooltip_container<V>(
.text_color(cx.theme().colors().text)
.py_1()
.px_2()
.map(|el| f(el, cx)),
.map(|el| f(el, window, cx)),
)
}
@ -141,7 +158,7 @@ pub struct LinkPreview {
}
impl LinkPreview {
pub fn new(url: &str, cx: &mut WindowContext) -> AnyView {
pub fn new(url: &str, cx: &mut App) -> AnyView {
let mut wrapped_url = String::new();
for (i, ch) in url.chars().enumerate() {
if i == 500 {
@ -153,7 +170,7 @@ impl LinkPreview {
}
wrapped_url.push(ch);
}
cx.new_view(|_cx| LinkPreview {
cx.new(|_| LinkPreview {
link: wrapped_url.into(),
})
.into()
@ -161,8 +178,8 @@ impl LinkPreview {
}
impl Render for LinkPreview {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
tooltip_container(cx, |el, _| {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
tooltip_container(window, cx, |el, _, _| {
el.child(
Label::new(self.link.clone())
.size(LabelSize::XSmall)