Use WindowHandles in a couple places

Nathan Sobo created

Change summary

crates/copilot/src/sign_in.rs | 44 +++++++++++++++++-----------------
crates/gpui/src/app.rs        | 47 ++++++++++++++++--------------------
2 files changed, 43 insertions(+), 48 deletions(-)

Detailed changes

crates/copilot/src/sign_in.rs 🔗

@@ -18,42 +18,42 @@ const COPILOT_SIGN_UP_URL: &'static str = "https://github.com/features/copilot";
 
 pub fn init(cx: &mut AppContext) {
     if let Some(copilot) = Copilot::global(cx) {
-        let mut code_verification: Option<WindowHandle<CopilotCodeVerification>> = None;
+        let mut verification_window: Option<WindowHandle<CopilotCodeVerification>> = None;
         cx.observe(&copilot, move |copilot, cx| {
             let status = copilot.read(cx).status();
 
             match &status {
                 crate::Status::SigningIn { prompt } => {
-                    if let Some(code_verification_handle) = code_verification.as_mut() {
-                        let window_id = code_verification_handle.window_id();
-                        let updated = cx.update_window(window_id, |cx| {
-                            code_verification_handle.update_root(cx, |code_verification, cx| {
-                                code_verification.set_status(status.clone(), cx)
-                            });
-                            cx.activate_window();
-                        });
-                        if updated.is_none() {
-                            code_verification = Some(create_copilot_auth_window(cx, &status));
+                    if let Some(window) = verification_window.as_mut() {
+                        let updated = window
+                            .root(cx)
+                            .map(|root| {
+                                root.update(cx, |verification, cx| {
+                                    verification.set_status(status.clone(), cx);
+                                    cx.activate_window();
+                                })
+                            })
+                            .is_some();
+                        if !updated {
+                            verification_window = Some(create_copilot_auth_window(cx, &status));
                         }
                     } else if let Some(_prompt) = prompt {
-                        code_verification = Some(create_copilot_auth_window(cx, &status));
+                        verification_window = Some(create_copilot_auth_window(cx, &status));
                     }
                 }
                 Status::Authorized | Status::Unauthorized => {
-                    if let Some(code_verification) = code_verification.as_ref() {
-                        let window_id = code_verification.window_id();
-                        cx.update_window(window_id, |cx| {
-                            code_verification.update_root(cx, |code_verification, cx| {
-                                code_verification.set_status(status, cx)
+                    if let Some(window) = verification_window.as_ref() {
+                        if let Some(verification) = window.root(cx) {
+                            verification.update(cx, |verification, cx| {
+                                verification.set_status(status, cx);
+                                cx.platform().activate(true);
+                                cx.activate_window();
                             });
-
-                            cx.platform().activate(true);
-                            cx.activate_window();
-                        });
+                        }
                     }
                 }
                 _ => {
-                    if let Some(code_verification) = code_verification.take() {
+                    if let Some(code_verification) = verification_window.take() {
                         code_verification.update(cx, |cx| cx.remove_window());
                     }
                 }

crates/gpui/src/app.rs 🔗

@@ -808,7 +808,7 @@ impl AppContext {
         result
     }
 
-    pub fn read_window<T, F: FnOnce(&WindowContext) -> T>(
+    fn read_window<T, F: FnOnce(&WindowContext) -> T>(
         &self,
         window_id: usize,
         callback: F,
@@ -3892,31 +3892,26 @@ impl<V: View> WindowHandle<V> {
         cx.read_window_with(self.window_id(), |cx| read(cx))
     }
 
-    pub fn update<C, F, R>(&self, cx: &mut C, update: F) -> R
+    pub fn update<C, F, R>(&self, cx: &mut C, update: F) -> C::Result<R>
     where
-        C: BorrowAppContext,
+        C: BorrowWindowContext,
         F: FnOnce(&mut WindowContext) -> R,
     {
-        cx.update(|cx| cx.update_window(self.window_id(), update).unwrap())
-    }
-
-    pub fn update_root<C, F, R>(&self, cx: &mut C, update: F) -> R
-    where
-        C: BorrowAppContext,
-        F: FnOnce(&mut V, &mut ViewContext<V>) -> R,
-    {
-        let window_id = self.window_id();
-        cx.update(|cx| {
-            cx.update_window(window_id, |cx| {
-                cx.root_view()
-                    .clone()
-                    .downcast::<V>()
-                    .unwrap()
-                    .update(cx, update)
-            })
-            .unwrap()
-        })
-    }
+        cx.update_window(self.window_id(), update)
+    }
+
+    // pub fn update_root<C, F, R>(&self, cx: &mut C, update: F) -> C::Result<Option<R>>
+    // where
+    //     C: BorrowWindowContext,
+    //     F: FnOnce(&mut V, &mut ViewContext<V>) -> R,
+    // {
+    //     cx.update_window(self.window_id, |cx| {
+    //         cx.root_view()
+    //             .clone()
+    //             .downcast::<V>()
+    //             .map(|v| v.update(cx, update))
+    //     })
+    // }
 
     pub fn read_root<'a>(&self, cx: &'a AppContext) -> &'a V {
         let root_view = cx
@@ -3940,9 +3935,9 @@ impl<V: View> WindowHandle<V> {
         })
     }
 
-    pub fn add_view<C, U, F>(&self, cx: &mut C, build_view: F) -> ViewHandle<U>
+    pub fn add_view<C, U, F>(&self, cx: &mut C, build_view: F) -> C::Result<ViewHandle<U>>
     where
-        C: BorrowAppContext,
+        C: BorrowWindowContext,
         U: View,
         F: FnOnce(&mut ViewContext<U>) -> U,
     {
@@ -4836,7 +4831,7 @@ mod tests {
         let called_defer = Rc::new(AtomicBool::new(false));
         let called_after_window_update = Rc::new(AtomicBool::new(false));
 
-        window.update_root(cx, |this, cx| {
+        window.root(cx).update(cx, |this, cx| {
             assert_eq!(this.render_count, 1);
             cx.defer({
                 let called_defer = called_defer.clone();