diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index eae798938e6690ffc5e5bb1ebb02950310eda399..4c197b0d89c29693a9f0e33b21a49e78a30b81c2 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -121,6 +121,11 @@ pub trait View: Entity + Sized { } } +pub trait AccessAppContext { + fn read T>(&self, f: F) -> T; + fn update T>(&mut self, f: F) -> T; +} + pub trait ReadModelWith { fn read_model_with( &self, @@ -148,11 +153,11 @@ pub trait UpgradeModelHandle { fn upgrade_any_model_handle(&self, handle: &AnyWeakModelHandle) -> Option; } -pub trait UpgradeViewHandle { - fn upgrade_view_handle(&self, handle: &WeakViewHandle) -> Option>; +// pub trait UpgradeViewHandle { +// fn upgrade_view_handle(&self, handle: &WeakViewHandle) -> Option>; - fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option; -} +// fn upgrade_any_view_handle(&self, handle: &AnyWeakViewHandle) -> Option; +// } pub trait ReadViewWith { fn read_view_with( @@ -179,9 +184,6 @@ pub trait UpdateView { #[derive(Clone)] pub struct App(Rc>); -#[derive(Clone)] -pub struct AsyncAppContext(Rc>); - impl App { pub fn new(asset_source: impl AssetSource) -> Result { let platform = platform::current::platform(); @@ -328,6 +330,9 @@ impl App { } } +#[derive(Clone)] +pub struct AsyncAppContext(Rc>); + impl AsyncAppContext { pub fn spawn(&self, f: F) -> Task where @@ -406,6 +411,16 @@ impl AsyncAppContext { } } +impl AccessAppContext for AsyncAppContext { + fn read T>(&self, f: F) -> T { + self.0.borrow().read(f) + } + + fn update T>(&mut self, f: F) -> T { + self.0.borrow_mut().update(f) + } +} + impl UpdateModel for AsyncAppContext { fn update_model( &mut self, @@ -2089,6 +2104,16 @@ impl AppContext { } } +impl AccessAppContext for AppContext { + fn read T>(&self, f: F) -> T { + f(self) + } + + fn update T>(&mut self, f: F) -> T { + f(self) + } +} + impl UpdateModel for AppContext { fn update_model( &mut self, @@ -3427,6 +3452,16 @@ impl<'a, 'b, V: View> ViewContext<'a, 'b, V> { } } +impl AccessAppContext for ViewContext<'_, '_, V> { + fn read T>(&self, f: F) -> T { + self.window_context.read(f) + } + + fn update T>(&mut self, f: F) -> T { + self.window_context.update(f) + } +} + impl UpgradeModelHandle for ViewContext<'_, '_, V> { fn upgrade_model_handle( &self, @@ -4223,13 +4258,13 @@ impl WeakHandle for WeakViewHandle { } } -impl WeakViewHandle { +impl WeakViewHandle { fn new(window_id: usize, view_id: usize) -> Self { Self { any_handle: AnyWeakViewHandle { window_id, view_id, - view_type: TypeId::of::(), + view_type: TypeId::of::(), }, view_type: PhantomData, } @@ -4247,8 +4282,20 @@ impl WeakViewHandle { self.any_handle } - pub fn upgrade(&self, cx: &impl UpgradeViewHandle) -> Option> { - cx.upgrade_view_handle(self) + pub fn upgrade(&self, cx: &impl AccessAppContext) -> Option> { + cx.read(|cx| cx.upgrade_view_handle(self)) + } + + pub fn update( + &self, + cx: &mut impl AccessAppContext, + update: impl FnOnce(&mut V, &mut ViewContext) -> T, + ) -> Option { + cx.update(|cx| { + let handle = cx.upgrade_view_handle(self)?; + + cx.update_window(self.window_id, |cx| handle.update(cx, update)) + }) } } diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index 33405e91631449b5f9b13e182df08fe62072184a..0738797f4b49fa73081427a0122bf5a2350d6ea5 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -13,11 +13,11 @@ use crate::{ }, text_layout::TextLayoutCache, util::post_inc, - Action, AnyModelHandle, AnyView, AnyViewHandle, AnyWeakModelHandle, AnyWeakViewHandle, - AppContext, Effect, Element, Entity, Handle, ModelContext, ModelHandle, MouseRegion, - MouseRegionId, ParentId, SceneBuilder, Subscription, UpdateModel, UpdateView, - UpgradeModelHandle, UpgradeViewHandle, View, ViewContext, ViewHandle, WeakModelHandle, - WeakViewHandle, WindowInvalidation, + AccessAppContext, Action, AnyModelHandle, AnyView, AnyViewHandle, AnyWeakModelHandle, + AnyWeakViewHandle, AppContext, Effect, Element, Entity, Handle, ModelContext, ModelHandle, + MouseRegion, MouseRegionId, ParentId, SceneBuilder, Subscription, UpdateModel, UpdateView, + UpgradeModelHandle, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle, + WindowInvalidation, }; use anyhow::{anyhow, bail, Result}; use collections::{HashMap, HashSet}; @@ -133,6 +133,16 @@ impl DerefMut for WindowContext<'_> { } } +impl AccessAppContext for WindowContext<'_> { + fn read T>(&self, f: F) -> T { + self.app_context.read(f) + } + + fn update T>(&mut self, f: F) -> T { + self.app_context.update(f) + } +} + impl UpdateModel for WindowContext<'_> { fn update_model( &mut self,