Checkpoint: Compiling

Marshall Bowers created

Change summary

crates/gpui3/src/app.rs             | 70 +++++++++++++++---------------
crates/gpui3/src/window.rs          |  6 +-
crates/storybook2/src/storybook2.rs |  4 +
3 files changed, 40 insertions(+), 40 deletions(-)

Detailed changes

crates/gpui3/src/app.rs 🔗

@@ -137,40 +137,38 @@ impl<Thread> AppContext<Thread> {
         AsyncContext(self.this.clone())
     }
 
-    // pub fn run_on_main<R>(
-    //     &self,
-    //     f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,
-    // ) -> impl Future<Output = R>
-    // 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<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 AppContext<MainThread>) -> F + Send + 'static,
-    // ) -> impl Future<Output = R>
-    // where
-    //     F: Future<Output = R> + '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 run_on_main<R>(
+        &self,
+        f: impl FnOnce(&mut AppContext<MainThread>) -> R + Send + 'static,
+    ) -> impl Future<Output = R>
+    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<MainThread>>(cx) };
+            main_thread_cx.update(|cx| f(cx))
+        })
+    }
+
+    pub fn spawn_on_main<F, R>(
+        &self,
+        f: impl FnOnce(&mut AppContext<MainThread>) -> F + Send + 'static,
+    ) -> impl Future<Output = R>
+    where
+        F: Future<Output = R> + '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<MainThread>>(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<MainThread> {
-    fn platform(&self) -> &dyn Platform {
+    pub(crate) fn platform(&self) -> &dyn Platform {
         self.platform.borrow_on_main_thread()
     }
 
@@ -334,7 +332,7 @@ impl AppContext<MainThread> {
     ) -> WindowHandle<S> {
         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);

crates/gpui3/src/window.rs 🔗

@@ -26,10 +26,9 @@ impl Window {
     pub fn new(
         handle: AnyWindowHandle,
         options: WindowOptions,
-        platform: &dyn Platform,
         cx: &mut AppContext<MainThread>,
     ) -> 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,

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));
+        });
     });
 }