diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index 39603f96f0..dbc68f7760 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -120,40 +120,40 @@ impl AppContext { AsyncContext(self.this.clone()) } - pub fn run_on_main( - &self, - f: impl FnOnce(&mut AppContext) -> R + Send + 'static, - ) -> impl Future - where - R: Send + 'static, - { - let this = self.this.upgrade().unwrap(); - run_on_main(self.dispatcher.clone(), move || { - let cx = &mut *this.lock(); - let main_thread_cx: &mut AppContext = unsafe { std::mem::transmute(cx) }; - main_thread_cx.update(|cx| f(cx)) - }) - } + // pub fn run_on_main( + // &self, + // f: impl FnOnce(&mut AppContext) -> R + Send + 'static, + // ) -> impl Future + // where + // R: Send + 'static, + // { + // let this = self.this.upgrade().unwrap(); + // run_on_main(self.dispatcher.clone(), move || { + // let cx = &mut *this.lock(); + // let main_thread_cx: &mut AppContext = unsafe { std::mem::transmute(cx) }; + // main_thread_cx.update(|cx| f(cx)) + // }) + // } - pub fn spawn_on_main( - &self, - f: impl FnOnce(&mut AppContext) -> F + Send + 'static, - ) -> impl Future - where - F: Future + 'static, - R: Send + 'static, - { - let this = self.this.upgrade().unwrap(); - spawn_on_main(self.dispatcher.clone(), move || { - let cx = &mut *this.lock(); - let platform = cx.platform.borrow_on_main_thread().clone(); - // todo!() - // cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref()))) + // pub fn spawn_on_main( + // &self, + // f: impl FnOnce(&mut AppContext) -> F + Send + 'static, + // ) -> impl Future + // where + // F: Future + 'static, + // R: Send + 'static, + // { + // let this = self.this.upgrade().unwrap(); + // spawn_on_main(self.dispatcher.clone(), move || { + // let cx = &mut *this.lock(); + // let platform = cx.platform.borrow_on_main_thread().clone(); + // // todo!() + // // cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref()))) - future::ready(()) - }) - // self.platform.read(move |platform| { - } + // future::ready(()) + // }) + // // self.platform.read(move |platform| { + // } pub fn text_style(&self) -> TextStyle { let mut style = TextStyle::default(); @@ -276,8 +276,8 @@ impl AppContext { } } -impl Context for AppContext { - type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T>; +impl Context for AppContext { + type EntityContext<'a, 'w, T: Send + Sync + 'static> = ModelContext<'a, T, Thread>; type Result = T; fn entity( diff --git a/crates/gpui3/src/app/model_context.rs b/crates/gpui3/src/app/model_context.rs index b16d29057a..95e7866493 100644 --- a/crates/gpui3/src/app/model_context.rs +++ b/crates/gpui3/src/app/model_context.rs @@ -1,14 +1,14 @@ use crate::{AppContext, Context, Effect, EntityId, Handle, Reference, WeakHandle}; use std::{marker::PhantomData, sync::Arc}; -pub struct ModelContext<'a, T> { - app: Reference<'a, AppContext>, +pub struct ModelContext<'a, T, Thread = ()> { + app: Reference<'a, AppContext>, entity_type: PhantomData, entity_id: EntityId, } -impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> { - pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self { +impl<'a, T: Send + Sync + 'static, Thread> ModelContext<'a, T, Thread> { + pub(crate) fn mutable(app: &'a mut AppContext, entity_id: EntityId) -> Self { Self { app: Reference::Mutable(app), entity_type: PhantomData, @@ -41,7 +41,7 @@ impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> { pub fn observe( &mut self, handle: &Handle, - on_notify: impl Fn(&mut T, Handle, &mut ModelContext<'_, T>) + Send + Sync + 'static, + on_notify: impl Fn(&mut T, Handle, &mut ModelContext<'_, T, Thread>) + Send + Sync + 'static, ) { let this = self.handle(); let handle = handle.downgrade(); diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index c145a9725b..0acfa07ce8 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -5,18 +5,18 @@ pub trait Element: 'static { type State; type FrameState; - fn layout( + fn layout( &mut self, state: &mut Self::State, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::FrameState)>; - fn paint( + fn paint( &mut self, layout: Layout, state: &mut Self::State, frame_state: &mut Self::FrameState, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()>; } @@ -42,12 +42,16 @@ pub trait ParentElement { } trait ElementObject { - fn layout(&mut self, state: &mut S, cx: &mut ViewContext) -> Result; - fn paint( + fn layout( + &mut self, + state: &mut S, + cx: &mut ViewContext, + ) -> Result; + fn paint( &mut self, state: &mut S, offset: Option>, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()>; } @@ -135,15 +139,19 @@ impl ElementObject for RenderedElement { pub struct AnyElement(Box>); impl AnyElement { - pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext) -> Result { + pub fn layout( + &mut self, + state: &mut S, + cx: &mut ViewContext, + ) -> Result { self.0.layout(state, cx) } - pub fn paint( + pub fn paint( &mut self, state: &mut S, offset: Option>, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()> { self.0.paint(state, offset, cx) } diff --git a/crates/gpui3/src/elements/div.rs b/crates/gpui3/src/elements/div.rs index c763619de1..aef32be22c 100644 --- a/crates/gpui3/src/elements/div.rs +++ b/crates/gpui3/src/elements/div.rs @@ -27,10 +27,10 @@ impl Element for Div { type State = S; type FrameState = Vec; - fn layout( + fn layout( &mut self, view: &mut S, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<(LayoutId, Self::FrameState)> { let style = self.computed_style(); let child_layout_ids = if let Some(text_style) = style.text_style(cx) { @@ -45,12 +45,12 @@ impl Element for Div { )) } - fn paint( + fn paint( &mut self, layout: Layout, state: &mut S, child_layouts: &mut Self::FrameState, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()> { let Layout { order, bounds } = layout; @@ -130,18 +130,22 @@ impl Div { offset } - fn layout_children(&mut self, view: &mut S, cx: &mut ViewContext) -> Result> { + fn layout_children( + &mut self, + view: &mut S, + cx: &mut ViewContext, + ) -> Result> { self.children .iter_mut() .map(|child| child.layout(view, cx)) .collect::>>() } - fn paint_children( + fn paint_children( &mut self, overflow: &Point, state: &mut S, - cx: &mut ViewContext, + cx: &mut ViewContext, ) -> Result<()> { let scroll_offset = self.scroll_offset(overflow); for child in &mut self.children { @@ -150,13 +154,13 @@ impl Div { Ok(()) } - fn handle_scroll( + fn handle_scroll( &mut self, _order: u32, bounds: Bounds, overflow: Point, child_layout_ids: &[LayoutId], - cx: &mut ViewContext, + cx: &mut ViewContext, ) { if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll { let mut scroll_max = Point::default(); diff --git a/crates/gpui3/src/style.rs b/crates/gpui3/src/style.rs index a8bac7ed78..b319b46075 100644 --- a/crates/gpui3/src/style.rs +++ b/crates/gpui3/src/style.rs @@ -171,7 +171,7 @@ pub struct HighlightStyle { impl Eq for HighlightStyle {} impl Style { - pub fn text_style(&self, _cx: &WindowContext) -> Option<&TextStyleRefinement> { + pub fn text_style(&self, _cx: &WindowContext) -> Option<&TextStyleRefinement> { if self.text.is_some() { Some(&self.text) } else { @@ -180,7 +180,11 @@ impl Style { } /// Paints the background of an element styled with this style. - pub fn paint_background(&self, _bounds: Bounds, cx: &mut ViewContext) { + pub fn paint_background( + &self, + _bounds: Bounds, + cx: &mut ViewContext, + ) { let _rem_size = cx.rem_size(); if let Some(_color) = self.fill.as_ref().and_then(Fill::color) { todo!(); @@ -188,7 +192,11 @@ impl Style { } /// Paints the foreground of an element styled with this style. - pub fn paint_foreground(&self, _bounds: Bounds, cx: &mut ViewContext) { + pub fn paint_foreground( + &self, + _bounds: Bounds, + cx: &mut ViewContext, + ) { let rem_size = cx.rem_size(); if let Some(_color) = self.border_color { diff --git a/crates/gpui3/src/view.rs b/crates/gpui3/src/view.rs index 86826606bc..261505ce51 100644 --- a/crates/gpui3/src/view.rs +++ b/crates/gpui3/src/view.rs @@ -1,14 +1,15 @@ use parking_lot::Mutex; use crate::{ - AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, Result, ViewContext, + AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, MainThread, Result, ViewContext, WindowContext, }; use std::{any::Any, marker::PhantomData, sync::Arc}; -pub struct View { +pub struct View { state: Handle, - render: Arc) -> AnyElement + Send + Sync + 'static>, + render: + Arc) -> AnyElement + Send + Sync + 'static>, parent_state_type: PhantomData

, } diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 6309cbc54e..ea81d1b815 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -63,12 +63,12 @@ impl Window { } #[derive(Deref, DerefMut)] -pub struct WindowContext<'a, 'b, Thread = ()> { +pub struct WindowContext<'a, 'w, Thread = ()> { thread: PhantomData, #[deref] #[deref_mut] app: Reference<'a, AppContext>, - window: Reference<'b, Window>, + window: Reference<'w, Window>, } impl<'a, 'w, Thread> WindowContext<'a, 'w, Thread> { @@ -158,8 +158,8 @@ impl WindowContext<'_, '_, MainThread> { } } -impl Context for WindowContext<'_, '_, Thread> { - type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>; +impl Context for WindowContext<'_, '_, Thread> { + type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T, Thread>; type Result = T; fn entity( @@ -167,7 +167,7 @@ impl Context for WindowContext<'_, '_, Thread> { build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T, ) -> Handle { let slot = self.entities.reserve(); - let entity = build_entity(&mut ViewContext::mutable( + let entity = build_entity(&mut ViewContext::<'_, '_, T, Thread>::mutable( &mut *self.app, &mut self.window, slot.id, @@ -191,7 +191,7 @@ impl Context for WindowContext<'_, '_, Thread> { } impl StackContext for ViewContext<'_, '_, S, Thread> { - fn app(&mut self) -> &mut AppContext { + fn app(&mut self) -> &mut AppContext { &mut *self.app } @@ -220,14 +220,18 @@ impl StackContext for ViewContext<'_, '_, S, Thread> { pub struct ViewContext<'a, 'w, S, Thread = ()> { #[deref] #[deref_mut] - window_cx: WindowContext<'a, 'w>, + window_cx: WindowContext<'a, 'w, Thread>, entity_type: PhantomData, entity_id: EntityId, thread: PhantomData, } impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { - fn mutable(app: &'a mut AppContext, window: &'w mut Window, entity_id: EntityId) -> Self { + fn mutable( + app: &'a mut AppContext, + window: &'w mut Window, + entity_id: EntityId, + ) -> Self { Self { window_cx: WindowContext::mutable(app, window), entity_id, @@ -243,7 +247,10 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { pub fn observe( &mut self, handle: &Handle, - on_notify: impl Fn(&mut S, Handle, &mut ViewContext<'_, '_, S>) + Send + Sync + 'static, + on_notify: impl Fn(&mut S, Handle, &mut ViewContext<'_, '_, S, Thread>) + + Send + + Sync + + 'static, ) { let this = self.handle(); let handle = handle.downgrade(); @@ -273,7 +280,10 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { self.window.dirty = true; } - pub(crate) fn erase_state(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R { + pub(crate) fn erase_state( + &mut self, + f: impl FnOnce(&mut ViewContext<(), Thread>) -> R, + ) -> R { let entity_id = self.unit_entity.id; let mut cx = ViewContext::mutable( &mut *self.window_cx.app, @@ -284,8 +294,8 @@ impl<'a, 'w, S: Send + Sync + 'static, Thread> ViewContext<'a, 'w, S, Thread> { } } -impl<'a, 'w, S: 'static, Thread> Context for ViewContext<'a, 'w, S, Thread> { - type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>; +impl<'a, 'w, S: 'static, Thread: 'static> Context for ViewContext<'a, 'w, S, Thread> { + type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U, Thread>; type Result = U; fn entity(