WIP - Start work on unfollowing automatically

Max Brunsfeld created

Change summary

crates/editor/src/items.rs        |  4 ++++
crates/workspace/src/workspace.rs | 16 ++++++++++++++++
2 files changed, 20 insertions(+)

Detailed changes

crates/editor/src/items.rs 🔗

@@ -158,6 +158,10 @@ impl FollowableItem for Editor {
         }
         Ok(())
     }
+
+    fn should_unfollow_on_event(event: &Self::Event, cx: &AppContext) -> bool {
+        false
+    }
 }
 
 fn serialize_selection(selection: &Selection<Anchor>) -> proto::Selection {

crates/workspace/src/workspace.rs 🔗

@@ -270,6 +270,7 @@ pub trait FollowableItem: Item {
         message: proto::update_view::Variant,
         cx: &mut ViewContext<Self>,
     ) -> Result<()>;
+    fn should_unfollow_on_event(event: &Self::Event, cx: &AppContext) -> bool;
 }
 
 pub trait FollowableItemHandle: ItemHandle {
@@ -285,6 +286,7 @@ pub trait FollowableItemHandle: ItemHandle {
         message: proto::update_view::Variant,
         cx: &mut MutableAppContext,
     ) -> Result<()>;
+    fn should_unfollow_on_event(&self, event: &dyn Any, cx: &AppContext) -> bool;
 }
 
 impl<T: FollowableItem> FollowableItemHandle for ViewHandle<T> {
@@ -313,6 +315,14 @@ impl<T: FollowableItem> FollowableItemHandle for ViewHandle<T> {
     ) -> Result<()> {
         self.update(cx, |this, cx| this.apply_update_message(message, cx))
     }
+
+    fn should_unfollow_on_event(&self, event: &dyn Any, cx: &AppContext) -> bool {
+        if let Some(event) = event.downcast_ref() {
+            T::should_unfollow_on_event(event, cx)
+        } else {
+            false
+        }
+    }
 }
 
 pub trait ItemHandle: 'static + fmt::Debug {
@@ -421,6 +431,12 @@ impl<T: Item> ItemHandle for ViewHandle<T> {
                 return;
             };
 
+            if let Some(item) = item.to_followable_item_handle(cx) {
+                if item.should_unfollow_on_event(event, cx) {
+                    workspace.unfollow(&pane, cx);
+                }
+            }
+
             if T::should_close_item_on_event(event) {
                 pane.update(cx, |pane, cx| pane.close_item(item.id(), cx));
                 return;