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,5 +1,7 @@
use editor::Editor;
use gpui::{div, IntoElement, ParentElement, Render, Subscription, View, ViewContext, WeakView};
use gpui::{
div, Context, Entity, IntoElement, ParentElement, Render, Subscription, WeakEntity, Window,
};
use language::LanguageName;
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, Tooltip};
use workspace::{item::ItemHandle, StatusItemView, Workspace};
@ -8,7 +10,7 @@ use crate::{LanguageSelector, Toggle};
pub struct ActiveBufferLanguage {
active_language: Option<Option<LanguageName>>,
workspace: WeakView<Workspace>,
workspace: WeakEntity<Workspace>,
_observe_active_editor: Option<Subscription>,
}
@ -21,7 +23,7 @@ impl ActiveBufferLanguage {
}
}
fn update_language(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
fn update_language(&mut self, editor: Entity<Editor>, _: &mut Window, cx: &mut Context<Self>) {
self.active_language = Some(None);
let editor = editor.read(cx);
@ -36,7 +38,7 @@ impl ActiveBufferLanguage {
}
impl Render for ActiveBufferLanguage {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div().when_some(self.active_language.as_ref(), |el, active_language| {
let active_language_text = if let Some(active_language_text) = active_language {
active_language_text.to_string()
@ -47,14 +49,16 @@ impl Render for ActiveBufferLanguage {
el.child(
Button::new("change-language", active_language_text)
.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| {
LanguageSelector::toggle(workspace, cx)
LanguageSelector::toggle(workspace, window, cx)
});
}
}))
.tooltip(|cx| Tooltip::for_action("Select Language", &Toggle, cx)),
.tooltip(|window, cx| {
Tooltip::for_action("Select Language", &Toggle, window, cx)
}),
)
})
}
@ -64,11 +68,13 @@ impl StatusItemView for ActiveBufferLanguage {
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._observe_active_editor = Some(cx.observe(&editor, Self::update_language));
self.update_language(editor, cx);
self._observe_active_editor =
Some(cx.observe_in(&editor, window, Self::update_language));
self.update_language(editor, window, cx);
} else {
self.active_language = None;
self._observe_active_editor = None;

View file

@ -7,8 +7,8 @@ use file_finder::file_finder_settings::FileFinderSettings;
use file_icons::FileIcons;
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
use gpui::{
actions, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Model,
ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView,
actions, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
ParentElement, Render, Styled, WeakEntity, Window,
};
use language::{Buffer, LanguageMatcher, LanguageName, LanguageRegistry};
use picker::{Picker, PickerDelegate};
@ -21,22 +21,30 @@ use workspace::{ModalView, Workspace};
actions!(language_selector, [Toggle]);
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(LanguageSelector::register).detach();
pub fn init(cx: &mut App) {
cx.observe_new(LanguageSelector::register).detach();
}
pub struct LanguageSelector {
picker: View<Picker<LanguageSelectorDelegate>>,
picker: Entity<Picker<LanguageSelectorDelegate>>,
}
impl LanguageSelector {
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(move |workspace, _: &Toggle, cx| {
Self::toggle(workspace, cx);
fn register(
workspace: &mut Workspace,
_window: Option<&mut Window>,
_: &mut Context<Workspace>,
) {
workspace.register_action(move |workspace, _: &Toggle, 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 registry = workspace.app_state().languages.clone();
let (_, buffer, _) = workspace
.active_item(cx)?
@ -45,38 +53,39 @@ impl LanguageSelector {
.active_excerpt(cx)?;
let project = workspace.project().clone();
workspace.toggle_modal(cx, move |cx| {
LanguageSelector::new(buffer, project, registry, cx)
workspace.toggle_modal(window, cx, move |window, cx| {
LanguageSelector::new(buffer, project, registry, window, cx)
});
Some(())
}
fn new(
buffer: Model<Buffer>,
project: Model<Project>,
buffer: Entity<Buffer>,
project: Entity<Project>,
language_registry: Arc<LanguageRegistry>,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let delegate = LanguageSelectorDelegate::new(
cx.view().downgrade(),
cx.model().downgrade(),
buffer,
project,
language_registry,
);
let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
Self { picker }
}
}
impl Render for LanguageSelector {
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 LanguageSelector {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
impl Focusable for LanguageSelector {
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
@ -85,9 +94,9 @@ impl EventEmitter<DismissEvent> for LanguageSelector {}
impl ModalView for LanguageSelector {}
pub struct LanguageSelectorDelegate {
language_selector: WeakView<LanguageSelector>,
buffer: Model<Buffer>,
project: Model<Project>,
language_selector: WeakEntity<LanguageSelector>,
buffer: Entity<Buffer>,
project: Entity<Project>,
language_registry: Arc<LanguageRegistry>,
candidates: Vec<StringMatchCandidate>,
matches: Vec<StringMatch>,
@ -96,9 +105,9 @@ pub struct LanguageSelectorDelegate {
impl LanguageSelectorDelegate {
fn new(
language_selector: WeakView<LanguageSelector>,
buffer: Model<Buffer>,
project: Model<Project>,
language_selector: WeakEntity<LanguageSelector>,
buffer: Entity<Buffer>,
project: Entity<Project>,
language_registry: Arc<LanguageRegistry>,
) -> Self {
let candidates = language_registry
@ -126,11 +135,7 @@ impl LanguageSelectorDelegate {
}
}
fn language_data_for_match(
&self,
mat: &StringMatch,
cx: &AppContext,
) -> (String, Option<Icon>) {
fn language_data_for_match(&self, mat: &StringMatch, cx: &App) -> (String, Option<Icon>) {
let mut label = mat.string.clone();
let buffer_language = self.buffer.read(cx).language();
let need_icon = FileFinderSettings::get_global(cx).file_icons;
@ -162,7 +167,7 @@ impl LanguageSelectorDelegate {
}
}
fn language_icon(&self, matcher: &LanguageMatcher, cx: &AppContext) -> Option<Icon> {
fn language_icon(&self, matcher: &LanguageMatcher, cx: &App) -> Option<Icon> {
matcher
.path_suffixes
.iter()
@ -181,7 +186,7 @@ impl LanguageSelectorDelegate {
impl PickerDelegate for LanguageSelectorDelegate {
type ListItem = ListItem;
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
"Select a language…".into()
}
@ -189,13 +194,13 @@ impl PickerDelegate for LanguageSelectorDelegate {
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(mat) = self.matches.get(self.selected_index) {
let language_name = &self.candidates[mat.candidate_id].string;
let language = self.language_registry.language_for_name(language_name);
let project = self.project.downgrade();
let buffer = self.buffer.downgrade();
cx.spawn(|_, mut cx| async move {
cx.spawn_in(window, |_, mut cx| async move {
let language = language.await?;
let project = project
.upgrade()
@ -209,10 +214,10 @@ impl PickerDelegate for LanguageSelectorDelegate {
})
.detach_and_log_err(cx);
}
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.language_selector
.update(cx, |_, cx| cx.emit(DismissEvent))
.log_err();
@ -222,18 +227,24 @@ impl PickerDelegate for LanguageSelectorDelegate {
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();
cx.spawn(|this, mut cx| async move {
cx.spawn_in(window, |this, mut cx| async move {
let matches = if query.is_empty() {
candidates
.into_iter()
@ -273,7 +284,8 @@ impl PickerDelegate for LanguageSelectorDelegate {
&self,
ix: usize,
selected: bool,
cx: &mut ViewContext<Picker<Self>>,
_: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Option<Self::ListItem> {
let mat = &self.matches[ix];
let (label, language_icon) = self.language_data_for_match(mat, cx);