WIP
This commit is contained in:
parent
7a6c27cf24
commit
c1a35a29a8
4 changed files with 78 additions and 80 deletions
|
@ -8,8 +8,8 @@ pub use model_context::*;
|
|||
use refineable::Refineable;
|
||||
|
||||
use crate::{
|
||||
current_platform, run_on_main, spawn_on_main, Context, LayoutId, MainThreadOnly, Platform,
|
||||
PlatformDispatcher, Reference, RootView, TextStyle, TextStyleRefinement, TextSystem, Window,
|
||||
current_platform, run_on_main, spawn_on_main, Context, LayoutId, MainThread, MainThreadOnly,
|
||||
Platform, PlatformDispatcher, RootView, TextStyle, TextStyleRefinement, TextSystem, Window,
|
||||
WindowContext, WindowHandle, WindowId,
|
||||
};
|
||||
use anyhow::{anyhow, Result};
|
||||
|
@ -20,6 +20,7 @@ use slotmap::SlotMap;
|
|||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
any::{type_name, Any, TypeId},
|
||||
marker::PhantomData,
|
||||
ops::{Deref, DerefMut},
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
@ -45,8 +46,10 @@ impl App {
|
|||
let unit_entity = entities.redeem(entities.reserve(), ());
|
||||
Self(Arc::new_cyclic(|this| {
|
||||
Mutex::new(AppContext {
|
||||
thread: PhantomData,
|
||||
this: this.clone(),
|
||||
platform: MainThreadOnly::new(platform, dispatcher),
|
||||
platform: MainThreadOnly::new(platform, dispatcher.clone()),
|
||||
dispatcher,
|
||||
text_system,
|
||||
pending_updates: 0,
|
||||
text_style_stack: Vec::new(),
|
||||
|
@ -74,9 +77,11 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
type Handlers = SmallVec<[Arc<dyn Fn(&mut AppContext) -> bool + Send + Sync + 'static>; 2]>;
|
||||
type Handlers<Thread> =
|
||||
SmallVec<[Arc<dyn Fn(&mut AppContext<Thread>) -> bool + Send + Sync + 'static>; 2]>;
|
||||
|
||||
pub struct AppContext {
|
||||
pub struct AppContext<Thread = ()> {
|
||||
thread: PhantomData<Thread>,
|
||||
this: Weak<Mutex<AppContext>>,
|
||||
platform: MainThreadOnly<dyn Platform>,
|
||||
dispatcher: Arc<dyn PlatformDispatcher>,
|
||||
|
@ -88,11 +93,25 @@ pub struct AppContext {
|
|||
pub(crate) entities: EntityMap,
|
||||
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
|
||||
pub(crate) pending_effects: VecDeque<Effect>,
|
||||
pub(crate) observers: HashMap<EntityId, Handlers>,
|
||||
pub(crate) observers: HashMap<EntityId, Handlers<Thread>>,
|
||||
pub(crate) layout_id_buffer: Vec<LayoutId>, // We recycle this memory across layout requests.
|
||||
}
|
||||
|
||||
impl AppContext {
|
||||
impl Deref for AppContext<MainThread> {
|
||||
type Target = AppContext<()>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for AppContext<MainThread> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Thread> AppContext<Thread> {
|
||||
pub fn text_system(&self) -> &Arc<TextSystem> {
|
||||
&self.text_system
|
||||
}
|
||||
|
@ -103,7 +122,7 @@ impl AppContext {
|
|||
|
||||
pub fn run_on_main<R>(
|
||||
&self,
|
||||
f: impl FnOnce(&mut MainThreadContext) -> R + Send + 'static,
|
||||
f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,
|
||||
) -> impl Future<Output = R>
|
||||
where
|
||||
R: Send + 'static,
|
||||
|
@ -111,14 +130,14 @@ impl AppContext {
|
|||
let this = self.this.upgrade().unwrap();
|
||||
run_on_main(self.dispatcher.clone(), move || {
|
||||
let cx = &mut *this.lock();
|
||||
let platform = cx.platform.borrow_on_main_thread().clone();
|
||||
cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref())))
|
||||
let main_thread_cx: &mut AppContext<MainThread> = unsafe { std::mem::transmute(cx) };
|
||||
main_thread_cx.update(|cx| f(cx))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn spawn_on_main<F, R>(
|
||||
&self,
|
||||
f: impl FnOnce(&mut MainThreadContext) -> F + Send + 'static,
|
||||
f: impl FnOnce(&mut AppContext<MainThread>) -> F + Send + 'static,
|
||||
) -> impl Future<Output = R>
|
||||
where
|
||||
F: Future<Output = R> + 'static,
|
||||
|
@ -128,21 +147,14 @@ impl AppContext {
|
|||
spawn_on_main(self.dispatcher.clone(), move || {
|
||||
let cx = &mut *this.lock();
|
||||
let platform = cx.platform.borrow_on_main_thread().clone();
|
||||
cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref())))
|
||||
// todo!()
|
||||
// cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref())))
|
||||
|
||||
future::ready(())
|
||||
})
|
||||
// self.platform.read(move |platform| {
|
||||
}
|
||||
|
||||
pub fn open_window<S: 'static + Send + Sync>(
|
||||
&mut self,
|
||||
options: crate::WindowOptions,
|
||||
build_root_view: impl FnOnce(&mut WindowContext) -> RootView<S> + Send + 'static,
|
||||
) -> impl Future<Output = WindowHandle<S>> {
|
||||
let id = self.windows.insert(None);
|
||||
let handle = WindowHandle::new(id);
|
||||
self.spawn_on_main(move |cx| future::ready(cx.open_window(options, build_root_view)))
|
||||
}
|
||||
|
||||
pub fn text_style(&self) -> TextStyle {
|
||||
let mut style = TextStyle::default();
|
||||
for refinement in &self.text_style_stack {
|
||||
|
@ -194,7 +206,7 @@ impl AppContext {
|
|||
pub(crate) fn update_window<R>(
|
||||
&mut self,
|
||||
id: WindowId,
|
||||
update: impl FnOnce(&mut WindowContext) -> R,
|
||||
update: impl FnOnce(&mut WindowContext<Thread>) -> R,
|
||||
) -> Result<R> {
|
||||
self.update(|cx| {
|
||||
let mut window = cx
|
||||
|
@ -289,21 +301,13 @@ impl Context for AppContext {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct MainThreadContext<'a> {
|
||||
app: Reference<'a, AppContext>,
|
||||
platform: &'a dyn Platform,
|
||||
}
|
||||
|
||||
impl<'a> MainThreadContext<'a> {
|
||||
fn mutable(cx: &'a mut AppContext, platform: &'a dyn Platform) -> Self {
|
||||
Self {
|
||||
app: Reference::Mutable(cx),
|
||||
platform,
|
||||
}
|
||||
impl AppContext<MainThread> {
|
||||
fn platform(&self) -> &dyn Platform {
|
||||
self.platform.borrow_on_main_thread()
|
||||
}
|
||||
|
||||
pub fn activate(&mut self, ignoring_other_apps: bool) {
|
||||
self.platform.activate(ignoring_other_apps);
|
||||
self.platform().activate(ignoring_other_apps);
|
||||
}
|
||||
|
||||
pub fn open_window<S: 'static + Send + Sync>(
|
||||
|
@ -313,29 +317,14 @@ impl<'a> MainThreadContext<'a> {
|
|||
) -> WindowHandle<S> {
|
||||
let id = self.windows.insert(None);
|
||||
let handle = WindowHandle::new(id);
|
||||
let cx = &mut *self.app;
|
||||
let mut window = Window::new(handle.into(), options, self.platform, cx);
|
||||
let root_view = build_root_view(&mut WindowContext::mutable(cx, &mut window));
|
||||
let mut window = Window::new(handle.into(), options, self.platform(), self);
|
||||
let root_view = build_root_view(&mut WindowContext::mutable(self, &mut window));
|
||||
window.root_view.replace(root_view.into_any());
|
||||
cx.windows.get_mut(id).unwrap().replace(window);
|
||||
self.windows.get_mut(id).unwrap().replace(window);
|
||||
handle
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for MainThreadContext<'a> {
|
||||
type Target = AppContext;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&*self.app
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DerefMut for MainThreadContext<'a> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut *self.app
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) enum Effect {
|
||||
Notify(EntityId),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue