mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-08 05:47:27 +03:00
Checkpoint
This commit is contained in:
parent
4212a45767
commit
0b13c0a437
@ -14,19 +14,19 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use collections::{HashMap, VecDeque};
|
use collections::{HashMap, VecDeque};
|
||||||
use futures::{future, Future};
|
use futures::Future;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use slotmap::SlotMap;
|
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,
|
mem,
|
||||||
sync::{Arc, Weak},
|
sync::{Arc, Weak},
|
||||||
};
|
};
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct App(Arc<Mutex<AppContext<MainThread>>>);
|
pub struct App(Arc<Mutex<MainThread<AppContext>>>);
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn production() -> Self {
|
pub fn production() -> Self {
|
||||||
@ -44,8 +44,7 @@ impl App {
|
|||||||
let entities = EntityMap::new();
|
let entities = EntityMap::new();
|
||||||
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(MainThread::new(AppContext {
|
||||||
thread: PhantomData,
|
|
||||||
this: this.clone(),
|
this: this.clone(),
|
||||||
platform: MainThreadOnly::new(platform, dispatcher.clone()),
|
platform: MainThreadOnly::new(platform, dispatcher.clone()),
|
||||||
dispatcher,
|
dispatcher,
|
||||||
@ -59,13 +58,13 @@ impl App {
|
|||||||
pending_effects: Default::default(),
|
pending_effects: Default::default(),
|
||||||
observers: Default::default(),
|
observers: Default::default(),
|
||||||
layout_id_buffer: Default::default(),
|
layout_id_buffer: Default::default(),
|
||||||
})
|
}))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run<F>(self, on_finish_launching: F)
|
pub fn run<F>(self, on_finish_launching: F)
|
||||||
where
|
where
|
||||||
F: 'static + FnOnce(&mut AppContext<MainThread>),
|
F: 'static + FnOnce(&mut MainThread<AppContext>),
|
||||||
{
|
{
|
||||||
let this = self.clone();
|
let this = self.clone();
|
||||||
let platform = self.0.lock().platform.clone();
|
let platform = self.0.lock().platform.clone();
|
||||||
@ -76,12 +75,10 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handlers<Thread> =
|
type Handlers = SmallVec<[Arc<dyn Fn(&mut AppContext) -> bool + Send + Sync + 'static>; 2]>;
|
||||||
SmallVec<[Arc<dyn Fn(&mut AppContext<Thread>) -> bool + Send + Sync + 'static>; 2]>;
|
|
||||||
|
|
||||||
pub struct AppContext<Thread = ()> {
|
pub struct AppContext {
|
||||||
thread: PhantomData<Thread>,
|
this: Weak<Mutex<MainThread<AppContext>>>,
|
||||||
this: Weak<Mutex<AppContext<Thread>>>,
|
|
||||||
platform: MainThreadOnly<dyn Platform>,
|
platform: MainThreadOnly<dyn Platform>,
|
||||||
dispatcher: Arc<dyn PlatformDispatcher>,
|
dispatcher: Arc<dyn PlatformDispatcher>,
|
||||||
text_system: Arc<TextSystem>,
|
text_system: Arc<TextSystem>,
|
||||||
@ -92,39 +89,89 @@ pub struct AppContext<Thread = ()> {
|
|||||||
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<Thread>>,
|
pub(crate) observers: HashMap<EntityId, Handlers>,
|
||||||
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<Thread: 'static + Send + Sync> AppContext<Thread> {
|
impl AppContext {
|
||||||
// TODO: Better names for these?
|
fn update<R>(&mut self, update: impl FnOnce(&mut Self) -> R) -> R {
|
||||||
#[inline]
|
self.pending_updates += 1;
|
||||||
pub fn downcast(&self) -> &AppContext<()> {
|
let result = update(self);
|
||||||
// Any `Thread` can become `()`.
|
if self.pending_updates == 1 {
|
||||||
//
|
self.flush_effects();
|
||||||
// Can't do this in a blanket `Deref` impl, as it infinitely recurses.
|
}
|
||||||
unsafe { std::mem::transmute::<&AppContext<Thread>, &AppContext<()>>(self) }
|
self.pending_updates -= 1;
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
pub(crate) fn update_window<R>(
|
||||||
pub fn downcast_mut(&mut self) -> &mut AppContext<()> {
|
&mut self,
|
||||||
// Any `Thread` can become `()`.
|
id: WindowId,
|
||||||
//
|
update: impl FnOnce(&mut WindowContext) -> R,
|
||||||
// Can't do this in a blanket `DerefMut` impl, as it infinitely recurses.
|
) -> Result<R> {
|
||||||
unsafe { std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<()>>(self) }
|
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> {
|
fn flush_effects(&mut self) {
|
||||||
&self.text_system
|
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> {
|
fn apply_notify_effect(&mut self, updated_entity: EntityId) {
|
||||||
AsyncContext(self.this.clone())
|
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>(
|
pub fn run_on_main<R>(
|
||||||
&self,
|
&self,
|
||||||
f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,
|
f: impl FnOnce(&mut MainThread<AppContext>) -> R + Send + 'static,
|
||||||
) -> impl Future<Output = R>
|
) -> impl Future<Output = R>
|
||||||
where
|
where
|
||||||
R: Send + 'static,
|
R: Send + 'static,
|
||||||
@ -132,16 +179,15 @@ impl<Thread: 'static + Send + Sync> AppContext<Thread> {
|
|||||||
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 main_thread_cx = unsafe {
|
cx.update(|cx| {
|
||||||
std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<MainThread>>(cx)
|
f(unsafe { mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx) })
|
||||||
};
|
})
|
||||||
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 AppContext<MainThread>) -> F + Send + 'static,
|
f: impl FnOnce(&mut MainThread<AppContext>) -> F + Send + 'static,
|
||||||
) -> impl Future<Output = R>
|
) -> impl Future<Output = R>
|
||||||
where
|
where
|
||||||
F: Future<Output = R> + 'static,
|
F: Future<Output = R> + 'static,
|
||||||
@ -150,13 +196,16 @@ impl<Thread: 'static + Send + Sync> AppContext<Thread> {
|
|||||||
let this = self.this.upgrade().unwrap();
|
let this = self.this.upgrade().unwrap();
|
||||||
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 main_thread_cx = unsafe {
|
cx.update(|cx| {
|
||||||
std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<MainThread>>(cx)
|
f(unsafe { mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx) })
|
||||||
};
|
})
|
||||||
main_thread_cx.update(|cx| f(cx))
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn text_system(&self) -> &Arc<TextSystem> {
|
||||||
|
&self.text_system
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@ -204,77 +253,6 @@ impl<Thread: 'static + Send + Sync> AppContext<Thread> {
|
|||||||
.and_then(|stack| stack.pop())
|
.and_then(|stack| stack.pop())
|
||||||
.expect("state stack underflow");
|
.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 {
|
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 {
|
pub(crate) fn platform(&self) -> &dyn Platform {
|
||||||
self.platform.borrow_on_main_thread()
|
self.platform.borrow_on_main_thread()
|
||||||
}
|
}
|
||||||
@ -320,8 +306,7 @@ impl AppContext<MainThread> {
|
|||||||
let id = cx.windows.insert(None);
|
let id = cx.windows.insert(None);
|
||||||
let handle = WindowHandle::new(id);
|
let handle = WindowHandle::new(id);
|
||||||
let mut window = Window::new(handle.into(), options, cx);
|
let mut window = Window::new(handle.into(), options, cx);
|
||||||
let root_view =
|
let root_view = build_root_view(&mut WindowContext::mutable(cx, &mut window));
|
||||||
build_root_view(&mut WindowContext::mutable(cx.downcast_mut(), &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);
|
cx.windows.get_mut(id).unwrap().replace(window);
|
||||||
handle
|
handle
|
||||||
|
@ -4,10 +4,10 @@ use parking_lot::Mutex;
|
|||||||
use std::sync::Weak;
|
use std::sync::Weak;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AsyncContext<Thread = ()>(pub(crate) Weak<Mutex<AppContext<Thread>>>);
|
pub struct AsyncContext(pub(crate) Weak<Mutex<AppContext>>);
|
||||||
|
|
||||||
impl Context for AsyncContext {
|
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>;
|
type Result<T> = Result<T>;
|
||||||
|
|
||||||
fn entity<T: Send + Sync + 'static>(
|
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>(
|
pub fn update_window<T>(
|
||||||
&self,
|
&self,
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use crate::{AppContext, Context, Effect, EntityId, Handle, Reference, WeakHandle};
|
use crate::{AppContext, Context, Effect, EntityId, Handle, Reference, WeakHandle};
|
||||||
use std::{marker::PhantomData, sync::Arc};
|
use std::{marker::PhantomData, sync::Arc};
|
||||||
|
|
||||||
pub struct ModelContext<'a, T, Thread = ()> {
|
pub struct ModelContext<'a, T> {
|
||||||
app: Reference<'a, AppContext<Thread>>,
|
app: Reference<'a, AppContext>,
|
||||||
entity_type: PhantomData<T>,
|
entity_type: PhantomData<T>,
|
||||||
entity_id: EntityId,
|
entity_id: EntityId,
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ pub use view::*;
|
|||||||
pub use window::*;
|
pub use window::*;
|
||||||
|
|
||||||
pub trait Context {
|
pub trait Context {
|
||||||
type EntityContext<'a, 'w, T: Send + Sync + 'static>;
|
type EntityContext<'a, 'w, T: 'static + Send + Sync>;
|
||||||
type Result<T>;
|
type Result<T>;
|
||||||
|
|
||||||
fn entity<T: Send + Sync + 'static>(
|
fn entity<T: Send + Sync + 'static>(
|
||||||
@ -60,6 +60,29 @@ pub trait Context {
|
|||||||
) -> Self::Result<R>;
|
) -> 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 {
|
pub trait StackContext {
|
||||||
fn app(&mut self) -> &mut AppContext;
|
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> {
|
pub(crate) struct MainThreadOnly<T: ?Sized> {
|
||||||
dispatcher: Arc<dyn PlatformDispatcher>,
|
dispatcher: Arc<dyn PlatformDispatcher>,
|
||||||
value: Arc<T>,
|
value: Arc<T>,
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, MainThread, Result, ViewContext,
|
AnyElement, Element, Handle, IntoAnyElement, Layout, LayoutId, Result, ViewContext,
|
||||||
WindowContext,
|
WindowContext,
|
||||||
};
|
};
|
||||||
use std::{any::Any, marker::PhantomData, sync::Arc};
|
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>,
|
state: Handle<S>,
|
||||||
render:
|
render: Arc<dyn Fn(&mut S, &mut ViewContext<S>) -> AnyElement<S> + Send + Sync + 'static>,
|
||||||
Arc<dyn Fn(&mut S, &mut ViewContext<S, Thread>) -> AnyElement<S> + Send + Sync + 'static>,
|
|
||||||
parent_state_type: PhantomData<P>,
|
parent_state_type: PhantomData<P>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
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, MainThread, MainThreadOnly, Pixels, Platform, PlatformWindow, Point, Reference,
|
LayoutId, MainThread, MainThreadOnly, Pixels, PlatformWindow, Point, Reference, Scene, Size,
|
||||||
Scene, Size, StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
|
StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use derive_more::{Deref, DerefMut};
|
|
||||||
use std::{any::TypeId, marker::PhantomData, sync::Arc};
|
use std::{any::TypeId, marker::PhantomData, sync::Arc};
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
|
||||||
@ -26,7 +25,7 @@ impl Window {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
options: WindowOptions,
|
options: WindowOptions,
|
||||||
cx: &mut AppContext<MainThread>,
|
cx: &mut MainThread<AppContext>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let platform_window = cx.platform().open_window(handle, options);
|
let platform_window = cx.platform().open_window(handle, options);
|
||||||
let mouse_position = platform_window.mouse_position();
|
let mouse_position = platform_window.mouse_position();
|
||||||
@ -62,19 +61,14 @@ impl Window {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deref, DerefMut)]
|
pub struct WindowContext<'a, 'w> {
|
||||||
pub struct WindowContext<'a, 'w, Thread = ()> {
|
app: Reference<'a, AppContext>,
|
||||||
thread: PhantomData<Thread>,
|
|
||||||
#[deref]
|
|
||||||
#[deref_mut]
|
|
||||||
app: Reference<'a, AppContext<Thread>>,
|
|
||||||
window: Reference<'w, Window>,
|
window: Reference<'w, Window>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'w> WindowContext<'a, 'w> {
|
impl<'a, 'w> WindowContext<'a, 'w> {
|
||||||
pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
|
pub(crate) fn mutable(app: &'a mut AppContext, 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),
|
||||||
}
|
}
|
||||||
@ -157,7 +151,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowContext<'_, '_, MainThread> {
|
impl MainThread<WindowContext<'_, '_>> {
|
||||||
// todo!("implement other methods that use platform window")
|
// todo!("implement other methods that use platform window")
|
||||||
fn platform_window(&self) -> &dyn PlatformWindow {
|
fn platform_window(&self) -> &dyn PlatformWindow {
|
||||||
self.window.platform_window.borrow_on_main_thread().as_ref()
|
self.window.platform_window.borrow_on_main_thread().as_ref()
|
||||||
@ -165,14 +159,14 @@ impl WindowContext<'_, '_, MainThread> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Context for WindowContext<'_, '_> {
|
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;
|
type Result<T> = T;
|
||||||
|
|
||||||
fn entity<T: Send + Sync + 'static>(
|
fn entity<T: Send + Sync + 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
|
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
|
||||||
) -> Handle<T> {
|
) -> Handle<T> {
|
||||||
let slot = self.entities.reserve();
|
let slot = self.app.entities.reserve();
|
||||||
let entity = build_entity(&mut ViewContext::mutable(
|
let entity = build_entity(&mut ViewContext::mutable(
|
||||||
&mut *self.app,
|
&mut *self.app,
|
||||||
&mut self.window,
|
&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> {
|
impl<S> StackContext for ViewContext<'_, '_, S> {
|
||||||
fn app(&mut self) -> &mut AppContext {
|
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
|
fn with_text_style<F, R>(&mut self, style: crate::TextStyleRefinement, f: F) -> R
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self) -> R,
|
F: FnOnce(&mut Self) -> R,
|
||||||
{
|
{
|
||||||
self.push_text_style(style);
|
self.window_cx.app.push_text_style(style);
|
||||||
let result = f(self);
|
let result = f(self);
|
||||||
self.pop_text_style();
|
self.window_cx.app.pop_text_style();
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,21 +223,17 @@ impl<S> StackContext for ViewContext<'_, '_, S> {
|
|||||||
where
|
where
|
||||||
F: FnOnce(&mut Self) -> R,
|
F: FnOnce(&mut Self) -> R,
|
||||||
{
|
{
|
||||||
self.push_state(state);
|
self.window_cx.app.push_state(state);
|
||||||
let result = f(self);
|
let result = f(self);
|
||||||
self.pop_state::<T>();
|
self.window_cx.app.pop_state::<T>();
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deref, DerefMut)]
|
pub struct ViewContext<'a, 'w, S> {
|
||||||
pub struct ViewContext<'a, 'w, S, Thread = ()> {
|
window_cx: WindowContext<'a, 'w>,
|
||||||
#[deref]
|
|
||||||
#[deref_mut]
|
|
||||||
window_cx: WindowContext<'a, 'w, Thread>,
|
|
||||||
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> 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),
|
window_cx: WindowContext::mutable(app, window),
|
||||||
entity_id,
|
entity_id,
|
||||||
entity_type: PhantomData,
|
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> {
|
impl<'a, 'w, S> Context for ViewContext<'a, 'w, S> {
|
||||||
type EntityContext<'b, 'c, U: Send + Sync + 'static> = ViewContext<'b, 'c, U>;
|
type EntityContext<'b, 'c, U: 'static + Send + Sync> = ViewContext<'b, 'c, U>;
|
||||||
type Result<U> = U;
|
type Result<U> = U;
|
||||||
|
|
||||||
fn entity<T2: Send + Sync + 'static>(
|
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)]
|
// #[derive(Clone, Copy, Eq, PartialEq, Hash)]
|
||||||
slotmap::new_key_type! { pub struct WindowId; }
|
slotmap::new_key_type! { pub struct WindowId; }
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
collab_panel::{collab_panel, CollabPanel},
|
collab_panel::{collab_panel, CollabPanel},
|
||||||
theme::{theme, themed},
|
theme::theme,
|
||||||
themes::rose_pine_dawn,
|
themes::rose_pine_dawn,
|
||||||
};
|
};
|
||||||
use gpui3::{
|
use gpui3::{
|
||||||
|
Loading…
Reference in New Issue
Block a user