Checkpoint

This commit is contained in:
Nathan Sobo 2023-10-04 15:05:04 -06:00
parent e68b24f839
commit 02d6b91b73
7 changed files with 156 additions and 98 deletions

View file

@ -29,7 +29,7 @@ use util::{
}; };
#[derive(Clone)] #[derive(Clone)]
pub struct App(Arc<Mutex<MainThread<AppContext>>>); pub struct App(Arc<Mutex<AppContext>>);
impl App { impl App {
pub fn production(asset_source: Arc<dyn AssetSource>) -> Self { pub fn production(asset_source: Arc<dyn AssetSource>) -> Self {
@ -53,9 +53,9 @@ impl App {
let executor = platform.executor(); let executor = platform.executor();
let text_system = Arc::new(TextSystem::new(platform.text_system())); let text_system = Arc::new(TextSystem::new(platform.text_system()));
let entities = EntityMap::new(); let entities = EntityMap::new();
let unit_entity = entities.redeem(entities.reserve(), ()); let unit_entity = entities.insert(entities.reserve(), ());
Self(Arc::new_cyclic(|this| { Self(Arc::new_cyclic(|this| {
Mutex::new(MainThread::new(AppContext { Mutex::new(AppContext {
this: this.clone(), this: this.clone(),
platform: MainThreadOnly::new(platform, executor.clone()), platform: MainThreadOnly::new(platform, executor.clone()),
executor, executor,
@ -71,7 +71,7 @@ impl App {
pending_effects: Default::default(), pending_effects: Default::default(),
observers: Default::default(), observers: Default::default(),
layout_id_buffer: Default::default(), layout_id_buffer: Default::default(),
})) })
})) }))
} }
@ -83,6 +83,7 @@ impl App {
let platform = self.0.lock().platform.clone(); let platform = self.0.lock().platform.clone();
platform.borrow_on_main_thread().run(Box::new(move || { platform.borrow_on_main_thread().run(Box::new(move || {
let cx = &mut *this.0.lock(); let cx = &mut *this.0.lock();
let cx = unsafe { mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx) };
on_finish_launching(cx); on_finish_launching(cx);
})); }));
} }
@ -91,7 +92,7 @@ impl App {
type Handlers = SmallVec<[Arc<dyn Fn(&mut AppContext) -> bool + Send + Sync + 'static>; 2]>; type Handlers = SmallVec<[Arc<dyn Fn(&mut AppContext) -> bool + Send + Sync + 'static>; 2]>;
pub struct AppContext { pub struct AppContext {
this: Weak<Mutex<MainThread<AppContext>>>, this: Weak<Mutex<AppContext>>,
platform: MainThreadOnly<dyn Platform>, platform: MainThreadOnly<dyn Platform>,
text_system: Arc<TextSystem>, text_system: Arc<TextSystem>,
pending_updates: usize, pending_updates: usize,
@ -109,7 +110,7 @@ pub struct AppContext {
} }
impl AppContext { impl AppContext {
fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R { pub(crate) fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
self.pending_updates += 1; self.pending_updates += 1;
let result = update(self); let result = update(self);
if self.pending_updates == 1 { if self.pending_updates == 1 {
@ -144,6 +145,7 @@ impl AppContext {
} }
fn flush_effects(&mut self) { fn flush_effects(&mut self) {
dbg!("flush effects");
while let Some(effect) = self.pending_effects.pop_front() { while let Some(effect) = self.pending_effects.pop_front() {
match effect { match effect {
Effect::Notify(entity_id) => self.apply_notify_effect(entity_id), Effect::Notify(entity_id) => self.apply_notify_effect(entity_id),
@ -163,6 +165,8 @@ impl AppContext {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
dbg!(&dirty_window_ids);
for dirty_window_id in dirty_window_ids { for dirty_window_id in dirty_window_ids {
self.update_window(dirty_window_id, |cx| cx.draw()) self.update_window(dirty_window_id, |cx| cx.draw())
.unwrap() .unwrap()
@ -180,8 +184,8 @@ impl AppContext {
} }
} }
pub fn to_async(&self) -> AsyncContext { pub fn to_async(&self) -> AsyncAppContext {
AsyncContext(unsafe { mem::transmute(self.this.clone()) }) AsyncAppContext(unsafe { mem::transmute(self.this.clone()) })
} }
pub fn executor(&self) -> &Executor { pub fn executor(&self) -> &Executor {
@ -213,7 +217,7 @@ impl AppContext {
f: impl FnOnce(&mut MainThread<AppContext>) -> F + Send + 'static, f: impl FnOnce(&mut MainThread<AppContext>) -> F + Send + 'static,
) -> Task<R> ) -> Task<R>
where where
F: Future<Output = R> + 'static, F: Future<Output = R> + Send + 'static,
R: Send + 'static, R: Send + 'static,
{ {
let this = self.this.upgrade().unwrap(); let this = self.this.upgrade().unwrap();
@ -225,14 +229,14 @@ impl AppContext {
}) })
} }
pub fn spawn<Fut, R>(&self, f: impl FnOnce(&mut AppContext) -> Fut + Send + 'static) -> Task<R> pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
where where
Fut: Future<Output = R> + Send + 'static, Fut: Future<Output = R> + Send + 'static,
R: Send + 'static, R: Send + 'static,
{ {
let this = self.this.upgrade().unwrap(); let cx = self.to_async();
self.executor.spawn(async move { self.executor.spawn(async move {
let future = f(&mut this.lock()); let future = f(cx);
future.await future.await
}) })
} }
@ -298,9 +302,11 @@ impl Context for AppContext {
&mut self, &mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T, build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Handle<T> { ) -> Handle<T> {
let slot = self.entities.reserve(); self.update(|cx| {
let entity = build_entity(&mut ModelContext::mutable(self, slot.id)); let slot = cx.entities.reserve();
self.entities.redeem(slot, entity) let entity = build_entity(&mut ModelContext::mutable(cx, slot.id));
cx.entities.insert(slot, entity)
})
} }
fn update_entity<T: Send + Sync + 'static, R>( fn update_entity<T: Send + Sync + 'static, R>(
@ -308,10 +314,12 @@ impl Context for AppContext {
handle: &Handle<T>, handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R, update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
) -> R { ) -> R {
let mut entity = self.entities.lease(handle); self.update(|cx| {
let result = update(&mut *entity, &mut ModelContext::mutable(self, handle.id)); let mut entity = cx.entities.lease(handle);
self.entities.end_lease(entity); let result = update(&mut entity, &mut ModelContext::mutable(cx, handle.id));
result cx.entities.end_lease(entity);
result
})
} }
} }

View file

@ -1,13 +1,15 @@
use crate::{AnyWindowHandle, AppContext, Context, Handle, ModelContext, Result, WindowContext}; use crate::{
AnyWindowHandle, AppContext, Context, Handle, ModelContext, Result, ViewContext, WindowContext,
};
use anyhow::anyhow; use anyhow::anyhow;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::Weak; use std::sync::Weak;
#[derive(Clone)] #[derive(Clone)]
pub struct AsyncContext(pub(crate) Weak<Mutex<AppContext>>); pub struct AsyncAppContext(pub(crate) Weak<Mutex<AppContext>>);
impl Context for AsyncContext { impl Context for AsyncAppContext {
type EntityContext<'a, 'b, T: 'static + Send + Sync> = ModelContext<'a, T>; type EntityContext<'a, 'w, T: 'static + Send + Sync> = ModelContext<'a, T>;
type Result<T> = Result<T>; type Result<T> = Result<T>;
fn entity<T: Send + Sync + 'static>( fn entity<T: Send + Sync + 'static>(
@ -18,7 +20,7 @@ impl Context for AsyncContext {
.0 .0
.upgrade() .upgrade()
.ok_or_else(|| anyhow!("app was released"))?; .ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock(); let mut lock = app.lock(); // Does not compile without this variable.
Ok(lock.entity(build_entity)) Ok(lock.entity(build_entity))
} }
@ -31,17 +33,17 @@ impl Context for AsyncContext {
.0 .0
.upgrade() .upgrade()
.ok_or_else(|| anyhow!("app was released"))?; .ok_or_else(|| anyhow!("app was released"))?;
let mut lock = app.lock(); let mut lock = app.lock(); // Does not compile without this variable.
Ok(lock.update_entity(handle, update)) Ok(lock.update_entity(handle, update))
} }
} }
impl AsyncContext { impl AsyncAppContext {
pub fn update_window<T>( pub fn update_window<R>(
&self, &self,
handle: AnyWindowHandle, handle: AnyWindowHandle,
update: impl FnOnce(&mut WindowContext) -> T + Send + Sync, update: impl FnOnce(&mut WindowContext) -> R,
) -> Result<T> { ) -> Result<R> {
let app = self let app = self
.0 .0
.upgrade() .upgrade()
@ -50,3 +52,37 @@ impl AsyncContext {
app_context.update_window(handle.id, update) app_context.update_window(handle.id, update)
} }
} }
#[derive(Clone)]
pub struct AsyncWindowContext {
app: AsyncAppContext,
window: AnyWindowHandle,
}
impl AsyncWindowContext {
pub fn new(app: AsyncAppContext, window: AnyWindowHandle) -> Self {
Self { app, window }
}
}
impl Context for AsyncWindowContext {
type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
type Result<T> = Result<T>;
fn entity<R: Send + Sync + 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, R>) -> R,
) -> Result<Handle<R>> {
self.app
.update_window(self.window, |cx| cx.entity(build_entity))
}
fn update_entity<T: Send + Sync + 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
) -> Result<R> {
self.app
.update_window(self.window, |cx| cx.update_entity(handle, update))
}
}

View file

@ -14,14 +14,6 @@ use std::{
slotmap::new_key_type! { pub struct EntityId; } slotmap::new_key_type! { pub struct EntityId; }
#[derive(Deref, DerefMut)]
pub struct Lease<T> {
#[deref]
#[deref_mut]
entity: Box<T>,
pub id: EntityId,
}
pub(crate) struct EntityMap { pub(crate) struct EntityMap {
ref_counts: Arc<RwLock<RefCounts>>, ref_counts: Arc<RwLock<RefCounts>>,
entities: Arc<Mutex<SecondaryMap<EntityId, Box<dyn Any + Send + Sync>>>>, entities: Arc<Mutex<SecondaryMap<EntityId, Box<dyn Any + Send + Sync>>>>,
@ -35,31 +27,38 @@ impl EntityMap {
} }
} }
/// Reserve a slot for an entity, which you can subsequently use with `insert`.
pub fn reserve<T: 'static + Send + Sync>(&self) -> Slot<T> { pub fn reserve<T: 'static + Send + Sync>(&self) -> Slot<T> {
let id = self.ref_counts.write().insert(1.into()); let id = self.ref_counts.write().insert(1.into());
Slot(Handle::new(id, Arc::downgrade(&self.ref_counts))) Slot(Handle::new(id, Arc::downgrade(&self.ref_counts)))
} }
pub fn redeem<T: 'static + Any + Send + Sync>(&self, slot: Slot<T>, entity: T) -> Handle<T> { /// Insert an entity into a slot obtained by calling `reserve`.
pub fn insert<T: 'static + Any + Send + Sync>(&self, slot: Slot<T>, entity: T) -> Handle<T> {
let handle = slot.0; let handle = slot.0;
self.entities.lock().insert(handle.id, Box::new(entity)); self.entities.lock().insert(handle.id, Box::new(entity));
handle handle
} }
/// Move an entity to the stack.
pub fn lease<T: 'static + Send + Sync>(&self, handle: &Handle<T>) -> Lease<T> { pub fn lease<T: 'static + Send + Sync>(&self, handle: &Handle<T>) -> Lease<T> {
let id = handle.id; let id = handle.id;
let entity = self let entity = Some(
.entities self.entities
.lock() .lock()
.remove(id) .remove(id)
.expect("Circular entity lease. Is the entity already being updated?") .expect("Circular entity lease. Is the entity already being updated?")
.downcast::<T>() .downcast::<T>()
.unwrap(); .unwrap(),
);
Lease { id, entity } Lease { id, entity }
} }
pub fn end_lease<T: 'static + Send + Sync>(&mut self, lease: Lease<T>) { /// Return an entity after moving it to the stack.
self.entities.lock().insert(lease.id, lease.entity); pub fn end_lease<T: 'static + Send + Sync>(&mut self, mut lease: Lease<T>) {
self.entities
.lock()
.insert(lease.id, lease.entity.take().unwrap());
} }
pub fn weak_handle<T: 'static + Send + Sync>(&self, id: EntityId) -> WeakHandle<T> { pub fn weak_handle<T: 'static + Send + Sync>(&self, id: EntityId) -> WeakHandle<T> {
@ -71,6 +70,34 @@ impl EntityMap {
} }
} }
pub struct Lease<T> {
entity: Option<Box<T>>,
pub id: EntityId,
}
impl<T> core::ops::Deref for Lease<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.entity.as_ref().unwrap()
}
}
impl<T> core::ops::DerefMut for Lease<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.entity.as_mut().unwrap()
}
}
impl<T> Drop for Lease<T> {
fn drop(&mut self) {
assert!(
self.entity.is_none(),
"Leases must be ended with EntityMap::end_lease"
);
}
}
#[derive(Deref, DerefMut)] #[derive(Deref, DerefMut)]
pub struct Slot<T: Send + Sync + 'static>(Handle<T>); pub struct Slot<T: Send + Sync + 'static>(Handle<T>);

View file

@ -35,7 +35,7 @@ impl<S> Img<S> {
} }
} }
impl<S: 'static> Element for Img<S> { impl<S: Send + Sync + 'static> Element for Img<S> {
type State = S; type State = S;
type FrameState = (); type FrameState = ();
@ -75,16 +75,18 @@ impl<S: 'static> Element for Img<S> {
let corner_radii = style.corner_radii.to_pixels(bounds, cx.rem_size()); let corner_radii = style.corner_radii.to_pixels(bounds, cx.rem_size());
cx.paint_image(bounds, corner_radii, order, data, self.grayscale)?; cx.paint_image(bounds, corner_radii, order, data, self.grayscale)?;
} else { } else {
log::warn!("image not loaded yet"); dbg!("not loaded");
cx.spawn(|cx| async move { cx.spawn(|view, mut cx| async move {
dbg!("awaiting image future");
if image_future.await.log_err().is_some() { if image_future.await.log_err().is_some() {
// this.update(&mut cx, |_, cx| cx.notify()).ok(); view.update(&mut cx, |_, cx| {
dbg!("image future loaded");
cx.notify();
})
.ok();
} }
}) })
.detach() .detach()
// cx.spawn(|this, mut cx| async move {
// })
// .detach();
} }
} }
Ok(()) Ok(())

