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

@ -8,9 +8,9 @@
//! If all of your elements are the same height, see [`UniformList`] for a simpler API
use crate::{
point, px, size, AnyElement, AvailableSpace, Bounds, ContentMask, DispatchPhase, Edges,
point, px, size, AnyElement, App, AvailableSpace, Bounds, ContentMask, DispatchPhase, Edges,
Element, FocusHandle, GlobalElementId, Hitbox, IntoElement, Pixels, Point, ScrollWheelEvent,
Size, Style, StyleRefinement, Styled, WindowContext,
Size, Style, StyleRefinement, Styled, Window,
};
use collections::VecDeque;
use refineable::Refineable as _;
@ -49,14 +49,14 @@ pub struct ListState(Rc<RefCell<StateInner>>);
struct StateInner {
last_layout_bounds: Option<Bounds<Pixels>>,
last_padding: Option<Edges<Pixels>>,
render_item: Box<dyn FnMut(usize, &mut WindowContext) -> AnyElement>,
render_item: Box<dyn FnMut(usize, &mut Window, &mut App) -> AnyElement>,
items: SumTree<ListItem>,
logical_scroll_top: Option<ListOffset>,
alignment: ListAlignment,
overdraw: Pixels,
reset: bool,
#[allow(clippy::type_complexity)]
scroll_handler: Option<Box<dyn FnMut(&ListScrollEvent, &mut WindowContext)>>,
scroll_handler: Option<Box<dyn FnMut(&ListScrollEvent, &mut Window, &mut App)>>,
}
/// Whether the list is scrolling from top to bottom or bottom to top.
@ -146,12 +146,12 @@ impl ListItem {
}
}
fn contains_focused(&self, cx: &WindowContext) -> bool {
fn contains_focused(&self, window: &Window, cx: &App) -> bool {
match self {
ListItem::Unmeasured { focus_handle } | ListItem::Measured { focus_handle, .. } => {
focus_handle
.as_ref()
.is_some_and(|handle| handle.contains_focused(cx))
.is_some_and(|handle| handle.contains_focused(window, cx))
}
}
}
@ -186,7 +186,7 @@ impl ListState {
render_item: R,
) -> Self
where
R: 'static + FnMut(usize, &mut WindowContext) -> AnyElement,
R: 'static + FnMut(usize, &mut Window, &mut App) -> AnyElement,
{
let this = Self(Rc::new(RefCell::new(StateInner {
last_layout_bounds: None,
@ -272,7 +272,7 @@ impl ListState {
/// Set a handler that will be called when the list is scrolled.
pub fn set_scroll_handler(
&self,
handler: impl FnMut(&ListScrollEvent, &mut WindowContext) + 'static,
handler: impl FnMut(&ListScrollEvent, &mut Window, &mut App) + 'static,
) {
self.0.borrow_mut().scroll_handler = Some(Box::new(handler))
}
@ -371,7 +371,8 @@ impl StateInner {
scroll_top: &ListOffset,
height: Pixels,
delta: Point<Pixels>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) {
// Drop scroll events after a reset, since we can't calculate
// the new logical scroll top without the item heights
@ -407,11 +408,12 @@ impl StateInner {
count: self.items.summary().count,
is_scrolled: self.logical_scroll_top.is_some(),
},
window,
cx,
);
}
cx.refresh();
window.refresh();
}
fn logical_scroll_top(&self) -> ListOffset {
@ -439,7 +441,8 @@ impl StateInner {
available_width: Option<Pixels>,
available_height: Pixels,
padding: &Edges<Pixels>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> LayoutItemsResponse {
let old_items = self.items.clone();
let mut measured_items = VecDeque::new();
@ -472,8 +475,8 @@ impl StateInner {
// If we're within the visible area or the height wasn't cached, render and measure the item's element
if visible_height < available_height || size.is_none() {
let item_index = scroll_top.item_ix + ix;
let mut element = (self.render_item)(item_index, cx);
let element_size = element.layout_as_root(available_item_space, cx);
let mut element = (self.render_item)(item_index, window, cx);
let element_size = element.layout_as_root(available_item_space, window, cx);
size = Some(element_size);
if visible_height < available_height {
item_layouts.push_back(ItemLayout {
@ -481,7 +484,7 @@ impl StateInner {
element,
size: element_size,
});
if item.contains_focused(cx) {
if item.contains_focused(window, cx) {
rendered_focused_item = true;
}
}
@ -507,8 +510,8 @@ impl StateInner {
cursor.prev(&());
if let Some(item) = cursor.item() {
let item_index = cursor.start().0;
let mut element = (self.render_item)(item_index, cx);
let element_size = element.layout_as_root(available_item_space, cx);
let mut element = (self.render_item)(item_index, window, cx);
let element_size = element.layout_as_root(available_item_space, window, cx);
let focus_handle = item.focus_handle();
rendered_height += element_size.height;
measured_items.push_front(ListItem::Measured {
@ -520,7 +523,7 @@ impl StateInner {
element,
size: element_size,
});
if item.contains_focused(cx) {
if item.contains_focused(window, cx) {
rendered_focused_item = true;
}
} else {
@ -556,8 +559,8 @@ impl StateInner {
let size = if let ListItem::Measured { size, .. } = item {
*size
} else {
let mut element = (self.render_item)(cursor.start().0, cx);
element.layout_as_root(available_item_space, cx)
let mut element = (self.render_item)(cursor.start().0, window, cx);
element.layout_as_root(available_item_space, window, cx)
};
leading_overdraw += size.height;
@ -587,10 +590,10 @@ impl StateInner {
.filter::<_, Count>(&(), |summary| summary.has_focus_handles);
cursor.next(&());
while let Some(item) = cursor.item() {
if item.contains_focused(cx) {
if item.contains_focused(window, cx) {
let item_index = cursor.start().0;
let mut element = (self.render_item)(cursor.start().0, cx);
let size = element.layout_as_root(available_item_space, cx);
let mut element = (self.render_item)(cursor.start().0, window, cx);
let size = element.layout_as_root(available_item_space, window, cx);
item_layouts.push_back(ItemLayout {
index: item_index,
element,
@ -614,25 +617,31 @@ impl StateInner {
bounds: Bounds<Pixels>,
padding: Edges<Pixels>,
autoscroll: bool,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> Result<LayoutItemsResponse, ListOffset> {
cx.transact(|cx| {
let mut layout_response =
self.layout_items(Some(bounds.size.width), bounds.size.height, &padding, cx);
window.transact(|window| {
let mut layout_response = self.layout_items(
Some(bounds.size.width),
bounds.size.height,
&padding,
window,
cx,
);
// Avoid honoring autoscroll requests from elements other than our children.
cx.take_autoscroll();
window.take_autoscroll();
// Only paint the visible items, if there is actually any space for them (taking padding into account)
if bounds.size.height > padding.top + padding.bottom {
let mut item_origin = bounds.origin + Point::new(px(0.), padding.top);
item_origin.y -= layout_response.scroll_top.offset_in_item;
for item in &mut layout_response.item_layouts {
cx.with_content_mask(Some(ContentMask { bounds }), |cx| {
item.element.prepaint_at(item_origin, cx);
window.with_content_mask(Some(ContentMask { bounds }), |window| {
item.element.prepaint_at(item_origin, window, cx);
});
if let Some(autoscroll_bounds) = cx.take_autoscroll() {
if let Some(autoscroll_bounds) = window.take_autoscroll() {
if autoscroll {
if autoscroll_bounds.top() < bounds.top() {
return Err(ListOffset {
@ -653,12 +662,13 @@ impl StateInner {
let Some(item) = cursor.item() else { break };
let size = item.size().unwrap_or_else(|| {
let mut item = (self.render_item)(cursor.start().0, cx);
let mut item =
(self.render_item)(cursor.start().0, window, cx);
let item_available_size = size(
bounds.size.width.into(),
AvailableSpace::MinContent,
);
item.layout_as_root(item_available_size, cx)
item.layout_as_root(item_available_size, window, cx)
});
height -= size.height;
}
@ -716,14 +726,15 @@ impl Element for List {
fn request_layout(
&mut self,
_id: Option<&GlobalElementId>,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> (crate::LayoutId, Self::RequestLayoutState) {
let layout_id = match self.sizing_behavior {
ListSizingBehavior::Infer => {
let mut style = Style::default();
style.overflow.y = Overflow::Scroll;
style.refine(&self.style);
cx.with_text_style(style.text_style().cloned(), |cx| {
window.with_text_style(style.text_style().cloned(), |window| {
let state = &mut *self.state.0.borrow_mut();
let available_height = if let Some(last_bounds) = state.last_layout_bounds {
@ -735,18 +746,19 @@ impl Element for List {
};
let padding = style.padding.to_pixels(
state.last_layout_bounds.unwrap_or_default().size.into(),
cx.rem_size(),
window.rem_size(),
);
let layout_response = state.layout_items(None, available_height, &padding, cx);
let layout_response =
state.layout_items(None, available_height, &padding, window, cx);
let max_element_width = layout_response.max_item_width;
let summary = state.items.summary();
let total_height = summary.height;
cx.request_measured_layout(
window.request_measured_layout(
style,
move |known_dimensions, available_space, _cx| {
move |known_dimensions, available_space, _window, _cx| {
let width =
known_dimensions
.width
@ -770,8 +782,8 @@ impl Element for List {
ListSizingBehavior::Auto => {
let mut style = Style::default();
style.refine(&self.style);
cx.with_text_style(style.text_style().cloned(), |cx| {
cx.request_layout(style, None)
window.with_text_style(style.text_style().cloned(), |window| {
window.request_layout(style, None, cx)
})
}
};
@ -783,7 +795,8 @@ impl Element for List {
_id: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) -> ListPrepaintState {
let state = &mut *self.state.0.borrow_mut();
state.reset = false;
@ -791,7 +804,7 @@ impl Element for List {
let mut style = Style::default();
style.refine(&self.style);
let hitbox = cx.insert_hitbox(bounds, false);
let hitbox = window.insert_hitbox(bounds, false);
// If the width of the list has changed, invalidate all cached item heights
if state.last_layout_bounds.map_or(true, |last_bounds| {
@ -807,12 +820,16 @@ impl Element for List {
state.items = new_items;
}
let padding = style.padding.to_pixels(bounds.size.into(), cx.rem_size());
let layout = match state.prepaint_items(bounds, padding, true, cx) {
let padding = style
.padding
.to_pixels(bounds.size.into(), window.rem_size());
let layout = match state.prepaint_items(bounds, padding, true, window, cx) {
Ok(layout) => layout,
Err(autoscroll_request) => {
state.logical_scroll_top = Some(autoscroll_request);
state.prepaint_items(bounds, padding, false, cx).unwrap()
state
.prepaint_items(bounds, padding, false, window, cx)
.unwrap()
}
};
@ -827,11 +844,12 @@ impl Element for List {
bounds: Bounds<crate::Pixels>,
_: &mut Self::RequestLayoutState,
prepaint: &mut Self::PrepaintState,
cx: &mut WindowContext,
window: &mut Window,
cx: &mut App,
) {
cx.with_content_mask(Some(ContentMask { bounds }), |cx| {
window.with_content_mask(Some(ContentMask { bounds }), |window| {
for item in &mut prepaint.layout.item_layouts {
item.element.paint(cx);
item.element.paint(window, cx);
}
});
@ -839,12 +857,13 @@ impl Element for List {
let height = bounds.size.height;
let scroll_top = prepaint.layout.scroll_top;
let hitbox_id = prepaint.hitbox.id;
cx.on_mouse_event(move |event: &ScrollWheelEvent, phase, cx| {
if phase == DispatchPhase::Bubble && hitbox_id.is_hovered(cx) {
window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| {
if phase == DispatchPhase::Bubble && hitbox_id.is_hovered(window) {
list_state.0.borrow_mut().scroll(
&scroll_top,
height,
event.delta.pixel_delta(px(20.)),
window,
cx,
)
}
@ -952,7 +971,7 @@ mod test {
let cx = cx.add_empty_window();
let state = ListState::new(5, crate::ListAlignment::Top, px(10.), |_, _| {
let state = ListState::new(5, crate::ListAlignment::Top, px(10.), |_, _, _| {
div().h(px(10.)).w_full().into_any()
});
@ -963,7 +982,7 @@ mod test {
});
// Paint
cx.draw(point(px(0.), px(0.)), size(px(100.), px(20.)), |_| {
cx.draw(point(px(0.), px(0.)), size(px(100.), px(20.)), |_, _| {
list(state.clone()).w_full().h_full()
});