This commit is contained in:
Antonio Scandurra 2023-10-04 20:09:55 +02:00
parent d28c81571c
commit ebc80597d5
2 changed files with 33 additions and 7 deletions

View file

@ -225,6 +225,18 @@ impl AppContext {
}) })
} }
pub fn spawn<Fut, R>(&self, f: impl FnOnce(&mut AppContext) -> Fut + Send + 'static) -> Task<R>
where
Fut: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
let this = self.this.upgrade().unwrap();
self.executor.spawn(async move {
let future = f(&mut this.lock());
future.await
})
}
pub fn text_system(&self) -> &Arc<TextSystem> { pub fn text_system(&self) -> &Arc<TextSystem> {
&self.text_system &self.text_system
} }

View file

@ -1,14 +1,14 @@
use crate::{ use crate::{
image_cache::RenderImageParams, px, AnyView, AppContext, AvailableSpace, BorrowAppContext, image_cache::RenderImageParams, px, AnyView, AppContext, AsyncContext, 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;
use std::{any::TypeId, borrow::Cow, marker::PhantomData, mem, sync::Arc}; use std::{any::TypeId, borrow::Cow, future::Future, marker::PhantomData, mem, sync::Arc};
use util::ResultExt; use util::ResultExt;
pub struct AnyWindow {} pub struct AnyWindow {}
@ -601,6 +601,20 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
} }
} }
pub fn spawn<Fut, R>(
&mut self,
f: impl FnOnce(&mut S, &mut ViewContext<'_, '_, S>) -> Fut + Send + 'static,
) -> Task<Result<R>>
where
Fut: Future<Output = R> + Send + 'static,
{
let handle = self.handle();
todo!()
// self.window_cx.spawn(|cx| {
// f
// })
}
pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R { pub(crate) fn erase_state<R>(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {
let entity_id = self.unit_entity.id; let entity_id = self.unit_entity.id;
let mut cx = ViewContext::mutable( let mut cx = ViewContext::mutable(