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:
parent
21b4a0d50e
commit
6fca1d2b0b
648 changed files with 36248 additions and 28208 deletions
|
@ -2,7 +2,7 @@
|
|||
use std::{cmp::Ordering, ops::Range, rc::Rc};
|
||||
|
||||
use gpui::{
|
||||
fill, point, size, AnyElement, AppContext, Bounds, Hsla, Point, UniformListDecoration, View,
|
||||
fill, point, size, AnyElement, App, Bounds, Entity, Hsla, Point, UniformListDecoration,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
|
@ -21,7 +21,7 @@ pub struct IndentGuideColors {
|
|||
|
||||
impl IndentGuideColors {
|
||||
/// Returns the indent guide colors that should be used for panels.
|
||||
pub fn panel(cx: &AppContext) -> Self {
|
||||
pub fn panel(cx: &App) -> Self {
|
||||
Self {
|
||||
default: cx.theme().colors().panel_indent_guide,
|
||||
hover: cx.theme().colors().panel_indent_guide_hover,
|
||||
|
@ -33,27 +33,28 @@ impl IndentGuideColors {
|
|||
pub struct IndentGuides {
|
||||
colors: IndentGuideColors,
|
||||
indent_size: Pixels,
|
||||
compute_indents_fn: Box<dyn Fn(Range<usize>, &mut WindowContext) -> SmallVec<[usize; 64]>>,
|
||||
compute_indents_fn: Box<dyn Fn(Range<usize>, &mut Window, &mut App) -> SmallVec<[usize; 64]>>,
|
||||
render_fn: Option<
|
||||
Box<
|
||||
dyn Fn(
|
||||
RenderIndentGuideParams,
|
||||
&mut WindowContext,
|
||||
&mut Window,
|
||||
&mut App,
|
||||
) -> SmallVec<[RenderedIndentGuide; 12]>,
|
||||
>,
|
||||
>,
|
||||
on_click: Option<Rc<dyn Fn(&IndentGuideLayout, &mut WindowContext)>>,
|
||||
on_click: Option<Rc<dyn Fn(&IndentGuideLayout, &mut Window, &mut App)>>,
|
||||
}
|
||||
|
||||
pub fn indent_guides<V: Render>(
|
||||
view: View<V>,
|
||||
model: Entity<V>,
|
||||
indent_size: Pixels,
|
||||
colors: IndentGuideColors,
|
||||
compute_indents_fn: impl Fn(&mut V, Range<usize>, &mut ViewContext<V>) -> SmallVec<[usize; 64]>
|
||||
compute_indents_fn: impl Fn(&mut V, Range<usize>, &mut Window, &mut Context<V>) -> SmallVec<[usize; 64]>
|
||||
+ 'static,
|
||||
) -> IndentGuides {
|
||||
let compute_indents_fn = Box::new(move |range, cx: &mut WindowContext| {
|
||||
view.update(cx, |this, cx| compute_indents_fn(this, range, cx))
|
||||
let compute_indents_fn = Box::new(move |range, window: &mut Window, cx: &mut App| {
|
||||
model.update(cx, |this, cx| compute_indents_fn(this, range, window, cx))
|
||||
});
|
||||
IndentGuides {
|
||||
colors,
|
||||
|
@ -68,7 +69,7 @@ impl IndentGuides {
|
|||
/// Sets the callback that will be called when the user clicks on an indent guide.
|
||||
pub fn on_click(
|
||||
mut self,
|
||||
on_click: impl Fn(&IndentGuideLayout, &mut WindowContext) + 'static,
|
||||
on_click: impl Fn(&IndentGuideLayout, &mut Window, &mut App) + 'static,
|
||||
) -> Self {
|
||||
self.on_click = Some(Rc::new(on_click));
|
||||
self
|
||||
|
@ -77,16 +78,17 @@ impl IndentGuides {
|
|||
/// Sets a custom callback that will be called when the indent guides need to be rendered.
|
||||
pub fn with_render_fn<V: Render>(
|
||||
mut self,
|
||||
view: View<V>,
|
||||
model: Entity<V>,
|
||||
render_fn: impl Fn(
|
||||
&mut V,
|
||||
RenderIndentGuideParams,
|
||||
&mut WindowContext,
|
||||
&mut Window,
|
||||
&mut App,
|
||||
) -> SmallVec<[RenderedIndentGuide; 12]>
|
||||
+ 'static,
|
||||
) -> Self {
|
||||
let render_fn = move |params, cx: &mut WindowContext| {
|
||||
view.update(cx, |this, cx| render_fn(this, params, cx))
|
||||
let render_fn = move |params, window: &mut Window, cx: &mut App| {
|
||||
model.update(cx, |this, cx| render_fn(this, params, window, cx))
|
||||
};
|
||||
self.render_fn = Some(Box::new(render_fn));
|
||||
self
|
||||
|
@ -141,7 +143,8 @@ mod uniform_list {
|
|||
bounds: Bounds<Pixels>,
|
||||
item_height: Pixels,
|
||||
item_count: usize,
|
||||
cx: &mut WindowContext,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> AnyElement {
|
||||
let mut visible_range = visible_range.clone();
|
||||
let includes_trailing_indent = visible_range.end < item_count;
|
||||
|
@ -151,7 +154,7 @@ mod uniform_list {
|
|||
if includes_trailing_indent {
|
||||
visible_range.end += 1;
|
||||
}
|
||||
let visible_entries = &(self.compute_indents_fn)(visible_range.clone(), cx);
|
||||
let visible_entries = &(self.compute_indents_fn)(visible_range.clone(), window, cx);
|
||||
let indent_guides = compute_indent_guides(
|
||||
&visible_entries,
|
||||
visible_range.start,
|
||||
|
@ -163,7 +166,7 @@ mod uniform_list {
|
|||
indent_size: self.indent_size,
|
||||
item_height,
|
||||
};
|
||||
custom_render(params, cx)
|
||||
custom_render(params, window, cx)
|
||||
} else {
|
||||
indent_guides
|
||||
.into_iter()
|
||||
|
@ -200,14 +203,15 @@ mod uniform_list {
|
|||
struct IndentGuidesElement {
|
||||
colors: IndentGuideColors,
|
||||
indent_guides: Rc<SmallVec<[RenderedIndentGuide; 12]>>,
|
||||
on_hovered_indent_guide_click: Option<Rc<dyn Fn(&IndentGuideLayout, &mut WindowContext)>>,
|
||||
on_hovered_indent_guide_click:
|
||||
Option<Rc<dyn Fn(&IndentGuideLayout, &mut Window, &mut App)>>,
|
||||
}
|
||||
|
||||
enum IndentGuidesElementPrepaintState {
|
||||
Static,
|
||||
Interactive {
|
||||
hitboxes: Rc<SmallVec<[Hitbox; 12]>>,
|
||||
on_hovered_indent_guide_click: Rc<dyn Fn(&IndentGuideLayout, &mut WindowContext)>,
|
||||
on_hovered_indent_guide_click: Rc<dyn Fn(&IndentGuideLayout, &mut Window, &mut App)>,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -222,9 +226,10 @@ mod uniform_list {
|
|||
fn request_layout(
|
||||
&mut self,
|
||||
_id: Option<&gpui::GlobalElementId>,
|
||||
cx: &mut WindowContext,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> (gpui::LayoutId, Self::RequestLayoutState) {
|
||||
(cx.request_layout(gpui::Style::default(), []), ())
|
||||
(window.request_layout(gpui::Style::default(), [], cx), ())
|
||||
}
|
||||
|
||||
fn prepaint(
|
||||
|
@ -232,7 +237,8 @@ mod uniform_list {
|
|||
_id: Option<&gpui::GlobalElementId>,
|
||||
_bounds: Bounds<Pixels>,
|
||||
_request_layout: &mut Self::RequestLayoutState,
|
||||
cx: &mut WindowContext,
|
||||
window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Self::PrepaintState {
|
||||
if let Some(on_hovered_indent_guide_click) = self.on_hovered_indent_guide_click.clone()
|
||||
{
|
||||
|
@ -240,7 +246,7 @@ mod uniform_list {
|
|||
.indent_guides
|
||||
.as_ref()
|
||||
.iter()
|
||||
.map(|guide| cx.insert_hitbox(guide.hitbox.unwrap_or(guide.bounds), false))
|
||||
.map(|guide| window.insert_hitbox(guide.hitbox.unwrap_or(guide.bounds), false))
|
||||
.collect();
|
||||
Self::PrepaintState::Interactive {
|
||||
hitboxes: Rc::new(hitboxes),
|
||||
|
@ -257,7 +263,8 @@ mod uniform_list {
|
|||
_bounds: Bounds<Pixels>,
|
||||
_request_layout: &mut Self::RequestLayoutState,
|
||||
prepaint: &mut Self::PrepaintState,
|
||||
cx: &mut WindowContext,
|
||||
window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) {
|
||||
match prepaint {
|
||||
IndentGuidesElementPrepaintState::Static => {
|
||||
|
@ -268,22 +275,22 @@ mod uniform_list {
|
|||
self.colors.default
|
||||
};
|
||||
|
||||
cx.paint_quad(fill(indent_guide.bounds, fill_color));
|
||||
window.paint_quad(fill(indent_guide.bounds, fill_color));
|
||||
}
|
||||
}
|
||||
IndentGuidesElementPrepaintState::Interactive {
|
||||
hitboxes,
|
||||
on_hovered_indent_guide_click,
|
||||
} => {
|
||||
cx.on_mouse_event({
|
||||
window.on_mouse_event({
|
||||
let hitboxes = hitboxes.clone();
|
||||
let indent_guides = self.indent_guides.clone();
|
||||
let on_hovered_indent_guide_click = on_hovered_indent_guide_click.clone();
|
||||
move |event: &MouseDownEvent, phase, cx| {
|
||||
move |event: &MouseDownEvent, phase, window, cx| {
|
||||
if phase == DispatchPhase::Bubble && event.button == MouseButton::Left {
|
||||
let mut active_hitbox_ix = None;
|
||||
for (i, hitbox) in hitboxes.iter().enumerate() {
|
||||
if hitbox.is_hovered(cx) {
|
||||
if hitbox.is_hovered(window) {
|
||||
active_hitbox_ix = Some(i);
|
||||
break;
|
||||
}
|
||||
|
@ -294,18 +301,18 @@ mod uniform_list {
|
|||
};
|
||||
|
||||
let active_indent_guide = &indent_guides[active_hitbox_ix].layout;
|
||||
on_hovered_indent_guide_click(active_indent_guide, cx);
|
||||
on_hovered_indent_guide_click(active_indent_guide, window, cx);
|
||||
|
||||
cx.stop_propagation();
|
||||
cx.prevent_default();
|
||||
window.prevent_default();
|
||||
}
|
||||
}
|
||||
});
|
||||
let mut hovered_hitbox_id = None;
|
||||
for (i, hitbox) in hitboxes.iter().enumerate() {
|
||||
cx.set_cursor_style(gpui::CursorStyle::PointingHand, hitbox);
|
||||
window.set_cursor_style(gpui::CursorStyle::PointingHand, hitbox);
|
||||
let indent_guide = &self.indent_guides[i];
|
||||
let fill_color = if hitbox.is_hovered(cx) {
|
||||
let fill_color = if hitbox.is_hovered(window) {
|
||||
hovered_hitbox_id = Some(hitbox.id);
|
||||
self.colors.hover
|
||||
} else if indent_guide.is_active {
|
||||
|
@ -314,16 +321,16 @@ mod uniform_list {
|
|||
self.colors.default
|
||||
};
|
||||
|
||||
cx.paint_quad(fill(indent_guide.bounds, fill_color));
|
||||
window.paint_quad(fill(indent_guide.bounds, fill_color));
|
||||
}
|
||||
|
||||
cx.on_mouse_event({
|
||||
window.on_mouse_event({
|
||||
let prev_hovered_hitbox_id = hovered_hitbox_id;
|
||||
let hitboxes = hitboxes.clone();
|
||||
move |_: &MouseMoveEvent, phase, cx| {
|
||||
move |_: &MouseMoveEvent, phase, window, _cx| {
|
||||
let mut hovered_hitbox_id = None;
|
||||
for hitbox in hitboxes.as_ref() {
|
||||
if hitbox.is_hovered(cx) {
|
||||
if hitbox.is_hovered(window) {
|
||||
hovered_hitbox_id = Some(hitbox.id);
|
||||
break;
|
||||
}
|
||||
|
@ -333,14 +340,14 @@ mod uniform_list {
|
|||
match (prev_hovered_hitbox_id, hovered_hitbox_id) {
|
||||
(Some(prev_id), Some(id)) => {
|
||||
if prev_id != id {
|
||||
cx.refresh();
|
||||
window.refresh();
|
||||
}
|
||||
}
|
||||
(None, Some(_)) => {
|
||||
cx.refresh();
|
||||
window.refresh();
|
||||
}
|
||||
(Some(_), None) => {
|
||||
cx.refresh();
|
||||
window.refresh();
|
||||
}
|
||||
(None, None) => {}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue