Checkpoint

This commit is contained in:
Antonio Scandurra 2023-10-04 19:59:24 +02:00
parent dc9a260425
commit d28c81571c
2 changed files with 20 additions and 40 deletions

View file

@ -14,7 +14,7 @@ use crate::{
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use collections::{HashMap, VecDeque}; use collections::{HashMap, VecDeque};
use futures::{channel::oneshot, Future}; use futures::Future;
use parking_lot::Mutex; use parking_lot::Mutex;
use slotmap::SlotMap; use slotmap::SlotMap;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -191,29 +191,21 @@ impl AppContext {
pub fn run_on_main<R>( pub fn run_on_main<R>(
&mut self, &mut self,
f: impl FnOnce(&mut MainThread<AppContext>) -> R + Send + 'static, f: impl FnOnce(&mut MainThread<AppContext>) -> R + Send + 'static,
) -> impl Future<Output = R> ) -> Task<R>
where where
R: Send + 'static, R: Send + 'static,
{ {
let (tx, rx) = oneshot::channel();
if self.executor.is_main_thread() { if self.executor.is_main_thread() {
let _ = tx.send(f(unsafe { Task::ready(f(unsafe {
mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(self) mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(self)
})); }))
} else { } else {
let this = self.this.upgrade().unwrap(); let this = self.this.upgrade().unwrap();
self.executor self.executor.run_on_main(move || {
.run_on_main(move || {
let cx = &mut *this.lock(); let cx = &mut *this.lock();
cx.update(|cx| { cx.update(|cx| f(unsafe { mem::transmute::<&mut Self, &mut MainThread<Self>>(cx) }))
let _ = tx.send(f(unsafe {
mem::transmute::<&mut Self, &mut MainThread<Self>>(cx)
}));
}) })
})
.detach();
} }
async move { rx.await.unwrap() }
} }
pub fn spawn_on_main<F, R>( pub fn spawn_on_main<F, R>(

View file

@ -3,11 +3,10 @@ use crate::{
Bounds, Context, Corners, DevicePixels, Effect, Element, EntityId, FontId, GlyphId, Handle, Bounds, Context, Corners, DevicePixels, Effect, Element, EntityId, FontId, GlyphId, Handle,
Hsla, ImageData, IsZero, LayerId, LayoutId, MainThread, MainThreadOnly, MonochromeSprite, Hsla, ImageData, IsZero, LayerId, LayoutId, MainThread, MainThreadOnly, MonochromeSprite,
Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Reference, RenderGlyphParams, Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Reference, RenderGlyphParams,
RenderSvgParams, ScaledPixels, Scene, SharedString, Size, Style, TaffyLayoutEngine, WeakHandle, RenderSvgParams, ScaledPixels, Scene, SharedString, Size, Style, TaffyLayoutEngine, Task,
WindowOptions, SUBPIXEL_VARIANTS, WeakHandle, WindowOptions, SUBPIXEL_VARIANTS,
}; };
use anyhow::Result; use anyhow::Result;
use futures::{channel::oneshot, Future};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{any::TypeId, borrow::Cow, marker::PhantomData, mem, sync::Arc}; use std::{any::TypeId, borrow::Cow, marker::PhantomData, mem, sync::Arc};
use util::ResultExt; use util::ResultExt;
@ -116,24 +115,18 @@ impl<'a, 'w> WindowContext<'a, 'w> {
fn run_on_main<R>( fn run_on_main<R>(
&mut self, &mut self,
f: impl FnOnce(&mut MainThread<WindowContext<'_, '_>>) -> R + Send + 'static, f: impl FnOnce(&mut MainThread<WindowContext<'_, '_>>) -> R + Send + 'static,
) -> impl Future<Output = R> ) -> Task<Result<R>>
where where
R: Send + 'static, R: Send + 'static,
{ {
let (tx, rx) = oneshot::channel();
if self.executor.is_main_thread() { if self.executor.is_main_thread() {
let _ = tx.send(f(unsafe { Task::ready(Ok(f(unsafe {
mem::transmute::<&mut Self, &mut MainThread<Self>>(self) mem::transmute::<&mut Self, &mut MainThread<Self>>(self)
})); })))
} else { } else {
let id = self.window.handle.id; let id = self.window.handle.id;
let _ = self.app.run_on_main(move |cx| { self.app.run_on_main(move |cx| cx.update_window(id, f))
cx.update_window(id, |cx| {
let _ = tx.send(f(cx));
})
});
} }
async move { rx.await.unwrap() }
} }
pub fn request_layout( pub fn request_layout(
@ -397,13 +390,14 @@ impl<'a, 'w> WindowContext<'a, 'w> {
cx.window.root_view = Some(root_view); cx.window.root_view = Some(root_view);
let scene = cx.window.scene.take(); let scene = cx.window.scene.take();
let _ = cx.run_on_main(view, |_, cx| { cx.run_on_main(view, |_, cx| {
cx.window cx.window
.platform_window .platform_window
.borrow_on_main_thread() .borrow_on_main_thread()
.draw(scene); .draw(scene);
cx.window.dirty = false; cx.window.dirty = false;
}); })
.detach();
Ok(()) Ok(())
}) })
@ -594,23 +588,17 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
&mut self, &mut self,
view: &mut S, view: &mut S,
f: impl FnOnce(&mut S, &mut MainThread<ViewContext<'_, '_, S>>) -> R + Send + 'static, f: impl FnOnce(&mut S, &mut MainThread<ViewContext<'_, '_, S>>) -> R + Send + 'static,
) -> impl Future<Output = R> ) -> Task<Result<R>>
where where
R: Send + 'static, R: Send + 'static,
{ {
let (tx, rx) = oneshot::channel();
if self.executor.is_main_thread() { if self.executor.is_main_thread() {
let cx = unsafe { mem::transmute::<&mut Self, &mut MainThread<Self>>(self) }; let cx = unsafe { mem::transmute::<&mut Self, &mut MainThread<Self>>(self) };
let _ = tx.send(f(view, cx)); Task::ready(Ok(f(view, cx)))
} else { } else {
let handle = self.handle().upgrade(self).unwrap(); let handle = self.handle().upgrade(self).unwrap();
let _ = self.window_cx.run_on_main(move |cx| { self.window_cx.run_on_main(move |cx| handle.update(cx, f))
handle.update(cx, |view, cx| {
let _ = tx.send(f(view, cx));
})
});
} }
async move { rx.await.unwrap() }
} }
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 {