Fix screen sharing panic introduced by call events

Joseph T. Lyons and Max Brunsfeld created

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>

Change summary

crates/call/src/call.rs           | 19 +++++++++++++------
crates/collab_ui/src/collab_ui.rs | 29 +++++++++++++++++++++++++----
2 files changed, 38 insertions(+), 10 deletions(-)

Detailed changes

crates/call/src/call.rs 🔗

@@ -263,7 +263,7 @@ impl ActiveCall {
             .borrow_mut()
             .take()
             .ok_or_else(|| anyhow!("no incoming call"))?;
-        self.report_call_event_for_room("decline incoming", call.room_id, cx);
+        Self::report_call_event_for_room("decline incoming", call.room_id, &self.client, cx);
         self.client.send(proto::DeclineCall {
             room_id: call.room_id,
         })?;
@@ -373,22 +373,29 @@ impl ActiveCall {
         self.room.as_ref().map(|(room, _)| room)
     }
 
+    pub fn client(&self) -> Arc<Client> {
+        self.client.clone()
+    }
+
     pub fn pending_invites(&self) -> &HashSet<u64> {
         &self.pending_invites
     }
 
     fn report_call_event(&self, operation: &'static str, cx: &AppContext) {
         if let Some(room) = self.room() {
-            self.report_call_event_for_room(operation, room.read(cx).id(), cx)
+            Self::report_call_event_for_room(operation, room.read(cx).id(), &self.client, cx)
         }
     }
 
-    fn report_call_event_for_room(&self, operation: &'static str, room_id: u64, cx: &AppContext) {
-        let telemetry = self.client.telemetry();
+    pub fn report_call_event_for_room(
+        operation: &'static str,
+        room_id: u64,
+        client: &Arc<Client>,
+        cx: &AppContext,
+    ) {
+        let telemetry = client.telemetry();
         let telemetry_settings = *settings::get::<TelemetrySettings>(cx);
-
         let event = ClickhouseEvent::Call { operation, room_id };
-
         telemetry.report_clickhouse_event(event, telemetry_settings);
     }
 }

crates/collab_ui/src/collab_ui.rs 🔗

@@ -11,7 +11,7 @@ mod sharing_status_indicator;
 
 use call::{ActiveCall, Room};
 pub use collab_titlebar_item::{CollabTitlebarItem, ToggleContactsMenu};
-use gpui::{actions, AppContext};
+use gpui::{actions, AppContext, Task};
 use std::sync::Arc;
 use util::ResultExt;
 use workspace::AppState;
@@ -44,9 +44,30 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
 }
 
 pub fn toggle_screen_sharing(_: &ToggleScreenSharing, cx: &mut AppContext) {
-    ActiveCall::global(cx).update(cx, |call, cx| {
-        call.toggle_screen_sharing(cx);
-    });
+    let call = ActiveCall::global(cx).read(cx);
+    if let Some(room) = call.room().cloned() {
+        let client = call.client();
+        let toggle_screen_sharing = room.update(cx, |room, cx| {
+            if room.is_screen_sharing() {
+                ActiveCall::report_call_event_for_room(
+                    "disable screen share",
+                    room.id(),
+                    &client,
+                    cx,
+                );
+                Task::ready(room.unshare_screen(cx))
+            } else {
+                ActiveCall::report_call_event_for_room(
+                    "enable screen share",
+                    room.id(),
+                    &client,
+                    cx,
+                );
+                room.share_screen(cx)
+            }
+        });
+        toggle_screen_sharing.detach_and_log_err(cx);
+    }
 }
 
 pub fn toggle_mute(_: &ToggleMute, cx: &mut AppContext) {