BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / shell.rs

shell.rs

shell.rs 48.6 KiB Download Raw
1mod team_keys;
2
3use std::sync::Arc;
4
5use gpui_kit::component::{elevation, h_flex, v_flex};
6use gpui_kit::*;
7
8use super::activity_panel::{ActivityPanel, ActivityPanelEvent};
9use super::actions::{NewChat, OpenSwitcher, ReplyToHovered, ToggleCallMute};
10use super::calendar_view::CalendarView;
11use super::conversation::ConversationView;
12use super::dialog_overlay::render_task_dialog;
13use super::forward_dialog::{ForwardDialog, ForwardDialogEvent};
14use super::image_viewer::{ImageViewer, ImageViewerEvent};
15use super::profile_card::{ProfileCard, ProfileCardEvent};
16use gpui_kit::prelude::FluentBuilder as _;
17use super::rail::RailView;
18use crate::chat_filter::ChatFilterPill;
19use super::calls_page::CallsPage;
20use super::quick_view_pane::{QuickViewEvent, QuickViewPane};
21use super::shortcuts::{
22 ClearChatFilters, DeclineCall, FilterMeetingChats, FilterMutedChats,
23 FilterNonMeetingChats, FilterUnreadChats, FindInChat, HangUpCall, JoinMeetingFromToast, JumpToLastReadOrNewest, NextConversation, OpenActivity,
24 OpenAgents, OpenCallsTab, OpenSettings, OpenChannelsTab, OpenChatFilter, OpenChatsTab, PopOutSelectedChat, OpenLobbyList, OpenSaved, OpenCalendar, PreviousConversation, PushToTalk,
25 Scope, ShowShortcuts, StartAudioCall, StartVideoCall, ToggleCallCamera,
26 ToggleCallHand, ToggleCallShare, ZoomIn, ZoomOut, ZoomReset, ZoomShareIn, ZoomShareOut,
27 ZoomShareReset,
28};
29use super::window_zoom::{WindowZoom, change_zoom};
30use super::call_pop_out::zoom_listener;
31use super::chat_pop_out;
32use super::shortcuts_dialog::{ShortcutsDialog, ShortcutsDialogEvent};
33use super::sidebar::{SidebarEvent, SidebarTab, SidebarView};
34use app_ui_kit::render::hooks::RenderHooks;
35use app_ui_kit::layout::{ISLAND_GAP, SIDEBAR_WIDTH};
36use super::connection_banner::render_connection_banner;
37use super::manage_team_page::ManageTeamPage;
38use super::splash::{self, Phase, Splash, StartupFacts};
39use super::notice_toast::NoticeToasts;
40use super::status_menu::{own_status_button, settings_more_button};
41use super::status_message_dialog::{StatusMessageDialog, StatusMessageDialogEvent};
42use super::switcher::{Switcher, SwitcherEvent, candidates_from};
43use super::title_bar::{mode_label, render_title_bar, update_button};
44use crate::activity::ActivityCenter;
45use crate::agents::{AgentRunsEvent, StartDialog, StartDialogEvent, StartRequest};
46use crate::app_state::{AppEvent, AppState, QuickView, Selection, selection_for};
47use crate::call::{CallMedia, CallWindows, ZoomStep};
48use crate::notice::NoticeAction;
49use crate::notify::NotificationCenter;
50use crate::settings::{SettingsDialog, SettingsDialogEvent};
51use crate::settings_section::SettingsSection;
52use crate::sidebar_model::Step;
53use crate::theme;
54use crate::updater::{self, IdleInputs, UpdateStatus};
55use crate::user_activity::{self, UserInput};
56
57pub fn bind_keys(cx: &mut App) {
58 super::composer::bind_keys(cx);
59 super::shortcuts::bind_scopes(
60 cx,
61 &[
62 Scope::Global,
63 Scope::OutsideComposer,
64 Scope::GlobalOverTextFields,
65 Scope::OutsideTextFields,
66 ],
67 );
68}
69
70#[derive(Debug, Clone, Copy)]
71pub enum OpenTarget {
72 Chat(usize),
73 Channel(usize),
74}
75
76#[derive(Default)]
77pub struct Startup {
78 pub switcher_query: Option<String>,
79 pub composer_text: Option<String>,
80 pub reply_to_last: bool,
81 pub launched_by_autostart: bool,
82}
83
84pub struct UserInputHandle(pub Arc<UserInput>);
85
86impl Global for UserInputHandle {}
87
88pub fn install_globals(state: &Entity<AppState>, user_input: Arc<UserInput>, cx: &mut App) {
89 cx.set_global(crate::app_state::AppHandle(state.clone()));
90 cx.set_global(UserInputHandle(user_input));
91 cx.set_global(RenderHooks {
92 open_profile_link: super::profile_card::open_profile_link,
93 jump_to_quoted,
94 inline_image,
95 open_link,
96 });
97}
98
99fn open_link(url: &str, cx: &mut App) {
100 if let Some(handle) = cx.try_global::<crate::app_state::AppHandle>() {
101 let text = opening_link_text(url);
102 handle.0.clone().update(cx, |state, cx| state.raise_notice(text, None, cx));
103 }
104 cx.open_url(url);
105}
106
107fn opening_link_text(url: &str) -> String {
108 let host = url
109 .split_once("://")
110 .map_or(url, |(_, rest)| rest)
111 .split(['/', '?', '#'])
112 .next()
113 .unwrap_or_default();
114 if host.is_empty() {
115 "Opening link...".to_owned()
116 } else {
117 format!("Opening {host}...")
118 }
119}
120
121fn inline_image(url: &str, cx: &App) -> Option<std::path::PathBuf> {
122 let handle = cx.try_global::<crate::app_state::AppHandle>()?;
123 let entry = handle.0.read(cx).directory.image(url)?;
124 Some(entry.path.clone())
125}
126
127fn jump_to_quoted(message_id: String, cx: &mut App) {
128 let Some(handle) = cx.try_global::<crate::app_state::AppHandle>() else {
129 return;
130 };
131 handle.0.clone().update(cx, |state, cx| {
132 if let Some(selection) = state.selection.clone() {
133 state.jump_to_message(selection, message_id, cx);
134 }
135 });
136}
137
138#[cfg(windows)]
139fn watch_releases(running_tag: &'static str, cx: &mut App) {
140 use app_base::release_feed;
141 cx.spawn(async move |cx| {
142 cx.background_executor()
143 .timer(release_feed::FIRST_CHECK_DELAY)
144 .await;
145 loop {
146 let Some(paths) = updater::current_paths() else {
147 return;
148 };
149 let fetch = cx.update(|cx| release_feed::fetch_update(running_tag, paths, cx));
150 match fetch.await {
151 Ok(Some(tag)) => log::info!("release {tag} downloaded for the next restart"),
152 Ok(None) => {}
153 Err(error) => log::warn!("release check failed: {error}"),
154 }
155 cx.background_executor()
156 .timer(release_feed::CHECK_INTERVAL)
157 .await;
158 }
159 })
160 .detach();
161}
162
163const DEMO_UPDATE_VARIABLE: &str = "TEAMS_DEMO_UPDATE";
164
165pub struct AppShell {
166 state: Entity<AppState>,
167 rail: Entity<RailView>,
168 sidebar: Entity<SidebarView>,
169 calendar: Entity<CalendarView>,
170 quick_pane: Entity<QuickViewPane>,
171 manage_team_page: Entity<ManageTeamPage>,
172 calls_page: Entity<CallsPage>,
173 shown_quick_view: Option<QuickView>,
174 conversation: Entity<ConversationView>,
175 switcher: Option<Entity<Switcher>>,
176 notifications: Entity<NotificationCenter>,
177 _call_windows: Entity<CallWindows>,
178 forward_dialog: Option<Entity<ForwardDialog>>,
179 image_viewer: Option<Entity<ImageViewer>>,
180 status_dialog: Option<Entity<StatusMessageDialog>>,
181 shortcuts_dialog: Option<Entity<ShortcutsDialog>>,
182 agent_dialog: Option<Entity<StartDialog>>,
183 settings_dialog: Option<Entity<SettingsDialog>>,
184 profile_card: Option<Entity<ProfileCard>>,
185 focus_handle: FocusHandle,
186 open_target: Option<OpenTarget>,
187 update: UpdateStatus,
188 splash: Splash,
189 toasts: NoticeToasts,
190 user_input: Arc<UserInput>,
191 _subscription: Subscription,
192 _panel_subscriptions: [Subscription; 2],
193 _sidebar_subscription: Subscription,
194 _agents_subscription: Option<Subscription>,
195 _agent_subscription: Option<Subscription>,
196 _escape_observation: Subscription,
197 _activation_observation: Subscription,
198 _appearance_observation: Subscription,
199}
200
201impl AppShell {
202 #[cfg(test)]
203 pub(crate) fn set_update_status(&mut self, update: UpdateStatus, cx: &mut Context<Self>) {
204 self.update = update;
205 cx.notify();
206 }
207
208 fn focus_opened_quick_view(&mut self, window: &mut Window, cx: &mut Context<Self>) {
209 let quick_view = self.state.read(cx).quick_view;
210 if std::mem::replace(&mut self.shown_quick_view, quick_view) != quick_view
211 && quick_view.is_some()
212 {
213 self.restore_focus(window, cx);
214 }
215 }
216
217 fn leave_calendar_on_selection(&mut self, cx: &mut Context<Self>) {
218 if self.sidebar.read(cx).tab() != SidebarTab::Calendar {
219 return;
220 }
221 let selection = self.state.read(cx).selection.clone();
222 let sidebar = self.sidebar.clone();
223 cx.defer(move |cx| {
224 sidebar.update(cx, |sidebar, cx| match selection {
225 Some(Selection::Channel(_)) => sidebar.show_channels(cx),
226 Some(Selection::Chat(_)) => sidebar.show_chats(cx),
227 None => {}
228 })
229 });
230 }
231
232 pub fn new(
233 state: Entity<AppState>,
234 open_target: Option<OpenTarget>,
235 startup: Startup,
236 window: &mut Window,
237 cx: &mut Context<Self>,
238 ) -> Self {
239 let call_media = cx.new(|_| CallMedia);
240 state.update(cx, |state, _| state.call_media = Some(call_media.clone()));
241 let activity = cx.new(|cx| ActivityCenter::new(state.clone(), window, cx));
242 let activity_panel = cx.new(|cx| ActivityPanel::new(state.clone(), activity.clone(), cx));
243 let quick_pane = cx.new(|cx| QuickViewPane::new(state.clone(), activity.clone(), cx));
244 let manage_team_page = cx.new(|cx| ManageTeamPage::new(state.clone(), window, cx));
245 let calls_page = cx.new(|cx| CallsPage::new(state.clone(), window, cx));
246 let agents_panel = crate::agents::runs_handle(cx)
247 .map(|runs| cx.new(|cx| crate::agents::AgentsPanel::new(runs, cx)));
248 let agents_subscription = agents_panel.as_ref().map(|panel| {
249 cx.subscribe(
250 panel,
251 |this, _, event: &crate::agents::AgentsPanelEvent, cx| {
252 let crate::agents::AgentsPanelEvent::Open {
253 conversation_id,
254 message_id,
255 } = event;
256 this.open_message(conversation_id, message_id, cx);
257 },
258 )
259 });
260 let panel_subscriptions = [
261 cx.subscribe(
262 &activity_panel,
263 |this, _, event: &ActivityPanelEvent, cx| match event {
264 ActivityPanelEvent::OpenSettings => this.state.update(cx, |state, cx| {
265 state.open_settings(SettingsSection::Notifications, cx)
266 }),
267 ActivityPanelEvent::Open {
268 conversation_id,
269 message_id,
270 link,
271 } => {
272 if !this.open_message(conversation_id, message_id, cx)
273 && let Some(link) = link
274 {
275 cx.open_url(link);
276 }
277 }
278 },
279 ),
280 cx.subscribe(&quick_pane, |this, _, event: &QuickViewEvent, cx| {
281 let QuickViewEvent::Open {
282 conversation_id,
283 message_id,
284 } = event;
285 this.state
286 .update(cx, |state, cx| state.close_quick_view(cx));
287 this.open_message(conversation_id, message_id.as_deref().unwrap_or_default(), cx);
288 }),
289 ];
290 let agent_subscription = crate::agents::runs_handle(cx).map(|runs| {
291 cx.subscribe_in(
292 &runs,
293 window,
294 |this, _, event: &AgentRunsEvent, window, cx| match event {
295 AgentRunsEvent::StartRequested(request) => {
296 this.open_agent_dialog(request.clone(), window, cx)
297 }
298 AgentRunsEvent::OpenSettings => {
299 this.open_settings(SettingsSection::Agents, window, cx)
300 }
301 AgentRunsEvent::Finished(_) => {}
302 },
303 )
304 });
305 let sidebar = cx.new(|cx| SidebarView::new(state.clone(), activity_panel, agents_panel, cx));
306 let sidebar_subscription = cx.subscribe_in(&sidebar, window, |this, _, event: &SidebarEvent, window, cx| {
307 match event {
308 SidebarEvent::RestoreFocus => this.restore_focus(window, cx),
309 SidebarEvent::TabChanged => {
310 if this.sidebar.read(cx).tab() == SidebarTab::Calendar {
311 this.restore_focus(window, cx);
312 }
313 cx.notify();
314 }
315 SidebarEvent::NewChannelPost => this
316 .conversation
317 .update(cx, |conversation, cx| conversation.start_channel_post(window, cx)),
318 }
319 });
320 let calendar = cx.new(|cx| CalendarView::new(state.clone(), cx));
321 let rail = cx.new(|cx| RailView::new(state.clone(), activity.clone(), sidebar.clone(), cx));
322 let conversation = cx.new(|cx| ConversationView::new(state.clone(), window, cx));
323 let subscription =
324 cx.subscribe_in(&state, window, |this, _, event: &AppEvent, window, cx| {
325 if matches!(event, AppEvent::Sidebar | AppEvent::Status) {
326 this.apply_open_target(cx);
327 }
328 if matches!(event, AppEvent::Sidebar) {
329 this.focus_opened_quick_view(window, cx);
330 }
331 if matches!(event, AppEvent::Selection) {
332 this.leave_calendar_on_selection(cx);
333 }
334 if matches!(event, AppEvent::Forward) {
335 this.open_forward_dialog(window, cx);
336 }
337 if matches!(event, AppEvent::ImageViewer) {
338 this.open_image_viewer(window, cx);
339 }
340 if matches!(event, AppEvent::StatusMessage) {
341 this.open_status_dialog(window, cx);
342 }
343 if let AppEvent::Settings(section) = event {
344 this.open_settings(*section, window, cx);
345 }
346 if matches!(event, AppEvent::Profile) {
347 this.open_profile_card(window, cx);
348 }
349 cx.notify();
350 });
351 let focus_handle = cx.focus_handle();
352 window.focus(&focus_handle, cx);
353 let notifications =
354 cx.new(|cx| NotificationCenter::new(state.clone(), activity.clone(), window, cx));
355 let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx));
356 let user_input = cx.global::<UserInputHandle>().0.clone();
357 let escape_observation = cx.observe_keystrokes(|this, event, _, cx| {
358 this.user_input.touch();
359 if event.keystroke.key == "escape" && event.action.is_none() {
360 this.state
361 .update(cx, |state, cx| state.escape_call_fullscreen(cx));
362 }
363 });
364 let appearance_observation = cx.observe_window_appearance(window, |_, window, cx| {
365 crate::active_appearance::system_appearance_changed(window.appearance(), cx);
366 });
367 let activation_observation = cx.observe_window_activation(window, |this, window, cx| {
368 if window.is_window_active() {
369 this.state
370 .update(cx, |state, cx| state.refresh_calendar(cx));
371 } else {
372 this.state
373 .update(cx, |state, cx| state.release_push_to_talk(cx));
374 }
375 });
376 let closing = notifications.clone();
377 window.on_window_should_close(cx, move |_, cx| !closing.read(cx).intercept_close(cx));
378 let mut shell = AppShell {
379 state,
380 rail,
381 sidebar,
382 calendar,
383 quick_pane,
384 manage_team_page,
385 calls_page,
386 shown_quick_view: None,
387 conversation,
388 switcher: None,
389 notifications,
390 _call_windows: call_windows,
391 forward_dialog: None,
392 image_viewer: None,
393 status_dialog: None,
394 shortcuts_dialog: None,
395 agent_dialog: None,
396 settings_dialog: None,
397 profile_card: None,
398 focus_handle,
399 open_target,
400 update: UpdateStatus::UpToDate,
401 splash: Splash::default(),
402 toasts: NoticeToasts::default(),
403 user_input,
404 _subscription: subscription,
405 _panel_subscriptions: panel_subscriptions,
406 _sidebar_subscription: sidebar_subscription,
407 _agents_subscription: agents_subscription,
408 _agent_subscription: agent_subscription,
409 _escape_observation: escape_observation,
410 _activation_observation: activation_observation,
411 _appearance_observation: appearance_observation,
412 };
413 shell.apply_open_target(cx);
414 shell
415 .state
416 .update(cx, |state, cx| state.refresh_connection_banner(cx));
417 if shell.state.read(cx).mode.demo && std::env::var_os(DEMO_UPDATE_VARIABLE).is_some() {
418 shell.update = UpdateStatus::Ready;
419 }
420 let Startup {
421 switcher_query,
422 composer_text,
423 reply_to_last,
424 launched_by_autostart,
425 } = startup;
426 if shell
427 .notifications
428 .read(cx)
429 .startup()
430 .starts_hidden(launched_by_autostart)
431 {
432 cx.defer_in(window, |this, window, cx| {
433 this.notifications.read(cx).start_in_background(window)
434 });
435 }
436 if let Some(query) = switcher_query {
437 shell.toggle_switcher(&query, window, cx);
438 }
439 if composer_text.is_some() || reply_to_last {
440 cx.defer_in(window, move |this, window, cx| {
441 this.conversation.update(cx, |conversation, cx| {
442 if reply_to_last {
443 conversation.reply_to_latest_from_others(window, cx);
444 }
445 if let Some(text) = composer_text {
446 conversation.set_composer_text(&text, window, cx);
447 }
448 });
449 });
450 }
451 #[cfg(windows)]
452 cx.spawn_in(window, async move |this, cx| {
453 loop {
454 cx.background_executor().timer(updater::POLL_INTERVAL).await;
455 if this
456 .update_in(cx, |this, window, cx| this.poll_update(window, cx))
457 .is_err()
458 {
459 break;
460 }
461 }
462 })
463 .detach();
464 #[cfg(windows)]
465 if let Some(running_tag) = app_base::version::release_tag() {
466 watch_releases(running_tag, cx);
467 }
468 shell
469 }
470
471 #[cfg_attr(not(windows), allow(dead_code))]
472 fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {
473 if matches!(
474 self.update,
475 UpdateStatus::Failed(_) | UpdateStatus::Installing
476 ) {
477 return;
478 }
479 let Some(paths) = updater::current_paths() else {
480 return;
481 };
482 if self.update == UpdateStatus::UpToDate {
483 if !updater::check(&paths) {
484 return;
485 }
486 self.update = UpdateStatus::Ready;
487 let update_exe = paths.update_exe.clone();
488 cx.background_spawn(async move { updater::prescan(&update_exe) })
489 .detach();
490 }
491 let conversation = self.conversation.read(cx);
492 let idle = updater::is_idle(IdleInputs {
493 window_active: window.is_window_active(),
494 composer_empty: conversation.composer_is_empty(cx),
495 send_in_flight: conversation.send_in_flight(),
496 });
497 if idle {
498 self.restart_into_update(cx);
499 }
500 cx.notify();
501 }
502
503 fn restart_into_update(&mut self, cx: &mut Context<Self>) {
504 if self.update == UpdateStatus::Installing {
505 return;
506 }
507 let Some(paths) = updater::current_paths() else {
508 return;
509 };
510 self.update = UpdateStatus::Installing;
511 cx.notify();
512 cx.spawn(async move |this, cx| {
513 let result = cx
514 .background_spawn(async move { updater::install_and_relaunch(&paths) })
515 .await;
516 let _ = this.update(cx, |this, cx| {
517 match result {
518 Ok(()) => cx.quit(),
519 Err(message) => this.update = UpdateStatus::Failed(message),
520 }
521 cx.notify();
522 });
523 })
524 .detach();
525 }
526
527 fn apply_open_target(&mut self, cx: &mut Context<Self>) {
528 let Some(target) = self.open_target else {
529 return;
530 };
531 let sidebar = &self.state.read(cx).sidebar;
532 let selection = match target {
533 OpenTarget::Chat(index) => sidebar
534 .chats
535 .get(index)
536 .map(|chat| Selection::Chat(chat.id.clone())),
537 OpenTarget::Channel(index) => sidebar
538 .teams
539 .iter()
540 .flat_map(|team| team.channels.iter())
541 .nth(index)
542 .map(|channel| Selection::Channel(channel.id.clone())),
543 };
544 if let Some(selection) = selection {
545 self.open_target = None;
546 self.state
547 .update(cx, |state, cx| state.select(selection, cx));
548 }
549 }
550
551 fn toggle_switcher(
552 &mut self,
553 initial_query: &str,
554 window: &mut Window,
555 cx: &mut Context<Self>,
556 ) {
557 if self.switcher.take().is_some() {
558 self.restore_focus(window, cx);
559 cx.notify();
560 return;
561 }
562 self.close_image_viewer(window, cx);
563 let candidates = candidates_from(self.state.read(cx));
564 let app = self.state.clone();
565 let switcher = cx.new(|cx| Switcher::new(app, candidates, initial_query, window, cx));
566 cx.subscribe_in(
567 &switcher,
568 window,
569 |this, _, event: &SwitcherEvent, window, cx| {
570 match event {
571 SwitcherEvent::Pick(selection) => {
572 let selection = selection.clone();
573 this.state
574 .update(cx, |state, cx| state.select(selection, cx));
575 }
576 SwitcherEvent::PickMessage {
577 selection,
578 message_id,
579 } => {
580 let (selection, message_id) = (selection.clone(), message_id.clone());
581 this.state.update(cx, |state, cx| {
582 state.jump_to_message(selection, message_id, cx)
583 });
584 }
585 SwitcherEvent::PickPerson { user_id } => {
586 let user_id = user_id.clone();
587 this.state
588 .update(cx, |state, cx| state.open_chat_with(&user_id, cx));
589 }
590 SwitcherEvent::Close => {}
591 }
592 this.switcher = None;
593 this.restore_focus(window, cx);
594 cx.notify();
595 },
596 )
597 .detach();
598 self.switcher = Some(switcher);
599 cx.notify();
600 }
601
602 fn open_message(
603 &mut self,
604 conversation_id: &str,
605 message_id: &str,
606 cx: &mut Context<Self>,
607 ) -> bool {
608 let Some(selection) = selection_for(&self.state.read(cx).sidebar, conversation_id) else {
609 return false;
610 };
611 let message_id = message_id.to_owned();
612 self.state.update(cx, |state, cx| {
613 if message_id.is_empty() {
614 state.select(selection, cx)
615 } else {
616 state.jump_to_message(selection, message_id, cx)
617 }
618 });
619 true
620 }
621
622 fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
623 if !self
624 .state
625 .update(cx, |state, _| state.take_status_message_request())
626 {
627 return;
628 }
629 self.close_image_viewer(window, cx);
630 self.switcher = None;
631 let app = self.state.clone();
632 let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx));
633 cx.subscribe_in(
634 &dialog,
635 window,
636 |this, _, _: &StatusMessageDialogEvent, window, cx| {
637 this.status_dialog = None;
638 this.restore_focus(window, cx);
639 cx.notify();
640 },
641 )
642 .detach();
643 self.status_dialog = Some(dialog);
644 cx.notify();
645 }
646
647 fn open_agent_dialog(
648 &mut self,
649 request: StartRequest,
650 window: &mut Window,
651 cx: &mut Context<Self>,
652 ) {
653 let Some(runs) = crate::agents::runs_handle(cx) else {
654 return;
655 };
656 let settings = runs.read(cx).settings();
657 let Some(action) = settings
658 .actions
659 .iter()
660 .find(|action| action.id == request.action_id)
661 .cloned()
662 else {
663 return;
664 };
665 let agent_name = settings
666 .agents
667 .iter()
668 .find(|agent| agent.id == action.agent_id)
669 .map(|agent| agent.name.clone())
670 .unwrap_or_default();
671 self.switcher = None;
672 let app = self.state.clone();
673 let dialog = cx.new(|cx| StartDialog::new(app, request, action, &agent_name, window, cx));
674 cx.subscribe_in(
675 &dialog,
676 window,
677 |this, _, _: &StartDialogEvent, window, cx| {
678 this.agent_dialog = None;
679 window.focus(&this.focus_handle, cx);
680 cx.notify();
681 },
682 )
683 .detach();
684 self.agent_dialog = Some(dialog);
685 cx.notify();
686 }
687
688 fn open_settings(
689 &mut self,
690 section: SettingsSection,
691 window: &mut Window,
692 cx: &mut Context<Self>,
693 ) {
694 if let Some(dialog) = &self.settings_dialog {
695 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx));
696 return;
697 }
698 self.close_image_viewer(window, cx);
699 self.switcher = None;
700 let (app, center) = (self.state.clone(), self.notifications.clone());
701 let dialog = cx.new(|cx| SettingsDialog::new(app, center, section, window, cx));
702 cx.subscribe_in(
703 &dialog,
704 window,
705 |this, _, _: &SettingsDialogEvent, window, cx| {
706 this.settings_dialog = None;
707 this.restore_focus(window, cx);
708 cx.notify();
709 },
710 )
711 .detach();
712 self.settings_dialog = Some(dialog);
713 cx.notify();
714 }
715
716 fn toggle_shortcuts_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
717 if self.shortcuts_dialog.take().is_some() {
718 self.restore_focus(window, cx);
719 cx.notify();
720 return;
721 }
722 self.close_image_viewer(window, cx);
723 self.switcher = None;
724 let dialog = cx.new(|cx| ShortcutsDialog::new(window, cx));
725 cx.subscribe_in(
726 &dialog,
727 window,
728 |this, _, _: &ShortcutsDialogEvent, window, cx| {
729 this.shortcuts_dialog = None;
730 this.restore_focus(window, cx);
731 cx.notify();
732 },
733 )
734 .detach();
735 self.shortcuts_dialog = Some(dialog);
736 cx.notify();
737 }
738
739 fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) {
740 let Some(request) = self
741 .state
742 .update(cx, |state, _| state.take_profile_request())
743 else {
744 return;
745 };
746 self.close_image_viewer(window, cx);
747 if let Some(card) = &self.profile_card {
748 card.update(cx, |card, cx| {
749 card.move_to(request.user_id, request.anchor, cx)
750 });
751 return;
752 }
753 let app = self.state.clone();
754 let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx));
755 cx.subscribe_in(
756 &card,
757 window,
758 |this, _, _: &ProfileCardEvent, window, cx| {
759 this.profile_card = None;
760 this.restore_focus(window, cx);
761 cx.notify();
762 },
763 )
764 .detach();
765 self.profile_card = Some(card);
766 cx.notify();
767 }
768
769 fn open_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
770 let Some(request) = self
771 .state
772 .update(cx, |state, _| state.take_image_viewer_request())
773 else {
774 return;
775 };
776 self.switcher = None;
777 let app = self.state.clone();
778 let viewer = cx.new(|cx| ImageViewer::new(app, request, window, cx));
779 cx.subscribe_in(
780 &viewer,
781 window,
782 |this, _, _: &ImageViewerEvent, window, cx| {
783 this.image_viewer = None;
784 window.focus(&this.focus_handle, cx);
785 cx.notify();
786 },
787 )
788 .detach();
789 self.image_viewer = Some(viewer);
790 cx.notify();
791 }
792
793 fn start_call_in_open_chat(&mut self, with_video: bool, cx: &mut Context<Self>) {
794 self.state.update(cx, |state, cx| {
795 if let Some(Selection::Chat(chat_id)) = state.selection.clone() {
796 if with_video {
797 state.start_chat_video_call(&chat_id, cx);
798 } else {
799 state.start_chat_call(&chat_id, cx);
800 }
801 }
802 });
803 }
804
805 fn close_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
806 if self.image_viewer.take().is_some() {
807 window.focus(&self.focus_handle, cx);
808 cx.notify();
809 }
810 }
811
812 fn restore_focus(&self, window: &mut Window, cx: &mut Context<Self>) {
813 match &self.image_viewer {
814 Some(viewer) => {
815 let handle = viewer.read(cx).focus_handle();
816 window.focus(&handle, cx)
817 }
818 None => window.focus(&self.focus_handle, cx),
819 }
820 }
821
822 fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
823 let Some(source) = self
824 .state
825 .update(cx, |state, _| state.take_forward_request())
826 else {
827 return;
828 };
829 self.switcher = None;
830 let candidates = candidates_from(self.state.read(cx));
831 let app = self.state.clone();
832 let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx));
833 cx.subscribe_in(
834 &dialog,
835 window,
836 |this, _, event: &ForwardDialogEvent, window, cx| {
837 this.forward_dialog = None;
838 this.restore_focus(window, cx);
839 if let ForwardDialogEvent::Sent { target, title } = event {
840 let target = target.clone();
841 let open = NoticeAction::new("Open", move |state, cx| {
842 state.select(target.clone(), cx)
843 });
844 this.state.update(cx, |state, cx| {
845 state.cancel_forward_selection(cx);
846 state.raise_notice(format!("Forwarded to {title}"), Some(open), cx)
847 });
848 }
849 cx.notify();
850 },
851 )
852 .detach();
853 self.forward_dialog = Some(dialog);
854 cx.notify();
855 }
856}
857
858impl AppShell {
859 fn render_splash(&mut self, cx: &mut Context<Self>) -> Option<AnyElement> {
860 let phase = if self.splash.is_up() {
861 let opening = self.conversation.read(cx).opening_title();
862 let state = self.state.read(cx);
863 let phase = splash::phase(&StartupFacts {
864 elapsed: self.splash.elapsed(),
865 connection: &state.connection,
866 has_chats: !state.sidebar.chats.is_empty() || !state.sidebar.teams.is_empty(),
867 synced: state.last_sync.is_some(),
868 demo: state.mode.demo,
869 opening,
870 });
871 if phase != Phase::Ready && cx.reduce_motion() && !self.splash.recheck_scheduled {
872 self.splash.recheck_scheduled = true;
873 cx.spawn(async move |this, cx| {
874 cx.background_executor().timer(splash::STILL_RECHECK).await;
875 this.update(cx, |this, cx| {
876 this.splash.recheck_scheduled = false;
877 cx.notify();
878 })
879 .ok();
880 })
881 .detach();
882 }
883 if phase == Phase::Ready {
884 self.splash.hide();
885 cx.spawn(async move |this, cx| {
886 cx.background_executor().timer(splash::CONTENT_RISE).await;
887 this.update(cx, |_, cx| cx.notify()).ok();
888 })
889 .detach();
890 }
891 phase
892 } else {
893 Phase::Ready
894 };
895 self.splash.render(&phase, cx)
896 }
897
898 fn islands(
899 &self,
900 show_sidebar: bool,
901 calendar_open: bool,
902 quick_view_open: bool,
903 calls_open: bool,
904 manage_team_open: bool,
905 ) -> Div {
906 h_flex()
907 .debug_selector(|| "shell/islands".into())
908 .flex_1()
909 .min_h_0()
910 .w_full()
911 .when(show_sidebar, |islands| islands.gap(px(ISLAND_GAP)))
912 .pr(px(ISLAND_GAP))
913 .pb(px(ISLAND_GAP))
914 .child(
915 h_flex()
916 .h_full()
917 .flex_none()
918 .child(self.rail.clone())
919 .when(show_sidebar, |group| {
920 group.child(
921 self.sidebar.clone().cached(
922 StyleRefinement::default()
923 .w(px(SIDEBAR_WIDTH))
924 .h_full()
925 .flex_none(),
926 ),
927 )
928 }),
929 )
930 .child(match () {
931 _ if calls_open => self
932 .calls_page
933 .clone()
934 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
935 .into_any_element(),
936 _ if manage_team_open => self
937 .manage_team_page
938 .clone()
939 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
940 .into_any_element(),
941 _ if quick_view_open => self
942 .quick_pane
943 .clone()
944 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
945 .into_any_element(),
946 _ if calendar_open => self
947 .calendar
948 .clone()
949 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
950 .into_any_element(),
951 _ => self
952 .conversation
953 .clone()
954 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
955 .into_any_element(),
956 })
957 }
958}
959
960impl Render for AppShell {
961 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
962 let open_dialogs = [
963 self.state.read(cx).task_dialog.is_some(),
964 self.forward_dialog.is_some(),
965 self.status_dialog.is_some(),
966 self.shortcuts_dialog.is_some(),
967 self.agent_dialog.is_some(),
968 self.settings_dialog.is_some(),
969 ]
970 .into_iter()
971 .filter(|open| *open)
972 .count();
973 elevation::set_app_modal_layers(window, open_dialogs, cx);
974 if open_dialogs > 0 {
975 crate::inline_video::stop(cx);
976 }
977 if let Some(dialog) = self
978 .state
979 .read(cx)
980 .task_dialog
981 .as_ref()
982 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
983 {
984 self.state.update(cx, |state, cx| {
985 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
986 });
987 }
988 if let Some(me) = self.state.read(cx).directory.me.clone() {
989 let app = self.state.clone();
990 cx.defer(move |cx| {
991 app.update(cx, |state, cx| {
992 state.request_avatars(vec![me.user_id.clone()], cx);
993 state.request_presence(vec![me.user_id], cx);
994 });
995 });
996 }
997 let notice = self.state.read(cx).notice.clone();
998 self.toasts.sync(&self.state, notice.as_ref(), window, cx);
999 let update_failure = match &self.update {
1000 UpdateStatus::Failed(message) => Some(message.clone()),
1001 _ => None,
1002 };
1003 self.toasts.update_failed(update_failure.as_deref(), window, cx);
1004 let calendar_tab = self.sidebar.read(cx).tab() == SidebarTab::Calendar;
1005 let chats_tab = self.sidebar.read(cx).tab() == SidebarTab::Chats;
1006 let calls_tab = self.sidebar.read(cx).tab() == SidebarTab::Calls;
1007 let splash = self.render_splash(cx);
1008 let state = self.state.read(cx);
1009 let calendar_open = calendar_tab;
1010 let quick_view_open = chats_tab && state.quick_view.is_some();
1011 let manage_team_open = !calendar_tab && !calls_tab && state.manage_team.is_some();
1012 let calls_open = calls_tab;
1013 let show_sidebar = !calendar_open && !calls_open;
1014 let banner = state.connection_banner_text().filter(|_| !self.splash.is_up()).map(|banner| {
1015 render_connection_banner(
1016 banner,
1017 cx.listener(|this, _, _, cx| {
1018 this.state.update(cx, |state, _| state.retry_live())
1019 }),
1020 )
1021 });
1022 let title_bar = render_title_bar(
1023 crate::active_appearance::current(cx).app_icon,
1024 mode_label(state.mode),
1025 [
1026 update_button(
1027 &self.update,
1028 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
1029 ),
1030 Some(settings_more_button(&self.state)),
1031 own_status_button(&self.state, state),
1032 ]
1033 .into_iter()
1034 .flatten(),
1035 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
1036 );
1037 let shell = div()
1038 .id("app-shell")
1039 .key_context("AppShell")
1040 .track_focus(&self.focus_handle);
1041 self.team_keys(shell, cx)
1042 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
1043 this.toggle_switcher("", window, cx);
1044 }))
1045 .on_action(cx.listener(|this, _: &NewChat, window, cx| {
1046 this.close_image_viewer(window, cx);
1047 this.state.update(cx, |state, cx| state.start_new_chat(cx));
1048 }))
1049 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
1050 this.state
1051 .update(cx, |state, cx| state.toggle_call_mute(cx));
1052 }))
1053 .on_action(cx.listener(|this, _: &PushToTalk, _, cx| {
1054 this.state
1055 .update(cx, |state, cx| state.press_push_to_talk(cx));
1056 }))
1057 .on_key_up(cx.listener(|this, event: &KeyUpEvent, _, cx| {
1058 if event.keystroke.key == "space" {
1059 this.state
1060 .update(cx, |state, cx| state.release_push_to_talk(cx));
1061 }
1062 }))
1063 .on_modifiers_changed(cx.listener(|this, event: &ModifiersChangedEvent, _, cx| {
1064 if !event.modifiers.control {
1065 this.state
1066 .update(cx, |state, cx| state.release_push_to_talk(cx));
1067 }
1068 }))
1069 .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
1070 this.open_settings(SettingsSection::General, window, cx);
1071 }))
1072 .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| {
1073 this.toggle_shortcuts_dialog(window, cx);
1074 }))
1075 .on_action(cx.listener(|this, _: &OpenActivity, window, cx| {
1076 this.close_image_viewer(window, cx);
1077 this.sidebar
1078 .update(cx, |sidebar, cx| sidebar.show_activity(cx));
1079 }))
1080 .on_action(cx.listener(|this, _: &OpenSaved, window, cx| {
1081 this.close_image_viewer(window, cx);
1082 this.sidebar
1083 .update(cx, |sidebar, cx| sidebar.open_quick_view(QuickView::Saved, cx));
1084 }))
1085 .on_action(cx.listener(|this, _: &OpenCalendar, window, cx| {
1086 this.close_image_viewer(window, cx);
1087 this.sidebar
1088 .update(cx, |sidebar, cx| sidebar.show_calendar(cx));
1089 }))
1090 .on_action(cx.listener(|this, _: &OpenAgents, window, cx| {
1091 this.close_image_viewer(window, cx);
1092 this.sidebar
1093 .update(cx, |sidebar, cx| sidebar.show_agents(cx));
1094 }))
1095 .on_action(cx.listener(|this, _: &OpenChatsTab, window, cx| {
1096 this.close_image_viewer(window, cx);
1097 this.sidebar
1098 .update(cx, |sidebar, cx| sidebar.show_chats(cx));
1099 }))
1100 .on_action(cx.listener(|this, _: &FindInChat, window, cx| {
1101 this.close_image_viewer(window, cx);
1102 this.state
1103 .update(cx, |state, cx| state.close_quick_view(cx));
1104 this.conversation
1105 .update(cx, |conversation, cx| conversation.open_find(window, cx));
1106 }))
1107 .on_action(cx.listener(|this, _: &PopOutSelectedChat, _, cx| {
1108 let selected = this.state.read(cx).selection.clone();
1109 if let Some(Selection::Chat(chat_id)) = selected {
1110 chat_pop_out::pop_out_chat(&this.state, &chat_id, cx);
1111 }
1112 }))
1113 .on_action(cx.listener(|this, _: &OpenChatFilter, window, cx| {
1114 this.close_image_viewer(window, cx);
1115 this.sidebar
1116 .update(cx, |sidebar, cx| sidebar.toggle_filter_box(window, cx));
1117 }))
1118 .on_action(cx.listener(|this, _: &FilterUnreadChats, _, cx| {
1119 this.sidebar
1120 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Unread, cx));
1121 }))
1122 .on_action(cx.listener(|this, _: &FilterNonMeetingChats, _, cx| {
1123 this.sidebar
1124 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Chats, cx));
1125 }))
1126 .on_action(cx.listener(|this, _: &FilterMeetingChats, _, cx| {
1127 this.sidebar
1128 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Meetings, cx));
1129 }))
1130 .on_action(cx.listener(|this, _: &FilterMutedChats, _, cx| {
1131 this.sidebar
1132 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Muted, cx));
1133 }))
1134 .on_action(cx.listener(|this, _: &ClearChatFilters, window, cx| {
1135 this.sidebar
1136 .update(cx, |sidebar, cx| sidebar.clear_filters(window, cx));
1137 }))
1138 .on_action(cx.listener(|this, _: &OpenChannelsTab, window, cx| {
1139 this.close_image_viewer(window, cx);
1140 this.sidebar
1141 .update(cx, |sidebar, cx| sidebar.show_channels(cx));
1142 }))
1143 .on_action(cx.listener(|this, _: &OpenCallsTab, window, cx| {
1144 this.close_image_viewer(window, cx);
1145 this.sidebar
1146 .update(cx, |sidebar, cx| sidebar.show_calls(cx));
1147 }))
1148 .on_action(cx.listener(|this, _: &PreviousConversation, window, cx| {
1149 this.close_image_viewer(window, cx);
1150 this.sidebar.update(cx, |sidebar, cx| {
1151 sidebar.select_adjacent(Step::Previous, cx)
1152 });
1153 }))
1154 .on_action(cx.listener(|this, _: &NextConversation, window, cx| {
1155 this.close_image_viewer(window, cx);
1156 this.sidebar
1157 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx));
1158 }))
1159 .on_action(cx.listener(|this, _: &DeclineCall, _, cx| {
1160 this.state
1161 .update(cx, |state, cx| state.decline_ringing_call(cx));
1162 }))
1163 .on_action(cx.listener(|this, _: &JoinMeetingFromToast, _, cx| {
1164 this.state
1165 .update(cx, |state, cx| state.join_latest_meeting_toast(cx));
1166 }))
1167 .on_action(cx.listener(|this, _: &HangUpCall, _, cx| {
1168 this.state.update(cx, |state, cx| state.leave_call(cx));
1169 }))
1170 .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| {
1171 this.state
1172 .update(cx, |state, cx| state.toggle_call_camera(cx));
1173 }))
1174 .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| {
1175 this.state
1176 .update(cx, |state, cx| state.toggle_call_share(cx));
1177 }))
1178 .on_action(cx.listener(|this, _: &ToggleCallHand, _, cx| {
1179 this.state
1180 .update(cx, |state, cx| state.toggle_call_hand(cx));
1181 }))
1182 .on_action(cx.listener(|this, _: &OpenLobbyList, _, cx| {
1183 this.state
1184 .update(cx, |state, cx| state.open_call_lobby_list(cx));
1185 }))
1186 .on_action(cx.listener(|this, _: &StartAudioCall, _, cx| {
1187 this.start_call_in_open_chat(false, cx);
1188 }))
1189 .on_action(cx.listener(|this, _: &StartVideoCall, _, cx| {
1190 this.start_call_in_open_chat(true, cx);
1191 }))
1192 .on_action(cx.listener(|this, _: &ZoomIn, _, cx| change_zoom(&this.state, WindowZoom::In, cx)))
1193 .on_action(cx.listener(|this, _: &ZoomOut, _, cx| change_zoom(&this.state, WindowZoom::Out, cx)))
1194 .on_action(cx.listener(|this, _: &ZoomReset, _, cx| change_zoom(&this.state, WindowZoom::Reset, cx)))
1195 .on_action(zoom_listener::<ZoomShareIn>(&self.state, ZoomStep::In))
1196 .on_action(zoom_listener::<ZoomShareOut>(&self.state, ZoomStep::Out))
1197 .on_action(zoom_listener::<ZoomShareReset>(&self.state, ZoomStep::Reset))
1198 .on_action(cx.listener(|this, _: &JumpToLastReadOrNewest, _, cx| {
1199 if this.image_viewer.is_some() {
1200 cx.propagate();
1201 return;
1202 }
1203 this.conversation
1204 .update(cx, |conversation, cx| conversation.jump_to_last_read_or_newest(cx));
1205 }))
1206 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
1207 this.close_image_viewer(window, cx);
1208 this.conversation.update(cx, |conversation, cx| {
1209 conversation.reply_to_hovered(window, cx)
1210 });
1211 }))
1212 .relative()
1213 .size_full()
1214 .bg(theme::background())
1215 .text_color(theme::text())
1216 .text_size(px(14.))
1217 .child(crate::frame_log::probe("first"))
1218 .child(user_activity::input_probe(self.user_input.clone()))
1219 .child(app_ui_kit::long_press::long_press_right_click())
1220 .child(
1221 v_flex()
1222 .debug_selector(|| "shell".into())
1223 .size_full()
1224 .child(title_bar)
1225 .children(banner)
1226 .child(rising_in(self.islands(show_sidebar, calendar_open, quick_view_open, calls_open, manage_team_open), &self.splash, cx)),
1227 )
1228 .children(splash)
1229 .children(self.image_viewer.clone())
1230 .children(self.switcher.clone())
1231 .children(render_task_dialog(self.state.read(cx), cx))
1232 .children(self.forward_dialog.clone())
1233 .children(self.status_dialog.clone())
1234 .children(self.shortcuts_dialog.clone())
1235 .children(self.agent_dialog.clone())
1236 .children(self.settings_dialog.clone())
1237 .children(self.profile_card.clone())
1238 .child(crate::frame_log::probe("last"))
1239 }
1240}
1241
1242fn rising_in(islands: Div, splash: &Splash, cx: &App) -> AnyElement {
1243 if splash.content_rising().is_none() || cx.reduce_motion() {
1244 return islands.into_any_element();
1245 }
1246 islands
1247 .relative()
1248 .with_animation("startup-content-rise", Animation::new(splash::CONTENT_RISE).with_easing(ease_out_quint()), |islands, delta| {
1249 islands.top(px(splash::CONTENT_RISE_OFFSET * (1. - delta))).opacity(delta)
1250 })
1251 .into_any_element()
1252}
1253
1254#[cfg(test)]
1255mod opening_link_tests {
1256 use super::opening_link_text;
1257
1258 #[test]
1259 fn the_notice_names_the_host() {
1260 assert_eq!(opening_link_text("https://example.com/a?b#c"), "Opening example.com...");
1261 assert_eq!(opening_link_text("mailto:"), "Opening mailto:...");
1262 assert_eq!(opening_link_text(""), "Opening link...");
1263 }
1264}
1265