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