BlueGit

ict rusty-teams

Files

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

shell.rs

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