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,8 +4,8 @@ use crate::{
List, ListItem, ListSeparator, ListSubHeader,
};
use gpui::{
px, Action, AnyElement, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView,
IntoElement, Render, Subscription, View, VisualContext,
px, Action, AnyElement, App, AppContext as _, DismissEvent, Entity, EventEmitter, FocusHandle,
Focusable, IntoElement, Render, Subscription,
};
use menu::{SelectFirst, SelectLast, SelectNext, SelectPrev};
use settings::Settings;
@ -18,20 +18,20 @@ pub enum ContextMenuItem {
Label(SharedString),
Entry(ContextMenuEntry),
CustomEntry {
entry_render: Box<dyn Fn(&mut WindowContext) -> AnyElement>,
handler: Rc<dyn Fn(Option<&FocusHandle>, &mut WindowContext)>,
entry_render: Box<dyn Fn(&mut Window, &mut App) -> AnyElement>,
handler: Rc<dyn Fn(Option<&FocusHandle>, &mut Window, &mut App)>,
selectable: bool,
},
}
impl ContextMenuItem {
pub fn custom_entry(
entry_render: impl Fn(&mut WindowContext) -> AnyElement + 'static,
handler: impl Fn(&mut WindowContext) + 'static,
entry_render: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
handler: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
Self::CustomEntry {
entry_render: Box::new(entry_render),
handler: Rc::new(move |_, cx| handler(cx)),
handler: Rc::new(move |_, window, cx| handler(window, cx)),
selectable: true,
}
}
@ -44,7 +44,7 @@ pub struct ContextMenuEntry {
icon_position: IconPosition,
icon_size: IconSize,
icon_color: Option<Color>,
handler: Rc<dyn Fn(Option<&FocusHandle>, &mut WindowContext)>,
handler: Rc<dyn Fn(Option<&FocusHandle>, &mut Window, &mut App)>,
action: Option<Box<dyn Action>>,
disabled: bool,
}
@ -58,7 +58,7 @@ impl ContextMenuEntry {
icon_position: IconPosition::Start,
icon_size: IconSize::Small,
icon_color: None,
handler: Rc::new(|_, _| {}),
handler: Rc::new(|_, _, _| {}),
action: None,
disabled: false,
}
@ -94,8 +94,8 @@ impl ContextMenuEntry {
self
}
pub fn handler(mut self, handler: impl Fn(&mut WindowContext) + 'static) -> Self {
self.handler = Rc::new(move |_, cx| handler(cx));
pub fn handler(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.handler = Rc::new(move |_, window, cx| handler(window, cx));
self
}
@ -122,8 +122,8 @@ pub struct ContextMenu {
keep_open_on_confirm: bool,
}
impl FocusableView for ContextMenu {
fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
impl Focusable for ContextMenu {
fn focus_handle(&self, _cx: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
@ -134,15 +134,18 @@ impl FluentBuilder for ContextMenu {}
impl ContextMenu {
pub fn build(
cx: &mut WindowContext,
f: impl FnOnce(Self, &mut ViewContext<Self>) -> Self,
) -> View<Self> {
cx.new_view(|cx| {
window: &mut Window,
cx: &mut App,
f: impl FnOnce(Self, &mut Window, &mut Context<Self>) -> Self,
) -> Entity<Self> {
cx.new(|cx| {
let focus_handle = cx.focus_handle();
let _on_blur_subscription = cx.on_blur(&focus_handle, |this: &mut ContextMenu, cx| {
this.cancel(&menu::Cancel, cx)
});
cx.refresh();
let _on_blur_subscription = cx.on_blur(
&focus_handle,
window,
|this: &mut ContextMenu, window, cx| this.cancel(&menu::Cancel, window, cx),
);
window.refresh();
f(
Self {
items: Default::default(),
@ -154,6 +157,7 @@ impl ContextMenu {
_on_blur_subscription,
keep_open_on_confirm: false,
},
window,
cx,
)
})
@ -188,12 +192,12 @@ impl ContextMenu {
mut self,
label: impl Into<SharedString>,
action: Option<Box<dyn Action>>,
handler: impl Fn(&mut WindowContext) + 'static,
handler: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
self.items.push(ContextMenuItem::Entry(ContextMenuEntry {
toggle: None,
label: label.into(),
handler: Rc::new(move |_, cx| handler(cx)),
handler: Rc::new(move |_, window, cx| handler(window, cx)),
icon: None,
icon_position: IconPosition::End,
icon_size: IconSize::Small,
@ -210,12 +214,12 @@ impl ContextMenu {
toggled: bool,
position: IconPosition,
action: Option<Box<dyn Action>>,
handler: impl Fn(&mut WindowContext) + 'static,
handler: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
self.items.push(ContextMenuItem::Entry(ContextMenuEntry {
toggle: Some((position, toggled)),
label: label.into(),
handler: Rc::new(move |_, cx| handler(cx)),
handler: Rc::new(move |_, window, cx| handler(window, cx)),
icon: None,
icon_position: position,
icon_size: IconSize::Small,
@ -228,11 +232,11 @@ impl ContextMenu {
pub fn custom_row(
mut self,
entry_render: impl Fn(&mut WindowContext) -> AnyElement + 'static,
entry_render: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
) -> Self {
self.items.push(ContextMenuItem::CustomEntry {
entry_render: Box::new(entry_render),
handler: Rc::new(|_, _| {}),
handler: Rc::new(|_, _, _| {}),
selectable: false,
});
self
@ -240,12 +244,12 @@ impl ContextMenu {
pub fn custom_entry(
mut self,
entry_render: impl Fn(&mut WindowContext) -> AnyElement + 'static,
handler: impl Fn(&mut WindowContext) + 'static,
entry_render: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
handler: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
self.items.push(ContextMenuItem::CustomEntry {
entry_render: Box::new(entry_render),
handler: Rc::new(move |_, cx| handler(cx)),
handler: Rc::new(move |_, window, cx| handler(window, cx)),
selectable: true,
});
self
@ -261,12 +265,11 @@ impl ContextMenu {
toggle: None,
label: label.into(),
action: Some(action.boxed_clone()),
handler: Rc::new(move |context, cx| {
handler: Rc::new(move |context, window, cx| {
if let Some(context) = &context {
cx.focus(context);
window.focus(context);
}
cx.dispatch_action(action.boxed_clone());
window.dispatch_action(action.boxed_clone(), cx);
}),
icon: None,
icon_position: IconPosition::End,
@ -287,11 +290,11 @@ impl ContextMenu {
label: label.into(),
action: Some(action.boxed_clone()),
handler: Rc::new(move |context, cx| {
handler: Rc::new(move |context, window, cx| {
if let Some(context) = &context {
cx.focus(context);
window.focus(context);
}
cx.dispatch_action(action.boxed_clone());
window.dispatch_action(action.boxed_clone(), cx);
}),
icon: None,
icon_size: IconSize::Small,
@ -308,7 +311,7 @@ impl ContextMenu {
label: label.into(),
action: Some(action.boxed_clone()),
handler: Rc::new(move |_, cx| cx.dispatch_action(action.boxed_clone())),
handler: Rc::new(move |_, window, cx| window.dispatch_action(action.boxed_clone(), cx)),
icon: Some(IconName::ArrowUpRight),
icon_size: IconSize::XSmall,
icon_position: IconPosition::End,
@ -323,7 +326,7 @@ impl ContextMenu {
self
}
pub fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
pub fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
let context = self.action_context.as_ref();
if let Some(
ContextMenuItem::Entry(ContextMenuEntry {
@ -334,7 +337,7 @@ impl ContextMenu {
| ContextMenuItem::CustomEntry { handler, .. },
) = self.selected_index.and_then(|ix| self.items.get(ix))
{
(handler)(context, cx)
(handler)(context, window, cx)
}
if !self.keep_open_on_confirm {
@ -342,12 +345,12 @@ impl ContextMenu {
}
}
pub fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
pub fn cancel(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
cx.emit(DismissEvent);
cx.emit(DismissEvent);
}
fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
fn select_first(&mut self, _: &SelectFirst, _: &mut Window, cx: &mut Context<Self>) {
self.selected_index = self.items.iter().position(|item| item.is_selectable());
cx.notify();
}
@ -362,17 +365,17 @@ impl ContextMenu {
None
}
fn handle_select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
fn handle_select_last(&mut self, _: &SelectLast, _: &mut Window, cx: &mut Context<Self>) {
if self.select_last().is_some() {
cx.notify();
}
}
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
fn select_next(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
if let Some(ix) = self.selected_index {
let next_index = ix + 1;
if self.items.len() <= next_index {
self.select_first(&SelectFirst, cx);
self.select_first(&SelectFirst, window, cx);
} else {
for (ix, item) in self.items.iter().enumerate().skip(next_index) {
if item.is_selectable() {
@ -383,14 +386,14 @@ impl ContextMenu {
}
}
} else {
self.select_first(&SelectFirst, cx);
self.select_first(&SelectFirst, window, cx);
}
}
pub fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
pub fn select_prev(&mut self, _: &SelectPrev, window: &mut Window, cx: &mut Context<Self>) {
if let Some(ix) = self.selected_index {
if ix == 0 {
self.handle_select_last(&SelectLast, cx);
self.handle_select_last(&SelectLast, window, cx);
} else {
for (ix, item) in self.items.iter().enumerate().take(ix).rev() {
if item.is_selectable() {
@ -401,11 +404,16 @@ impl ContextMenu {
}
}
} else {
self.handle_select_last(&SelectLast, cx);
self.handle_select_last(&SelectLast, window, cx);
}
}
pub fn on_action_dispatch(&mut self, dispatched: &dyn Action, cx: &mut ViewContext<Self>) {
pub fn on_action_dispatch(
&mut self,
dispatched: &dyn Action,
window: &mut Window,
cx: &mut Context<Self>,
) {
if self.clicked {
cx.propagate();
return;
@ -427,13 +435,15 @@ impl ContextMenu {
self.delayed = true;
cx.notify();
let action = dispatched.boxed_clone();
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
cx.background_executor()
.timer(Duration::from_millis(50))
.await;
this.update(&mut cx, |this, cx| {
this.cancel(&menu::Cancel, cx);
cx.dispatch_action(action);
cx.update(|window, cx| {
this.update(cx, |this, cx| {
this.cancel(&menu::Cancel, window, cx);
window.dispatch_action(action, cx);
})
})
})
.detach_and_log_err(cx);
@ -461,7 +471,7 @@ impl ContextMenuItem {
}
impl Render for ContextMenu {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let ui_font_size = ThemeSettings::get_global(cx).ui_font_size;
WithRemSize::new(ui_font_size)
@ -473,11 +483,13 @@ impl Render for ContextMenu {
v_flex()
.id("context-menu")
.min_w(px(200.))
.max_h(vh(0.75, cx))
.max_h(vh(0.75, window))
.flex_1()
.overflow_y_scroll()
.track_focus(&self.focus_handle(cx))
.on_mouse_down_out(cx.listener(|this, _, cx| this.cancel(&menu::Cancel, cx)))
.on_mouse_down_out(
cx.listener(|this, _, window, cx| this.cancel(&menu::Cancel, window, cx)),
)
.key_context("menu")
.on_action(cx.listener(ContextMenu::select_first))
.on_action(cx.listener(ContextMenu::handle_select_last))
@ -527,7 +539,7 @@ impl Render for ContextMenu {
disabled,
}) => {
let handler = handler.clone();
let menu = cx.view().downgrade();
let menu = cx.model().downgrade();
let icon_color = if *disabled {
Color::Muted
} else {
@ -595,19 +607,21 @@ impl Render for ContextMenu {
.as_ref()
.map(|focus| {
KeyBinding::for_action_in(
&**action, focus, cx,
&**action, focus, window,
)
})
.unwrap_or_else(|| {
KeyBinding::for_action(&**action, cx)
KeyBinding::for_action(
&**action, window,
)
})
.map(|binding| div().ml_4().child(binding))
})),
)
.on_click({
let context = self.action_context.clone();
move |_, cx| {
handler(context.as_ref(), cx);
move |_, window, cx| {
handler(context.as_ref(), window, cx);
menu.update(cx, |menu, cx| {
menu.clicked = true;
cx.emit(DismissEvent);
@ -623,7 +637,7 @@ impl Render for ContextMenu {
selectable,
} => {
let handler = handler.clone();
let menu = cx.view().downgrade();
let menu = cx.model().downgrade();
let selectable = *selectable;
ListItem::new(ix)
.inset(true)
@ -636,8 +650,8 @@ impl Render for ContextMenu {
.when(selectable, |item| {
item.on_click({
let context = self.action_context.clone();
move |_, cx| {
handler(context.as_ref(), cx);
move |_, window, cx| {
handler(context.as_ref(), window, cx);
menu.update(cx, |menu, cx| {
menu.clicked = true;
cx.emit(DismissEvent);
@ -646,7 +660,7 @@ impl Render for ContextMenu {
}
})
})
.child(entry_render(cx))
.child(entry_render(window, cx))
.into_any_element()
}
}