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