From 8cac89d17cde7db81de9f44f7ff9d7e5a3cbbaad Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Fri, 29 Sep 2023 20:51:45 -0400 Subject: [PATCH] Checkpoint: Compiling --- crates/gpui3/src/app.rs | 68 ++++++++++++++--------------- crates/gpui3/src/window.rs | 6 +-- crates/storybook2/src/storybook2.rs | 4 +- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index 5d33f57247..1bbcc8c608 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -137,40 +137,38 @@ impl AppContext { AsyncContext(self.this.clone()) } - // pub fn run_on_main( - // &self, - // f: impl FnOnce(&mut AppContext) -> R + Send + 'static, - // ) -> impl Future - // where - // R: Send + 'static, - // { - // let this = self.this.upgrade().unwrap(); - // run_on_main(self.dispatcher.clone(), move || { - // let cx = &mut *this.lock(); - // let main_thread_cx: &mut AppContext = unsafe { std::mem::transmute(cx) }; - // main_thread_cx.update(|cx| f(cx)) - // }) - // } + pub fn run_on_main( + &self, + f: impl FnOnce(&mut AppContext) -> R + Send + 'static, + ) -> impl Future + where + R: Send + 'static, + { + 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, &mut AppContext>(cx) }; + main_thread_cx.update(|cx| f(cx)) + }) + } - // pub fn spawn_on_main( - // &self, - // f: impl FnOnce(&mut AppContext) -> F + Send + 'static, - // ) -> impl Future - // where - // F: Future + 'static, - // R: Send + 'static, - // { - // let this = self.this.upgrade().unwrap(); - // spawn_on_main(self.dispatcher.clone(), move || { - // let cx = &mut *this.lock(); - // let platform = cx.platform.borrow_on_main_thread().clone(); - // // todo!() - // // cx.update(|cx| f(&mut MainThreadContext::mutable(cx, platform.as_ref()))) - - // future::ready(()) - // }) - // // self.platform.read(move |platform| { - // } + pub fn spawn_on_main( + &self, + f: impl FnOnce(&mut AppContext) -> F + Send + 'static, + ) -> impl Future + where + F: Future + 'static, + R: Send + 'static, + { + 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, &mut AppContext>(cx) }; + main_thread_cx.update(|cx| f(cx)) + }) + } pub fn text_style(&self) -> TextStyle { let mut style = TextStyle::default(); @@ -319,7 +317,7 @@ impl Context for AppContext { } impl AppContext { - fn platform(&self) -> &dyn Platform { + pub(crate) fn platform(&self) -> &dyn Platform { self.platform.borrow_on_main_thread() } @@ -334,7 +332,7 @@ impl AppContext { ) -> WindowHandle { let id = self.windows.insert(None); let handle = WindowHandle::new(id); - let mut window = Window::new(handle.into(), options, self.platform(), self); + let mut window = Window::new(handle.into(), options, self); let root_view = build_root_view(&mut WindowContext::mutable(self, &mut window)); window.root_view.replace(root_view.into_any()); self.windows.get_mut(id).unwrap().replace(window); diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 346b5cb25b..fda69ba650 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -26,10 +26,9 @@ impl Window { pub fn new( handle: AnyWindowHandle, options: WindowOptions, - platform: &dyn Platform, cx: &mut AppContext, ) -> Self { - let platform_window = platform.open_window(handle, options); + let platform_window = cx.platform().open_window(handle, options); let mouse_position = platform_window.mouse_position(); let content_size = platform_window.content_size(); let scale_factor = platform_window.scale_factor(); @@ -46,7 +45,8 @@ impl Window { } })); - let platform_window = MainThreadOnly::new(Arc::new(platform_window), platform.dispatcher()); + let platform_window = + MainThreadOnly::new(Arc::new(platform_window), cx.platform().dispatcher()); Window { handle, diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/storybook2.rs index bc927b78fa..a9fbc050b7 100644 --- a/crates/storybook2/src/storybook2.rs +++ b/crates/storybook2/src/storybook2.rs @@ -17,7 +17,9 @@ fn main() { SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); gpui3::App::production().run(|cx| { - let window = cx.open_window(Default::default(), |cx| workspace(cx)); + cx.run_on_main(|cx| { + let window = cx.open_window(Default::default(), |cx| workspace(cx)); + }); }); }