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,7 +1,7 @@
use editor::Editor;
use gpui::{
div, AsyncWindowContext, IntoElement, ParentElement, Render, Subscription, Task, View,
ViewContext, WeakModel, WeakView,
div, AsyncWindowContext, Context, Entity, IntoElement, ParentElement, Render, Subscription,
Task, WeakEntity, Window,
};
use language::{Buffer, BufferEvent, LanguageName, Toolchain};
use project::{Project, WorktreeId};
@ -13,24 +13,24 @@ use crate::ToolchainSelector;
pub struct ActiveToolchain {
active_toolchain: Option<Toolchain>,
term: SharedString,
workspace: WeakView<Workspace>,
active_buffer: Option<(WorktreeId, WeakModel<Buffer>, Subscription)>,
workspace: WeakEntity<Workspace>,
active_buffer: Option<(WorktreeId, WeakEntity<Buffer>, Subscription)>,
_update_toolchain_task: Task<Option<()>>,
}
impl ActiveToolchain {
pub fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
pub fn new(workspace: &Workspace, window: &mut Window, cx: &mut Context<Self>) -> Self {
Self {
active_toolchain: None,
active_buffer: None,
term: SharedString::new_static("Toolchain"),
workspace: workspace.weak_handle(),
_update_toolchain_task: Self::spawn_tracker_task(cx),
_update_toolchain_task: Self::spawn_tracker_task(window, cx),
}
}
fn spawn_tracker_task(cx: &mut ViewContext<Self>) -> Task<Option<()>> {
cx.spawn(|this, mut cx| async move {
fn spawn_tracker_task(window: &mut Window, cx: &mut Context<Self>) -> Task<Option<()>> {
cx.spawn_in(window, |this, mut cx| async move {
let active_file = this
.update(&mut cx, |this, _| {
this.active_buffer
@ -62,7 +62,7 @@ impl ActiveToolchain {
.ok()
.flatten()?;
let toolchain =
Self::active_toolchain(workspace, worktree_id, language_name, cx.clone()).await?;
Self::active_toolchain(workspace, worktree_id, language_name, &mut cx).await?;
let _ = this.update(&mut cx, |this, cx| {
this.active_toolchain = Some(toolchain);
@ -72,17 +72,26 @@ impl ActiveToolchain {
})
}
fn update_lister(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
fn update_lister(
&mut self,
editor: Entity<Editor>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let editor = editor.read(cx);
if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
if let Some(worktree_id) = buffer.read(cx).file().map(|file| file.worktree_id(cx)) {
let subscription = cx.subscribe(&buffer, |this, _, event: &BufferEvent, cx| {
if matches!(event, BufferEvent::LanguageChanged) {
this._update_toolchain_task = Self::spawn_tracker_task(cx);
}
});
let subscription = cx.subscribe_in(
&buffer,
window,
|this, _, event: &BufferEvent, window, cx| {
if matches!(event, BufferEvent::LanguageChanged) {
this._update_toolchain_task = Self::spawn_tracker_task(window, cx);
}
},
);
self.active_buffer = Some((worktree_id, buffer.downgrade(), subscription));
self._update_toolchain_task = Self::spawn_tracker_task(cx);
self._update_toolchain_task = Self::spawn_tracker_task(window, cx);
}
}
@ -90,10 +99,10 @@ impl ActiveToolchain {
}
fn active_toolchain(
workspace: WeakView<Workspace>,
workspace: WeakEntity<Workspace>,
worktree_id: WorktreeId,
language_name: LanguageName,
cx: AsyncWindowContext,
cx: &mut AsyncWindowContext,
) -> Task<Option<Toolchain>> {
cx.spawn(move |mut cx| async move {
let workspace_id = workspace
@ -115,7 +124,7 @@ impl ActiveToolchain {
.update(&mut cx, |this, _| this.project().clone())
.ok()?;
let toolchains = cx
.update(|cx| {
.update(|_, cx| {
project
.read(cx)
.available_toolchains(worktree_id, language_name, cx)
@ -143,20 +152,20 @@ impl ActiveToolchain {
}
impl Render for ActiveToolchain {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div().when_some(self.active_toolchain.as_ref(), |el, active_toolchain| {
let term = self.term.clone();
el.child(
Button::new("change-toolchain", active_toolchain.name.clone())
.label_size(LabelSize::Small)
.on_click(cx.listener(|this, _, cx| {
.on_click(cx.listener(|this, _, window, cx| {
if let Some(workspace) = this.workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
ToolchainSelector::toggle(workspace, cx)
ToolchainSelector::toggle(workspace, window, cx)
});
}
}))
.tooltip(move |cx| Tooltip::text(format!("Select {}", &term), cx)),
.tooltip(Tooltip::text(format!("Select {}", &term))),
)
})
}
@ -166,11 +175,12 @@ impl StatusItemView for ActiveToolchain {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
self.active_toolchain.take();
self.update_lister(editor, cx);
self.update_lister(editor, window, cx);
}
cx.notify();
}

View file

@ -4,8 +4,8 @@ pub use active_toolchain::ActiveToolchain;
use editor::Editor;
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
use gpui::{
actions, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Model,
ParentElement, Render, Styled, Task, View, ViewContext, VisualContext, WeakView,
actions, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
ParentElement, Render, Styled, Task, WeakEntity, Window,
};
use language::{LanguageName, Toolchain, ToolchainList};
use picker::{Picker, PickerDelegate};
@ -17,22 +17,30 @@ use workspace::{ModalView, Workspace};
actions!(toolchain, [Select]);
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(ToolchainSelector::register).detach();
pub fn init(cx: &mut App) {
cx.observe_new(ToolchainSelector::register).detach();
}
pub struct ToolchainSelector {
picker: View<Picker<ToolchainSelectorDelegate>>,
picker: Entity<Picker<ToolchainSelectorDelegate>>,
}
impl ToolchainSelector {
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(move |workspace, _: &Select, cx| {
Self::toggle(workspace, cx);
fn register(
workspace: &mut Workspace,
_window: Option<&mut Window>,
_: &mut Context<Workspace>,
) {
workspace.register_action(move |workspace, _: &Select, window, cx| {
Self::toggle(workspace, window, cx);
});
}
fn toggle(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<()> {
fn toggle(
workspace: &mut Workspace,
window: &mut Window,
cx: &mut Context<Workspace>,
) -> Option<()> {
let (_, buffer, _) = workspace
.active_item(cx)?
.act_as::<Editor>(cx)?
@ -49,15 +57,15 @@ impl ToolchainSelector {
.abs_path();
let workspace_id = workspace.database_id()?;
let weak = workspace.weak_handle();
cx.spawn(move |workspace, mut cx| async move {
cx.spawn_in(window, move |workspace, mut cx| async move {
let active_toolchain = workspace::WORKSPACE_DB
.toolchain(workspace_id, worktree_id, language_name.clone())
.await
.ok()
.flatten();
workspace
.update(&mut cx, |this, cx| {
this.toggle_modal(cx, move |cx| {
.update_in(&mut cx, |this, window, cx| {
this.toggle_modal(window, cx, move |window, cx| {
ToolchainSelector::new(
weak,
project,
@ -65,6 +73,7 @@ impl ToolchainSelector {
worktree_id,
worktree_root_path,
language_name,
window,
cx,
)
});
@ -76,41 +85,44 @@ impl ToolchainSelector {
Some(())
}
#[allow(clippy::too_many_arguments)]
fn new(
workspace: WeakView<Workspace>,
project: Model<Project>,
workspace: WeakEntity<Workspace>,
project: Entity<Project>,
active_toolchain: Option<Toolchain>,
worktree_id: WorktreeId,
worktree_root: Arc<Path>,
language_name: LanguageName,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let view = cx.view().downgrade();
let picker = cx.new_view(|cx| {
let toolchain_selector = cx.model().downgrade();
let picker = cx.new(|cx| {
let delegate = ToolchainSelectorDelegate::new(
active_toolchain,
view,
toolchain_selector,
workspace,
worktree_id,
worktree_root,
project,
language_name,
window,
cx,
);
Picker::uniform_list(delegate, cx)
Picker::uniform_list(delegate, window, cx)
});
Self { picker }
}
}
impl Render for ToolchainSelector {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
v_flex().w(rems(34.)).child(self.picker.clone())
}
}
impl FocusableView for ToolchainSelector {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
impl Focusable for ToolchainSelector {
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
@ -119,11 +131,11 @@ impl EventEmitter<DismissEvent> for ToolchainSelector {}
impl ModalView for ToolchainSelector {}
pub struct ToolchainSelectorDelegate {
toolchain_selector: WeakView<ToolchainSelector>,
toolchain_selector: WeakEntity<ToolchainSelector>,
candidates: ToolchainList,
matches: Vec<StringMatch>,
selected_index: usize,
workspace: WeakView<Workspace>,
workspace: WeakEntity<Workspace>,
worktree_id: WorktreeId,
worktree_abs_path_root: Arc<Path>,
placeholder_text: Arc<str>,
@ -134,15 +146,16 @@ impl ToolchainSelectorDelegate {
#[allow(clippy::too_many_arguments)]
fn new(
active_toolchain: Option<Toolchain>,
language_selector: WeakView<ToolchainSelector>,
workspace: WeakView<Workspace>,
toolchain_selector: WeakEntity<ToolchainSelector>,
workspace: WeakEntity<Workspace>,
worktree_id: WorktreeId,
worktree_abs_path_root: Arc<Path>,
project: Model<Project>,
project: Entity<Project>,
language_name: LanguageName,
cx: &mut ViewContext<Picker<Self>>,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Self {
let _fetch_candidates_task = cx.spawn({
let _fetch_candidates_task = cx.spawn_in(window, {
let project = project.clone();
move |this, mut cx| async move {
let term = project
@ -152,9 +165,9 @@ impl ToolchainSelectorDelegate {
.ok()?
.await?;
let placeholder_text = format!("Select a {}", term.to_lowercase()).into();
let _ = this.update(&mut cx, move |this, cx| {
let _ = this.update_in(&mut cx, move |this, window, cx| {
this.delegate.placeholder_text = placeholder_text;
this.refresh_placeholder(cx);
this.refresh_placeholder(window, cx);
});
let available_toolchains = project
.update(&mut cx, |this, cx| {
@ -163,7 +176,7 @@ impl ToolchainSelectorDelegate {
.ok()?
.await?;
let _ = this.update(&mut cx, move |this, cx| {
let _ = this.update_in(&mut cx, move |this, window, cx| {
this.delegate.candidates = available_toolchains;
if let Some(active_toolchain) = active_toolchain {
@ -174,10 +187,10 @@ impl ToolchainSelectorDelegate {
.iter()
.position(|toolchain| *toolchain == active_toolchain)
{
this.delegate.set_selected_index(position, cx);
this.delegate.set_selected_index(position, window, cx);
}
}
this.update_matches(this.query(cx), cx);
this.update_matches(this.query(cx), window, cx);
});
Some(())
@ -185,7 +198,7 @@ impl ToolchainSelectorDelegate {
});
let placeholder_text = "Select a toolchain…".to_string().into();
Self {
toolchain_selector: language_selector,
toolchain_selector,
candidates: Default::default(),
matches: vec![],
selected_index: 0,
@ -209,7 +222,7 @@ impl ToolchainSelectorDelegate {
impl PickerDelegate for ToolchainSelectorDelegate {
type ListItem = ListItem;
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
self.placeholder_text.clone()
}
@ -217,7 +230,7 @@ impl PickerDelegate for ToolchainSelectorDelegate {
self.matches.len()
}
fn confirm(&mut self, _: bool, cx: &mut ViewContext<Picker<Self>>) {
fn confirm(&mut self, _: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
if let Some(string_match) = self.matches.get(self.selected_index) {
let toolchain = self.candidates.toolchains[string_match.candidate_id].clone();
if let Some(workspace_id) = self
@ -228,7 +241,7 @@ impl PickerDelegate for ToolchainSelectorDelegate {
{
let workspace = self.workspace.clone();
let worktree_id = self.worktree_id;
cx.spawn(|_, mut cx| async move {
cx.spawn_in(window, |_, mut cx| async move {
workspace::WORKSPACE_DB
.set_toolchain(workspace_id, worktree_id, toolchain.clone())
.await
@ -246,10 +259,10 @@ impl PickerDelegate for ToolchainSelectorDelegate {
.detach();
}
}
self.dismissed(cx);
self.dismissed(window, cx);
}
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
fn dismissed(&mut self, _: &mut Window, cx: &mut Context<Picker<Self>>) {
self.toolchain_selector
.update(cx, |_, cx| cx.emit(DismissEvent))
.log_err();
@ -259,19 +272,25 @@ impl PickerDelegate for ToolchainSelectorDelegate {
self.selected_index
}
fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext<Picker<Self>>) {
fn set_selected_index(
&mut self,
ix: usize,
_window: &mut Window,
_: &mut Context<Picker<Self>>,
) {
self.selected_index = ix;
}
fn update_matches(
&mut self,
query: String,
cx: &mut ViewContext<Picker<Self>>,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> gpui::Task<()> {
let background = cx.background_executor().clone();
let candidates = self.candidates.clone();
let worktree_root_path = self.worktree_abs_path_root.clone();
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
let matches = if query.is_empty() {
candidates
.toolchains
@ -326,7 +345,8 @@ impl PickerDelegate for ToolchainSelectorDelegate {
&self,
ix: usize,
selected: bool,
_: &mut ViewContext<Picker<Self>>,
_window: &mut Window,
_: &mut Context<Picker<Self>>,
) -> Option<Self::ListItem> {
let mat = &self.matches[ix];
let toolchain = &self.candidates.toolchains[mat.candidate_id];