diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index ad8e861b69..a03d141783 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -1,10 +1,12 @@ +mod async_context; mod model_context; +pub use async_context::*; pub use model_context::*; use crate::{ - current_platform, AnyWindowHandle, Context, LayoutId, MainThreadOnly, Platform, RootView, - TextSystem, Window, WindowContext, WindowHandle, WindowId, + current_platform, Context, LayoutId, MainThreadOnly, Platform, RootView, TextSystem, Window, + WindowContext, WindowHandle, WindowId, }; use anyhow::{anyhow, Result}; use collections::{HashMap, VecDeque}; @@ -72,14 +74,13 @@ pub struct AppContext { this: Weak>, platform: MainThreadOnly, text_system: Arc, + pending_updates: usize, pub(crate) unit_entity: Handle<()>, pub(crate) entities: SlotMap>>, pub(crate) windows: SlotMap>, - pending_updates: usize, pub(crate) pending_effects: VecDeque, pub(crate) observers: HashMap, - // We recycle this memory across layout requests. - pub(crate) layout_id_buffer: Vec, + pub(crate) layout_id_buffer: Vec, // We recycle this memory across layout requests. } impl AppContext { @@ -230,54 +231,6 @@ impl Context for AppContext { } } -#[derive(Clone)] -pub struct AsyncContext(Weak>); - -impl Context for AsyncContext { - type EntityContext<'a, 'b, T: Send + Sync + 'static> = ModelContext<'a, T>; - type Result = Result; - - fn entity( - &mut self, - build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T, - ) -> Result> { - let app = self - .0 - .upgrade() - .ok_or_else(|| anyhow!("app was released"))?; - let mut lock = app.lock(); - Ok(lock.entity(build_entity)) - } - - fn update_entity( - &mut self, - handle: &Handle, - update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R, - ) -> Result { - let app = self - .0 - .upgrade() - .ok_or_else(|| anyhow!("app was released"))?; - let mut lock = app.lock(); - Ok(lock.update_entity(handle, update)) - } -} - -impl AsyncContext { - pub fn update_window( - &self, - handle: AnyWindowHandle, - update: impl FnOnce(&mut WindowContext) -> T + Send + Sync, - ) -> Result { - let app = self - .0 - .upgrade() - .ok_or_else(|| anyhow!("app was released"))?; - let mut app_context = app.lock(); - app_context.update_window(handle.id, update) - } -} - slotmap::new_key_type! { pub struct EntityId; } pub struct Handle { diff --git a/crates/gpui3/src/app/async_context.rs b/crates/gpui3/src/app/async_context.rs new file mode 100644 index 0000000000..750138cc55 --- /dev/null +++ b/crates/gpui3/src/app/async_context.rs @@ -0,0 +1,52 @@ +use crate::{AnyWindowHandle, AppContext, Context, Handle, ModelContext, Result, WindowContext}; +use anyhow::anyhow; +use parking_lot::Mutex; +use std::sync::Weak; + +#[derive(Clone)] +pub struct AsyncContext(pub(crate) Weak>); + +impl Context for AsyncContext { + type EntityContext<'a, 'b, T: Send + Sync + 'static> = ModelContext<'a, T>; + type Result = Result; + + fn entity( + &mut self, + build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T, + ) -> Result> { + let app = self + .0 + .upgrade() + .ok_or_else(|| anyhow!("app was released"))?; + let mut lock = app.lock(); + Ok(lock.entity(build_entity)) + } + + fn update_entity( + &mut self, + handle: &Handle, + update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R, + ) -> Result { + let app = self + .0 + .upgrade() + .ok_or_else(|| anyhow!("app was released"))?; + let mut lock = app.lock(); + Ok(lock.update_entity(handle, update)) + } +} + +impl AsyncContext { + pub fn update_window( + &self, + handle: AnyWindowHandle, + update: impl FnOnce(&mut WindowContext) -> T + Send + Sync, + ) -> Result { + let app = self + .0 + .upgrade() + .ok_or_else(|| anyhow!("app was released"))?; + let mut app_context = app.lock(); + app_context.update_window(handle.id, update) + } +}