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
|
@ -6,8 +6,7 @@ use anyhow::{Context as _, Result};
|
|||
use collections::{hash_map, HashMap, HashSet};
|
||||
use futures::{channel::oneshot, StreamExt};
|
||||
use gpui::{
|
||||
hash, prelude::*, AppContext, EventEmitter, Img, Model, ModelContext, Subscription, Task,
|
||||
WeakModel,
|
||||
hash, prelude::*, App, Context, Entity, EventEmitter, Img, Subscription, Task, WeakEntity,
|
||||
};
|
||||
use language::{DiskState, File};
|
||||
use rpc::{AnyProtoClient, ErrorExt as _};
|
||||
|
@ -42,7 +41,7 @@ pub enum ImageItemEvent {
|
|||
impl EventEmitter<ImageItemEvent> for ImageItem {}
|
||||
|
||||
pub enum ImageStoreEvent {
|
||||
ImageAdded(Model<ImageItem>),
|
||||
ImageAdded(Entity<ImageItem>),
|
||||
}
|
||||
|
||||
impl EventEmitter<ImageStoreEvent> for ImageStore {}
|
||||
|
@ -55,7 +54,7 @@ pub struct ImageItem {
|
|||
}
|
||||
|
||||
impl ImageItem {
|
||||
pub fn project_path(&self, cx: &AppContext) -> ProjectPath {
|
||||
pub fn project_path(&self, cx: &App) -> ProjectPath {
|
||||
ProjectPath {
|
||||
worktree_id: self.file.worktree_id(cx),
|
||||
path: self.file.path().clone(),
|
||||
|
@ -66,7 +65,7 @@ impl ImageItem {
|
|||
self.file.path()
|
||||
}
|
||||
|
||||
fn file_updated(&mut self, new_file: Arc<dyn File>, cx: &mut ModelContext<Self>) {
|
||||
fn file_updated(&mut self, new_file: Arc<dyn File>, cx: &mut Context<Self>) {
|
||||
let mut file_changed = false;
|
||||
|
||||
let old_file = self.file.as_ref();
|
||||
|
@ -90,7 +89,7 @@ impl ImageItem {
|
|||
}
|
||||
}
|
||||
|
||||
fn reload(&mut self, cx: &mut ModelContext<Self>) -> Option<oneshot::Receiver<()>> {
|
||||
fn reload(&mut self, cx: &mut Context<Self>) -> Option<oneshot::Receiver<()>> {
|
||||
let local_file = self.file.as_local()?;
|
||||
let (tx, rx) = futures::channel::oneshot::channel();
|
||||
|
||||
|
@ -116,10 +115,10 @@ impl ImageItem {
|
|||
|
||||
impl ProjectItem for ImageItem {
|
||||
fn try_open(
|
||||
project: &Model<Project>,
|
||||
project: &Entity<Project>,
|
||||
path: &ProjectPath,
|
||||
cx: &mut AppContext,
|
||||
) -> Option<Task<gpui::Result<Model<Self>>>> {
|
||||
cx: &mut App,
|
||||
) -> Option<Task<gpui::Result<Entity<Self>>>> {
|
||||
let path = path.clone();
|
||||
let project = project.clone();
|
||||
|
||||
|
@ -152,11 +151,11 @@ impl ProjectItem for ImageItem {
|
|||
}
|
||||
}
|
||||
|
||||
fn entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
|
||||
fn entry_id(&self, _: &App) -> Option<ProjectEntryId> {
|
||||
worktree::File::from_dyn(Some(&self.file))?.entry_id
|
||||
}
|
||||
|
||||
fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
|
||||
fn project_path(&self, cx: &App) -> Option<ProjectPath> {
|
||||
Some(self.project_path(cx).clone())
|
||||
}
|
||||
|
||||
|
@ -169,17 +168,17 @@ trait ImageStoreImpl {
|
|||
fn open_image(
|
||||
&self,
|
||||
path: Arc<Path>,
|
||||
worktree: Model<Worktree>,
|
||||
cx: &mut ModelContext<ImageStore>,
|
||||
) -> Task<Result<Model<ImageItem>>>;
|
||||
worktree: Entity<Worktree>,
|
||||
cx: &mut Context<ImageStore>,
|
||||
) -> Task<Result<Entity<ImageItem>>>;
|
||||
|
||||
fn reload_images(
|
||||
&self,
|
||||
images: HashSet<Model<ImageItem>>,
|
||||
cx: &mut ModelContext<ImageStore>,
|
||||
images: HashSet<Entity<ImageItem>>,
|
||||
cx: &mut Context<ImageStore>,
|
||||
) -> Task<Result<()>>;
|
||||
|
||||
fn as_local(&self) -> Option<Model<LocalImageStore>>;
|
||||
fn as_local(&self) -> Option<Entity<LocalImageStore>>;
|
||||
}
|
||||
|
||||
struct RemoteImageStore {}
|
||||
|
@ -187,26 +186,26 @@ struct RemoteImageStore {}
|
|||
struct LocalImageStore {
|
||||
local_image_ids_by_path: HashMap<ProjectPath, ImageId>,
|
||||
local_image_ids_by_entry_id: HashMap<ProjectEntryId, ImageId>,
|
||||
image_store: WeakModel<ImageStore>,
|
||||
image_store: WeakEntity<ImageStore>,
|
||||
_subscription: Subscription,
|
||||
}
|
||||
|
||||
pub struct ImageStore {
|
||||
state: Box<dyn ImageStoreImpl>,
|
||||
opened_images: HashMap<ImageId, WeakModel<ImageItem>>,
|
||||
worktree_store: Model<WorktreeStore>,
|
||||
opened_images: HashMap<ImageId, WeakEntity<ImageItem>>,
|
||||
worktree_store: Entity<WorktreeStore>,
|
||||
#[allow(clippy::type_complexity)]
|
||||
loading_images_by_path: HashMap<
|
||||
ProjectPath,
|
||||
postage::watch::Receiver<Option<Result<Model<ImageItem>, Arc<anyhow::Error>>>>,
|
||||
postage::watch::Receiver<Option<Result<Entity<ImageItem>, Arc<anyhow::Error>>>>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl ImageStore {
|
||||
pub fn local(worktree_store: Model<WorktreeStore>, cx: &mut ModelContext<Self>) -> Self {
|
||||
pub fn local(worktree_store: Entity<WorktreeStore>, cx: &mut Context<Self>) -> Self {
|
||||
let this = cx.weak_model();
|
||||
Self {
|
||||
state: Box::new(cx.new_model(|cx| {
|
||||
state: Box::new(cx.new(|cx| {
|
||||
let subscription = cx.subscribe(
|
||||
&worktree_store,
|
||||
|this: &mut LocalImageStore, _, event, cx| {
|
||||
|
@ -230,32 +229,32 @@ impl ImageStore {
|
|||
}
|
||||
|
||||
pub fn remote(
|
||||
worktree_store: Model<WorktreeStore>,
|
||||
worktree_store: Entity<WorktreeStore>,
|
||||
_upstream_client: AnyProtoClient,
|
||||
_remote_id: u64,
|
||||
cx: &mut ModelContext<Self>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
Self {
|
||||
state: Box::new(cx.new_model(|_| RemoteImageStore {})),
|
||||
state: Box::new(cx.new(|_| RemoteImageStore {})),
|
||||
opened_images: Default::default(),
|
||||
loading_images_by_path: Default::default(),
|
||||
worktree_store,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn images(&self) -> impl '_ + Iterator<Item = Model<ImageItem>> {
|
||||
pub fn images(&self) -> impl '_ + Iterator<Item = Entity<ImageItem>> {
|
||||
self.opened_images
|
||||
.values()
|
||||
.filter_map(|image| image.upgrade())
|
||||
}
|
||||
|
||||
pub fn get(&self, image_id: ImageId) -> Option<Model<ImageItem>> {
|
||||
pub fn get(&self, image_id: ImageId) -> Option<Entity<ImageItem>> {
|
||||
self.opened_images
|
||||
.get(&image_id)
|
||||
.and_then(|image| image.upgrade())
|
||||
}
|
||||
|
||||
pub fn get_by_path(&self, path: &ProjectPath, cx: &AppContext) -> Option<Model<ImageItem>> {
|
||||
pub fn get_by_path(&self, path: &ProjectPath, cx: &App) -> Option<Entity<ImageItem>> {
|
||||
self.images()
|
||||
.find(|image| &image.read(cx).project_path(cx) == path)
|
||||
}
|
||||
|
@ -263,8 +262,8 @@ impl ImageStore {
|
|||
pub fn open_image(
|
||||
&mut self,
|
||||
project_path: ProjectPath,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<Model<ImageItem>>> {
|
||||
cx: &mut Context<Self>,
|
||||
) -> Task<Result<Entity<ImageItem>>> {
|
||||
let existing_image = self.get_by_path(&project_path, cx);
|
||||
if let Some(existing_image) = existing_image {
|
||||
return Task::ready(Ok(existing_image));
|
||||
|
@ -317,9 +316,9 @@ impl ImageStore {
|
|||
|
||||
pub async fn wait_for_loading_image(
|
||||
mut receiver: postage::watch::Receiver<
|
||||
Option<Result<Model<ImageItem>, Arc<anyhow::Error>>>,
|
||||
Option<Result<Entity<ImageItem>, Arc<anyhow::Error>>>,
|
||||
>,
|
||||
) -> Result<Model<ImageItem>, Arc<anyhow::Error>> {
|
||||
) -> Result<Entity<ImageItem>, Arc<anyhow::Error>> {
|
||||
loop {
|
||||
if let Some(result) = receiver.borrow().as_ref() {
|
||||
match result {
|
||||
|
@ -333,8 +332,8 @@ impl ImageStore {
|
|||
|
||||
pub fn reload_images(
|
||||
&self,
|
||||
images: HashSet<Model<ImageItem>>,
|
||||
cx: &mut ModelContext<ImageStore>,
|
||||
images: HashSet<Entity<ImageItem>>,
|
||||
cx: &mut Context<ImageStore>,
|
||||
) -> Task<Result<()>> {
|
||||
if images.is_empty() {
|
||||
return Task::ready(Ok(()));
|
||||
|
@ -343,11 +342,7 @@ impl ImageStore {
|
|||
self.state.reload_images(images, cx)
|
||||
}
|
||||
|
||||
fn add_image(
|
||||
&mut self,
|
||||
image: Model<ImageItem>,
|
||||
cx: &mut ModelContext<ImageStore>,
|
||||
) -> Result<()> {
|
||||
fn add_image(&mut self, image: Entity<ImageItem>, cx: &mut Context<ImageStore>) -> Result<()> {
|
||||
let image_id = image.read(cx).id;
|
||||
|
||||
self.opened_images.insert(image_id, image.downgrade());
|
||||
|
@ -359,9 +354,9 @@ impl ImageStore {
|
|||
|
||||
fn on_image_event(
|
||||
&mut self,
|
||||
image: Model<ImageItem>,
|
||||
image: Entity<ImageItem>,
|
||||
event: &ImageItemEvent,
|
||||
cx: &mut ModelContext<Self>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
match event {
|
||||
ImageItemEvent::FileHandleChanged => {
|
||||
|
@ -376,13 +371,13 @@ impl ImageStore {
|
|||
}
|
||||
}
|
||||
|
||||
impl ImageStoreImpl for Model<LocalImageStore> {
|
||||
impl ImageStoreImpl for Entity<LocalImageStore> {
|
||||
fn open_image(
|
||||
&self,
|
||||
path: Arc<Path>,
|
||||
worktree: Model<Worktree>,
|
||||
cx: &mut ModelContext<ImageStore>,
|
||||
) -> Task<Result<Model<ImageItem>>> {
|
||||
worktree: Entity<Worktree>,
|
||||
cx: &mut Context<ImageStore>,
|
||||
) -> Task<Result<Entity<ImageItem>>> {
|
||||
let this = self.clone();
|
||||
|
||||
let load_file = worktree.update(cx, |worktree, cx| {
|
||||
|
@ -392,7 +387,7 @@ impl ImageStoreImpl for Model<LocalImageStore> {
|
|||
let LoadedBinaryFile { file, content } = load_file.await?;
|
||||
let image = create_gpui_image(content)?;
|
||||
|
||||
let model = cx.new_model(|cx| ImageItem {
|
||||
let model = cx.new(|cx| ImageItem {
|
||||
id: cx.entity_id().as_non_zero_u64().into(),
|
||||
file: file.clone(),
|
||||
image,
|
||||
|
@ -426,8 +421,8 @@ impl ImageStoreImpl for Model<LocalImageStore> {
|
|||
|
||||
fn reload_images(
|
||||
&self,
|
||||
images: HashSet<Model<ImageItem>>,
|
||||
cx: &mut ModelContext<ImageStore>,
|
||||
images: HashSet<Entity<ImageItem>>,
|
||||
cx: &mut Context<ImageStore>,
|
||||
) -> Task<Result<()>> {
|
||||
cx.spawn(move |_, mut cx| async move {
|
||||
for image in images {
|
||||
|
@ -439,13 +434,13 @@ impl ImageStoreImpl for Model<LocalImageStore> {
|
|||
})
|
||||
}
|
||||
|
||||
fn as_local(&self) -> Option<Model<LocalImageStore>> {
|
||||
fn as_local(&self) -> Option<Entity<LocalImageStore>> {
|
||||
Some(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl LocalImageStore {
|
||||
fn subscribe_to_worktree(&mut self, worktree: &Model<Worktree>, cx: &mut ModelContext<Self>) {
|
||||
fn subscribe_to_worktree(&mut self, worktree: &Entity<Worktree>, cx: &mut Context<Self>) {
|
||||
cx.subscribe(worktree, |this, worktree, event, cx| {
|
||||
if worktree.read(cx).is_local() {
|
||||
match event {
|
||||
|
@ -461,9 +456,9 @@ impl LocalImageStore {
|
|||
|
||||
fn local_worktree_entries_changed(
|
||||
&mut self,
|
||||
worktree_handle: &Model<Worktree>,
|
||||
worktree_handle: &Entity<Worktree>,
|
||||
changes: &[(Arc<Path>, ProjectEntryId, PathChange)],
|
||||
cx: &mut ModelContext<Self>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let snapshot = worktree_handle.read(cx).snapshot();
|
||||
for (path, entry_id, _) in changes {
|
||||
|
@ -475,9 +470,9 @@ impl LocalImageStore {
|
|||
&mut self,
|
||||
entry_id: ProjectEntryId,
|
||||
path: &Arc<Path>,
|
||||
worktree: &Model<worktree::Worktree>,
|
||||
worktree: &Entity<worktree::Worktree>,
|
||||
snapshot: &worktree::Snapshot,
|
||||
cx: &mut ModelContext<Self>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Option<()> {
|
||||
let project_path = ProjectPath {
|
||||
worktree_id: snapshot.id(),
|
||||
|
@ -576,7 +571,7 @@ impl LocalImageStore {
|
|||
None
|
||||
}
|
||||
|
||||
fn image_changed_file(&mut self, image: Model<ImageItem>, cx: &mut AppContext) -> Option<()> {
|
||||
fn image_changed_file(&mut self, image: Entity<ImageItem>, cx: &mut App) -> Option<()> {
|
||||
let file = worktree::File::from_dyn(Some(&image.read(cx).file))?;
|
||||
|
||||
let image_id = image.read(cx).id;
|
||||
|
@ -620,13 +615,13 @@ fn create_gpui_image(content: Vec<u8>) -> anyhow::Result<Arc<gpui::Image>> {
|
|||
}))
|
||||
}
|
||||
|
||||
impl ImageStoreImpl for Model<RemoteImageStore> {
|
||||
impl ImageStoreImpl for Entity<RemoteImageStore> {
|
||||
fn open_image(
|
||||
&self,
|
||||
_path: Arc<Path>,
|
||||
_worktree: Model<Worktree>,
|
||||
_cx: &mut ModelContext<ImageStore>,
|
||||
) -> Task<Result<Model<ImageItem>>> {
|
||||
_worktree: Entity<Worktree>,
|
||||
_cx: &mut Context<ImageStore>,
|
||||
) -> Task<Result<Entity<ImageItem>>> {
|
||||
Task::ready(Err(anyhow::anyhow!(
|
||||
"Opening images from remote is not supported"
|
||||
)))
|
||||
|
@ -634,15 +629,15 @@ impl ImageStoreImpl for Model<RemoteImageStore> {
|
|||
|
||||
fn reload_images(
|
||||
&self,
|
||||
_images: HashSet<Model<ImageItem>>,
|
||||
_cx: &mut ModelContext<ImageStore>,
|
||||
_images: HashSet<Entity<ImageItem>>,
|
||||
_cx: &mut Context<ImageStore>,
|
||||
) -> Task<Result<()>> {
|
||||
Task::ready(Err(anyhow::anyhow!(
|
||||
"Reloading images from remote is not supported"
|
||||
)))
|
||||
}
|
||||
|
||||
fn as_local(&self) -> Option<Model<LocalImageStore>> {
|
||||
fn as_local(&self) -> Option<Entity<LocalImageStore>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue