Checkpoint

This commit is contained in:
Nathan Sobo 2023-10-02 12:47:45 -06:00
parent 4212a45767
commit 0b13c0a437
7 changed files with 177 additions and 157 deletions

View File

@ -14,19 +14,19 @@ use crate::{
};
use anyhow::{anyhow, Result};
use collections::{HashMap, VecDeque};
use futures::{future, Future};
use futures::Future;
use parking_lot::Mutex;
use slotmap::SlotMap;
use smallvec::SmallVec;
use std::{
any::{type_name, Any, TypeId},
marker::PhantomData,
mem,
sync::{Arc, Weak},
};
use util::ResultExt;
#[derive(Clone)]
pub struct App(Arc<Mutex<AppContext<MainThread>>>);
pub struct App(Arc<Mutex<MainThread<AppContext>>>);
impl App {
pub fn production() -> Self {
@ -44,8 +44,7 @@ impl App {
let entities = EntityMap::new();
let unit_entity = entities.redeem(entities.reserve(), ());
Self(Arc::new_cyclic(|this| {
Mutex::new(AppContext {
thread: PhantomData,
Mutex::new(MainThread::new(AppContext {
this: this.clone(),
platform: MainThreadOnly::new(platform, dispatcher.clone()),
dispatcher,
@ -59,13 +58,13 @@ impl App {
pending_effects: Default::default(),
observers: Default::default(),
layout_id_buffer: Default::default(),
})
}))
}))
}
pub fn run<F>(self, on_finish_launching: F)
where
F: 'static + FnOnce(&mut AppContext<MainThread>),
F: 'static + FnOnce(&mut MainThread<AppContext>),
{
let this = self.clone();
let platform = self.0.lock().platform.clone();
@ -76,12 +75,10 @@ impl App {
}
}
type Handlers<Thread> =
SmallVec<[Arc<dyn Fn(&mut AppContext<Thread>) -> bool + Send + Sync + 'static>; 2]>;
type Handlers = SmallVec<[Arc<dyn Fn(&mut AppContext) -> bool + Send + Sync + 'static>; 2]>;
pub struct AppContext<Thread = ()> {
thread: PhantomData<Thread>,
this: Weak<Mutex<AppContext<Thread>>>,
pub struct AppContext {
this: Weak<Mutex<MainThread<AppContext>>>,
platform: MainThreadOnly<dyn Platform>,
dispatcher: Arc<dyn PlatformDispatcher>,
text_system: Arc<TextSystem>,
@ -92,39 +89,89 @@ pub struct AppContext<Thread = ()> {
pub(crate) entities: EntityMap,
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
pub(crate) pending_effects: VecDeque<Effect>,
pub(crate) observers: HashMap<EntityId, Handlers<Thread>>,
pub(crate) observers: HashMap<EntityId, Handlers>,
pub(crate) layout_id_buffer: Vec<LayoutId>, // We recycle this memory across layout requests.
}
impl<Thread: 'static + Send + Sync> AppContext<Thread> {
// TODO: Better names for these?
#[inline]
pub fn downcast(&self) -> &AppContext<()> {
// Any `Thread` can become `()`.
//
// Can't do this in a blanket `Deref` impl, as it infinitely recurses.
unsafe { std::mem::transmute::<&AppContext<Thread>, &AppContext<()>>(self) }
impl AppContext {
fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
self.pending_updates += 1;
let result = update(self);
if self.pending_updates == 1 {
self.flush_effects();
}
self.pending_updates -= 1;
result
}
#[inline]
pub fn downcast_mut(&mut self) -> &mut AppContext<()> {
// Any `Thread` can become `()`.
//
// Can't do this in a blanket `DerefMut` impl, as it infinitely recurses.
unsafe { std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<()>>(self) }
pub(crate) fn update_window<R>(
&mut self,
id: WindowId,
update: impl FnOnce(&mut WindowContext) -> R,
) -> Result<R> {
self.update(|cx| {
let mut window = cx
.windows
.get_mut(id)
.ok_or_else(|| anyhow!("window not found"))?
.take()
.unwrap();
let result = update(&mut WindowContext::mutable(cx, &mut window));
cx.windows
.get_mut(id)
.ok_or_else(|| anyhow!("window not found"))?
.replace(window);
Ok(result)
})
}
pub fn text_system(&self) -> &Arc<TextSystem> {
&self.text_system
fn flush_effects(&mut self) {
while let Some(effect) = self.pending_effects.pop_front() {
match effect {
Effect::Notify(entity_id) => self.apply_notify_effect(entity_id),
}
}
let dirty_window_ids = self
.windows
.iter()
.filter_map(|(window_id, window)| {
let window = window.as_ref().unwrap();
if window.dirty {
Some(window_id)
} else {
None
}
})
.collect::<Vec<_>>();
for dirty_window_id in dirty_window_ids {
self.update_window(dirty_window_id, |cx| cx.draw())
.unwrap()
.log_err();
}
}
pub fn to_async(&self) -> AsyncContext<Thread> {
AsyncContext(self.this.clone())
fn apply_notify_effect(&mut self, updated_entity: EntityId) {
if let Some(mut handlers) = self.observers.remove(&updated_entity) {
handlers.retain(|handler| handler(self));
if let Some(new_handlers) = self.observers.remove(&updated_entity) {
handlers.extend(new_handlers);
}
self.observers.insert(updated_entity, handlers);
}
}
pub fn to_async(&self) -> AsyncContext {
AsyncContext(unsafe { mem::transmute(self.this.clone()) })
}
pub fn run_on_main<R>(
&self,
f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,
f: impl FnOnce(&mut MainThread<AppContext>) -> R + Send + 'static,
) -> impl Future<Output = R>
where
R: Send + 'static,
@ -132,16 +179,15 @@ impl<Thread: 'static + Send + Sync> AppContext<Thread> {
let this = self.this.upgrade().unwrap();
run_on_main(self.dispatcher.clone(), move || {
let cx = &mut *this.lock();
let main_thread_cx = unsafe {
std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<MainThread>>(cx)
};
main_thread_cx.update(|cx| f(cx))
cx.update(|cx| {
f(unsafe { mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx) })
})
})
}
pub fn spawn_on_main<F, R>(
&self,
f: impl FnOnce(&mut AppContext<MainThread>) -> F + Send + 'static,
f: impl FnOnce(&mut MainThread<AppContext>) -> F + Send + 'static,
) -> impl Future<Output = R>
where
F: Future<Output = R> + 'static,
@ -150,13 +196,16 @@ impl<Thread: 'static + Send + Sync> AppContext<Thread> {
let this = self.this.upgrade().unwrap();
spawn_on_main(self.dispatcher.clone(), move || {
let cx = &mut *this.lock();
let main_thread_cx = unsafe {
std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<MainThread>>(cx)
};
main_thread_cx.update(|cx| f(cx))
cx.update(|cx| {
f(unsafe { mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx) })
})
})
}
pub fn text_system(&self) -> &Arc<TextSystem> {
&self.text_system
}
pub fn text_style(&self) -> TextStyle {
let mut style = TextStyle::default();
for refinement in &self.text_style_stack {
@ -204,77 +253,6 @@ impl<Thread: 'static + Send + Sync> AppContext<Thread> {
.and_then(|stack| stack.pop())
.expect("state stack underflow");
}
pub(crate) fn update_window<R>(
&mut self,
id: WindowId,
update: impl FnOnce(&mut WindowContext) -> R,
) -> Result<R> {
self.update(|cx| {
let mut window = cx
.windows
.get_mut(id)
.ok_or_else(|| anyhow!("window not found"))?
.take()
.unwrap();
let result = update(&mut WindowContext::mutable(cx.downcast_mut(), &mut window));
cx.windows
.get_mut(id)
.ok_or_else(|| anyhow!("window not found"))?
.replace(window);
Ok(result)
})
}
fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
self.pending_updates += 1;
let result = update(self);
if self.pending_updates == 1 {
self.flush_effects();
}
self.pending_updates -= 1;
result
}
fn flush_effects(&mut self) {
while let Some(effect) = self.pending_effects.pop_front() {
match effect {
Effect::Notify(entity_id) => self.apply_notify_effect(entity_id),
}
}
let dirty_window_ids = self
.windows
.iter()
.filter_map(|(window_id, window)| {
let window = window.as_ref().unwrap();
if window.dirty {
Some(window_id)
} else {
None
}
})
.collect::<Vec<_>>();
for dirty_window_id in dirty_window_ids {
self.update_window(dirty_window_id, |cx| cx.draw())
.unwrap()
.log_err();
}
}
fn apply_notify_effect(&mut self, updated_entity: EntityId) {
if let Some(mut handlers) = self.observers.remove(&updated_entity) {
handlers.retain(|handler| handler(self));
if let Some(new_handlers) = self.observers.remove(&updated_entity) {
handlers.extend(new_handlers);
}
self.observers.insert(updated_entity, handlers);
}
}
}
impl Context for AppContext {
@ -302,7 +280,15 @@ impl Context for AppContext {
}
}
impl AppContext<MainThread> {
impl MainThread<AppContext> {
fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
self.0.update(|cx| {
update(unsafe {
std::mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx)
})
})
}
pub(crate) fn platform(&self) -> &dyn Platform {
self.platform.borrow_on_main_thread()
}
@ -320,8 +306,7 @@ impl AppContext<MainThread> {
let id = cx.windows.insert(None);
let handle = WindowHandle::new(id);
let mut window = Window::new(handle.into(), options, cx);
let root_view =
build_root_view(&mut WindowContext::mutable(cx.downcast_mut(), &mut window));
let root_view = build_root_view(&mut WindowContext::mutable(cx, &mut window));
window.root_view.replace(root_view.into_any());
cx.windows.get_mut(id).unwrap().replace(window);
handle

View File

@ -4,10 +4,10 @@ use parking_lot::Mutex;
use std::sync::Weak;
#[derive(Clone)]
pub struct AsyncContext<Thread = ()>(pub(crate) Weak<Mutex<AppContext<Thread>>>);
pub struct AsyncContext(pub(crate) Weak<Mutex<AppContext>>);
impl Context for AsyncContext {
type EntityContext<'a, 'b, T: Send + Sync + 'static> = ModelContext<'a, T>;
type EntityContext<'a, 'b, T: 'static + Send + Sync> = ModelContext<'a, T>;
type Result<T> = Result<T>;
fn entity<T: Send + Sync + 'static>(
@ -36,7 +36,7 @@ impl Context for AsyncContext {
}
}
impl<Thread: 'static + Send + Sync> AsyncContext<Thread> {
impl AsyncContext {
pub fn update_window<T>(
&self,
handle: AnyWindowHandle,

View File

@ -1,8 +1,8 @@
use crate::{AppContext, Context, Effect, EntityId, Handle, Reference, WeakHandle};
use std::{marker::PhantomData, sync::Arc};
pub struct ModelContext<'a, T, Thread = ()> {
app: Reference<'a, AppContext<Thread>>,
pub struct ModelContext<'a, T> {
app: Reference<'a, AppContext>,
entity_type: PhantomData<T>,
entity_id: EntityId,
}

View File

@ -45,7 +45,7 @@ pub use view::*;
pub use window::*;
pub trait Context {
type EntityContext<'a, 'w, T: Send + Sync + 'static>;
type EntityContext<'a, 'w, T: 'static + Send + Sync>;
type Result<T>;
fn entity<T: Send + Sync + 'static>(
@ -60,6 +60,29 @@ pub trait Context {
) -> Self::Result<R>;
}
#[repr(transparent)]
pub struct MainThread<T>(T);
impl<T> MainThread<T> {
fn new(value: T) -> Self {
Self(value)
}
}
impl<T> Deref for MainThread<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for MainThread<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait StackContext {
fn app(&mut self) -> &mut AppContext;
@ -154,8 +177,6 @@ impl<'a, T> DerefMut for Reference<'a, T> {
}
}
pub struct MainThread;
pub(crate) struct MainThreadOnly<T: ?Sized> {
dispatcher: Arc<dyn PlatformDispatcher>,
value: Arc<T>,

View File

@ -1,15 +1,14 @@
use parking_lot::Mutex;
use crate::{
AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, MainThread, Result, ViewContext,
AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, Result, ViewContext,
WindowContext,
};
use std::{any::Any, marker::PhantomData, sync::Arc};
pub struct View<S: Send + Sync, P, Thread = ()> {
pub struct View<S: Send + Sync, P> {
state: Handle<S>,
render:
Arc<dyn Fn(&mut S, &mut ViewContext<S, Thread>) -> AnyElement<S> + Send + Sync + 'static>,
render: Arc<dyn Fn(&mut S, &mut ViewContext<S>) -> AnyElement<S> + Send + Sync + 'static>,
parent_state_type: PhantomData<P>,
}

View File

@ -1,10 +1,9 @@
use crate::{
px, AnyView, AppContext, AvailableSpace, Bounds, Context, Effect, Element, EntityId, Handle,
LayoutId, MainThread, MainThreadOnly, Pixels, Platform, PlatformWindow, Point, Reference,
Scene, Size, StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
LayoutId, MainThread, MainThreadOnly, Pixels, PlatformWindow, Point, Reference, Scene, Size,
StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
};
use anyhow::Result;
use derive_more::{Deref, DerefMut};
use std::{any::TypeId, marker::PhantomData, sync::Arc};
use util::ResultExt;
@ -26,7 +25,7 @@ impl Window {
pub fn new(
handle: AnyWindowHandle,
options: WindowOptions,
cx: &mut AppContext<MainThread>,
cx: &mut MainThread<AppContext>,
) -> Self {
let platform_window = cx.platform().open_window(handle, options);
let mouse_position = platform_window.mouse_position();
@ -62,19 +61,14 @@ impl Window {
}
}
#[derive(Deref, DerefMut)]
pub struct WindowContext<'a, 'w, Thread = ()> {
thread: PhantomData<Thread>,
#[deref]
#[deref_mut]
app: Reference<'a, AppContext<Thread>>,
pub struct WindowContext<'a, 'w> {
app: Reference<'a, AppContext>,
window: Reference<'w, Window>,
}
impl<'a, 'w> WindowContext<'a, 'w> {
pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
Self {
thread: PhantomData,
app: Reference::Mutable(app),
window: Reference::Mutable(window),
}
@ -157,7 +151,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
}
}
impl WindowContext<'_, '_, MainThread> {
impl MainThread<WindowContext<'_, '_>> {
// todo!("implement other methods that use platform window")
fn platform_window(&self) -> &dyn PlatformWindow {
self.window.platform_window.borrow_on_main_thread().as_ref()
@ -165,14 +159,14 @@ impl WindowContext<'_, '_, MainThread> {
}
impl Context for WindowContext<'_, '_> {
type EntityContext<'a, 'w, T: Send + Sync + 'static> = ViewContext<'a, 'w, T>;
type EntityContext<'a, 'w, T: 'static + Send + Sync> = ViewContext<'a, 'w, T>;
type Result<T> = T;
fn entity<T: Send + Sync + 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Handle<T> {
let slot = self.entities.reserve();
let slot = self.app.entities.reserve();
let entity = build_entity(&mut ViewContext::mutable(
&mut *self.app,
&mut self.window,
@ -196,18 +190,32 @@ impl Context for WindowContext<'_, '_> {
}
}
impl<'a, 'w> std::ops::Deref for WindowContext<'a, 'w> {
type Target = AppContext;
fn deref(&self) -> &Self::Target {
&self.app
}
}
impl<'a, 'w> std::ops::DerefMut for WindowContext<'a, 'w> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.app
}
}
impl<S> StackContext for ViewContext<'_, '_, S> {
fn app(&mut self) -> &mut AppContext {
&mut *self.app
&mut *self.window_cx.app
}
fn with_text_style<F, R>(&mut self, style: crate::TextStyleRefinement, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
self.push_text_style(style);
self.window_cx.app.push_text_style(style);
let result = f(self);
self.pop_text_style();
self.window_cx.app.pop_text_style();
result
}
@ -215,21 +223,17 @@ impl<S> StackContext for ViewContext<'_, '_, S> {
where
F: FnOnce(&mut Self) -> R,
{
self.push_state(state);
self.window_cx.app.push_state(state);
let result = f(self);
self.pop_state::<T>();
self.window_cx.app.pop_state::<T>();
result
}
}
#[derive(Deref, DerefMut)]
pub struct ViewContext<'a, 'w, S, Thread = ()> {
#[deref]
#[deref_mut]
window_cx: WindowContext<'a, 'w, Thread>,
pub struct ViewContext<'a, 'w, S> {
window_cx: WindowContext<'a, 'w>,
entity_type: PhantomData<S>,
entity_id: EntityId,
thread: PhantomData<Thread>,
}
impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
@ -238,7 +242,6 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
window_cx: WindowContext::mutable(app, window),
entity_id,
entity_type: PhantomData,
thread: PhantomData,
}
}
@ -290,8 +293,8 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
}
}
impl<'a, 'w, S: 'static> Context for ViewContext<'a, 'w, S> {
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>;
impl<'a, 'w, S> Context for ViewContext<'a, 'w, S> {
type EntityContext<'b, 'c, U: 'static + Send + Sync> = ViewContext<'b, 'c, U>;
type Result<U> = U;
fn entity<T2: Send + Sync + 'static>(
@ -310,7 +313,19 @@ impl<'a, 'w, S: 'static> Context for ViewContext<'a, 'w, S> {
}
}
impl<S> ViewContext<'_, '_, S, MainThread> {}
impl<'a, 'w, S: 'static> std::ops::Deref for ViewContext<'a, 'w, S> {
type Target = WindowContext<'a, 'w>;
fn deref(&self) -> &Self::Target {
&self.window_cx
}
}
impl<'a, 'w, S: 'static> std::ops::DerefMut for ViewContext<'a, 'w, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.window_cx
}
}
// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
slotmap::new_key_type! { pub struct WindowId; }

View File

@ -1,6 +1,6 @@
use crate::{
collab_panel::{collab_panel, CollabPanel},
theme::{theme, themed},
theme::theme,
themes::rose_pine_dawn,
};
use gpui3::{