WIP

Nathan Sobo created

Change summary

crates/workspace2/src/workspace2.rs | 112 ++++++++++++++----------------
1 file changed, 54 insertions(+), 58 deletions(-)

Detailed changes

crates/workspace2/src/workspace2.rs 🔗

@@ -23,9 +23,9 @@ use futures::{
     FutureExt,
 };
 use gpui2::{
-    AnyModel, AnyView, AppContext, AsyncAppContext, AsyncWindowContext, DisplayId, EventEmitter,
-    MainThread, Model, ModelContext, Subscription, Task, View, ViewContext, VisualContext,
-    WeakModel, WeakView, WindowBounds, WindowHandle, WindowOptions,
+    AnyModel, AnyView, AppContext, AsyncAppContext, AsyncWindowContext, DisplayId, Entity,
+    EventEmitter, MainThread, Model, ModelContext, Subscription, Task, View, ViewContext,
+    VisualContext, WeakModel, WeakView, WindowBounds, WindowContext, WindowHandle, WindowOptions,
 };
 use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem};
 use language2::LanguageRegistry;
@@ -426,7 +426,7 @@ pub struct AppState {
 }
 
 pub struct WorkspaceStore {
-    workspaces: HashSet<WeakView<Workspace>>,
+    workspaces: HashSet<WindowHandle<Workspace>>,
     followers: Vec<Follower>,
     client: Arc<Client>,
     _subscriptions: Vec<client2::Subscription>,
@@ -3023,7 +3023,7 @@ impl Workspace {
         &self,
         project_only: bool,
         update: proto::update_followers::Variant,
-        cx: &mut AppContext,
+        cx: &mut WindowContext,
     ) -> Option<()> {
         let project_id = if project_only {
             self.project.read(cx).remote_id()
@@ -3895,17 +3895,16 @@ impl EventEmitter for Workspace {
 
 impl WorkspaceStore {
     pub fn new(client: Arc<Client>, cx: &mut ModelContext<Self>) -> Self {
-        // Self {
-        //     workspaces: Default::default(),
-        //     followers: Default::default(),
-        //     _subscriptions: vec![
-        //         client.add_request_handler(cx.weak_model(), Self::handle_follow),
-        //         client.add_message_handler(cx.weak_model(), Self::handle_unfollow),
-        //         client.add_message_handler(cx.weak_model(), Self::handle_update_followers),
-        //     ],
-        //     client,
-        // }
-        todo!()
+        Self {
+            workspaces: Default::default(),
+            followers: Default::default(),
+            _subscriptions: vec![],
+            //     client.add_request_handler(cx.weak_model(), Self::handle_follow),
+            //     client.add_message_handler(cx.weak_model(), Self::handle_unfollow),
+            //     client.add_message_handler(cx.weak_model(), Self::handle_update_followers),
+            // ],
+            client,
+        }
     }
 
     pub fn update_followers(
@@ -3943,53 +3942,50 @@ impl WorkspaceStore {
             .log_err()
     }
 
-    // async fn handle_follow(
-    //     this: Model<Self>,
-    //     envelope: TypedEnvelope<proto::Follow>,
-    //     _: Arc<Client>,
-    //     mut cx: AsyncAppContext,
-    // ) -> Result<proto::FollowResponse> {
-    //     this.update(&mut cx, |this, cx| {
-    //         let follower = Follower {
-    //             project_id: envelope.payload.project_id,
-    //             peer_id: envelope.original_sender_id()?,
-    //         };
-    //         let active_project = ActiveCall::global(cx)
-    //             .read(cx)
-    //             .location()
-    //             .map(|project| project.id());
+    async fn handle_follow(
+        this: Model<Self>,
+        envelope: TypedEnvelope<proto::Follow>,
+        _: Arc<Client>,
+        mut cx: AsyncAppContext,
+    ) -> Result<proto::FollowResponse> {
+        this.update(&mut cx, |this, cx| {
+            let follower = Follower {
+                project_id: envelope.payload.project_id,
+                peer_id: envelope.original_sender_id()?,
+            };
+            let active_project = ActiveCall::global(cx).read(cx).location();
 
-    //         let mut response = proto::FollowResponse::default();
-    //         for workspace in &this.workspaces {
-    //             let Some(workspace) = workspace.upgrade(cx) else {
-    //                 continue;
-    //             };
+            let mut response = proto::FollowResponse::default();
+            for workspace in &this.workspaces {
+                let Some(workspace) = workspace.upgrade(cx) else {
+                    continue;
+                };
 
-    //             workspace.update(cx.as_mut(), |workspace, cx| {
-    //                 let handler_response = workspace.handle_follow(follower.project_id, cx);
-    //                 if response.views.is_empty() {
-    //                     response.views = handler_response.views;
-    //                 } else {
-    //                     response.views.extend_from_slice(&handler_response.views);
-    //                 }
+                workspace.update(cx, |workspace, cx| {
+                    let handler_response = workspace.handle_follow(follower.project_id, cx);
+                    if response.views.is_empty() {
+                        response.views = handler_response.views;
+                    } else {
+                        response.views.extend_from_slice(&handler_response.views);
+                    }
 
-    //                 if let Some(active_view_id) = handler_response.active_view_id.clone() {
-    //                     if response.active_view_id.is_none()
-    //                         || Some(workspace.project.id()) == active_project
-    //                     {
-    //                         response.active_view_id = Some(active_view_id);
-    //                     }
-    //                 }
-    //             });
-    //         }
+                    if let Some(active_view_id) = handler_response.active_view_id.clone() {
+                        if response.active_view_id.is_none()
+                            || Some(workspace.project.downgrade()) == active_project
+                        {
+                            response.active_view_id = Some(active_view_id);
+                        }
+                    }
+                });
+            }
 
-    //         if let Err(ix) = this.followers.binary_search(&follower) {
-    //             this.followers.insert(ix, follower);
-    //         }
+            if let Err(ix) = this.followers.binary_search(&follower) {
+                this.followers.insert(ix, follower);
+            }
 
-    //         Ok(response)
-    //     })
-    // }
+            Ok(response)
+        })?
+    }
 
     async fn handle_unfollow(
         model: Model<Self>,