BlueGit

ict rusty-teams

Files

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

shell.rs

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