Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'zed2' into zed2-workspace

Antonio Scandurra00c92ae40783
diff --cc crates/gpui2/src/app/async_context.rs
index a10f0eb9e7f,042a75848e4..c5c03c4e9ad
--- a/crates/gpui2/src/app/async_context.rs
+++ b/crates/gpui2/src/app/async_context.rs
@@@ -34,9 -37,12 +34,9 @@@ impl Context for AsyncAppContext
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
) -> Self::Result<R> {
- let app = self
- .app
- .upgrade()
- .ok_or_else(|| anyhow!("app was released"))?;
+ let app = self.app.upgrade().context("app was released")?;
let mut lock = app.lock(); // Need this to compile
- Ok(lock.update_entity(handle, update))
+ Ok(lock.update_model(handle, update))
}
}
diff --cc crates/gpui2/src/view.rs
index 2b8794d8697,3c3ad034b45..de10c7f3387
--- a/crates/gpui2/src/view.rs
+++ b/crates/gpui2/src/view.rs
@@@ -1,14 -1,10 +1,10 @@@
use crate::{
- AnyBox, AnyElement, AnyModel, AppContext, AvailableSpace, BorrowWindow, Bounds, Component,
- Element, ElementId, EntityHandle, EntityId, Flatten, LayoutId, Model, Pixels, Size,
- ViewContext, VisualContext, WeakModel, WindowContext,
+ private::Sealed, AnyBox, AnyElement, AnyModel, AppContext, AvailableSpace, BorrowWindow,
- Bounds, Component, Element, ElementId, Entity, EntityId, LayoutId, Model, Pixels, Size,
- ViewContext, VisualContext, WeakModel, WindowContext,
++ Bounds, Component, Element, ElementId, Entity, EntityId, Flatten, LayoutId, Model, Pixels,
++ Size, ViewContext, VisualContext, WeakModel, WindowContext,
};
use anyhow::{Context, Result};
- use std::{
- any::{Any, TypeId},
- marker::PhantomData,
- sync::Arc,
- };
+ use std::{any::TypeId, marker::PhantomData, sync::Arc};
pub trait Render: 'static + Sized {
type Element: Element<Self> + 'static + Send;
@@@ -134,21 -134,16 +134,20 @@@ pub struct WeakView<V>
impl<V: 'static> WeakView<V> {
pub fn upgrade(&self) -> Option<View<V>> {
- let model = self.model.upgrade()?;
- Some(View { model })
+ Entity::upgrade_from(self)
}
- pub fn update<R>(
+ pub fn update<C, R>(
&self,
- cx: &mut WindowContext,
- f: impl FnOnce(&mut V, &mut ViewContext<V>) -> R,
- ) -> Result<R> {
+ cx: &mut C,
+ f: impl FnOnce(&mut V, &mut C::ViewContext<'_, '_, V>) -> R,
+ ) -> Result<R>
+ where
+ C: VisualContext,
+ Result<C::Result<R>>: Flatten<R>,
+ {
let view = self.upgrade().context("error upgrading view")?;
- Ok(view.update(cx, f))
+ Ok(view.update(cx, f)).flatten()
}
}
@@@ -272,8 -269,12 +273,12 @@@ wher
pub struct AnyView(Arc<dyn ViewObject>);
impl AnyView {
- pub fn downcast<V: 'static + Send>(self) -> Option<View<V>> {
- self.0.model().downcast().map(|model| View { model })
- pub fn downcast<V: 'static + Send>(self) -> Result<View<V>, AnyView> {
++ pub fn downcast<V: 'static>(self) -> Result<View<V>, AnyView> {
+ self.0
+ .model()
+ .downcast()
+ .map(|model| View { model })
+ .map_err(|_| self)
}
pub(crate) fn entity_type(&self) -> TypeId {
diff --cc crates/gpui2/src/window.rs
index 2cf706eda4e,7898ac34fce..5d47fffb6e1
--- a/crates/gpui2/src/window.rs
+++ b/crates/gpui2/src/window.rs
@@@ -376,35 -378,6 +376,35 @@@ impl<'a, 'w> WindowContext<'a, 'w>
self.notify();
}
- pub fn subscribe<E, H>(
++ pub fn subscribe<Emitter, E>(
+ &mut self,
- handle: &H,
- mut on_event: impl FnMut(H, &E::Event, &mut WindowContext<'_, '_>) + Send + 'static,
++ entity: &E,
++ mut on_event: impl FnMut(E, &Emitter::Event, &mut WindowContext<'_, '_>) + Send + 'static,
+ ) -> Subscription
+ where
- E: EventEmitter,
- H: EntityHandle<E>,
++ Emitter: EventEmitter,
++ E: Entity<Emitter>,
+ {
- let entity_id = handle.entity_id();
- let handle = handle.downgrade();
++ let entity_id = entity.entity_id();
++ let entity = entity.downgrade();
+ let window_handle = self.window.handle;
+ self.app.event_listeners.insert(
+ entity_id,
+ Box::new(move |event, cx| {
+ cx.update_window(window_handle, |cx| {
- if let Some(handle) = H::upgrade_from(&handle) {
++ if let Some(handle) = E::upgrade_from(&entity) {
+ let event = event.downcast_ref().expect("invalid event type");
+ on_event(handle, event, cx);
+ true
+ } else {
+ false
+ }
+ })
+ .unwrap_or(false)
+ }),
+ )
+ }
+
/// Schedule the given closure to be run on the main thread. It will be invoked with
/// a `MainThread<WindowContext>`, which provides access to platform-specific functionality
/// of the window.
@@@ -1595,23 -1568,25 +1595,25 @@@ impl<'a, 'w, V: 'static> ViewContext<'a
self.window_cx.on_next_frame(move |cx| view.update(cx, f));
}
- pub fn observe<E>(
+ pub fn observe<V2, E>(
&mut self,
- handle: &Model<E>,
- mut on_notify: impl FnMut(&mut V, Model<E>, &mut ViewContext<'_, '_, V>) + Send + 'static,
+ entity: &E,
+ mut on_notify: impl FnMut(&mut V, E, &mut ViewContext<'_, '_, V>) + Send + 'static,
) -> Subscription
where
- E: 'static,
+ V2: 'static,
V: Any + Send,
+ E: Entity<V2>,
{
let view = self.view();
- let handle = handle.downgrade();
+ let entity_id = entity.entity_id();
+ let entity = entity.downgrade();
let window_handle = self.window.handle;
self.app.observers.insert(
- handle.entity_id,
+ entity_id,
Box::new(move |cx| {
- cx.update_window(window_handle.id, |cx| {
+ cx.update_window(window_handle, |cx| {
- if let Some(handle) = handle.upgrade() {
+ if let Some(handle) = E::upgrade_from(&entity) {
view.update(cx, |this, cx| on_notify(this, handle, cx))
.is_ok()
} else {
@@@ -1639,8 -1614,8 +1641,8 @@@
self.app.event_listeners.insert(
entity_id,
Box::new(move |event, cx| {
- cx.update_window(window_handle.id, |cx| {
+ cx.update_window(window_handle, |cx| {
- if let Some(handle) = H::upgrade_from(&handle) {
+ if let Some(handle) = E::upgrade_from(&handle) {
let event = event.downcast_ref().expect("invalid event type");
view.update(cx, |this, cx| on_event(this, handle, event, cx))
.is_ok()
@@@ -1675,14 -1650,17 +1677,17 @@@
) -> Subscription
where
V: Any + Send,
+ V2: 'static,
+ E: Entity<V2>,
{
let view = self.view();
+ let entity_id = entity.entity_id();
let window_handle = self.window.handle;
self.app.release_listeners.insert(
- handle.entity_id,
+ entity_id,
Box::new(move |entity, cx| {
let entity = entity.downcast_mut().expect("invalid entity type");
- let _ = cx.update_window(window_handle.id, |cx| {
+ let _ = cx.update_window(window_handle, |cx| {
view.update(cx, |this, cx| on_release(this, entity, cx))
});
}),