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 refineable::Refineable;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
current_platform, run_on_main, spawn_on_main, Context, LayoutId, MainThreadOnly, Platform,
|
current_platform, run_on_main, spawn_on_main, Context, LayoutId, MainThread, MainThreadOnly,
|
||||||
PlatformDispatcher, Reference, RootView, TextStyle, TextStyleRefinement, TextSystem, Window,
|
Platform, PlatformDispatcher, RootView, TextStyle, TextStyleRefinement, TextSystem, Window,
|
||||||
WindowContext, WindowHandle, WindowId,
|
WindowContext, WindowHandle, WindowId,
|
||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
@ -20,6 +20,7 @@ use slotmap::SlotMap;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::{
|
use std::{
|
||||||
any::{type_name, Any, TypeId},
|
any::{type_name, Any, TypeId},
|
||||||
|
marker::PhantomData,
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
sync::{Arc, Weak},
|
sync::{Arc, Weak},
|
||||||
};
|
};
|
||||||
|
@ -45,8 +46,10 @@ impl App {
|
||||||
let unit_entity = entities.redeem(entities.reserve(), ());
|
let unit_entity = entities.redeem(entities.reserve(), ());
|
||||||
Self(Arc::new_cyclic(|this| {
|
Self(Arc::new_cyclic(|this| {
|
||||||
Mutex::new(AppContext {
|
Mutex::new(AppContext {
|
||||||
|
thread: PhantomData,
|
||||||
this: this.clone(),
|
this: this.clone(),
|
||||||
platform: MainThreadOnly::new(platform, dispatcher),
|
platform: MainThreadOnly::new(platform, dispatcher.clone()),
|
||||||
|
dispatcher,
|
||||||
text_system,
|
text_system,
|
||||||
pending_updates: 0,
|
pending_updates: 0,
|
||||||
text_style_stack: Vec::new(),
|
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>>,
|
this: Weak<Mutex<AppContext>>,
|
||||||
platform: MainThreadOnly<dyn Platform>,
|
platform: MainThreadOnly<dyn Platform>,
|
||||||
dispatcher: Arc<dyn PlatformDispatcher>,
|
dispatcher: Arc<dyn PlatformDispatcher>,
|
||||||
|
@ -88,11 +93,25 @@ pub struct AppContext {
|
||||||
pub(crate) entities: EntityMap,
|
pub(crate) entities: EntityMap,
|
||||||
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
|
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
|
||||||
pub(crate) pending_effects: VecDeque<Effect>,
|
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.
|
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> {
|
pub fn text_system(&self) -> &Arc<TextSystem> {
|
||||||
&self.text_system
|
&self.text_system
|
||||||
}
|
}
|
||||||
|
@ -103,7 +122,7 @@ impl AppContext {
|
||||||
|
|
||||||
pub fn run_on_main<R>(
|
pub fn run_on_main<R>(
|
||||||
&self,
|
&self,
|
||||||
f: impl FnOnce(&mut MainThreadContext) -> R + Send + 'static,
|
f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,
|
||||||
) -> impl Future<Output = R>
|
) -> impl Future<Output = R>
|
||||||
where
|
where
|
||||||
R: Send + 'static,
|
R: Send + 'static,
|
||||||
|
@ -111,14 +130,14 @@ impl AppContext {
|
||||||
let this = self.this.upgrade().unwrap();
|
let this = self.this.upgrade().unwrap();
|
||||||
run_on_main(self.dispatcher.clone(), move || {
|
run_on_main(self.dispatcher.clone(), move || {
|
||||||
let cx = &mut *this.lock();
|
let cx = &mut *this.lock();
|
||||||
let platform = cx.platform.borrow_on_main_thread().clone();
|
let main_thread_cx: &mut AppContext<MainThread> = unsafe { std::mem::transmute(cx) };
|
||||||
cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref())))
|
main_thread_cx.update(|cx| f(cx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawn_on_main<F, R>(
|
pub fn spawn_on_main<F, R>(
|
||||||
&self,
|
&self,
|
||||||
f: impl FnOnce(&mut MainThreadContext) -> F + Send + 'static,
|
f: impl FnOnce(&mut AppContext<MainThread>) -> F + Send + 'static,
|
||||||
) -> impl Future<Output = R>
|
) -> impl Future<Output = R>
|
||||||
where
|
where
|
||||||
F: Future<Output = R> + 'static,
|
F: Future<Output = R> + 'static,
|
||||||
|
@ -128,21 +147,14 @@ impl AppContext {
|
||||||
spawn_on_main(self.dispatcher.clone(), move || {
|
spawn_on_main(self.dispatcher.clone(), move || {
|
||||||
let cx = &mut *this.lock();
|
let cx = &mut *this.lock();
|
||||||
let platform = cx.platform.borrow_on_main_thread().clone();
|
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| {
|
// 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 {
|
pub fn text_style(&self) -> TextStyle {
|
||||||
let mut style = TextStyle::default();
|
let mut style = TextStyle::default();
|
||||||
for refinement in &self.text_style_stack {
|
for refinement in &self.text_style_stack {
|
||||||
|
@ -194,7 +206,7 @@ impl AppContext {
|
||||||
pub(crate) fn update_window<R>(
|
pub(crate) fn update_window<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: WindowId,
|
id: WindowId,
|
||||||
update: impl FnOnce(&mut WindowContext) -> R,
|
update: impl FnOnce(&mut WindowContext<Thread>) -> R,
|
||||||
) -> Result<R> {
|
) -> Result<R> {
|
||||||
self.update(|cx| {
|
self.update(|cx| {
|
||||||
let mut window = cx
|
let mut window = cx
|
||||||
|
@ -289,21 +301,13 @@ impl Context for AppContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MainThreadContext<'a> {
|
impl AppContext<MainThread> {
|
||||||
app: Reference<'a, AppContext>,
|
fn platform(&self) -> &dyn Platform {
|
||||||
platform: &'a dyn Platform,
|
self.platform.borrow_on_main_thread()
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> MainThreadContext<'a> {
|
|
||||||
fn mutable(cx: &'a mut AppContext, platform: &'a dyn Platform) -> Self {
|
|
||||||
Self {
|
|
||||||
app: Reference::Mutable(cx),
|
|
||||||
platform,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn activate(&mut self, ignoring_other_apps: bool) {
|
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>(
|
pub fn open_window<S: 'static + Send + Sync>(
|
||||||
|
@ -313,29 +317,14 @@ impl<'a> MainThreadContext<'a> {
|
||||||
) -> WindowHandle<S> {
|
) -> WindowHandle<S> {
|
||||||
let id = self.windows.insert(None);
|
let id = self.windows.insert(None);
|
||||||
let handle = WindowHandle::new(id);
|
let handle = WindowHandle::new(id);
|
||||||
let cx = &mut *self.app;
|
let mut window = Window::new(handle.into(), options, self.platform(), self);
|
||||||
let mut window = Window::new(handle.into(), options, self.platform, cx);
|
let root_view = build_root_view(&mut WindowContext::mutable(self, &mut window));
|
||||||
let root_view = build_root_view(&mut WindowContext::mutable(cx, &mut window));
|
|
||||||
window.root_view.replace(root_view.into_any());
|
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
|
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 {
|
pub(crate) enum Effect {
|
||||||
Notify(EntityId),
|
Notify(EntityId),
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,6 +154,8 @@ impl<'a, T> DerefMut for Reference<'a, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct MainThread;
|
||||||
|
|
||||||
pub(crate) struct MainThreadOnly<T: ?Sized> {
|
pub(crate) struct MainThreadOnly<T: ?Sized> {
|
||||||
dispatcher: Arc<dyn PlatformDispatcher>,
|
dispatcher: Arc<dyn PlatformDispatcher>,
|
||||||
value: Arc<T>,
|
value: Arc<T>,
|
||||||
|
@ -175,7 +177,7 @@ impl<T: 'static + ?Sized> MainThreadOnly<T> {
|
||||||
Self { dispatcher, value }
|
Self { dispatcher, value }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn borrow_on_main_thread(&self) -> &Arc<T> {
|
pub(crate) fn borrow_on_main_thread(&self) -> &T {
|
||||||
assert!(self.dispatcher.is_main_thread());
|
assert!(self.dispatcher.is_main_thread());
|
||||||
&self.value
|
&self.value
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
px, AnyView, AppContext, AvailableSpace, Bounds, Context, Effect, Element, EntityId, Handle,
|
px, AnyView, AppContext, AvailableSpace, Bounds, Context, Effect, Element, EntityId, Handle,
|
||||||
LayoutId, MainThreadOnly, Pixels, Platform, PlatformWindow, Point, Reference, Scene, Size,
|
LayoutId, MainThread, MainThreadOnly, Pixels, Platform, PlatformWindow, Point, Reference,
|
||||||
StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
|
Scene, Size, StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
use std::{any::TypeId, future, marker::PhantomData, sync::Arc};
|
use std::{any::TypeId, marker::PhantomData, sync::Arc};
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
|
||||||
pub struct AnyWindow {}
|
pub struct AnyWindow {}
|
||||||
|
@ -27,7 +27,7 @@ impl Window {
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
options: WindowOptions,
|
options: WindowOptions,
|
||||||
platform: &dyn Platform,
|
platform: &dyn Platform,
|
||||||
cx: &mut AppContext,
|
cx: &mut AppContext<MainThread>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let platform_window = platform.open_window(handle, options);
|
let platform_window = platform.open_window(handle, options);
|
||||||
let mouse_position = platform_window.mouse_position();
|
let mouse_position = platform_window.mouse_position();
|
||||||
|
@ -63,16 +63,18 @@ impl Window {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deref, DerefMut)]
|
#[derive(Deref, DerefMut)]
|
||||||
pub struct WindowContext<'a, 'b> {
|
pub struct WindowContext<'a, 'b, Thread = ()> {
|
||||||
|
thread: PhantomData<Thread>,
|
||||||
#[deref]
|
#[deref]
|
||||||
#[deref_mut]
|
#[deref_mut]
|
||||||
app: Reference<'a, AppContext>,
|
app: Reference<'a, AppContext<Thread>>,
|
||||||
window: Reference<'b, Window>,
|
window: Reference<'b, Window>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> WindowContext<'a, 'w> {
|
impl<'a, 'w, Thread> WindowContext<'a, 'w, Thread> {
|
||||||
pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
|
pub(crate) fn mutable(app: &'a mut AppContext<Thread>, window: &'w mut Window) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
thread: PhantomData,
|
||||||
app: Reference::Mutable(app),
|
app: Reference::Mutable(app),
|
||||||
window: Reference::Mutable(window),
|
window: Reference::Mutable(window),
|
||||||
}
|
}
|
||||||
|
@ -93,12 +95,13 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
let scene = cx.window.scene.take();
|
let scene = cx.window.scene.take();
|
||||||
dbg!(&scene);
|
dbg!(&scene);
|
||||||
|
|
||||||
self.run_on_main(|cx| {
|
// todo!
|
||||||
cx.window
|
// self.run_on_main(|cx| {
|
||||||
.platform_window
|
// cx.window
|
||||||
.borrow_on_main_thread()
|
// .platform_window
|
||||||
.draw(scene);
|
// .borrow_on_main_thread()
|
||||||
});
|
// .draw(scene);
|
||||||
|
// });
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
@ -148,7 +151,14 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context for WindowContext<'_, '_> {
|
impl WindowContext<'_, '_, MainThread> {
|
||||||
|
// todo!("implement other methods that use platform window")
|
||||||
|
fn platform_window(&self) -> &dyn PlatformWindow {
|
||||||
|
self.window.platform_window.borrow_on_main_thread().as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Thread> Context for WindowContext<'_, '_, Thread> {
|
||||||
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>;
|
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>;
|
||||||
type Result<T> = T;
|
type Result<T> = T;
|
||||||
|
|
||||||
|
@ -180,7 +190,7 @@ impl Context for WindowContext<'_, '_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> StackContext for ViewContext<'_, '_, S> {
|
impl<S, Thread> StackContext for ViewContext<'_, '_, S, Thread> {
|
||||||
fn app(&mut self) -> &mut AppContext {
|
fn app(&mut self) -> &mut AppContext {
|
||||||
&mut *self.app
|
&mut *self.app
|
||||||
}
|
}
|
||||||
|
@ -207,20 +217,22 @@ impl<S> StackContext for ViewContext<'_, '_, S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deref, DerefMut)]
|
#[derive(Deref, DerefMut)]
|
||||||
pub struct ViewContext<'a, 'w, S> {
|
pub struct ViewContext<'a, 'w, S, Thread = ()> {
|
||||||
#[deref]
|
#[deref]
|
||||||
#[deref_mut]
|
#[deref_mut]
|
||||||
window_cx: WindowContext<'a, 'w>,
|
window_cx: WindowContext<'a, 'w>,
|
||||||
entity_type: PhantomData<S>,
|
entity_type: PhantomData<S>,
|
||||||
entity_id: EntityId,
|
entity_id: EntityId,
|
||||||
|
thread: PhantomData<Thread>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
|
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 {
|
Self {
|
||||||
window_cx: WindowContext::mutable(app, window),
|
window_cx: WindowContext::mutable(app, window),
|
||||||
entity_id,
|
entity_id,
|
||||||
entity_type: PhantomData,
|
entity_type: PhantomData,
|
||||||
|
thread: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,7 +284,7 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w, S: 'static> Context for ViewContext<'a, 'w, S> {
|
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>;
|
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>;
|
||||||
type Result<U> = U;
|
type Result<U> = U;
|
||||||
|
|
||||||
|
@ -292,6 +304,8 @@ impl<'a, 'w, S: 'static> Context for ViewContext<'a, 'w, S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<S> ViewContext<'_, '_, S, MainThread> {}
|
||||||
|
|
||||||
// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
|
// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
|
||||||
slotmap::new_key_type! { pub struct WindowId; }
|
slotmap::new_key_type! { pub struct WindowId; }
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
#![allow(dead_code, unused_variables)]
|
#![allow(dead_code, unused_variables)]
|
||||||
|
|
||||||
use std::future;
|
|
||||||
|
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use simplelog::SimpleLogger;
|
use simplelog::SimpleLogger;
|
||||||
|
|
||||||
|
@ -20,11 +18,6 @@ fn main() {
|
||||||
|
|
||||||
gpui3::App::production().run(|cx| {
|
gpui3::App::production().run(|cx| {
|
||||||
let window = cx.open_window(Default::default(), |cx| workspace(cx));
|
let window = cx.open_window(Default::default(), |cx| workspace(cx));
|
||||||
|
|
||||||
cx.spawn_on_main(move |platform, _| {
|
|
||||||
platform.activate(true);
|
|
||||||
future::ready(())
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue