| 1 | use gpui_kit::component::{h_flex, v_flex}; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use super::activity_panel::{ActivityPanel, ActivityPanelEvent}; |
| 5 | use super::conversation::{ConversationView, ReplyToHovered}; |
| 6 | use super::dialog_overlay::render_task_dialog; |
| 7 | use super::forward_dialog::{ForwardDialog, ForwardDialogEvent}; |
| 8 | use super::profile_card::{ProfileCard, ProfileCardEvent}; |
| 9 | use super::saved_panel::{SavedPanel, SavedPanelEvent}; |
| 10 | use super::sidebar::SidebarView; |
| 11 | use super::status_bar::render_status_bar; |
| 12 | use super::status_menu::own_status_button; |
| 13 | use super::status_message_dialog::{StatusMessageDialog, StatusMessageDialogEvent}; |
| 14 | use super::switcher::{Switcher, SwitcherEvent, candidates_from}; |
| 15 | use super::title_bar::render_title_bar; |
| 16 | use crate::activity::ActivityCenter; |
| 17 | use crate::app_state::{AppEvent, AppState, Selection}; |
| 18 | use crate::notice::NoticeAction; |
| 19 | use crate::notify::{NotificationCenter, selection_for}; |
| 20 | use crate::theme; |
| 21 | use crate::updater::{self, IdleInputs, UpdateStatus}; |
| 22 | |
| 23 | actions!(teams, [OpenSwitcher, NewChat]); |
| 24 | |
| 25 | pub fn bind_keys(cx: &mut App) { |
| 26 | super::composer::bind_keys(cx); |
| 27 | cx.bind_keys([ |
| 28 | KeyBinding::new("ctrl-k", OpenSwitcher, None), |
| 29 | KeyBinding::new("ctrl-n", NewChat, None), |
| 30 | KeyBinding::new("alt-r", ReplyToHovered, None), |
| 31 | #[cfg(target_os = "macos")] |
| 32 | KeyBinding::new("cmd-k", OpenSwitcher, None), |
| 33 | #[cfg(target_os = "macos")] |
| 34 | KeyBinding::new("cmd-n", NewChat, None), |
| 35 | ]); |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Clone, Copy)] |
| 39 | pub enum OpenTarget { |
| 40 | Chat(usize), |
| 41 | Channel(usize), |
| 42 | } |
| 43 | |
| 44 | #[derive(Default)] |
| 45 | pub struct Startup { |
| 46 | pub switcher_query: Option<String>, |
| 47 | pub composer_text: Option<String>, |
| 48 | pub reply_to_last: bool, |
| 49 | } |
| 50 | |
| 51 | pub struct AppShell { |
| 52 | state: Entity<AppState>, |
| 53 | sidebar: Entity<SidebarView>, |
| 54 | conversation: Entity<ConversationView>, |
| 55 | switcher: Option<Entity<Switcher>>, |
| 56 | notifications: Entity<NotificationCenter>, |
| 57 | activity: Entity<ActivityCenter>, |
| 58 | activity_panel: Option<Entity<ActivityPanel>>, |
| 59 | saved_panel: Option<Entity<SavedPanel>>, |
| 60 | forward_dialog: Option<Entity<ForwardDialog>>, |
| 61 | status_dialog: Option<Entity<StatusMessageDialog>>, |
| 62 | profile_card: Option<Entity<ProfileCard>>, |
| 63 | focus_handle: FocusHandle, |
| 64 | open_target: Option<OpenTarget>, |
| 65 | update: UpdateStatus, |
| 66 | _subscription: Subscription, |
| 67 | _activity_observation: Subscription, |
| 68 | } |
| 69 | |
| 70 | impl AppShell { |
| 71 | pub fn new( |
| 72 | state: Entity<AppState>, |
| 73 | open_target: Option<OpenTarget>, |
| 74 | startup: Startup, |
| 75 | window: &mut Window, |
| 76 | cx: &mut Context<Self>, |
| 77 | ) -> Self { |
| 78 | let sidebar = cx.new(|cx| SidebarView::new(state.clone(), cx)); |
| 79 | let conversation = cx.new(|cx| ConversationView::new(state.clone(), window, cx)); |
| 80 | let subscription = |
| 81 | cx.subscribe_in(&state, window, |this, _, event: &AppEvent, window, cx| { |
| 82 | if matches!(event, AppEvent::Sidebar | AppEvent::Status) { |
| 83 | this.apply_open_target(cx); |
| 84 | } |
| 85 | if matches!(event, AppEvent::Forward) { |
| 86 | this.open_forward_dialog(window, cx); |
| 87 | } |
| 88 | if matches!(event, AppEvent::StatusMessage) { |
| 89 | this.open_status_dialog(window, cx); |
| 90 | } |
| 91 | if matches!(event, AppEvent::NotificationSettings) { |
| 92 | this.notifications |
| 93 | .update(cx, |center, cx| center.open_settings(cx)); |
| 94 | } |
| 95 | if matches!(event, AppEvent::Profile) { |
| 96 | this.open_profile_card(window, cx); |
| 97 | } |
| 98 | cx.notify(); |
| 99 | }); |
| 100 | let focus_handle = cx.focus_handle(); |
| 101 | window.focus(&focus_handle, cx); |
| 102 | let notifications = cx.new(|cx| NotificationCenter::new(state.clone(), window, cx)); |
| 103 | let activity = cx.new(|cx| ActivityCenter::new(state.clone(), window, cx)); |
| 104 | let activity_observation = cx.observe(&activity, |_, _, cx| cx.notify()); |
| 105 | let closing = notifications.clone(); |
| 106 | window.on_window_should_close(cx, move |_, cx| !closing.read(cx).intercept_close(cx)); |
| 107 | let mut shell = AppShell { |
| 108 | state, |
| 109 | sidebar, |
| 110 | conversation, |
| 111 | switcher: None, |
| 112 | notifications, |
| 113 | activity, |
| 114 | activity_panel: None, |
| 115 | saved_panel: None, |
| 116 | forward_dialog: None, |
| 117 | status_dialog: None, |
| 118 | profile_card: None, |
| 119 | focus_handle, |
| 120 | open_target, |
| 121 | update: UpdateStatus::UpToDate, |
| 122 | _subscription: subscription, |
| 123 | _activity_observation: activity_observation, |
| 124 | }; |
| 125 | shell.apply_open_target(cx); |
| 126 | let Startup { |
| 127 | switcher_query, |
| 128 | composer_text, |
| 129 | reply_to_last, |
| 130 | } = startup; |
| 131 | if let Some(query) = switcher_query { |
| 132 | shell.toggle_switcher(&query, window, cx); |
| 133 | } |
| 134 | if composer_text.is_some() || reply_to_last { |
| 135 | cx.defer_in(window, move |this, window, cx| { |
| 136 | this.conversation.update(cx, |conversation, cx| { |
| 137 | if reply_to_last { |
| 138 | conversation.reply_to_latest_from_others(window, cx); |
| 139 | } |
| 140 | if let Some(text) = composer_text { |
| 141 | conversation.set_composer_text(&text, window, cx); |
| 142 | } |
| 143 | }); |
| 144 | }); |
| 145 | } |
| 146 | #[cfg(windows)] |
| 147 | cx.spawn_in(window, async move |this, cx| { |
| 148 | loop { |
| 149 | cx.background_executor().timer(updater::POLL_INTERVAL).await; |
| 150 | if this |
| 151 | .update_in(cx, |this, window, cx| this.poll_update(window, cx)) |
| 152 | .is_err() |
| 153 | { |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | }) |
| 158 | .detach(); |
| 159 | shell |
| 160 | } |
| 161 | |
| 162 | #[cfg_attr(not(windows), allow(dead_code))] |
| 163 | fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 164 | if matches!(self.update, UpdateStatus::Failed(_)) { |
| 165 | return; |
| 166 | } |
| 167 | let Some(paths) = updater::current_paths() else { |
| 168 | return; |
| 169 | }; |
| 170 | if self.update == UpdateStatus::UpToDate { |
| 171 | if !updater::check(&paths) { |
| 172 | return; |
| 173 | } |
| 174 | self.update = UpdateStatus::Ready; |
| 175 | } |
| 176 | let conversation = self.conversation.read(cx); |
| 177 | let idle = updater::is_idle(IdleInputs { |
| 178 | window_active: window.is_window_active(), |
| 179 | composer_empty: conversation.composer_is_empty(cx), |
| 180 | send_in_flight: conversation.send_in_flight(), |
| 181 | }); |
| 182 | if idle { |
| 183 | self.restart_into_update(cx); |
| 184 | } |
| 185 | cx.notify(); |
| 186 | } |
| 187 | |
| 188 | fn restart_into_update(&mut self, cx: &mut Context<Self>) { |
| 189 | let Some(paths) = updater::current_paths() else { |
| 190 | return; |
| 191 | }; |
| 192 | match updater::install_and_relaunch(&paths) { |
| 193 | Ok(()) => cx.quit(), |
| 194 | Err(message) => self.update = UpdateStatus::Failed(message), |
| 195 | } |
| 196 | cx.notify(); |
| 197 | } |
| 198 | |
| 199 | fn apply_open_target(&mut self, cx: &mut Context<Self>) { |
| 200 | let Some(target) = self.open_target else { |
| 201 | return; |
| 202 | }; |
| 203 | let sidebar = &self.state.read(cx).sidebar; |
| 204 | let selection = match target { |
| 205 | OpenTarget::Chat(index) => sidebar |
| 206 | .chats |
| 207 | .get(index) |
| 208 | .map(|chat| Selection::Chat(chat.id.clone())), |
| 209 | OpenTarget::Channel(index) => sidebar |
| 210 | .teams |
| 211 | .iter() |
| 212 | .flat_map(|team| team.channels.iter()) |
| 213 | .nth(index) |
| 214 | .map(|channel| Selection::Channel(channel.id.clone())), |
| 215 | }; |
| 216 | if let Some(selection) = selection { |
| 217 | self.open_target = None; |
| 218 | self.state |
| 219 | .update(cx, |state, cx| state.select(selection, cx)); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | fn toggle_switcher( |
| 224 | &mut self, |
| 225 | initial_query: &str, |
| 226 | window: &mut Window, |
| 227 | cx: &mut Context<Self>, |
| 228 | ) { |
| 229 | if self.switcher.take().is_some() { |
| 230 | window.focus(&self.focus_handle, cx); |
| 231 | cx.notify(); |
| 232 | return; |
| 233 | } |
| 234 | self.activity_panel = None; |
| 235 | self.saved_panel = None; |
| 236 | let candidates = candidates_from(self.state.read(cx)); |
| 237 | let app = self.state.clone(); |
| 238 | let switcher = cx.new(|cx| Switcher::new(app, candidates, initial_query, window, cx)); |
| 239 | cx.subscribe_in( |
| 240 | &switcher, |
| 241 | window, |
| 242 | |this, _, event: &SwitcherEvent, window, cx| { |
| 243 | match event { |
| 244 | SwitcherEvent::Pick(selection) => { |
| 245 | let selection = selection.clone(); |
| 246 | this.state |
| 247 | .update(cx, |state, cx| state.select(selection, cx)); |
| 248 | } |
| 249 | SwitcherEvent::PickMessage { |
| 250 | selection, |
| 251 | message_id, |
| 252 | } => { |
| 253 | let (selection, message_id) = (selection.clone(), message_id.clone()); |
| 254 | this.state.update(cx, |state, cx| { |
| 255 | state.jump_to_message(selection, message_id, cx) |
| 256 | }); |
| 257 | } |
| 258 | SwitcherEvent::Close => {} |
| 259 | } |
| 260 | this.switcher = None; |
| 261 | window.focus(&this.focus_handle, cx); |
| 262 | cx.notify(); |
| 263 | }, |
| 264 | ) |
| 265 | .detach(); |
| 266 | self.switcher = Some(switcher); |
| 267 | cx.notify(); |
| 268 | } |
| 269 | |
| 270 | fn toggle_activity_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 271 | if self.activity_panel.take().is_some() { |
| 272 | window.focus(&self.focus_handle, cx); |
| 273 | cx.notify(); |
| 274 | return; |
| 275 | } |
| 276 | self.switcher = None; |
| 277 | self.saved_panel = None; |
| 278 | let (app, activity) = (self.state.clone(), self.activity.clone()); |
| 279 | let panel = cx.new(|cx| ActivityPanel::new(app, activity, window, cx)); |
| 280 | cx.subscribe_in( |
| 281 | &panel, |
| 282 | window, |
| 283 | |this, _, event: &ActivityPanelEvent, window, cx| { |
| 284 | this.activity_panel = None; |
| 285 | window.focus(&this.focus_handle, cx); |
| 286 | match event { |
| 287 | ActivityPanelEvent::Close => {} |
| 288 | ActivityPanelEvent::OpenSettings => this |
| 289 | .notifications |
| 290 | .update(cx, |center, cx| center.open_settings(cx)), |
| 291 | ActivityPanelEvent::Open { |
| 292 | conversation_id, |
| 293 | message_id, |
| 294 | } => { |
| 295 | let selection = |
| 296 | selection_for(&this.state.read(cx).sidebar, conversation_id); |
| 297 | if let Some(selection) = selection { |
| 298 | let message_id = message_id.clone(); |
| 299 | this.state.update(cx, |state, cx| { |
| 300 | if message_id.is_empty() { |
| 301 | state.select(selection, cx) |
| 302 | } else { |
| 303 | state.jump_to_message(selection, message_id, cx) |
| 304 | } |
| 305 | }); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | cx.notify(); |
| 310 | }, |
| 311 | ) |
| 312 | .detach(); |
| 313 | self.activity_panel = Some(panel); |
| 314 | cx.notify(); |
| 315 | } |
| 316 | fn toggle_saved_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 317 | if self.saved_panel.take().is_some() { |
| 318 | window.focus(&self.focus_handle, cx); |
| 319 | cx.notify(); |
| 320 | return; |
| 321 | } |
| 322 | self.switcher = None; |
| 323 | self.activity_panel = None; |
| 324 | let app = self.state.clone(); |
| 325 | let panel = cx.new(|cx| SavedPanel::new(app, window, cx)); |
| 326 | cx.subscribe_in( |
| 327 | &panel, |
| 328 | window, |
| 329 | |this, _, event: &SavedPanelEvent, window, cx| { |
| 330 | this.saved_panel = None; |
| 331 | window.focus(&this.focus_handle, cx); |
| 332 | if let SavedPanelEvent::Open { |
| 333 | conversation_id, |
| 334 | message_id, |
| 335 | } = event |
| 336 | { |
| 337 | let selection = selection_for(&this.state.read(cx).sidebar, conversation_id); |
| 338 | if let Some(selection) = selection { |
| 339 | let message_id = message_id.clone(); |
| 340 | this.state.update(cx, |state, cx| { |
| 341 | state.jump_to_message(selection, message_id, cx) |
| 342 | }); |
| 343 | } |
| 344 | } |
| 345 | cx.notify(); |
| 346 | }, |
| 347 | ) |
| 348 | .detach(); |
| 349 | self.saved_panel = Some(panel); |
| 350 | cx.notify(); |
| 351 | } |
| 352 | |
| 353 | fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 354 | if !self |
| 355 | .state |
| 356 | .update(cx, |state, _| state.take_status_message_request()) |
| 357 | { |
| 358 | return; |
| 359 | } |
| 360 | self.switcher = None; |
| 361 | self.activity_panel = None; |
| 362 | self.saved_panel = None; |
| 363 | let app = self.state.clone(); |
| 364 | let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx)); |
| 365 | cx.subscribe_in( |
| 366 | &dialog, |
| 367 | window, |
| 368 | |this, _, _: &StatusMessageDialogEvent, window, cx| { |
| 369 | this.status_dialog = None; |
| 370 | window.focus(&this.focus_handle, cx); |
| 371 | cx.notify(); |
| 372 | }, |
| 373 | ) |
| 374 | .detach(); |
| 375 | self.status_dialog = Some(dialog); |
| 376 | cx.notify(); |
| 377 | } |
| 378 | |
| 379 | fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 380 | let Some(request) = self |
| 381 | .state |
| 382 | .update(cx, |state, _| state.take_profile_request()) |
| 383 | else { |
| 384 | return; |
| 385 | }; |
| 386 | if let Some(card) = &self.profile_card { |
| 387 | card.update(cx, |card, cx| card.move_to(request.user_id, request.anchor, cx)); |
| 388 | return; |
| 389 | } |
| 390 | let app = self.state.clone(); |
| 391 | let card = |
| 392 | cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx)); |
| 393 | cx.subscribe_in( |
| 394 | &card, |
| 395 | window, |
| 396 | |this, _, _: &ProfileCardEvent, window, cx| { |
| 397 | this.profile_card = None; |
| 398 | window.focus(&this.focus_handle, cx); |
| 399 | cx.notify(); |
| 400 | }, |
| 401 | ) |
| 402 | .detach(); |
| 403 | self.profile_card = Some(card); |
| 404 | cx.notify(); |
| 405 | } |
| 406 | |
| 407 | fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 408 | let Some(source) = self |
| 409 | .state |
| 410 | .update(cx, |state, _| state.take_forward_request()) |
| 411 | else { |
| 412 | return; |
| 413 | }; |
| 414 | self.switcher = None; |
| 415 | self.activity_panel = None; |
| 416 | self.saved_panel = None; |
| 417 | let candidates = candidates_from(self.state.read(cx)); |
| 418 | let app = self.state.clone(); |
| 419 | let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx)); |
| 420 | cx.subscribe_in( |
| 421 | &dialog, |
| 422 | window, |
| 423 | |this, _, event: &ForwardDialogEvent, window, cx| { |
| 424 | this.forward_dialog = None; |
| 425 | window.focus(&this.focus_handle, cx); |
| 426 | if let ForwardDialogEvent::Sent { target, title } = event { |
| 427 | let target = target.clone(); |
| 428 | let open = NoticeAction::new("Open", move |state, cx| { |
| 429 | state.select(target.clone(), cx) |
| 430 | }); |
| 431 | this.state.update(cx, |state, cx| { |
| 432 | state.raise_notice(format!("Forwarded to {title}"), Some(open), cx) |
| 433 | }); |
| 434 | } |
| 435 | cx.notify(); |
| 436 | }, |
| 437 | ) |
| 438 | .detach(); |
| 439 | self.forward_dialog = Some(dialog); |
| 440 | cx.notify(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | impl Render for AppShell { |
| 445 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 446 | if let Some(dialog) = self |
| 447 | .state |
| 448 | .read(cx) |
| 449 | .task_dialog |
| 450 | .as_ref() |
| 451 | .map(|dialog| (dialog.scope.clone(), dialog.card.clone())) |
| 452 | { |
| 453 | self.state.update(cx, |state, cx| { |
| 454 | state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx) |
| 455 | }); |
| 456 | } |
| 457 | if let Some(me) = self.state.read(cx).directory.me.clone() { |
| 458 | let app = self.state.clone(); |
| 459 | cx.defer(move |cx| { |
| 460 | app.update(cx, |state, cx| { |
| 461 | state.request_avatars(vec![me.user_id.clone()], cx); |
| 462 | state.request_presence(vec![me.user_id], cx); |
| 463 | }); |
| 464 | }); |
| 465 | } |
| 466 | let state = self.state.read(cx); |
| 467 | let status = render_status_bar( |
| 468 | state, |
| 469 | &self.update, |
| 470 | cx.listener(|this, _, _, cx| this.restart_into_update(cx)), |
| 471 | cx.listener(|this, _, _, cx| { |
| 472 | this.state |
| 473 | .update(cx, |state, cx| state.run_notice_action(cx)) |
| 474 | }), |
| 475 | ); |
| 476 | let title_bar = render_title_bar( |
| 477 | own_status_button(&self.state, state), |
| 478 | self.activity.read(cx).feed().unread_count(), |
| 479 | cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)), |
| 480 | cx.listener(|this, _, window, cx| this.toggle_saved_panel(window, cx)), |
| 481 | cx.listener(|this, _, window, cx| this.toggle_activity_panel(window, cx)), |
| 482 | ); |
| 483 | div() |
| 484 | .id("app-shell") |
| 485 | .key_context("AppShell") |
| 486 | .track_focus(&self.focus_handle) |
| 487 | .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| { |
| 488 | this.toggle_switcher("", window, cx); |
| 489 | })) |
| 490 | .on_action(cx.listener(|this, _: &NewChat, _, cx| { |
| 491 | this.state.update(cx, |state, cx| state.start_new_chat(cx)); |
| 492 | })) |
| 493 | .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| { |
| 494 | this.conversation.update(cx, |conversation, cx| { |
| 495 | conversation.reply_to_hovered(window, cx) |
| 496 | }); |
| 497 | })) |
| 498 | .relative() |
| 499 | .size_full() |
| 500 | .bg(theme::background()) |
| 501 | .text_color(theme::text()) |
| 502 | .text_size(px(14.)) |
| 503 | .child(crate::frame_log::probe("first")) |
| 504 | .child( |
| 505 | v_flex() |
| 506 | .size_full() |
| 507 | .child(title_bar) |
| 508 | .child( |
| 509 | h_flex() |
| 510 | .flex_1() |
| 511 | .min_h_0() |
| 512 | .w_full() |
| 513 | .child(self.sidebar.clone()) |
| 514 | .child(self.conversation.clone()), |
| 515 | ) |
| 516 | .child(status), |
| 517 | ) |
| 518 | .children(self.activity_panel.clone()) |
| 519 | .children(self.saved_panel.clone()) |
| 520 | .children(self.switcher.clone()) |
| 521 | .children(render_task_dialog(self.state.read(cx), cx)) |
| 522 | .children(self.forward_dialog.clone()) |
| 523 | .children(self.status_dialog.clone()) |
| 524 | .children(self.profile_card.clone()) |
| 525 | .child(crate::frame_log::probe("last")) |
| 526 | } |
| 527 | } |