| 1 | use calling::{ |
| 2 | CallCommand, CallSpec, Callee, EndKind, EndReason, HoldState, |
| 3 | }; |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | use super::background::BackgroundPick; |
| 7 | use super::demo::DemoCall; |
| 8 | use super::health::HealthBanner; |
| 9 | use super::model::{ActiveCall, BreakoutState, CallKind, CallModel, PendingMove}; |
| 10 | use super::recording_menu::RecordAction; |
| 11 | use super::target::plan_for_chat; |
| 12 | use crate::app_state::{AppEvent, AppState}; |
| 13 | use crate::data::is_one_on_one; |
| 14 | use crate::views::call_transcript::TranscriptPanel; |
| 15 | |
| 16 | const DEMO_ROOM_GUESTS: usize = 3; |
| 17 | |
| 18 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 19 | pub struct TransferCandidate { |
| 20 | pub mri: String, |
| 21 | pub name: String, |
| 22 | pub user_id: Option<String>, |
| 23 | pub chat_id: String, |
| 24 | } |
| 25 | |
| 26 | pub fn room_closed_move(call: &ActiveCall, reason: &EndReason) -> Option<PendingMove> { |
| 27 | let room = call.model.breakout.as_ref()?; |
| 28 | if *reason != EndReason::Remote(EndKind::RoomClosed) { |
| 29 | return None; |
| 30 | } |
| 31 | Some(return_move(room)) |
| 32 | } |
| 33 | |
| 34 | fn return_move(room: &BreakoutState) -> PendingMove { |
| 35 | PendingMove { |
| 36 | target: room.main.clone().expect("a room with a way back"), |
| 37 | title: room.main_title.clone(), |
| 38 | breakout: None, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl AppState { |
| 43 | pub fn run_call_record_action(&mut self, action: RecordAction, _cx: &mut Context<Self>) { |
| 44 | let Some(call) = self.call.as_ref().filter(|call| call.model.can_record()) else { |
| 45 | return; |
| 46 | }; |
| 47 | let command = match action { |
| 48 | RecordAction::Start(kind) => CallCommand::StartRecording { |
| 49 | kind, |
| 50 | title: call.model.title.clone(), |
| 51 | }, |
| 52 | RecordAction::Stop(scope) => CallCommand::StopRecording { scope }, |
| 53 | }; |
| 54 | self.send_call_command(command); |
| 55 | } |
| 56 | |
| 57 | pub fn open_call_transcript( |
| 58 | &mut self, |
| 59 | panel: Entity<TranscriptPanel>, |
| 60 | cx: &mut Context<Self>, |
| 61 | ) { |
| 62 | self.transcript_panel = Some(panel); |
| 63 | self.show_call_transcript(cx); |
| 64 | } |
| 65 | |
| 66 | pub(super) fn show_call_transcript(&mut self, cx: &mut Context<Self>) { |
| 67 | let Some(call) = self |
| 68 | .call |
| 69 | .as_mut() |
| 70 | .filter(|call| call.model.kind == CallKind::Meeting) |
| 71 | else { |
| 72 | return; |
| 73 | }; |
| 74 | call.chat_open = false; |
| 75 | call.people_open = false; |
| 76 | call.transcript_open = true; |
| 77 | let captions_off = !call.model.captions_on(); |
| 78 | if captions_off { |
| 79 | self.send_call_command(CallCommand::SetCaptions(true)); |
| 80 | } |
| 81 | cx.emit(AppEvent::Call); |
| 82 | cx.notify(); |
| 83 | } |
| 84 | |
| 85 | pub fn close_call_transcript(&mut self, cx: &mut Context<Self>) { |
| 86 | if let Some(call) = self.call.as_mut() { |
| 87 | call.transcript_open = false; |
| 88 | } |
| 89 | self.transcript_panel = None; |
| 90 | cx.emit(AppEvent::Call); |
| 91 | cx.notify(); |
| 92 | } |
| 93 | |
| 94 | pub fn choose_call_caption_language(&mut self, tag: &str, cx: &mut Context<Self>) { |
| 95 | let Some(call) = self |
| 96 | .call |
| 97 | .as_mut() |
| 98 | .filter(|call| call.model.is_active() && call.model.kind == CallKind::Meeting) |
| 99 | else { |
| 100 | return; |
| 101 | }; |
| 102 | call.model.choose_caption_language(tag); |
| 103 | let captions_off = !call.model.captions_on(); |
| 104 | if captions_off { |
| 105 | self.send_call_command(CallCommand::SetCaptions(true)); |
| 106 | } |
| 107 | self.send_ready_caption_language(); |
| 108 | cx.notify(); |
| 109 | } |
| 110 | |
| 111 | fn send_ready_caption_language(&mut self) { |
| 112 | let language = self |
| 113 | .call |
| 114 | .as_mut() |
| 115 | .and_then(|call| call.model.take_ready_caption_language()); |
| 116 | if let Some(language) = language { |
| 117 | self.send_call_command(CallCommand::SetCaptionLanguage(language)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub fn accept_recording_notice(&mut self, _cx: &mut Context<Self>) { |
| 122 | if self |
| 123 | .call |
| 124 | .as_ref() |
| 125 | .is_some_and(|call| call.model.consent_required) |
| 126 | { |
| 127 | self.send_call_command(CallCommand::ConsentToRecording); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pub fn toggle_call_hold(&mut self, _cx: &mut Context<Self>) { |
| 132 | let Some(call) = self.call.as_ref() else { |
| 133 | return; |
| 134 | }; |
| 135 | let hold = match call.model.hold { |
| 136 | HoldState::Local => false, |
| 137 | HoldState::Active if call.model.can_hold() => true, |
| 138 | _ => return, |
| 139 | }; |
| 140 | self.send_call_command(CallCommand::Hold(hold)); |
| 141 | } |
| 142 | |
| 143 | pub fn set_call_incoming_video(&mut self, enabled: bool, cx: &mut Context<Self>) { |
| 144 | self.send_call_command(CallCommand::SetIncomingVideo(enabled)); |
| 145 | cx.notify(); |
| 146 | } |
| 147 | |
| 148 | pub fn close_call_health_banner(&mut self, banner: HealthBanner, cx: &mut Context<Self>) { |
| 149 | if let Some(call) = self.call.as_mut() { |
| 150 | call.model.health.close_banner(banner); |
| 151 | cx.notify(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | pub fn turn_off_call_effects(&mut self, cx: &mut Context<Self>) { |
| 156 | self.set_call_background(BackgroundPick::None, cx); |
| 157 | let effects = calling::CameraEffects { |
| 158 | auto_framing: false, |
| 159 | adjust_brightness: false, |
| 160 | soft_focus: false, |
| 161 | ..self.call_effects |
| 162 | }; |
| 163 | self.set_call_effects(effects, cx); |
| 164 | } |
| 165 | |
| 166 | pub fn open_call_whiteboard(&mut self, cx: &mut Context<Self>) { |
| 167 | let Some(call) = self |
| 168 | .call |
| 169 | .as_ref() |
| 170 | .filter(|call| call.model.can_open_whiteboard()) |
| 171 | else { |
| 172 | return; |
| 173 | }; |
| 174 | if let Some(url) = call.model.whiteboard_url() { |
| 175 | cx.open_url(url); |
| 176 | return; |
| 177 | } |
| 178 | let title = call.model.title.clone(); |
| 179 | self.send_call_command(CallCommand::OpenWhiteboard { title }); |
| 180 | } |
| 181 | |
| 182 | pub fn transfer_candidates(&self) -> Vec<TransferCandidate> { |
| 183 | let peer = self |
| 184 | .call |
| 185 | .as_ref() |
| 186 | .and_then(|call| call.model.tiles.first().map(|tile| tile.mri.clone())); |
| 187 | self.sidebar |
| 188 | .chats |
| 189 | .iter() |
| 190 | .filter(|chat| is_one_on_one(chat)) |
| 191 | .filter_map(|chat| { |
| 192 | let plan = plan_for_chat(chat, self.directory.me.as_ref())?; |
| 193 | let (mri, name) = plan.callees.into_iter().next()?; |
| 194 | Some(TransferCandidate { |
| 195 | user_id: mri.strip_prefix("8:orgid:").map(str::to_owned), |
| 196 | mri, |
| 197 | name, |
| 198 | chat_id: chat.id.clone(), |
| 199 | }) |
| 200 | }) |
| 201 | .filter(|candidate| Some(&candidate.mri) != peer.as_ref()) |
| 202 | .collect() |
| 203 | } |
| 204 | |
| 205 | pub fn can_transfer_call(&self) -> bool { |
| 206 | self.call |
| 207 | .as_ref() |
| 208 | .is_some_and(|call| call.model.can_transfer() && call.consult.is_none()) |
| 209 | && self.held_call.is_none() |
| 210 | } |
| 211 | |
| 212 | pub fn open_transfer_picker( |
| 213 | &mut self, |
| 214 | picker: Entity<crate::views::transfer_picker::TransferPicker>, |
| 215 | cx: &mut Context<Self>, |
| 216 | ) { |
| 217 | if self.can_transfer_call() { |
| 218 | self.transfer_picker = Some(picker); |
| 219 | cx.notify(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | pub fn close_transfer_picker(&mut self, cx: &mut Context<Self>) { |
| 224 | if self.transfer_picker.take().is_some() { |
| 225 | cx.notify(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | pub fn transfer_call_blind(&mut self, target: TransferCandidate, cx: &mut Context<Self>) { |
| 230 | self.close_transfer_picker(cx); |
| 231 | if !self.can_transfer_call() { |
| 232 | return; |
| 233 | } |
| 234 | let callee = Callee { |
| 235 | mri: target.mri, |
| 236 | display_name: target.name, |
| 237 | }; |
| 238 | self.send_call_command(CallCommand::Transfer { |
| 239 | target: callee, |
| 240 | replaces: None, |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | pub(super) fn begin_breakout_move( |
| 245 | &mut self, |
| 246 | moved: calling::BreakoutMove, |
| 247 | cx: &mut Context<Self>, |
| 248 | ) { |
| 249 | let Some(call) = self |
| 250 | .call |
| 251 | .as_mut() |
| 252 | .filter(|call| call.pending_move.is_none()) |
| 253 | else { |
| 254 | return; |
| 255 | }; |
| 256 | let (main, main_title) = match &call.model.breakout { |
| 257 | Some(room) => ( |
| 258 | room.main |
| 259 | .clone() |
| 260 | .or_else(|| call.model.main_meeting.clone()), |
| 261 | room.main_title.clone(), |
| 262 | ), |
| 263 | None => (call.meeting_target.clone(), call.model.title.clone()), |
| 264 | }; |
| 265 | let pending = if moved.returning { |
| 266 | PendingMove { |
| 267 | target: moved.target, |
| 268 | title: main_title, |
| 269 | breakout: None, |
| 270 | } |
| 271 | } else { |
| 272 | let breakout = BreakoutState { |
| 273 | room_name: moved.room_name.clone(), |
| 274 | main, |
| 275 | main_title, |
| 276 | }; |
| 277 | PendingMove { |
| 278 | target: moved.target, |
| 279 | title: moved.room_name.clone(), |
| 280 | breakout: Some(breakout), |
| 281 | } |
| 282 | }; |
| 283 | let notice = if moved.returning { |
| 284 | "Moving you back to the main meeting".to_owned() |
| 285 | } else { |
| 286 | format!("Moving you to {}", moved.room_name) |
| 287 | }; |
| 288 | call.pending_move = Some(pending); |
| 289 | self.raise_notice(notice, None, cx); |
| 290 | self.send_call_command(CallCommand::Hangup); |
| 291 | } |
| 292 | |
| 293 | pub fn return_to_main_meeting(&mut self, _cx: &mut Context<Self>) { |
| 294 | let Some(call) = self |
| 295 | .call |
| 296 | .as_mut() |
| 297 | .filter(|call| call.pending_move.is_none()) |
| 298 | else { |
| 299 | return; |
| 300 | }; |
| 301 | let Some(room) = call |
| 302 | .model |
| 303 | .breakout |
| 304 | .as_ref() |
| 305 | .filter(|room| room.main.is_some()) |
| 306 | else { |
| 307 | return; |
| 308 | }; |
| 309 | call.pending_move = Some(return_move(room)); |
| 310 | self.send_call_command(CallCommand::Hangup); |
| 311 | } |
| 312 | |
| 313 | pub(super) fn join_moved_call(&mut self, moved: PendingMove, cx: &mut Context<Self>) { |
| 314 | let demo = DemoCall::Meeting { |
| 315 | guests: super::demo::demo_guests(DEMO_ROOM_GUESTS), |
| 316 | lobby: false, |
| 317 | organizer: false, |
| 318 | }; |
| 319 | let Some(handle) = |
| 320 | self.place_call(CallSpec::Meeting(moved.target.clone()), demo, false, cx) |
| 321 | else { |
| 322 | return; |
| 323 | }; |
| 324 | let mut model = CallModel::meeting(&moved.title, false); |
| 325 | model.breakout = moved.breakout; |
| 326 | self.begin_call(handle, model, Some(moved.target.thread_id.clone()), cx); |
| 327 | if let Some(call) = self.call.as_mut() { |
| 328 | call.meeting_target = Some(moved.target); |
| 329 | } |
| 330 | } |
| 331 | } |