| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use gpui_kit::component::{h_flex, v_flex}; |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | use super::activity_panel::{ActivityPanel, ActivityPanelEvent}; |
| 7 | use super::call_mini::render_call_mini; |
| 8 | use super::call_stage::{render_call_overlay, render_stage_overlay}; |
| 9 | use super::call_view::{CHAT_PANEL_WIDTH, OVERLAY_BELOW as CALL_COLUMN_MIN, render_call_view}; |
| 10 | use super::conversation::{ConversationView, ReplyToHovered}; |
| 11 | use super::dialog_overlay::render_task_dialog; |
| 12 | use super::forward_dialog::{ForwardDialog, ForwardDialogEvent}; |
| 13 | use super::image_viewer::{ImageViewer, ImageViewerEvent}; |
| 14 | use super::prejoin_view::PrejoinView; |
| 15 | use super::profile_card::{ProfileCard, ProfileCardEvent}; |
| 16 | use gpui_kit::prelude::FluentBuilder as _; |
| 17 | use super::rail::{RAIL_WIDTH, RailView}; |
| 18 | use super::saved_panel::{SavedPanel, SavedPanelEvent}; |
| 19 | use super::shortcuts::{ |
| 20 | AcceptCall, DeclineCall, HangUpCall, JoinMeetingFromToast, NextConversation, OpenActivity, |
| 21 | OpenCallsTab, OpenChannelsTab, OpenChatsTab, OpenLobbyList, OpenSaved, PreviousConversation, PushToTalk, |
| 22 | Scope, ShowShortcuts, StartAudioCall, StartVideoCall, ToggleCallCamera, ToggleCallFullscreen, |
| 23 | ToggleCallHand, ToggleCallShare, ZoomShareIn, ZoomShareOut, ZoomShareReset, |
| 24 | }; |
| 25 | use super::call_pop_out::zoom_listener; |
| 26 | use super::shortcuts_dialog::{ShortcutsDialog, ShortcutsDialogEvent}; |
| 27 | use super::sidebar::{SIDEBAR_WIDTH, SidebarView}; |
| 28 | use super::status_bar::render_status_bar; |
| 29 | use super::status_menu::own_status_button; |
| 30 | use super::status_message_dialog::{StatusMessageDialog, StatusMessageDialogEvent}; |
| 31 | use super::switcher::{Switcher, SwitcherEvent, candidates_from}; |
| 32 | use super::title_bar::render_title_bar; |
| 33 | use crate::activity::ActivityCenter; |
| 34 | use crate::agents::{AgentRunsEvent, StartDialog, StartDialogEvent, StartRequest}; |
| 35 | use crate::app_state::{AppEvent, AppState, Selection}; |
| 36 | use crate::call::{CallMedia, CallWindows, ZoomStep}; |
| 37 | use crate::notice::NoticeAction; |
| 38 | use crate::notify::{NotificationCenter, selection_for}; |
| 39 | use crate::sidebar_model::Step; |
| 40 | use crate::theme; |
| 41 | use crate::updater::{self, IdleInputs, UpdateStatus}; |
| 42 | use crate::user_activity::{self, UserInput}; |
| 43 | |
| 44 | actions!(teams, [OpenSwitcher, NewChat, ToggleCallMute]); |
| 45 | |
| 46 | pub fn bind_keys(cx: &mut App) { |
| 47 | super::composer::bind_keys(cx); |
| 48 | super::shortcuts::bind_scopes( |
| 49 | cx, |
| 50 | &[ |
| 51 | Scope::Global, |
| 52 | Scope::OutsideComposer, |
| 53 | Scope::GlobalOverTextFields, |
| 54 | Scope::OutsideTextFields, |
| 55 | ], |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | #[derive(Debug, Clone, Copy)] |
| 60 | pub enum OpenTarget { |
| 61 | Chat(usize), |
| 62 | Channel(usize), |
| 63 | } |
| 64 | |
| 65 | #[derive(Default)] |
| 66 | pub struct Startup { |
| 67 | pub switcher_query: Option<String>, |
| 68 | pub composer_text: Option<String>, |
| 69 | pub reply_to_last: bool, |
| 70 | } |
| 71 | |
| 72 | pub const ISLAND_GAP: f32 = 6.; |
| 73 | pub const SIDEBAR_HIDDEN_BELOW: f32 = 960.; |
| 74 | |
| 75 | pub fn sidebar_hidden_in_call(window_width: f32, panel_open: bool) -> bool { |
| 76 | let panel_width = if panel_open { CHAT_PANEL_WIDTH + ISLAND_GAP } else { 0. }; |
| 77 | window_width < SIDEBAR_HIDDEN_BELOW |
| 78 | || window_width - RAIL_WIDTH - SIDEBAR_WIDTH - 2. * ISLAND_GAP - panel_width < CALL_COLUMN_MIN |
| 79 | } |
| 80 | |
| 81 | pub struct UserInputHandle(pub Arc<UserInput>); |
| 82 | |
| 83 | impl Global for UserInputHandle {} |
| 84 | |
| 85 | pub fn install_globals(state: &Entity<AppState>, user_input: Arc<UserInput>, cx: &mut App) { |
| 86 | cx.set_global(crate::app_state::AppHandle(state.clone())); |
| 87 | cx.set_global(UserInputHandle(user_input)); |
| 88 | } |
| 89 | |
| 90 | pub struct AppShell { |
| 91 | state: Entity<AppState>, |
| 92 | rail: Entity<RailView>, |
| 93 | sidebar: Entity<SidebarView>, |
| 94 | conversation: Entity<ConversationView>, |
| 95 | call_chat: Option<(String, Entity<ConversationView>)>, |
| 96 | prejoin: Option<Entity<PrejoinView>>, |
| 97 | switcher: Option<Entity<Switcher>>, |
| 98 | notifications: Entity<NotificationCenter>, |
| 99 | _call_windows: Entity<CallWindows>, |
| 100 | forward_dialog: Option<Entity<ForwardDialog>>, |
| 101 | image_viewer: Option<Entity<ImageViewer>>, |
| 102 | status_dialog: Option<Entity<StatusMessageDialog>>, |
| 103 | shortcuts_dialog: Option<Entity<ShortcutsDialog>>, |
| 104 | agent_dialog: Option<Entity<StartDialog>>, |
| 105 | profile_card: Option<Entity<ProfileCard>>, |
| 106 | focus_handle: FocusHandle, |
| 107 | call_window_fullscreen: bool, |
| 108 | window_was_fullscreen: bool, |
| 109 | open_target: Option<OpenTarget>, |
| 110 | update: UpdateStatus, |
| 111 | user_input: Arc<UserInput>, |
| 112 | _subscription: Subscription, |
| 113 | _call_media_observation: Subscription, |
| 114 | _panel_subscriptions: [Subscription; 2], |
| 115 | _agent_subscription: Option<Subscription>, |
| 116 | _escape_observation: Subscription, |
| 117 | _activation_observation: Subscription, |
| 118 | } |
| 119 | |
| 120 | impl AppShell { |
| 121 | fn sync_call_chat( |
| 122 | &mut self, |
| 123 | thread: Option<String>, |
| 124 | window: &mut Window, |
| 125 | cx: &mut Context<Self>, |
| 126 | ) { |
| 127 | match (thread, &self.call_chat) { |
| 128 | (Some(wanted), Some((open, _))) if &wanted == open => {} |
| 129 | (Some(wanted), _) => { |
| 130 | let app = self.state.clone(); |
| 131 | let selection = Selection::Chat(wanted.clone()); |
| 132 | let view = cx.new(|cx| ConversationView::pinned_to(app, selection, window, cx)); |
| 133 | self.call_chat = Some((wanted, view)); |
| 134 | } |
| 135 | (None, _) => self.call_chat = None, |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | fn sync_prejoin(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 140 | let wanted = self.state.read(cx).prejoin.clone(); |
| 141 | let open = self.prejoin.as_ref().map(|view| view.read(cx).pending_id()); |
| 142 | match (wanted, open) { |
| 143 | (Some(pending), Some(id)) if pending.id == id => {} |
| 144 | (Some(pending), _) => { |
| 145 | let app = self.state.clone(); |
| 146 | self.prejoin = Some(cx.new(|cx| PrejoinView::new(app, &pending, window, cx))); |
| 147 | } |
| 148 | (None, _) => self.prejoin = None, |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | pub fn new( |
| 153 | state: Entity<AppState>, |
| 154 | open_target: Option<OpenTarget>, |
| 155 | startup: Startup, |
| 156 | window: &mut Window, |
| 157 | cx: &mut Context<Self>, |
| 158 | ) -> Self { |
| 159 | let call_media = cx.new(|_| CallMedia); |
| 160 | state.update(cx, |state, _| state.call_media = Some(call_media.clone())); |
| 161 | let call_media_observation = cx.observe(&call_media, |_, _, cx| cx.notify()); |
| 162 | let activity = cx.new(|cx| ActivityCenter::new(state.clone(), window, cx)); |
| 163 | let activity_panel = cx.new(|cx| ActivityPanel::new(state.clone(), activity.clone(), cx)); |
| 164 | let saved_panel = cx.new(|cx| SavedPanel::new(state.clone(), cx)); |
| 165 | let panel_subscriptions = [ |
| 166 | cx.subscribe( |
| 167 | &activity_panel, |
| 168 | |this, _, event: &ActivityPanelEvent, cx| match event { |
| 169 | ActivityPanelEvent::OpenSettings => this |
| 170 | .notifications |
| 171 | .update(cx, |center, cx| center.open_settings(cx)), |
| 172 | ActivityPanelEvent::Open { |
| 173 | conversation_id, |
| 174 | message_id, |
| 175 | } => this.open_message(conversation_id, message_id, cx), |
| 176 | }, |
| 177 | ), |
| 178 | cx.subscribe(&saved_panel, |this, _, event: &SavedPanelEvent, cx| { |
| 179 | let SavedPanelEvent::Open { |
| 180 | conversation_id, |
| 181 | message_id, |
| 182 | } = event; |
| 183 | this.open_message(conversation_id, message_id, cx); |
| 184 | }), |
| 185 | ]; |
| 186 | let agent_subscription = crate::agents::runs_handle(cx).map(|runs| { |
| 187 | cx.subscribe_in( |
| 188 | &runs, |
| 189 | window, |
| 190 | |this, _, event: &AgentRunsEvent, window, cx| match event { |
| 191 | AgentRunsEvent::StartRequested(request) => { |
| 192 | this.open_agent_dialog(request.clone(), window, cx) |
| 193 | } |
| 194 | AgentRunsEvent::OpenSettings => this |
| 195 | .notifications |
| 196 | .update(cx, |center, cx| center.open_agent_settings(cx)), |
| 197 | AgentRunsEvent::Finished(_) => {} |
| 198 | }, |
| 199 | ) |
| 200 | }); |
| 201 | let sidebar = cx.new(|cx| SidebarView::new(state.clone(), activity_panel, saved_panel, cx)); |
| 202 | let rail = cx.new(|cx| RailView::new(state.clone(), activity.clone(), sidebar.clone(), cx)); |
| 203 | let conversation = cx.new(|cx| ConversationView::new(state.clone(), window, cx)); |
| 204 | let subscription = |
| 205 | cx.subscribe_in(&state, window, |this, _, event: &AppEvent, window, cx| { |
| 206 | if matches!(event, AppEvent::Sidebar | AppEvent::Status) { |
| 207 | this.apply_open_target(cx); |
| 208 | } |
| 209 | if matches!(event, AppEvent::Forward) { |
| 210 | this.open_forward_dialog(window, cx); |
| 211 | } |
| 212 | if matches!(event, AppEvent::ImageViewer) { |
| 213 | this.open_image_viewer(window, cx); |
| 214 | } |
| 215 | if matches!(event, AppEvent::StatusMessage) { |
| 216 | this.open_status_dialog(window, cx); |
| 217 | } |
| 218 | if matches!(event, AppEvent::NotificationSettings) { |
| 219 | this.notifications |
| 220 | .update(cx, |center, cx| center.open_settings(cx)); |
| 221 | } |
| 222 | if matches!(event, AppEvent::Profile) { |
| 223 | this.open_profile_card(window, cx); |
| 224 | } |
| 225 | cx.notify(); |
| 226 | }); |
| 227 | let focus_handle = cx.focus_handle(); |
| 228 | window.focus(&focus_handle, cx); |
| 229 | let notifications = cx.new(|cx| NotificationCenter::new(state.clone(), window, cx)); |
| 230 | let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx)); |
| 231 | let user_input = cx.global::<UserInputHandle>().0.clone(); |
| 232 | let escape_observation = cx.observe_keystrokes(|this, event, _, cx| { |
| 233 | this.user_input.touch(); |
| 234 | if event.keystroke.key == "escape" && event.action.is_none() { |
| 235 | this.state |
| 236 | .update(cx, |state, cx| state.escape_call_fullscreen(cx)); |
| 237 | } |
| 238 | }); |
| 239 | let activation_observation = cx.observe_window_activation(window, |this, window, cx| { |
| 240 | if window.is_window_active() { |
| 241 | this.state |
| 242 | .update(cx, |state, cx| state.refresh_calendar(cx)); |
| 243 | } else { |
| 244 | this.state |
| 245 | .update(cx, |state, cx| state.release_push_to_talk(cx)); |
| 246 | } |
| 247 | }); |
| 248 | let closing = notifications.clone(); |
| 249 | window.on_window_should_close(cx, move |_, cx| !closing.read(cx).intercept_close(cx)); |
| 250 | let mut shell = AppShell { |
| 251 | state, |
| 252 | rail, |
| 253 | sidebar, |
| 254 | conversation, |
| 255 | call_chat: None, |
| 256 | prejoin: None, |
| 257 | switcher: None, |
| 258 | notifications, |
| 259 | _call_windows: call_windows, |
| 260 | forward_dialog: None, |
| 261 | image_viewer: None, |
| 262 | status_dialog: None, |
| 263 | shortcuts_dialog: None, |
| 264 | agent_dialog: None, |
| 265 | profile_card: None, |
| 266 | focus_handle, |
| 267 | call_window_fullscreen: false, |
| 268 | window_was_fullscreen: false, |
| 269 | open_target, |
| 270 | update: UpdateStatus::UpToDate, |
| 271 | user_input, |
| 272 | _subscription: subscription, |
| 273 | _call_media_observation: call_media_observation, |
| 274 | _panel_subscriptions: panel_subscriptions, |
| 275 | _agent_subscription: agent_subscription, |
| 276 | _escape_observation: escape_observation, |
| 277 | _activation_observation: activation_observation, |
| 278 | }; |
| 279 | shell.apply_open_target(cx); |
| 280 | let Startup { |
| 281 | switcher_query, |
| 282 | composer_text, |
| 283 | reply_to_last, |
| 284 | } = startup; |
| 285 | if let Some(query) = switcher_query { |
| 286 | shell.toggle_switcher(&query, window, cx); |
| 287 | } |
| 288 | if composer_text.is_some() || reply_to_last { |
| 289 | cx.defer_in(window, move |this, window, cx| { |
| 290 | this.conversation.update(cx, |conversation, cx| { |
| 291 | if reply_to_last { |
| 292 | conversation.reply_to_latest_from_others(window, cx); |
| 293 | } |
| 294 | if let Some(text) = composer_text { |
| 295 | conversation.set_composer_text(&text, window, cx); |
| 296 | } |
| 297 | }); |
| 298 | }); |
| 299 | } |
| 300 | #[cfg(windows)] |
| 301 | cx.spawn_in(window, async move |this, cx| { |
| 302 | loop { |
| 303 | cx.background_executor().timer(updater::POLL_INTERVAL).await; |
| 304 | if this |
| 305 | .update_in(cx, |this, window, cx| this.poll_update(window, cx)) |
| 306 | .is_err() |
| 307 | { |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | }) |
| 312 | .detach(); |
| 313 | shell |
| 314 | } |
| 315 | |
| 316 | #[cfg_attr(not(windows), allow(dead_code))] |
| 317 | fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 318 | if matches!( |
| 319 | self.update, |
| 320 | UpdateStatus::Failed(_) | UpdateStatus::Installing |
| 321 | ) { |
| 322 | return; |
| 323 | } |
| 324 | let Some(paths) = updater::current_paths() else { |
| 325 | return; |
| 326 | }; |
| 327 | if self.update == UpdateStatus::UpToDate { |
| 328 | if !updater::check(&paths) { |
| 329 | return; |
| 330 | } |
| 331 | self.update = UpdateStatus::Ready; |
| 332 | let update_exe = paths.update_exe.clone(); |
| 333 | cx.background_spawn(async move { updater::prescan(&update_exe) }) |
| 334 | .detach(); |
| 335 | } |
| 336 | let conversation = self.conversation.read(cx); |
| 337 | let idle = updater::is_idle(IdleInputs { |
| 338 | window_active: window.is_window_active(), |
| 339 | composer_empty: conversation.composer_is_empty(cx), |
| 340 | send_in_flight: conversation.send_in_flight(), |
| 341 | }); |
| 342 | if idle { |
| 343 | self.restart_into_update(cx); |
| 344 | } |
| 345 | cx.notify(); |
| 346 | } |
| 347 | |
| 348 | fn restart_into_update(&mut self, cx: &mut Context<Self>) { |
| 349 | if self.update == UpdateStatus::Installing { |
| 350 | return; |
| 351 | } |
| 352 | let Some(paths) = updater::current_paths() else { |
| 353 | return; |
| 354 | }; |
| 355 | self.update = UpdateStatus::Installing; |
| 356 | cx.notify(); |
| 357 | cx.spawn(async move |this, cx| { |
| 358 | let result = cx |
| 359 | .background_spawn(async move { updater::install_and_relaunch(&paths) }) |
| 360 | .await; |
| 361 | let _ = this.update(cx, |this, cx| { |
| 362 | match result { |
| 363 | Ok(()) => cx.quit(), |
| 364 | Err(message) => this.update = UpdateStatus::Failed(message), |
| 365 | } |
| 366 | cx.notify(); |
| 367 | }); |
| 368 | }) |
| 369 | .detach(); |
| 370 | } |
| 371 | |
| 372 | fn apply_open_target(&mut self, cx: &mut Context<Self>) { |
| 373 | let Some(target) = self.open_target else { |
| 374 | return; |
| 375 | }; |
| 376 | let sidebar = &self.state.read(cx).sidebar; |
| 377 | let selection = match target { |
| 378 | OpenTarget::Chat(index) => sidebar |
| 379 | .chats |
| 380 | .get(index) |
| 381 | .map(|chat| Selection::Chat(chat.id.clone())), |
| 382 | OpenTarget::Channel(index) => sidebar |
| 383 | .teams |
| 384 | .iter() |
| 385 | .flat_map(|team| team.channels.iter()) |
| 386 | .nth(index) |
| 387 | .map(|channel| Selection::Channel(channel.id.clone())), |
| 388 | }; |
| 389 | if let Some(selection) = selection { |
| 390 | self.open_target = None; |
| 391 | self.state |
| 392 | .update(cx, |state, cx| state.select(selection, cx)); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | fn toggle_switcher( |
| 397 | &mut self, |
| 398 | initial_query: &str, |
| 399 | window: &mut Window, |
| 400 | cx: &mut Context<Self>, |
| 401 | ) { |
| 402 | if self.switcher.take().is_some() { |
| 403 | self.restore_focus(window, cx); |
| 404 | cx.notify(); |
| 405 | return; |
| 406 | } |
| 407 | self.close_image_viewer(window, cx); |
| 408 | let candidates = candidates_from(self.state.read(cx)); |
| 409 | let app = self.state.clone(); |
| 410 | let switcher = cx.new(|cx| Switcher::new(app, candidates, initial_query, window, cx)); |
| 411 | cx.subscribe_in( |
| 412 | &switcher, |
| 413 | window, |
| 414 | |this, _, event: &SwitcherEvent, window, cx| { |
| 415 | match event { |
| 416 | SwitcherEvent::Pick(selection) => { |
| 417 | let selection = selection.clone(); |
| 418 | this.state |
| 419 | .update(cx, |state, cx| state.select(selection, cx)); |
| 420 | } |
| 421 | SwitcherEvent::PickMessage { |
| 422 | selection, |
| 423 | message_id, |
| 424 | } => { |
| 425 | let (selection, message_id) = (selection.clone(), message_id.clone()); |
| 426 | this.state.update(cx, |state, cx| { |
| 427 | state.jump_to_message(selection, message_id, cx) |
| 428 | }); |
| 429 | } |
| 430 | SwitcherEvent::Close => {} |
| 431 | } |
| 432 | this.switcher = None; |
| 433 | this.restore_focus(window, cx); |
| 434 | cx.notify(); |
| 435 | }, |
| 436 | ) |
| 437 | .detach(); |
| 438 | self.switcher = Some(switcher); |
| 439 | cx.notify(); |
| 440 | } |
| 441 | |
| 442 | fn open_message(&mut self, conversation_id: &str, message_id: &str, cx: &mut Context<Self>) { |
| 443 | let Some(selection) = selection_for(&self.state.read(cx).sidebar, conversation_id) else { |
| 444 | return; |
| 445 | }; |
| 446 | let message_id = message_id.to_owned(); |
| 447 | self.state.update(cx, |state, cx| { |
| 448 | if message_id.is_empty() { |
| 449 | state.select(selection, cx) |
| 450 | } else { |
| 451 | state.jump_to_message(selection, message_id, cx) |
| 452 | } |
| 453 | }); |
| 454 | } |
| 455 | |
| 456 | fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 457 | if !self |
| 458 | .state |
| 459 | .update(cx, |state, _| state.take_status_message_request()) |
| 460 | { |
| 461 | return; |
| 462 | } |
| 463 | self.close_image_viewer(window, cx); |
| 464 | self.switcher = None; |
| 465 | let app = self.state.clone(); |
| 466 | let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx)); |
| 467 | cx.subscribe_in( |
| 468 | &dialog, |
| 469 | window, |
| 470 | |this, _, _: &StatusMessageDialogEvent, window, cx| { |
| 471 | this.status_dialog = None; |
| 472 | this.restore_focus(window, cx); |
| 473 | cx.notify(); |
| 474 | }, |
| 475 | ) |
| 476 | .detach(); |
| 477 | self.status_dialog = Some(dialog); |
| 478 | cx.notify(); |
| 479 | } |
| 480 | |
| 481 | fn open_agent_dialog( |
| 482 | &mut self, |
| 483 | request: StartRequest, |
| 484 | window: &mut Window, |
| 485 | cx: &mut Context<Self>, |
| 486 | ) { |
| 487 | let Some(runs) = crate::agents::runs_handle(cx) else { |
| 488 | return; |
| 489 | }; |
| 490 | let settings = runs.read(cx).settings(); |
| 491 | let Some(action) = settings |
| 492 | .actions |
| 493 | .iter() |
| 494 | .find(|action| action.id == request.action_id) |
| 495 | .cloned() |
| 496 | else { |
| 497 | return; |
| 498 | }; |
| 499 | let agent_name = settings |
| 500 | .agents |
| 501 | .iter() |
| 502 | .find(|agent| agent.id == action.agent_id) |
| 503 | .map(|agent| agent.name.clone()) |
| 504 | .unwrap_or_default(); |
| 505 | self.switcher = None; |
| 506 | let app = self.state.clone(); |
| 507 | let dialog = cx.new(|cx| StartDialog::new(app, request, action, &agent_name, window, cx)); |
| 508 | cx.subscribe_in( |
| 509 | &dialog, |
| 510 | window, |
| 511 | |this, _, _: &StartDialogEvent, window, cx| { |
| 512 | this.agent_dialog = None; |
| 513 | window.focus(&this.focus_handle, cx); |
| 514 | cx.notify(); |
| 515 | }, |
| 516 | ) |
| 517 | .detach(); |
| 518 | self.agent_dialog = Some(dialog); |
| 519 | cx.notify(); |
| 520 | } |
| 521 | |
| 522 | fn toggle_shortcuts_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 523 | if self.shortcuts_dialog.take().is_some() { |
| 524 | self.restore_focus(window, cx); |
| 525 | cx.notify(); |
| 526 | return; |
| 527 | } |
| 528 | self.close_image_viewer(window, cx); |
| 529 | self.switcher = None; |
| 530 | let dialog = cx.new(|cx| ShortcutsDialog::new(window, cx)); |
| 531 | cx.subscribe_in( |
| 532 | &dialog, |
| 533 | window, |
| 534 | |this, _, _: &ShortcutsDialogEvent, window, cx| { |
| 535 | this.shortcuts_dialog = None; |
| 536 | this.restore_focus(window, cx); |
| 537 | cx.notify(); |
| 538 | }, |
| 539 | ) |
| 540 | .detach(); |
| 541 | self.shortcuts_dialog = Some(dialog); |
| 542 | cx.notify(); |
| 543 | } |
| 544 | |
| 545 | fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 546 | let Some(request) = self |
| 547 | .state |
| 548 | .update(cx, |state, _| state.take_profile_request()) |
| 549 | else { |
| 550 | return; |
| 551 | }; |
| 552 | self.close_image_viewer(window, cx); |
| 553 | if let Some(card) = &self.profile_card { |
| 554 | card.update(cx, |card, cx| { |
| 555 | card.move_to(request.user_id, request.anchor, cx) |
| 556 | }); |
| 557 | return; |
| 558 | } |
| 559 | let app = self.state.clone(); |
| 560 | let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx)); |
| 561 | cx.subscribe_in( |
| 562 | &card, |
| 563 | window, |
| 564 | |this, _, _: &ProfileCardEvent, window, cx| { |
| 565 | this.profile_card = None; |
| 566 | this.restore_focus(window, cx); |
| 567 | cx.notify(); |
| 568 | }, |
| 569 | ) |
| 570 | .detach(); |
| 571 | self.profile_card = Some(card); |
| 572 | cx.notify(); |
| 573 | } |
| 574 | |
| 575 | fn open_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 576 | let Some(request) = self |
| 577 | .state |
| 578 | .update(cx, |state, _| state.take_image_viewer_request()) |
| 579 | else { |
| 580 | return; |
| 581 | }; |
| 582 | self.switcher = None; |
| 583 | let app = self.state.clone(); |
| 584 | let viewer = cx.new(|cx| ImageViewer::new(app, request, window, cx)); |
| 585 | cx.subscribe_in( |
| 586 | &viewer, |
| 587 | window, |
| 588 | |this, _, _: &ImageViewerEvent, window, cx| { |
| 589 | this.image_viewer = None; |
| 590 | window.focus(&this.focus_handle, cx); |
| 591 | cx.notify(); |
| 592 | }, |
| 593 | ) |
| 594 | .detach(); |
| 595 | self.image_viewer = Some(viewer); |
| 596 | cx.notify(); |
| 597 | } |
| 598 | |
| 599 | fn start_call_in_open_chat(&mut self, with_video: bool, cx: &mut Context<Self>) { |
| 600 | self.state.update(cx, |state, cx| { |
| 601 | if let Some(Selection::Chat(chat_id)) = state.selection.clone() { |
| 602 | if with_video { |
| 603 | state.start_chat_video_call(&chat_id, cx); |
| 604 | } else { |
| 605 | state.start_chat_call(&chat_id, cx); |
| 606 | } |
| 607 | } |
| 608 | }); |
| 609 | } |
| 610 | |
| 611 | fn sync_window_fullscreen(&mut self, wanted: bool, window: &mut Window) { |
| 612 | if wanted == self.call_window_fullscreen { |
| 613 | return; |
| 614 | } |
| 615 | self.call_window_fullscreen = wanted; |
| 616 | if wanted { |
| 617 | self.window_was_fullscreen = window.is_fullscreen(); |
| 618 | } |
| 619 | if wanted != window.is_fullscreen() && (wanted || !self.window_was_fullscreen) { |
| 620 | window.toggle_fullscreen(); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | fn close_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 625 | if self.image_viewer.take().is_some() { |
| 626 | window.focus(&self.focus_handle, cx); |
| 627 | cx.notify(); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | fn restore_focus(&self, window: &mut Window, cx: &mut Context<Self>) { |
| 632 | match &self.image_viewer { |
| 633 | Some(viewer) => { |
| 634 | let handle = viewer.read(cx).focus_handle(); |
| 635 | window.focus(&handle, cx) |
| 636 | } |
| 637 | None => window.focus(&self.focus_handle, cx), |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 642 | let Some(source) = self |
| 643 | .state |
| 644 | .update(cx, |state, _| state.take_forward_request()) |
| 645 | else { |
| 646 | return; |
| 647 | }; |
| 648 | self.switcher = None; |
| 649 | let candidates = candidates_from(self.state.read(cx)); |
| 650 | let app = self.state.clone(); |
| 651 | let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx)); |
| 652 | cx.subscribe_in( |
| 653 | &dialog, |
| 654 | window, |
| 655 | |this, _, event: &ForwardDialogEvent, window, cx| { |
| 656 | this.forward_dialog = None; |
| 657 | this.restore_focus(window, cx); |
| 658 | if let ForwardDialogEvent::Sent { target, title } = event { |
| 659 | let target = target.clone(); |
| 660 | let open = NoticeAction::new("Open", move |state, cx| { |
| 661 | state.select(target.clone(), cx) |
| 662 | }); |
| 663 | this.state.update(cx, |state, cx| { |
| 664 | state.raise_notice(format!("Forwarded to {title}"), Some(open), cx) |
| 665 | }); |
| 666 | } |
| 667 | cx.notify(); |
| 668 | }, |
| 669 | ) |
| 670 | .detach(); |
| 671 | self.forward_dialog = Some(dialog); |
| 672 | cx.notify(); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | impl AppShell { |
| 677 | fn islands(&self, call_view: Option<AnyElement>, show_sidebar: bool) -> impl IntoElement { |
| 678 | h_flex() |
| 679 | .debug_selector(|| "shell/islands".into()) |
| 680 | .flex_1() |
| 681 | .min_h_0() |
| 682 | .w_full() |
| 683 | .gap(px(ISLAND_GAP)) |
| 684 | .pr(px(ISLAND_GAP)) |
| 685 | .py(px(ISLAND_GAP)) |
| 686 | .child( |
| 687 | h_flex() |
| 688 | .h_full() |
| 689 | .flex_none() |
| 690 | .child(self.rail.clone()) |
| 691 | .when(show_sidebar, |group| { |
| 692 | group.child( |
| 693 | self.sidebar.clone().cached( |
| 694 | StyleRefinement::default() |
| 695 | .w(px(SIDEBAR_WIDTH)) |
| 696 | .h_full() |
| 697 | .flex_none(), |
| 698 | ), |
| 699 | ) |
| 700 | }), |
| 701 | ) |
| 702 | .child(match (call_view, self.prejoin.clone()) { |
| 703 | (Some(call_view), _) => call_view, |
| 704 | (None, Some(prejoin)) => prejoin |
| 705 | .cached(StyleRefinement::default().flex_1().min_w_0().h_full()) |
| 706 | .into_any_element(), |
| 707 | (None, None) => self |
| 708 | .conversation |
| 709 | .clone() |
| 710 | .cached( |
| 711 | StyleRefinement::default() |
| 712 | .flex_1() |
| 713 | .min_w_0() |
| 714 | .h_full() |
| 715 | .rounded(px(10.)) |
| 716 | .bg(theme::surface()) |
| 717 | .overflow_hidden(), |
| 718 | ) |
| 719 | .into_any_element(), |
| 720 | }) |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | impl Render for AppShell { |
| 725 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 726 | if let Some(dialog) = self |
| 727 | .state |
| 728 | .read(cx) |
| 729 | .task_dialog |
| 730 | .as_ref() |
| 731 | .map(|dialog| (dialog.scope.clone(), dialog.card.clone())) |
| 732 | { |
| 733 | self.state.update(cx, |state, cx| { |
| 734 | state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx) |
| 735 | }); |
| 736 | } |
| 737 | if let Some(me) = self.state.read(cx).directory.me.clone() { |
| 738 | let app = self.state.clone(); |
| 739 | cx.defer(move |cx| { |
| 740 | app.update(cx, |state, cx| { |
| 741 | state.request_avatars(vec![me.user_id.clone()], cx); |
| 742 | state.request_presence(vec![me.user_id], cx); |
| 743 | }); |
| 744 | }); |
| 745 | } |
| 746 | let chat_thread = self.state.read(cx).call_chat_thread(); |
| 747 | self.sync_call_chat(chat_thread, window, cx); |
| 748 | self.sync_prejoin(window, cx); |
| 749 | let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone()); |
| 750 | let state = self.state.read(cx); |
| 751 | let call_fullscreen = state.call_fullscreen(); |
| 752 | let window_width = f32::from(window.viewport_size().width); |
| 753 | let panel_open = state |
| 754 | .call |
| 755 | .as_ref() |
| 756 | .is_some_and(|call| call.chat_open || call.people_open || call.transcript_open); |
| 757 | let show_sidebar = !(state.viewing_call() |
| 758 | && !call_fullscreen |
| 759 | && sidebar_hidden_in_call(window_width, panel_open)); |
| 760 | let island_width = window_width |
| 761 | - RAIL_WIDTH |
| 762 | - if show_sidebar { SIDEBAR_WIDTH } else { 0. } |
| 763 | - 2. * ISLAND_GAP; |
| 764 | let call_view = render_call_view(&self.state, state, chat_panel, call_fullscreen, island_width) |
| 765 | .filter(|_| state.viewing_call()); |
| 766 | let (call_view, call_overlay) = match call_view { |
| 767 | Some(view) if call_fullscreen => (None, Some(render_call_overlay(view))), |
| 768 | view => (view, None), |
| 769 | }; |
| 770 | self.sync_window_fullscreen(call_fullscreen, window); |
| 771 | let call_mini = render_call_mini(&self.state, state); |
| 772 | let stage_overlay = render_stage_overlay(&self.state, state); |
| 773 | let status = render_status_bar( |
| 774 | state, |
| 775 | &self.update, |
| 776 | cx.listener(|this, _, _, cx| this.restart_into_update(cx)), |
| 777 | cx.listener(|this, _, _, cx| { |
| 778 | this.state |
| 779 | .update(cx, |state, cx| state.run_notice_action(cx)) |
| 780 | }), |
| 781 | ); |
| 782 | let title_bar = render_title_bar( |
| 783 | own_status_button(&self.state, state), |
| 784 | cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)), |
| 785 | ); |
| 786 | div() |
| 787 | .id("app-shell") |
| 788 | .key_context("AppShell") |
| 789 | .track_focus(&self.focus_handle) |
| 790 | .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| { |
| 791 | this.toggle_switcher("", window, cx); |
| 792 | })) |
| 793 | .on_action(cx.listener(|this, _: &NewChat, window, cx| { |
| 794 | this.close_image_viewer(window, cx); |
| 795 | this.state.update(cx, |state, cx| state.start_new_chat(cx)); |
| 796 | })) |
| 797 | .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| { |
| 798 | this.state |
| 799 | .update(cx, |state, cx| state.toggle_call_mute(cx)); |
| 800 | })) |
| 801 | .on_action(cx.listener(|this, _: &PushToTalk, _, cx| { |
| 802 | this.state |
| 803 | .update(cx, |state, cx| state.press_push_to_talk(cx)); |
| 804 | })) |
| 805 | .on_key_up(cx.listener(|this, event: &KeyUpEvent, _, cx| { |
| 806 | if event.keystroke.key == "space" { |
| 807 | this.state |
| 808 | .update(cx, |state, cx| state.release_push_to_talk(cx)); |
| 809 | } |
| 810 | })) |
| 811 | .on_modifiers_changed(cx.listener(|this, event: &ModifiersChangedEvent, _, cx| { |
| 812 | if !event.modifiers.control { |
| 813 | this.state |
| 814 | .update(cx, |state, cx| state.release_push_to_talk(cx)); |
| 815 | } |
| 816 | })) |
| 817 | .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| { |
| 818 | this.toggle_shortcuts_dialog(window, cx); |
| 819 | })) |
| 820 | .on_action(cx.listener(|this, _: &OpenActivity, window, cx| { |
| 821 | this.close_image_viewer(window, cx); |
| 822 | this.sidebar |
| 823 | .update(cx, |sidebar, cx| sidebar.show_activity(cx)); |
| 824 | })) |
| 825 | .on_action(cx.listener(|this, _: &OpenSaved, window, cx| { |
| 826 | this.close_image_viewer(window, cx); |
| 827 | this.sidebar |
| 828 | .update(cx, |sidebar, cx| sidebar.show_saved(cx)); |
| 829 | })) |
| 830 | .on_action(cx.listener(|this, _: &OpenChatsTab, window, cx| { |
| 831 | this.close_image_viewer(window, cx); |
| 832 | this.sidebar |
| 833 | .update(cx, |sidebar, cx| sidebar.show_chats(cx)); |
| 834 | })) |
| 835 | .on_action(cx.listener(|this, _: &OpenChannelsTab, window, cx| { |
| 836 | this.close_image_viewer(window, cx); |
| 837 | this.sidebar |
| 838 | .update(cx, |sidebar, cx| sidebar.show_channels(cx)); |
| 839 | })) |
| 840 | .on_action(cx.listener(|this, _: &OpenCallsTab, window, cx| { |
| 841 | this.close_image_viewer(window, cx); |
| 842 | this.sidebar |
| 843 | .update(cx, |sidebar, cx| sidebar.show_calls(cx)); |
| 844 | })) |
| 845 | .on_action(cx.listener(|this, _: &PreviousConversation, window, cx| { |
| 846 | this.close_image_viewer(window, cx); |
| 847 | this.sidebar.update(cx, |sidebar, cx| { |
| 848 | sidebar.select_adjacent(Step::Previous, cx) |
| 849 | }); |
| 850 | })) |
| 851 | .on_action(cx.listener(|this, _: &NextConversation, window, cx| { |
| 852 | this.close_image_viewer(window, cx); |
| 853 | this.sidebar |
| 854 | .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx)); |
| 855 | })) |
| 856 | .on_action(cx.listener(|this, _: &AcceptCall, _, cx| { |
| 857 | this.state |
| 858 | .update(cx, |state, cx| state.accept_ringing_call(cx)); |
| 859 | })) |
| 860 | .on_action(cx.listener(|this, _: &DeclineCall, _, cx| { |
| 861 | this.state |
| 862 | .update(cx, |state, cx| state.decline_ringing_call(cx)); |
| 863 | })) |
| 864 | .on_action(cx.listener(|this, _: &JoinMeetingFromToast, _, cx| { |
| 865 | this.state |
| 866 | .update(cx, |state, cx| state.join_latest_meeting_toast(cx)); |
| 867 | })) |
| 868 | .on_action(cx.listener(|this, _: &HangUpCall, _, cx| { |
| 869 | this.state.update(cx, |state, cx| state.leave_call(cx)); |
| 870 | })) |
| 871 | .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| { |
| 872 | this.state |
| 873 | .update(cx, |state, cx| state.toggle_call_camera(cx)); |
| 874 | })) |
| 875 | .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| { |
| 876 | this.state |
| 877 | .update(cx, |state, cx| state.toggle_call_share(cx)); |
| 878 | })) |
| 879 | .on_action(cx.listener(|this, _: &ToggleCallHand, _, cx| { |
| 880 | this.state |
| 881 | .update(cx, |state, cx| state.toggle_call_hand(cx)); |
| 882 | })) |
| 883 | .on_action(cx.listener(|this, _: &OpenLobbyList, _, cx| { |
| 884 | this.state |
| 885 | .update(cx, |state, cx| state.open_call_lobby_list(cx)); |
| 886 | })) |
| 887 | .on_action(cx.listener(|this, _: &StartAudioCall, _, cx| { |
| 888 | this.start_call_in_open_chat(false, cx); |
| 889 | })) |
| 890 | .on_action(cx.listener(|this, _: &StartVideoCall, _, cx| { |
| 891 | this.start_call_in_open_chat(true, cx); |
| 892 | })) |
| 893 | .on_action(cx.listener(|this, _: &ToggleCallFullscreen, _, cx| { |
| 894 | if this.image_viewer.is_some() || !this.state.read(cx).viewing_call() { |
| 895 | cx.propagate(); |
| 896 | return; |
| 897 | } |
| 898 | this.state |
| 899 | .update(cx, |state, cx| state.toggle_call_fullscreen(cx)); |
| 900 | })) |
| 901 | .on_action(zoom_listener::<ZoomShareIn>(&self.state, ZoomStep::In)) |
| 902 | .on_action(zoom_listener::<ZoomShareOut>(&self.state, ZoomStep::Out)) |
| 903 | .on_action(zoom_listener::<ZoomShareReset>(&self.state, ZoomStep::Reset)) |
| 904 | .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| { |
| 905 | this.close_image_viewer(window, cx); |
| 906 | this.conversation.update(cx, |conversation, cx| { |
| 907 | conversation.reply_to_hovered(window, cx) |
| 908 | }); |
| 909 | })) |
| 910 | .relative() |
| 911 | .size_full() |
| 912 | .bg(theme::background()) |
| 913 | .text_color(theme::text()) |
| 914 | .text_size(px(14.)) |
| 915 | .child(crate::frame_log::probe("first")) |
| 916 | .child(user_activity::input_probe(self.user_input.clone())) |
| 917 | .child( |
| 918 | v_flex() |
| 919 | .debug_selector(|| "shell".into()) |
| 920 | .size_full() |
| 921 | .child(title_bar) |
| 922 | .child(self.islands(call_view, show_sidebar)) |
| 923 | .child(status), |
| 924 | ) |
| 925 | .children(call_mini) |
| 926 | .children(call_overlay) |
| 927 | .children(stage_overlay) |
| 928 | .children(self.image_viewer.clone()) |
| 929 | .children(self.switcher.clone()) |
| 930 | .children(render_task_dialog(self.state.read(cx), cx)) |
| 931 | .children(self.forward_dialog.clone()) |
| 932 | .children(self.status_dialog.clone()) |
| 933 | .children(self.shortcuts_dialog.clone()) |
| 934 | .children(self.agent_dialog.clone()) |
| 935 | .children(self.profile_card.clone()) |
| 936 | .child(crate::frame_log::probe("last")) |
| 937 | } |
| 938 | } |