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 'fewer-bounds' into zed2

Nathan Sobo2285a350163e
diff --cc crates/gpui2/src/app.rs
index 4674893693c,78cbd116006..2dc5e1510c6
--- a/crates/gpui2/src/app.rs
+++ b/crates/gpui2/src/app.rs
@@@ -22,8 -22,9 +22,10 @@@ use parking_lot::{Mutex, RwLock}
use slotmap::SlotMap;
use std::{
any::{type_name, Any, TypeId},
+ borrow::Borrow,
+ marker::PhantomData,
mem,
+ ops::{Deref, DerefMut},
sync::{atomic::Ordering::SeqCst, Arc, Weak},
time::Duration,
};
@@@ -714,34 -721,12 +713,34 @@@ wher
pub fn open_url(&self, url: &str) {
self.platform().open_url(url);
}
+}
+
+impl MainThread<AppContext> {
+ fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
+ self.0.update(|cx| {
+ update(unsafe {
+ std::mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx)
+ })
+ })
+ }
+
+ pub(crate) fn update_window<R>(
+ &mut self,
+ id: WindowId,
+ update: impl FnOnce(&mut MainThread<WindowContext>) -> R,
+ ) -> Result<R> {
+ self.0.update_window(id, |cx| {
+ update(unsafe {
+ std::mem::transmute::<&mut WindowContext, &mut MainThread<WindowContext>>(cx)
+ })
+ })
+ }
- pub fn open_window<S: 'static + Send + Sync>(
+ pub fn open_window<V: 'static>(
&mut self,
options: crate::WindowOptions,
- build_root_view: impl FnOnce(&mut WindowContext) -> View<S> + Send + 'static,
- ) -> WindowHandle<S> {
+ build_root_view: impl FnOnce(&mut WindowContext) -> View<V> + Send + 'static,
+ ) -> WindowHandle<V> {
self.update(|cx| {
let id = cx.windows.insert(None);
let handle = WindowHandle::new(id);
@@@ -780,11 -765,36 +779,39 @@@ pub(crate) enum Effect
NotifyGlobalObservers {
global_type: TypeId,
},
+ Defer {
+ callback: Box<dyn FnOnce(&mut AppContext) + Send + Sync + 'static>,
+ },
}
+ pub(crate) struct GlobalLease<G: 'static> {
+ global: AnyBox,
+ global_type: PhantomData<G>,
+ }
+
+ impl<G: 'static> GlobalLease<G> {
+ fn new(global: AnyBox) -> Self {
+ GlobalLease {
+ global,
+ global_type: PhantomData,
+ }
+ }
+ }
+
+ impl<G: 'static> Deref for GlobalLease<G> {
+ type Target = G;
+
+ fn deref(&self) -> &Self::Target {
+ self.global.downcast_ref().unwrap()
+ }
+ }
+
+ impl<G: 'static> DerefMut for GlobalLease<G> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ self.global.downcast_mut().unwrap()
+ }
+ }
+
pub(crate) struct AnyDrag {
pub drag_handle_view: AnyView,
pub cursor_offset: Point<Pixels>,
diff --cc crates/gpui2/src/app/model_context.rs
index 35237d91cf6,d3520bfdd68..faf766ea7f5
--- a/crates/gpui2/src/app/model_context.rs
+++ b/crates/gpui2/src/app/model_context.rs
@@@ -5,10 -5,8 +5,9 @@@ use crate::
use derive_more::{Deref, DerefMut};
use futures::FutureExt;
use std::{
- any::TypeId,
+ any::{Any, TypeId},
+ borrow::{Borrow, BorrowMut},
future::Future,
- marker::PhantomData,
};
#[derive(Deref, DerefMut)]
@@@ -179,24 -196,16 +197,28 @@@ impl<'a, T: 'static> ModelContext<'a, T
let this = self.weak_handle();
self.app.spawn(|cx| f(this, cx))
}
+
+ pub fn spawn_on_main<Fut, R>(
+ &self,
+ f: impl FnOnce(WeakHandle<T>, MainThread<AsyncAppContext>) -> Fut + Send + 'static,
+ ) -> Task<R>
+ where
+ Fut: Future<Output = R> + 'static,
+ R: Send + 'static,
+ {
+ let this = self.weak_handle();
+ self.app.spawn_on_main(|cx| f(this, cx))
+ }
}
- impl<'a, T: EventEmitter + Send + Sync + 'static> ModelContext<'a, T> {
+ impl<'a, T> ModelContext<'a, T>
+ where
+ T: EventEmitter,
+ T::Event: Send + Sync,
+ {
pub fn emit(&mut self, event: T::Event) {
self.app.pending_effects.push_back(Effect::Emit {
- emitter: self.entity_id,
+ emitter: self.model_state.entity_id,
event: Box::new(event),
});
}
diff --cc crates/gpui2/src/gpui2.rs
index 95b96c18682,c742c0f358e..ef95fdfdf20
--- a/crates/gpui2/src/gpui2.rs
+++ b/crates/gpui2/src/gpui2.rs
@@@ -162,8 -158,8 +167,8 @@@ wher
result
}
- fn set_global<T: Send + Sync + 'static>(&mut self, global: T) {
- fn set_global<G: Any + Send + Sync>(&mut self, global: G) {
- self.app_mut().set_global(global)
++ fn set_global<G: 'static + Send + Sync>(&mut self, global: G) {
+ self.borrow_mut().set_global(global)
}
}
diff --cc crates/gpui2/src/window.rs
index fcc49d2ef36,a7d5ee194fa..c6eff3590d1
--- a/crates/gpui2/src/window.rs
+++ b/crates/gpui2/src/window.rs
@@@ -1138,14 -1122,23 +1138,17 @@@ impl<'a, 'w> WindowContext<'a, 'w>
}
}
-impl<'a, 'w> MainThread<WindowContext<'a, 'w>> {
- fn platform(&self) -> &dyn Platform {
- self.platform.borrow_on_main_thread()
- }
-}
-
impl Context for WindowContext<'_, '_> {
- type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
+ type EntityContext<'a, 'w, T> = ViewContext<'a, 'w, T>;
type Result<T> = T;
- fn entity<T: Send + Sync + 'static>(
+ fn entity<T>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
- ) -> Handle<T> {
+ ) -> Handle<T>
+ where
+ T: Any + Send + Sync,
+ {
let slot = self.app.entities.reserve();
let entity = build_entity(&mut ViewContext::mutable(
&mut *self.app,
@@@ -1346,22 -1327,13 +1355,21 @@@ impl BorrowMut<Window> for WindowContex
}
}
+impl<T> BorrowWindow for T where T: BorrowMut<AppContext> + BorrowMut<Window> {}
+
pub struct ViewContext<'a, 'w, V> {
window_cx: WindowContext<'a, 'w>,
- entity_type: PhantomData<V>,
- entity_id: EntityId,
+ view_state: WeakHandle<V>,
}
-impl<V> BorrowAppContext for ViewContext<'_, '_, V> {
- fn app_mut(&mut self) -> &mut AppContext {
+impl<V> Borrow<AppContext> for ViewContext<'_, '_, V> {
+ fn borrow(&self) -> &AppContext {
+ &*self.window_cx.app
+ }
+}
+
+impl<V> BorrowMut<AppContext> for ViewContext<'_, '_, V> {
+ fn borrow_mut(&mut self) -> &mut AppContext {
&mut *self.window_cx.app
}
}