View file

@ -76,14 +76,13 @@ impl Executor {
/// closure returns a future which will be run to completion on the main thread. /// closure returns a future which will be run to completion on the main thread.
pub fn spawn_on_main<F, R>(&self, func: impl FnOnce() -> F + Send + 'static) -> Task<R> pub fn spawn_on_main<F, R>(&self, func: impl FnOnce() -> F + Send + 'static) -> Task<R>
where where
F: Future<Output = R> + 'static, F: Future<Output = R> + Send + 'static,
R: Send + 'static, R: Send + 'static,
{ {
let dispatcher = self.dispatcher.clone(); let dispatcher = self.dispatcher.clone();
let (runnable, task) = let (runnable, task) = async_task::spawn(async move { func().await }, move |runnable| {
async_task::spawn_local(async move { func().await }, move |runnable| { dispatcher.dispatch_on_main_thread(runnable)
dispatcher.dispatch_on_main_thread(runnable) });
});
runnable.schedule(); runnable.schedule();
Task::Spawned(task) Task::Spawned(task)
} }

View file

@ -69,12 +69,6 @@ pub trait Context {
#[repr(transparent)] #[repr(transparent)]
pub struct MainThread<T>(T); pub struct MainThread<T>(T);
impl<T> MainThread<T> {
fn new(value: T) -> Self {
Self(value)
}
}
impl<T> Deref for MainThread<T> { impl<T> Deref for MainThread<T> {
type Target = T; type Target = T;

View file

@ -1,10 +1,10 @@
use crate::{ use crate::{
image_cache::RenderImageParams, px, AnyView, AppContext, AvailableSpace, BorrowAppContext, image_cache::RenderImageParams, px, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
Bounds, Context, Corners, DevicePixels, Effect, Element, EntityId, FontId, GlyphId, Handle, BorrowAppContext, Bounds, Context, Corners, DevicePixels, Effect, Element, EntityId, FontId,
Hsla, ImageData, IsZero, LayerId, LayoutId, MainThread, MainThreadOnly, MonochromeSprite, GlyphId, Handle, Hsla, ImageData, IsZero, LayerId, LayoutId, MainThread, MainThreadOnly,
Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Reference, RenderGlyphParams, MonochromeSprite, Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Reference,
RenderSvgParams, ScaledPixels, Scene, SharedString, Size, Style, TaffyLayoutEngine, Task, RenderGlyphParams, RenderSvgParams, ScaledPixels, Scene, SharedString, Size, Style,
WeakHandle, WindowOptions, SUBPIXEL_VARIANTS, TaffyLayoutEngine, Task, WeakHandle, WindowOptions, SUBPIXEL_VARIANTS,
}; };
use anyhow::Result; use anyhow::Result;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -109,6 +109,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
} }
pub fn notify(&mut self) { pub fn notify(&mut self) {
dbg!("ViewContext::notify");
self.window.dirty = true; self.window.dirty = true;
} }
@ -129,33 +130,23 @@ impl<'a, 'w> WindowContext<'a, 'w> {
} }
} }
pub fn to_async(&self) -> AsyncWindowContext {
AsyncWindowContext::new(self.app.to_async(), self.window.handle)
}
pub fn spawn<Fut, R>( pub fn spawn<Fut, R>(
&mut self, &mut self,
f: impl FnOnce(&mut WindowContext<'_, '_>) -> Fut + Send + 'static, f: impl FnOnce(AnyWindowHandle, AsyncWindowContext) -> Fut + Send + 'static,
) -> Task<Result<R>> ) -> Task<R>
where where
R: Send + 'static, R: Send + 'static,
Fut: Future<Output = R> + Send + 'static, Fut: Future<Output = R> + Send + 'static,
{ {
let id = self.window.handle.id; let window = self.window.handle;
self.app.spawn(move |cx| { self.app.spawn(move |app| {
let future = cx.update_window(id, f); let cx = AsyncWindowContext::new(app, window);
async move { Ok(future?.await) } let future = f(window, cx);
}) async move { future.await }
}
pub fn try_spawn<Fut, R>(
&mut self,
f: impl FnOnce(&mut WindowContext<'_, '_>) -> Fut + Send + 'static,
) -> Task<Result<R>>
where
R: Send + 'static,
Fut: Future<Output = Result<R>> + Send + 'static,
{
let id = self.window.handle.id;
self.app.spawn(move |cx| {
let future = cx.update_window(id, f);
async move { future?.await }
}) })
} }
@ -448,7 +439,7 @@ impl Context for WindowContext<'_, '_> {
&mut self.window, &mut self.window,
slot.id, slot.id,
)); ));
self.entities.redeem(slot, entity) self.entities.insert(slot, entity)
} }
fn update_entity<T: Send + Sync + 'static, R>( fn update_entity<T: Send + Sync + 'static, R>(
@ -607,6 +598,7 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
} }
pub fn notify(&mut self) { pub fn notify(&mut self) {
dbg!("ViewContext::notify");
self.window_cx.notify(); self.window_cx.notify();
self.window_cx self.window_cx
.app .app
@ -633,16 +625,16 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
pub fn spawn<Fut, R>( pub fn spawn<Fut, R>(
&mut self, &mut self,
f: impl FnOnce(&mut S, &mut ViewContext<'_, '_, S>) -> Fut + Send + 'static, f: impl FnOnce(WeakHandle<S>, AsyncWindowContext) -> Fut + Send + 'static,
) -> Task<Result<R>> ) -> Task<R>
where where
R: Send + 'static, R: Send + 'static,
Fut: Future<Output = R> + Send + 'static, Fut: Future<Output = R> + Send + 'static,
{ {
let handle = self.handle(); let handle = self.handle();
self.window_cx.try_spawn(move |cx| { self.window_cx.spawn(move |_, cx| {
let result = handle.update(cx, f); let result = f(handle, cx);
async move { Ok(result?.await) } async move { result.await }
}) })
} }