| 1 | use app_ui_kit::layout::ISLAND_GAP; |
| 2 | use gpui_kit::component::{TitleBar, elevation, h_flex, v_flex}; |
| 3 | use gpui_kit::prelude::FluentBuilder as _; |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | use super::actions::ToggleCallMute; |
| 7 | use super::call_end_view::render_call_end; |
| 8 | use super::call_pop_out::zoom_listener; |
| 9 | use super::call_stage::render_stage_overlay; |
| 10 | use super::call_view::render_call_view; |
| 11 | use super::conversation::ConversationView; |
| 12 | use super::prejoin_view::PrejoinView; |
| 13 | use super::shortcuts::{ |
| 14 | HangUpCall, OpenLobbyList, PushToTalk, ToggleCallCamera, ToggleCallFullscreen, ToggleCallHand, |
| 15 | ToggleCallShare, ZoomShareIn, ZoomShareOut, ZoomShareReset, |
| 16 | }; |
| 17 | use super::title_bar::{TITLE_BAR_HEIGHT, app_icon_image}; |
| 18 | use crate::app_state::{AppState, Selection}; |
| 19 | use crate::call::ZoomStep; |
| 20 | use crate::theme; |
| 21 | |
| 22 | pub const CALL_WINDOW_TAG: &str = "call-window"; |
| 23 | pub const CALL_WINDOW_SIZE: (f32, f32) = (1180., 760.); |
| 24 | pub const CALL_WINDOW_MIN_SIZE: (f32, f32) = (560., 420.); |
| 25 | const FALLBACK_TITLE: &str = "Call"; |
| 26 | const KEY_CONTEXT: &str = "CallWindow"; |
| 27 | const APP_ICON_SIZE: f32 = 18.; |
| 28 | |
| 29 | pub fn call_window_title(state: &AppState) -> String { |
| 30 | state |
| 31 | .call |
| 32 | .as_ref() |
| 33 | .map(|call| call.model.title.clone()) |
| 34 | .or_else(|| { |
| 35 | state |
| 36 | .prejoin |
| 37 | .as_ref() |
| 38 | .map(|pending| pending.join.title.clone()) |
| 39 | }) |
| 40 | .filter(|title| !title.is_empty()) |
| 41 | .unwrap_or_else(|| FALLBACK_TITLE.to_owned()) |
| 42 | } |
| 43 | |
| 44 | /// The meeting window: like Teams, a call or meeting runs in its own window and the main window stays free for chats. |
| 45 | pub struct CallWindowView { |
| 46 | app: Entity<AppState>, |
| 47 | focus: FocusHandle, |
| 48 | call_chat: Option<(String, Entity<ConversationView>)>, |
| 49 | prejoin: Option<Entity<PrejoinView>>, |
| 50 | fullscreen: bool, |
| 51 | was_fullscreen: bool, |
| 52 | _app_subscription: Subscription, |
| 53 | _media_subscription: Option<Subscription>, |
| 54 | } |
| 55 | |
| 56 | impl CallWindowView { |
| 57 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 58 | let app_subscription = cx.observe(&app, |_, _, cx| cx.notify()); |
| 59 | let media_subscription = app |
| 60 | .read(cx) |
| 61 | .call_media |
| 62 | .clone() |
| 63 | .map(|media| cx.observe(&media, |_, _, cx| cx.notify())); |
| 64 | let closing_app = app.clone(); |
| 65 | window.on_window_should_close(cx, move |_, cx| { |
| 66 | leave_or_dismiss(&closing_app, cx); |
| 67 | true |
| 68 | }); |
| 69 | let focus = cx.focus_handle(); |
| 70 | window.focus(&focus, cx); |
| 71 | CallWindowView { |
| 72 | app, |
| 73 | focus, |
| 74 | call_chat: None, |
| 75 | prejoin: None, |
| 76 | fullscreen: false, |
| 77 | was_fullscreen: false, |
| 78 | _app_subscription: app_subscription, |
| 79 | _media_subscription: media_subscription, |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn sync_call_chat( |
| 84 | &mut self, |
| 85 | thread: Option<String>, |
| 86 | window: &mut Window, |
| 87 | cx: &mut Context<Self>, |
| 88 | ) { |
| 89 | match (thread, &self.call_chat) { |
| 90 | (Some(wanted), Some((open, _))) if &wanted == open => {} |
| 91 | (Some(wanted), _) => { |
| 92 | let selection = Selection::Chat(wanted.clone()); |
| 93 | let app = self.app.clone(); |
| 94 | let view = cx.new(|cx| ConversationView::pinned_to(app, selection, window, cx)); |
| 95 | self.call_chat = Some((wanted, view)); |
| 96 | } |
| 97 | (None, _) => self.call_chat = None, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | fn sync_prejoin(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 102 | let wanted = self.app.read(cx).prejoin.clone(); |
| 103 | let open = self.prejoin.as_ref().map(|view| view.read(cx).pending_id()); |
| 104 | match (wanted, open) { |
| 105 | (Some(pending), Some(id)) if pending.id == id => {} |
| 106 | (Some(pending), _) => { |
| 107 | let app = self.app.clone(); |
| 108 | self.prejoin = Some(cx.new(|cx| PrejoinView::new(app, &pending, window, cx))); |
| 109 | } |
| 110 | (None, _) => self.prejoin = None, |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn sync_fullscreen(&mut self, wanted: bool, window: &mut Window) { |
| 115 | if wanted == self.fullscreen { |
| 116 | return; |
| 117 | } |
| 118 | self.fullscreen = wanted; |
| 119 | if wanted { |
| 120 | self.was_fullscreen = window.is_fullscreen(); |
| 121 | } |
| 122 | if wanted != window.is_fullscreen() && (wanted || !self.was_fullscreen) { |
| 123 | window.toggle_fullscreen(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | fn title_bar(&self, title: String, cx: &App) -> impl IntoElement { |
| 128 | let app_icon = crate::active_appearance::current(cx).app_icon; |
| 129 | let app = self.app.clone(); |
| 130 | TitleBar::new() |
| 131 | .h(px(TITLE_BAR_HEIGHT)) |
| 132 | .border_b_0() |
| 133 | .bg(theme::background()) |
| 134 | .on_close_window(move |_, window, cx| { |
| 135 | leave_or_dismiss(&app, cx); |
| 136 | window.remove_window(); |
| 137 | }) |
| 138 | .child( |
| 139 | h_flex() |
| 140 | .debug_selector(|| "call-window/title-bar".into()) |
| 141 | .flex_1() |
| 142 | .min_w_0() |
| 143 | .h_full() |
| 144 | .gap(px(12.)) |
| 145 | .items_center() |
| 146 | .child( |
| 147 | img(app_icon_image(app_icon)) |
| 148 | .size(px(APP_ICON_SIZE)) |
| 149 | .flex_none(), |
| 150 | ) |
| 151 | .child( |
| 152 | div() |
| 153 | .min_w_0() |
| 154 | .truncate() |
| 155 | .text_size(px(13.)) |
| 156 | .font_weight(FontWeight::SEMIBOLD) |
| 157 | .text_color(theme::text()) |
| 158 | .child(title), |
| 159 | ), |
| 160 | ) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn call_action<A: Action>( |
| 165 | app: &Entity<AppState>, |
| 166 | action: fn(&mut AppState, &mut Context<AppState>), |
| 167 | ) -> impl Fn(&A, &mut Window, &mut App) + 'static { |
| 168 | let app = app.clone(); |
| 169 | move |_, _, cx| app.update(cx, |state, cx| action(state, cx)) |
| 170 | } |
| 171 | |
| 172 | fn leave_or_dismiss(app: &Entity<AppState>, cx: &mut App) { |
| 173 | app.update(cx, |state, cx| { |
| 174 | if state.call.is_some() { |
| 175 | state.leave_call(cx); |
| 176 | } else if state.prejoin.is_some() { |
| 177 | state.cancel_prejoin(cx); |
| 178 | } else { |
| 179 | state.dismiss_call_end(cx); |
| 180 | } |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | impl Focusable for CallWindowView { |
| 185 | fn focus_handle(&self, _: &App) -> FocusHandle { |
| 186 | self.focus.clone() |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | impl Render for CallWindowView { |
| 191 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 192 | let chat_thread = self.app.read(cx).call_chat_thread(); |
| 193 | self.sync_call_chat(chat_thread, window, cx); |
| 194 | self.sync_prejoin(window, cx); |
| 195 | let state = self.app.read(cx); |
| 196 | let fullscreen = state.call_fullscreen(); |
| 197 | let open_dialogs = [ |
| 198 | state.transfer_picker.is_some(), |
| 199 | state |
| 200 | .call |
| 201 | .as_ref() |
| 202 | .is_some_and(|call| call.options_dialog.is_some()), |
| 203 | state |
| 204 | .call |
| 205 | .as_ref() |
| 206 | .is_some_and(|call| call.model.consent_required), |
| 207 | ] |
| 208 | .into_iter() |
| 209 | .filter(|open| *open) |
| 210 | .count(); |
| 211 | let title = call_window_title(state); |
| 212 | window.set_window_title(&title); |
| 213 | let window_width = f32::from(window.viewport_size().width); |
| 214 | let stage_width = window_width - 2. * ISLAND_GAP; |
| 215 | let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone().into()); |
| 216 | let stage = render_call_view(&self.app, state, chat_panel, fullscreen, stage_width) |
| 217 | .or_else(|| render_call_end(&self.app, state)) |
| 218 | .or_else(|| { |
| 219 | self.prejoin |
| 220 | .clone() |
| 221 | .map(|prejoin| prejoin.into_any_element()) |
| 222 | }); |
| 223 | let stage_overlay = render_stage_overlay(&self.app, state); |
| 224 | elevation::set_app_modal_layers(window, open_dialogs, cx); |
| 225 | self.sync_fullscreen(fullscreen, window); |
| 226 | let title_bar = (!fullscreen).then(|| self.title_bar(title, cx)); |
| 227 | let release_app = self.app.clone(); |
| 228 | let modifier_app = self.app.clone(); |
| 229 | let escape_app = self.app.clone(); |
| 230 | v_flex() |
| 231 | .id(CALL_WINDOW_TAG) |
| 232 | .debug_selector(|| CALL_WINDOW_TAG.into()) |
| 233 | .key_context(KEY_CONTEXT) |
| 234 | .track_focus(&self.focus) |
| 235 | .size_full() |
| 236 | .bg(theme::background()) |
| 237 | .text_color(theme::text()) |
| 238 | .text_size(px(14.)) |
| 239 | .on_action(call_action::<ToggleCallMute>( |
| 240 | &self.app, |
| 241 | AppState::toggle_call_mute, |
| 242 | )) |
| 243 | .on_action(call_action::<PushToTalk>( |
| 244 | &self.app, |
| 245 | AppState::press_push_to_talk, |
| 246 | )) |
| 247 | .on_action(call_action::<HangUpCall>(&self.app, AppState::leave_call)) |
| 248 | .on_action(call_action::<ToggleCallCamera>( |
| 249 | &self.app, |
| 250 | AppState::toggle_call_camera, |
| 251 | )) |
| 252 | .on_action(call_action::<ToggleCallShare>( |
| 253 | &self.app, |
| 254 | AppState::toggle_call_share, |
| 255 | )) |
| 256 | .on_action(call_action::<ToggleCallHand>( |
| 257 | &self.app, |
| 258 | AppState::toggle_call_hand, |
| 259 | )) |
| 260 | .on_action(call_action::<OpenLobbyList>( |
| 261 | &self.app, |
| 262 | AppState::open_call_lobby_list, |
| 263 | )) |
| 264 | .on_action(call_action::<ToggleCallFullscreen>( |
| 265 | &self.app, |
| 266 | AppState::toggle_call_fullscreen, |
| 267 | )) |
| 268 | .on_action(zoom_listener::<ZoomShareIn>(&self.app, ZoomStep::In)) |
| 269 | .on_action(zoom_listener::<ZoomShareOut>(&self.app, ZoomStep::Out)) |
| 270 | .on_action(zoom_listener::<ZoomShareReset>(&self.app, ZoomStep::Reset)) |
| 271 | .on_key_down(move |event: &KeyDownEvent, _, cx| { |
| 272 | if event.keystroke.key == "escape" { |
| 273 | escape_app.update(cx, |state, cx| state.escape_call_fullscreen(cx)); |
| 274 | } |
| 275 | }) |
| 276 | .on_key_up(move |event: &KeyUpEvent, _, cx| { |
| 277 | if event.keystroke.key == "space" { |
| 278 | release_app.update(cx, |state, cx| state.release_push_to_talk(cx)); |
| 279 | } |
| 280 | }) |
| 281 | .on_modifiers_changed(move |event: &ModifiersChangedEvent, _, cx| { |
| 282 | if !event.modifiers.control { |
| 283 | modifier_app.update(cx, |state, cx| state.release_push_to_talk(cx)); |
| 284 | } |
| 285 | }) |
| 286 | .children(title_bar) |
| 287 | .child( |
| 288 | h_flex() |
| 289 | .flex_1() |
| 290 | .min_h_0() |
| 291 | .w_full() |
| 292 | .when(!fullscreen, |row| row.px(px(ISLAND_GAP)).pb(px(ISLAND_GAP))) |
| 293 | .children(stage), |
| 294 | ) |
| 295 | .children(stage_overlay) |
| 296 | } |
| 297 | } |