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,8 +1,8 @@
use editor::{scroll::Autoscroll, styled_runs_for_code_label, Bias, Editor};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
rems, AppContext, DismissEvent, FontWeight, Model, ParentElement, StyledText, Task, View,
ViewContext, WeakView, WindowContext,
rems, App, Context, DismissEvent, Entity, FontWeight, ParentElement, StyledText, Task,
WeakEntity, Window,
};
use ordered_float::OrderedFloat;
use picker::{Picker, PickerDelegate};
@ -15,27 +15,29 @@ use workspace::{
Workspace,
};
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(
|workspace: &mut Workspace, _: &mut ViewContext<Workspace>| {
workspace.register_action(|workspace, _: &workspace::ToggleProjectSymbols, cx| {
let project = workspace.project().clone();
let handle = cx.view().downgrade();
workspace.toggle_modal(cx, move |cx| {
let delegate = ProjectSymbolsDelegate::new(handle, project);
Picker::uniform_list(delegate, cx).width(rems(34.))
})
});
pub fn init(cx: &mut App) {
cx.observe_new(
|workspace: &mut Workspace, _window, _: &mut Context<Workspace>| {
workspace.register_action(
|workspace, _: &workspace::ToggleProjectSymbols, window, cx| {
let project = workspace.project().clone();
let handle = cx.model().downgrade();
workspace.toggle_modal(window, cx, move |window, cx| {
let delegate = ProjectSymbolsDelegate::new(handle, project);
Picker::uniform_list(delegate, window, cx).width(rems(34.))
})
},
);
},
)
.detach();
}
pub type ProjectSymbols = View<Picker<ProjectSymbolsDelegate>>;
pub type ProjectSymbols = Entity<Picker<ProjectSymbolsDelegate>>;
pub struct ProjectSymbolsDelegate {
workspace: WeakView<Workspace>,
project: Model<Project>,
workspace: WeakEntity<Workspace>,
project: Entity<Project>,
selected_match_index: usize,
symbols: Vec<Symbol>,
visible_match_candidates: Vec<StringMatchCandidate>,
@ -45,7 +47,7 @@ pub struct ProjectSymbolsDelegate {
}
impl ProjectSymbolsDelegate {
fn new(workspace: WeakView<Workspace>, project: Model<Project>) -> Self {
fn new(workspace: WeakEntity<Workspace>, project: Entity<Project>) -> Self {
Self {
workspace,
project,
@ -58,7 +60,7 @@ impl ProjectSymbolsDelegate {
}
}
fn filter(&mut self, query: &str, cx: &mut ViewContext<Picker<Self>>) {
fn filter(&mut self, query: &str, window: &mut Window, cx: &mut Context<Picker<Self>>) {
const MAX_MATCHES: usize = 100;
let mut visible_matches = cx.background_executor().block(fuzzy::match_strings(
&self.visible_match_candidates,
@ -95,17 +97,17 @@ impl ProjectSymbolsDelegate {
}
self.matches = matches;
self.set_selected_index(0, cx);
self.set_selected_index(0, window, cx);
}
}
impl PickerDelegate for ProjectSymbolsDelegate {
type ListItem = ListItem;
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
"Search project symbols...".into()
}
fn confirm(&mut self, secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
fn confirm(&mut self, secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
if let Some(symbol) = self
.matches
.get(self.selected_match_index)
@ -116,23 +118,23 @@ impl PickerDelegate for ProjectSymbolsDelegate {
});
let symbol = symbol.clone();
let workspace = self.workspace.clone();
cx.spawn(|_, mut cx| async move {
cx.spawn_in(window, |_, mut cx| async move {
let buffer = buffer.await?;
workspace.update(&mut cx, |workspace, cx| {
workspace.update_in(&mut cx, |workspace, window, cx| {
let position = buffer
.read(cx)
.clip_point_utf16(symbol.range.start, Bias::Left);
let pane = if secondary {
workspace.adjacent_pane(cx)
workspace.adjacent_pane(window, cx)
} else {
workspace.active_pane().clone()
};
let editor =
workspace.open_project_item::<Editor>(pane, buffer, true, true, cx);
workspace.open_project_item::<Editor>(pane, buffer, true, true, window, cx);
editor.update(cx, |editor, cx| {
editor.change_selections(Some(Autoscroll::center()), cx, |s| {
editor.change_selections(Some(Autoscroll::center()), window, cx, |s| {
s.select_ranges([position..position])
});
});
@ -144,7 +146,7 @@ impl PickerDelegate for ProjectSymbolsDelegate {
}
}
fn dismissed(&mut self, _cx: &mut ViewContext<Picker<Self>>) {}
fn dismissed(&mut self, _window: &mut Window, _cx: &mut Context<Picker<Self>>) {}
fn match_count(&self) -> usize {
self.matches.len()
@ -154,20 +156,30 @@ impl PickerDelegate for ProjectSymbolsDelegate {
self.selected_match_index
}
fn set_selected_index(&mut self, ix: usize, _cx: &mut ViewContext<Picker<Self>>) {
fn set_selected_index(
&mut self,
ix: usize,
_window: &mut Window,
_cx: &mut Context<Picker<Self>>,
) {
self.selected_match_index = ix;
}
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
self.filter(&query, cx);
fn update_matches(
&mut self,
query: String,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Task<()> {
self.filter(&query, window, cx);
self.show_worktree_root_name = self.project.read(cx).visible_worktrees(cx).count() > 1;
let symbols = self
.project
.update(cx, |project, cx| project.symbols(&query, cx));
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
let symbols = symbols.await.log_err();
if let Some(symbols) = symbols {
this.update(&mut cx, |this, cx| {
this.update_in(&mut cx, |this, window, cx| {
let delegate = &mut this.delegate;
let project = delegate.project.read(cx);
let (visible_match_candidates, external_match_candidates) = symbols
@ -185,7 +197,7 @@ impl PickerDelegate for ProjectSymbolsDelegate {
delegate.visible_match_candidates = visible_match_candidates;
delegate.external_match_candidates = external_match_candidates;
delegate.symbols = symbols;
delegate.filter(&query, cx);
delegate.filter(&query, window, cx);
})
.log_err();
}
@ -196,7 +208,8 @@ impl PickerDelegate for ProjectSymbolsDelegate {
&self,
ix: usize,
selected: bool,
cx: &mut ViewContext<Picker<Self>>,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Option<Self::ListItem> {
let string_match = &self.matches[ix];
let symbol = &self.symbols[string_match.candidate_id];
@ -240,7 +253,7 @@ impl PickerDelegate for ProjectSymbolsDelegate {
.child(
LabelLike::new().child(
StyledText::new(label)
.with_highlights(&cx.text_style().clone(), highlights),
.with_highlights(&window.text_style().clone(), highlights),
),
)
.child(Label::new(path).color(Color::Muted)),
@ -333,12 +346,14 @@ mod tests {
},
);
let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
let (workspace, cx) =
cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
// Create the project symbols view.
let symbols = cx.new_view(|cx| {
let symbols = cx.new_window_model(|window, cx| {
Picker::uniform_list(
ProjectSymbolsDelegate::new(workspace.downgrade(), project.clone()),
window,
cx,
)
});
@ -346,10 +361,10 @@ mod tests {
// Spawn multiples updates before the first update completes,
// such that in the end, there are no matches. Testing for regression:
// https://github.com/zed-industries/zed/issues/861
symbols.update(cx, |p, cx| {
p.update_matches("o".to_string(), cx);
p.update_matches("on".to_string(), cx);
p.update_matches("onex".to_string(), cx);
symbols.update_in(cx, |p, window, cx| {
p.update_matches("o".to_string(), window, cx);
p.update_matches("on".to_string(), window, cx);
p.update_matches("onex".to_string(), window, cx);
});
cx.run_until_parked();
@ -358,9 +373,9 @@ mod tests {
});
// Spawn more updates such that in the end, there are matches.
symbols.update(cx, |p, cx| {
p.update_matches("one".to_string(), cx);
p.update_matches("on".to_string(), cx);
symbols.update_in(cx, |p, window, cx| {
p.update_matches("one".to_string(), window, cx);
p.update_matches("on".to_string(), window, cx);
});
cx.run_until_parked();
@ -372,9 +387,9 @@ mod tests {
});
// Spawn more updates such that in the end, there are again no matches.
symbols.update(cx, |p, cx| {
p.update_matches("o".to_string(), cx);
p.update_matches("".to_string(), cx);
symbols.update_in(cx, |p, window, cx| {
p.update_matches("o".to_string(), window, cx);
p.update_matches("".to_string(), window, cx);
});
cx.run_until_parked();