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