mirror of
https://github.com/zed-industries/zed.git
synced 2024-10-27 16:57:08 +00:00
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),
|
||||
}
|
||||
|
|
|
@ -154,6 +154,8 @@ impl<'a, T> DerefMut for Reference<'a, T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct MainThread;
|
||||
|
||||
pub(crate) struct MainThreadOnly<T: ?Sized> {
|
||||
dispatcher: Arc<dyn PlatformDispatcher>,
|
||||
value: Arc<T>,
|
||||
|
@ -175,7 +177,7 @@ impl<T: 'static + ?Sized> MainThreadOnly<T> {
|
|||
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());
|
||||
&self.value
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use crate::{
|
||||
px, AnyView, AppContext, AvailableSpace, Bounds, Context, Effect, Element, EntityId, Handle,
|
||||
LayoutId, MainThreadOnly, Pixels, Platform, PlatformWindow, Point, Reference, Scene, Size,
|
||||
StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
|
||||
LayoutId, MainThread, MainThreadOnly, Pixels, Platform, PlatformWindow, Point, Reference,
|
||||
Scene, Size, StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions,
|
||||
};
|
||||
use anyhow::Result;
|
||||
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;
|
||||
|
||||
pub struct AnyWindow {}
|
||||
|
@ -27,7 +27,7 @@ impl Window {
|
|||
handle: AnyWindowHandle,
|
||||
options: WindowOptions,
|
||||
platform: &dyn Platform,
|
||||
cx: &mut AppContext,
|
||||
cx: &mut AppContext<MainThread>,
|
||||
) -> Self {
|
||||
let platform_window = platform.open_window(handle, options);
|
||||
let mouse_position = platform_window.mouse_position();
|
||||
|
@ -63,16 +63,18 @@ impl Window {
|
|||
}
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
pub struct WindowContext<'a, 'b> {
|
||||
pub struct WindowContext<'a, 'b, Thread = ()> {
|
||||
thread: PhantomData<Thread>,
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
app: Reference<'a, AppContext>,
|
||||
app: Reference<'a, AppContext<Thread>>,
|
||||
window: Reference<'b, Window>,
|
||||
}
|
||||
|
||||
impl<'a, 'w> WindowContext<'a, 'w> {
|
||||
pub(crate) fn mutable(app: &'a mut AppContext, window: &'w mut Window) -> Self {
|
||||
impl<'a, 'w, Thread> WindowContext<'a, 'w, Thread> {
|
||||
pub(crate) fn mutable(app: &'a mut AppContext<Thread>, window: &'w mut Window) -> Self {
|
||||
Self {
|
||||
thread: PhantomData,
|
||||
app: Reference::Mutable(app),
|
||||
window: Reference::Mutable(window),
|
||||
}
|
||||
|
@ -93,12 +95,13 @@ impl<'a, 'w> WindowContext<'a, 'w> {
|
|||
let scene = cx.window.scene.take();
|
||||
dbg!(&scene);
|
||||
|
||||
self.run_on_main(|cx| {
|
||||
cx.window
|
||||
.platform_window
|
||||
.borrow_on_main_thread()
|
||||
.draw(scene);
|
||||
});
|
||||
// todo!
|
||||
// self.run_on_main(|cx| {
|
||||
// cx.window
|
||||
// .platform_window
|
||||
// .borrow_on_main_thread()
|
||||
// .draw(scene);
|
||||
// });
|
||||
|
||||
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 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 {
|
||||
&mut *self.app
|
||||
}
|
||||
|
@ -207,20 +217,22 @@ impl<S> StackContext for ViewContext<'_, '_, S> {
|
|||
}
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
pub struct ViewContext<'a, 'w, S> {
|
||||
pub struct ViewContext<'a, 'w, S, Thread = ()> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
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> {
|
||||
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 {
|
||||
Self {
|
||||
window_cx: WindowContext::mutable(app, window),
|
||||
entity_id,
|
||||
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 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)]
|
||||
slotmap::new_key_type! { pub struct WindowId; }
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
#![allow(dead_code, unused_variables)]
|
||||
|
||||
use std::future;
|
||||
|
||||
use log::LevelFilter;
|
||||
use simplelog::SimpleLogger;
|
||||
|
||||
|
@ -20,11 +18,6 @@ fn main() {
|
|||
|
||||
gpui3::App::production().run(|cx| {
|
||||
let window = cx.open_window(Default::default(), |cx| workspace(cx));
|
||||
|
||||
cx.spawn_on_main(move |platform, _| {
|
||||
platform.activate(true);
|
||||
future::ready(())
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue