Checkpoint: Storybook window showing

This commit is contained in:
Marshall Bowers 2023-09-29 21:51:27 -04:00
parent 3b38641f98
commit 43a1296150
3 changed files with 28 additions and 14 deletions

View file

@ -27,7 +27,7 @@ use std::{
use util::ResultExt; use util::ResultExt;
#[derive(Clone)] #[derive(Clone)]
pub struct App(Arc<Mutex<AppContext>>); pub struct App(Arc<Mutex<AppContext<MainThread>>>);
impl App { impl App {
pub fn production() -> Self { pub fn production() -> Self {
@ -66,7 +66,7 @@ impl App {
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), F: 'static + FnOnce(&mut AppContext<MainThread>),
{ {
let this = self.clone(); let this = self.clone();
let platform = self.0.lock().platform.clone(); let platform = self.0.lock().platform.clone();
@ -82,7 +82,7 @@ type Handlers<Thread> =
pub struct AppContext<Thread = ()> { pub struct AppContext<Thread = ()> {
thread: PhantomData<Thread>, thread: PhantomData<Thread>,
this: Weak<Mutex<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>,
@ -97,7 +97,7 @@ pub struct AppContext<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<Thread> AppContext<Thread> { impl<Thread: 'static + Send + Sync> AppContext<Thread> {
// TODO: Better names for these? // TODO: Better names for these?
#[inline] #[inline]
pub fn downcast(&self) -> &AppContext<()> { pub fn downcast(&self) -> &AppContext<()> {
@ -119,7 +119,7 @@ impl<Thread> AppContext<Thread> {
&self.text_system &self.text_system
} }
pub fn to_async(&self) -> AsyncContext { pub fn to_async(&self) -> AsyncContext<Thread> {
AsyncContext(self.this.clone()) AsyncContext(self.this.clone())
} }
@ -133,8 +133,9 @@ impl<Thread> 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 = let main_thread_cx = unsafe {
unsafe { std::mem::transmute::<&mut AppContext, &mut AppContext<MainThread>>(cx) }; std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<MainThread>>(cx)
};
main_thread_cx.update(|cx| f(cx)) main_thread_cx.update(|cx| f(cx))
}) })
} }
@ -150,8 +151,9 @@ impl<Thread> 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 = let main_thread_cx = unsafe {
unsafe { std::mem::transmute::<&mut AppContext, &mut AppContext<MainThread>>(cx) }; std::mem::transmute::<&mut AppContext<Thread>, &mut AppContext<MainThread>>(cx)
};
main_thread_cx.update(|cx| f(cx)) main_thread_cx.update(|cx| f(cx))
}) })
} }

View file

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

View file

@ -1,5 +1,6 @@
#![allow(dead_code, unused_variables)] #![allow(dead_code, unused_variables)]
use gpui3::{Bounds, WindowBounds, WindowOptions};
use log::LevelFilter; use log::LevelFilter;
use simplelog::SimpleLogger; use simplelog::SimpleLogger;
@ -19,9 +20,20 @@ fn main() {
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
gpui3::App::production().run(|cx| { gpui3::App::production().run(|cx| {
cx.run_on_main(|cx| { let window = cx.open_window(
let window = cx.open_window(Default::default(), |cx| workspace(cx)); WindowOptions {
}); bounds: WindowBounds::Fixed(Bounds {
size: gpui3::Size {
width: 1400_f32.into(),
height: 900_f32.into(),
},
..Default::default()
}),
..Default::default()
},
|cx| workspace(cx),
);
cx.activate(true);
}); });
} }