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,9 +4,9 @@ mod tab_switcher_tests;
use collections::HashMap;
use editor::items::entry_git_aware_label_color;
use gpui::{
actions, impl_actions, rems, Action, AnyElement, AppContext, DismissEvent, EntityId,
EventEmitter, FocusHandle, FocusableView, Model, Modifiers, ModifiersChangedEvent, MouseButton,
MouseUpEvent, ParentElement, Render, Styled, Task, View, ViewContext, VisualContext, WeakView,
actions, impl_actions, rems, Action, AnyElement, App, Context, DismissEvent, Entity, EntityId,
EventEmitter, FocusHandle, Focusable, Modifiers, ModifiersChangedEvent, MouseButton,
MouseUpEvent, ParentElement, Render, Styled, Task, WeakEntity, Window,
};
use picker::{Picker, PickerDelegate};
use project::Project;
@ -34,33 +34,42 @@ impl_actions!(tab_switcher, [Toggle]);
actions!(tab_switcher, [CloseSelectedItem]);
pub struct TabSwitcher {
picker: View<Picker<TabSwitcherDelegate>>,
picker: Entity<Picker<TabSwitcherDelegate>>,
init_modifiers: Option<Modifiers>,
}
impl ModalView for TabSwitcher {}
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(TabSwitcher::register).detach();
pub fn init(cx: &mut App) {
cx.observe_new(TabSwitcher::register).detach();
}
impl TabSwitcher {
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(|workspace, action: &Toggle, cx| {
fn register(
workspace: &mut Workspace,
_window: Option<&mut Window>,
_: &mut Context<Workspace>,
) {
workspace.register_action(|workspace, action: &Toggle, window, cx| {
let Some(tab_switcher) = workspace.active_modal::<Self>(cx) else {
Self::open(action, workspace, cx);
Self::open(action, workspace, window, cx);
return;
};
tab_switcher.update(cx, |tab_switcher, cx| {
tab_switcher
.picker
.update(cx, |picker, cx| picker.cycle_selection(cx))
.update(cx, |picker, cx| picker.cycle_selection(window, cx))
});
});
}
fn open(action: &Toggle, workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
fn open(
action: &Toggle,
workspace: &mut Workspace,
window: &mut Window,
cx: &mut Context<Workspace>,
) {
let mut weak_pane = workspace.active_pane().downgrade();
for dock in [
workspace.left_dock(),
@ -70,7 +79,7 @@ impl TabSwitcher {
dock.update(cx, |this, cx| {
let Some(panel) = this
.active_panel()
.filter(|panel| panel.focus_handle(cx).contains_focused(cx))
.filter(|panel| panel.panel_focus_handle(cx).contains_focused(window, cx))
else {
return;
};
@ -81,24 +90,31 @@ impl TabSwitcher {
}
let project = workspace.project().clone();
workspace.toggle_modal(cx, |cx| {
let delegate =
TabSwitcherDelegate::new(project, action, cx.view().downgrade(), weak_pane, cx);
TabSwitcher::new(delegate, cx)
workspace.toggle_modal(window, cx, |window, cx| {
let delegate = TabSwitcherDelegate::new(
project,
action,
cx.model().downgrade(),
weak_pane,
window,
cx,
);
TabSwitcher::new(delegate, window, cx)
});
}
fn new(delegate: TabSwitcherDelegate, cx: &mut ViewContext<Self>) -> Self {
fn new(delegate: TabSwitcherDelegate, window: &mut Window, cx: &mut Context<Self>) -> Self {
Self {
picker: cx.new_view(|cx| Picker::nonsearchable_uniform_list(delegate, cx)),
init_modifiers: cx.modifiers().modified().then_some(cx.modifiers()),
picker: cx.new(|cx| Picker::nonsearchable_uniform_list(delegate, window, cx)),
init_modifiers: window.modifiers().modified().then_some(window.modifiers()),
}
}
fn handle_modifiers_changed(
&mut self,
event: &ModifiersChangedEvent,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(init_modifiers) = self.init_modifiers else {
return;
@ -108,30 +124,35 @@ impl TabSwitcher {
if self.picker.read(cx).delegate.matches.is_empty() {
cx.emit(DismissEvent)
} else {
cx.dispatch_action(menu::Confirm.boxed_clone());
window.dispatch_action(menu::Confirm.boxed_clone(), cx);
}
}
}
fn handle_close_selected_item(&mut self, _: &CloseSelectedItem, cx: &mut ViewContext<Self>) {
fn handle_close_selected_item(
&mut self,
_: &CloseSelectedItem,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.picker.update(cx, |picker, cx| {
picker
.delegate
.close_item_at(picker.delegate.selected_index(), cx)
.close_item_at(picker.delegate.selected_index(), window, cx)
});
}
}
impl EventEmitter<DismissEvent> for TabSwitcher {}
impl FocusableView for TabSwitcher {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
impl Focusable for TabSwitcher {
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
impl Render for TabSwitcher {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.key_context("TabSwitcher")
.w(rems(PANEL_WIDTH_REMS))
@ -150,22 +171,23 @@ struct TabMatch {
pub struct TabSwitcherDelegate {
select_last: bool,
tab_switcher: WeakView<TabSwitcher>,
tab_switcher: WeakEntity<TabSwitcher>,
selected_index: usize,
pane: WeakView<Pane>,
project: Model<Project>,
pane: WeakEntity<Pane>,
project: Entity<Project>,
matches: Vec<TabMatch>,
}
impl TabSwitcherDelegate {
fn new(
project: Model<Project>,
project: Entity<Project>,
action: &Toggle,
tab_switcher: WeakView<TabSwitcher>,
pane: WeakView<Pane>,
cx: &mut ViewContext<TabSwitcher>,
tab_switcher: WeakEntity<TabSwitcher>,
pane: WeakEntity<Pane>,
window: &mut Window,
cx: &mut Context<TabSwitcher>,
) -> Self {
Self::subscribe_to_updates(&pane, cx);
Self::subscribe_to_updates(&pane, window, cx);
Self {
select_last: action.select_last,
tab_switcher,
@ -176,16 +198,20 @@ impl TabSwitcherDelegate {
}
}
fn subscribe_to_updates(pane: &WeakView<Pane>, cx: &mut ViewContext<TabSwitcher>) {
fn subscribe_to_updates(
pane: &WeakEntity<Pane>,
window: &mut Window,
cx: &mut Context<TabSwitcher>,
) {
let Some(pane) = pane.upgrade() else {
return;
};
cx.subscribe(&pane, |tab_switcher, _, event, cx| {
cx.subscribe_in(&pane, window, |tab_switcher, _, event, window, cx| {
match event {
PaneEvent::AddItem { .. }
| PaneEvent::RemovedItem { .. }
| PaneEvent::Remove { .. } => tab_switcher.picker.update(cx, |picker, cx| {
picker.delegate.update_matches(cx);
picker.delegate.update_matches(window, cx);
cx.notify();
}),
_ => {}
@ -194,7 +220,7 @@ impl TabSwitcherDelegate {
.detach();
}
fn update_matches(&mut self, cx: &mut WindowContext) {
fn update_matches(&mut self, _window: &mut Window, cx: &mut App) {
let selected_item_id = self.selected_item_id();
self.matches.clear();
let Some(pane) = self.pane.upgrade() else {
@ -272,7 +298,12 @@ impl TabSwitcherDelegate {
0
}
fn close_item_at(&mut self, ix: usize, cx: &mut ViewContext<Picker<TabSwitcherDelegate>>) {
fn close_item_at(
&mut self,
ix: usize,
window: &mut Window,
cx: &mut Context<Picker<TabSwitcherDelegate>>,
) {
let Some(tab_match) = self.matches.get(ix) else {
return;
};
@ -280,7 +311,7 @@ impl TabSwitcherDelegate {
return;
};
pane.update(cx, |pane, cx| {
pane.close_item_by_id(tab_match.item.item_id(), SaveIntent::Close, cx)
pane.close_item_by_id(tab_match.item.item_id(), SaveIntent::Close, window, cx)
.detach_and_log_err(cx);
});
}
@ -289,11 +320,11 @@ impl TabSwitcherDelegate {
impl PickerDelegate for TabSwitcherDelegate {
type ListItem = ListItem;
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
Arc::default()
}
fn no_matches_text(&self, _cx: &mut WindowContext) -> SharedString {
fn no_matches_text(&self, _window: &mut Window, _cx: &mut App) -> SharedString {
"No tabs".into()
}
@ -305,7 +336,7 @@ impl PickerDelegate for TabSwitcherDelegate {
self.selected_index
}
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>) {
fn set_selected_index(&mut self, ix: usize, _: &mut Window, cx: &mut Context<Picker<Self>>) {
self.selected_index = ix;
cx.notify();
}
@ -317,13 +348,19 @@ impl PickerDelegate for TabSwitcherDelegate {
fn update_matches(
&mut self,
_raw_query: String,
cx: &mut ViewContext<Picker<Self>>,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Task<()> {
self.update_matches(cx);
self.update_matches(window, cx);
Task::ready(())
}
fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<TabSwitcherDelegate>>) {
fn confirm(
&mut self,
_secondary: bool,
window: &mut Window,
cx: &mut Context<Picker<TabSwitcherDelegate>>,
) {
let Some(pane) = self.pane.upgrade() else {
return;
};
@ -331,11 +368,11 @@ impl PickerDelegate for TabSwitcherDelegate {
return;
};
pane.update(cx, |pane, cx| {
pane.activate_item(selected_match.item_index, true, true, cx);
pane.activate_item(selected_match.item_index, true, true, window, cx);
});
}
fn dismissed(&mut self, cx: &mut ViewContext<Picker<TabSwitcherDelegate>>) {
fn dismissed(&mut self, _: &mut Window, cx: &mut Context<Picker<TabSwitcherDelegate>>) {
self.tab_switcher
.update(cx, |_, cx| cx.emit(DismissEvent))
.log_err();
@ -345,7 +382,8 @@ impl PickerDelegate for TabSwitcherDelegate {
&self,
ix: usize,
selected: bool,
cx: &mut ViewContext<Picker<Self>>,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Option<Self::ListItem> {
let tab_match = self
.matches
@ -357,9 +395,9 @@ impl PickerDelegate for TabSwitcherDelegate {
selected: true,
preview: tab_match.preview,
};
let label = tab_match.item.tab_content(params, cx);
let label = tab_match.item.tab_content(params, window, cx);
let icon = tab_match.item.tab_icon(cx).map(|icon| {
let icon = tab_match.item.tab_icon(window, cx).map(|icon| {
let git_status_color = ItemSettings::get_global(cx)
.git_status
.then(|| {
@ -403,16 +441,16 @@ impl PickerDelegate for TabSwitcherDelegate {
// See the same handler in Picker for more details.
.on_mouse_up(
MouseButton::Right,
cx.listener(move |picker, _: &MouseUpEvent, cx| {
cx.listener(move |picker, _: &MouseUpEvent, window, cx| {
cx.stop_propagation();
picker.delegate.close_item_at(ix, cx);
picker.delegate.close_item_at(ix, window, cx);
}),
)
.child(
IconButton::new("close_tab", IconName::Close)
.icon_size(IconSize::Small)
.icon_color(indicator_color)
.tooltip(|cx| Tooltip::text("Close", cx)),
.tooltip(Tooltip::text("Close")),
)
.into_any_element();

View file

@ -35,7 +35,8 @@ async fn test_open_with_prev_tab_selected_and_cycle_on_toggle_action(
.await;
let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
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));
let tab_1 = open_buffer("1.txt", &workspace, cx).await;
let tab_2 = open_buffer("2.txt", &workspace, cx).await;
@ -90,7 +91,8 @@ async fn test_open_with_last_tab_selected(cx: &mut gpui::TestAppContext) {
.await;
let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
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));
let tab_1 = open_buffer("1.txt", &workspace, cx).await;
let tab_2 = open_buffer("2.txt", &workspace, cx).await;
@ -123,7 +125,8 @@ async fn test_open_item_on_modifiers_release(cx: &mut gpui::TestAppContext) {
.await;
let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
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));
let tab_1 = open_buffer("1.txt", &workspace, cx).await;
let tab_2 = open_buffer("2.txt", &workspace, cx).await;
@ -150,7 +153,8 @@ async fn test_open_on_empty_pane(cx: &mut gpui::TestAppContext) {
app_state.fs.as_fake().insert_tree("/root", json!({})).await;
let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
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));
cx.simulate_modifiers_change(Modifiers::control());
let tab_switcher = open_tab_switcher(false, &workspace, cx);
@ -172,7 +176,8 @@ async fn test_open_with_single_item(cx: &mut gpui::TestAppContext) {
.await;
let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
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));
let tab = open_buffer("1.txt", &workspace, cx).await;
@ -199,7 +204,8 @@ async fn test_close_selected_item(cx: &mut gpui::TestAppContext) {
.await;
let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
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));
let tab_1 = open_buffer("1.txt", &workspace, cx).await;
let tab_2 = open_buffer("2.txt", &workspace, cx).await;
@ -245,7 +251,8 @@ async fn test_close_preserves_selected_position(cx: &mut gpui::TestAppContext) {
.await;
let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
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));
let tab_1 = open_buffer("1.txt", &workspace, cx).await;
let tab_2 = open_buffer("2.txt", &workspace, cx).await;
@ -291,18 +298,18 @@ fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
#[track_caller]
fn open_tab_switcher(
select_last: bool,
workspace: &View<Workspace>,
workspace: &Entity<Workspace>,
cx: &mut VisualTestContext,
) -> View<Picker<TabSwitcherDelegate>> {
) -> Entity<Picker<TabSwitcherDelegate>> {
cx.dispatch_action(Toggle { select_last });
get_active_tab_switcher(workspace, cx)
}
#[track_caller]
fn get_active_tab_switcher(
workspace: &View<Workspace>,
workspace: &Entity<Workspace>,
cx: &mut VisualTestContext,
) -> View<Picker<TabSwitcherDelegate>> {
) -> Entity<Picker<TabSwitcherDelegate>> {
workspace.update(cx, |workspace, cx| {
workspace
.active_modal::<TabSwitcher>(cx)
@ -315,7 +322,7 @@ fn get_active_tab_switcher(
async fn open_buffer(
file_path: &str,
workspace: &View<Workspace>,
workspace: &Entity<Workspace>,
cx: &mut gpui::VisualTestContext,
) -> Box<dyn ItemHandle> {
let project = workspace.update(cx, |workspace, _| workspace.project().clone());
@ -328,8 +335,8 @@ async fn open_buffer(
path: Arc::from(Path::new(file_path)),
};
workspace
.update(cx, move |workspace, cx| {
workspace.open_path(project_path, None, true, cx)
.update_in(cx, move |workspace, window, cx| {
workspace.open_path(project_path, None, true, window, cx)
})
.await
.unwrap()
@ -364,7 +371,7 @@ fn assert_match_at_position(
}
#[track_caller]
fn assert_tab_switcher_is_closed(workspace: View<Workspace>, cx: &mut VisualTestContext) {
fn assert_tab_switcher_is_closed(workspace: Entity<Workspace>, cx: &mut VisualTestContext) {
workspace.update(cx, |workspace, cx| {
assert!(
workspace.active_modal::<TabSwitcher>(cx).is_none(),