BlueGit

ict rusty-teams

Files

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

shell.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
shell.rs 49.4 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 has_chats: !state.sidebar.chats.is_empty() || !state.sidebar.teams.is_empty(),
878 synced: state.last_sync.is_some(),
879 demo: state.mode.demo,
880 opening,
881 });
882 if phase != Phase::Ready && cx.reduce_motion() && !self.splash.recheck_scheduled {
883 self.splash.recheck_scheduled = true;
884 cx.spawn(async move |this, cx| {
885 cx.background_executor().timer(splash::STILL_RECHECK).await;
886 this.update(cx, |this, cx| {
887 this.splash.recheck_scheduled = false;
888 cx.notify();
889 })
890 .ok();
891 })
892 .detach();
893 }
894 if phase == Phase::Ready {
895 self.splash.hide();
896 cx.spawn(async move |this, cx| {
897 cx.background_executor().timer(splash::CONTENT_RISE).await;
898 this.update(cx, |_, cx| cx.notify()).ok();
899 })
900 .detach();
901 }
902 phase
903 } else {
904 Phase::Ready
905 };
906 self.splash.render(&phase, cx)
907 }
908
909 fn islands(
910 &self,
911 show_sidebar: bool,
912 calendar_open: bool,
913 quick_view_open: bool,
914 calls_open: bool,
915 manage_team_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(h_flex().h_full().flex_none().child(self.rail.clone()).when(
926 show_sidebar,
927 |group| {
928 group.child(
929 self.sidebar.clone().cached(
930 StyleRefinement::default()
931 .w(px(SIDEBAR_WIDTH))
932 .h_full()
933 .flex_none(),
934 ),
935 )
936 },
937 ))
938 .child(match () {
939 _ if calls_open => self
940 .calls_page
941 .clone()
942 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
943 .into_any_element(),
944 _ if manage_team_open => self
945 .manage_team_page
946 .clone()
947 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
948 .into_any_element(),
949 _ if quick_view_open => self
950 .quick_pane
951 .clone()
952 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
953 .into_any_element(),
954 _ if calendar_open => self
955 .calendar
956 .clone()
957 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
958 .into_any_element(),
959 _ => self
960 .conversation
961 .clone()
962 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
963 .into_any_element(),
964 })
965 }
966}
967
968impl Render for AppShell {
969 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
970 let open_dialogs = [
971 self.state.read(cx).task_dialog.is_some(),
972 self.forward_dialog.is_some(),
973 self.status_dialog.is_some(),
974 self.shortcuts_dialog.is_some(),
975 self.agent_dialog.is_some(),
976 self.settings_dialog.is_some(),
977 ]
978 .into_iter()
979 .filter(|open| *open)
980 .count();
981 elevation::set_app_modal_layers(window, open_dialogs, cx);
982 if open_dialogs > 0 {
983 crate::inline_video::stop(cx);
984 }
985 if let Some(dialog) = self
986 .state
987 .read(cx)
988 .task_dialog
989 .as_ref()
990 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
991 {
992 self.state.update(cx, |state, cx| {
993 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
994 });
995 }
996 if let Some(me) = self.state.read(cx).directory.me.clone() {
997 let app = self.state.clone();
998 cx.defer(move |cx| {
999 app.update(cx, |state, cx| {
1000 state.request_avatars(vec![me.user_id.clone()], cx);
1001 state.request_presence(vec![me.user_id], cx);
1002 });
1003 });
1004 }
1005 let notice = self.state.read(cx).notice.clone();
1006 self.toasts.sync(&self.state, notice.as_ref(), window, cx);
1007 let update_failure = match &self.update {
1008 UpdateStatus::Failed(message) => Some(message.clone()),
1009 _ => None,
1010 };
1011 self.toasts
1012 .update_failed(update_failure.as_deref(), window, cx);
1013 let calendar_tab = self.sidebar.read(cx).tab() == SidebarTab::Calendar;
1014 let chats_tab = self.sidebar.read(cx).tab() == SidebarTab::Chats;
1015 let calls_tab = self.sidebar.read(cx).tab() == SidebarTab::Calls;
1016 let splash = self.render_splash(cx);
1017 let state = self.state.read(cx);
1018 let calendar_open = calendar_tab;
1019 let quick_view_open = chats_tab && state.quick_view.is_some();
1020 let manage_team_open = !calendar_tab && !calls_tab && state.manage_team.is_some();
1021 let calls_open = calls_tab;
1022 let show_sidebar = !calendar_open && !calls_open;
1023 let banner = state
1024 .connection_banner_text()
1025 .filter(|_| !self.splash.is_up())
1026 .map(|banner| {
1027 render_connection_banner(
1028 banner,
1029 cx.listener(|this, _, _, cx| {
1030 this.state.update(cx, |state, _| state.retry_live())
1031 }),
1032 )
1033 });
1034 let title_bar = render_title_bar(
1035 crate::active_appearance::current(cx).app_icon,
1036 mode_label(state.mode),
1037 [
1038 update_button(
1039 &self.update,
1040 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
1041 ),
1042 Some(settings_more_button(&self.state)),
1043 own_status_button(&self.state, state),
1044 ]
1045 .into_iter()
1046 .flatten(),
1047 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
1048 );
1049 let shell = div()
1050 .id("app-shell")
1051 .key_context("AppShell")
1052 .track_focus(&self.focus_handle);
1053 self.team_keys(shell, cx)
1054 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
1055 this.toggle_switcher("", window, cx);
1056 }))
1057 .on_action(cx.listener(|this, _: &NewChat, window, cx| {
1058 this.close_image_viewer(window, cx);
1059 this.state.update(cx, |state, cx| state.start_new_chat(cx));
1060 }))
1061 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
1062 this.state
1063 .update(cx, |state, cx| state.toggle_call_mute(cx));
1064 }))
1065 .on_action(cx.listener(|this, _: &PushToTalk, _, cx| {
1066 this.state
1067 .update(cx, |state, cx| state.press_push_to_talk(cx));
1068 }))
1069 .on_key_up(cx.listener(|this, event: &KeyUpEvent, _, cx| {
1070 if event.keystroke.key == "space" {
1071 this.state
1072 .update(cx, |state, cx| state.release_push_to_talk(cx));
1073 }
1074 }))
1075 .on_modifiers_changed(cx.listener(|this, event: &ModifiersChangedEvent, _, cx| {
1076 if !event.modifiers.control {
1077 this.state
1078 .update(cx, |state, cx| state.release_push_to_talk(cx));
1079 }
1080 }))
1081 .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
1082 this.open_settings(SettingsSection::General, window, cx);
1083 }))
1084 .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| {
1085 this.toggle_shortcuts_dialog(window, cx);
1086 }))
1087 .on_action(cx.listener(|this, _: &OpenActivity, window, cx| {
1088 this.close_image_viewer(window, cx);
1089 this.sidebar
1090 .update(cx, |sidebar, cx| sidebar.show_activity(cx));
1091 }))
1092 .on_action(cx.listener(|this, _: &OpenSaved, window, cx| {
1093 this.close_image_viewer(window, cx);
1094 this.sidebar.update(cx, |sidebar, cx| {
1095 sidebar.open_quick_view(QuickView::Saved, cx)
1096 });
1097 }))
1098 .on_action(cx.listener(|this, _: &OpenCalendar, window, cx| {
1099 this.close_image_viewer(window, cx);
1100 this.sidebar
1101 .update(cx, |sidebar, cx| sidebar.show_calendar(cx));
1102 }))
1103 .on_action(cx.listener(|this, _: &OpenAgents, window, cx| {
1104 this.close_image_viewer(window, cx);
1105 this.sidebar
1106 .update(cx, |sidebar, cx| sidebar.show_agents(cx));
1107 }))
1108 .on_action(cx.listener(|this, _: &OpenChatsTab, window, cx| {
1109 this.close_image_viewer(window, cx);
1110 this.sidebar
1111 .update(cx, |sidebar, cx| sidebar.show_chats(cx));
1112 }))
1113 .on_action(cx.listener(|this, _: &FindInChat, window, cx| {
1114 this.close_image_viewer(window, cx);
1115 this.state
1116 .update(cx, |state, cx| state.close_quick_view(cx));
1117 this.conversation
1118 .update(cx, |conversation, cx| conversation.open_find(window, cx));
1119 }))
1120 .on_action(cx.listener(|this, _: &PopOutSelectedChat, _, cx| {
1121 let selected = this.state.read(cx).selection.clone();
1122 if let Some(Selection::Chat(chat_id)) = selected {
1123 chat_pop_out::pop_out_chat(&this.state, &chat_id, cx);
1124 }
1125 }))
1126 .on_action(cx.listener(|this, _: &OpenChatFilter, window, cx| {
1127 this.close_image_viewer(window, cx);
1128 this.sidebar
1129 .update(cx, |sidebar, cx| sidebar.toggle_filter_box(window, cx));
1130 }))
1131 .on_action(cx.listener(|this, _: &FilterUnreadChats, _, cx| {
1132 this.sidebar.update(cx, |sidebar, cx| {
1133 sidebar.toggle_pill(ChatFilterPill::Unread, cx)
1134 });
1135 }))
1136 .on_action(cx.listener(|this, _: &FilterNonMeetingChats, _, cx| {
1137 this.sidebar.update(cx, |sidebar, cx| {
1138 sidebar.toggle_pill(ChatFilterPill::Chats, cx)
1139 });
1140 }))
1141 .on_action(cx.listener(|this, _: &FilterMeetingChats, _, cx| {
1142 this.sidebar.update(cx, |sidebar, cx| {
1143 sidebar.toggle_pill(ChatFilterPill::Meetings, cx)
1144 });
1145 }))
1146 .on_action(cx.listener(|this, _: &FilterMutedChats, _, cx| {
1147 this.sidebar.update(cx, |sidebar, cx| {
1148 sidebar.toggle_pill(ChatFilterPill::Muted, cx)
1149 });
1150 }))
1151 .on_action(cx.listener(|this, _: &ClearChatFilters, window, cx| {
1152 this.sidebar
1153 .update(cx, |sidebar, cx| sidebar.clear_filters(window, cx));
1154 }))
1155 .on_action(cx.listener(|this, _: &OpenChannelsTab, window, cx| {
1156 this.close_image_viewer(window, cx);
1157 this.sidebar
1158 .update(cx, |sidebar, cx| sidebar.show_channels(cx));
1159 }))
1160 .on_action(cx.listener(|this, _: &OpenCallsTab, window, cx| {
1161 this.close_image_viewer(window, cx);
1162 this.sidebar
1163 .update(cx, |sidebar, cx| sidebar.show_calls(cx));
1164 }))
1165 .on_action(cx.listener(|this, _: &PreviousConversation, window, cx| {
1166 this.close_image_viewer(window, cx);
1167 this.sidebar.update(cx, |sidebar, cx| {
1168 sidebar.select_adjacent(Step::Previous, cx)
1169 });
1170 }))
1171 .on_action(cx.listener(|this, _: &NextConversation, window, cx| {
1172 this.close_image_viewer(window, cx);
1173 this.sidebar
1174 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx));
1175 }))
1176 .on_action(cx.listener(|this, _: &DeclineCall, _, cx| {
1177 this.state
1178 .update(cx, |state, cx| state.decline_ringing_call(cx));
1179 }))
1180 .on_action(cx.listener(|this, _: &JoinMeetingFromToast, _, cx| {
1181 this.state
1182 .update(cx, |state, cx| state.join_latest_meeting_toast(cx));
1183 }))
1184 .on_action(cx.listener(|this, _: &HangUpCall, _, cx| {
1185 this.state.update(cx, |state, cx| state.leave_call(cx));
1186 }))
1187 .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| {
1188 this.state
1189 .update(cx, |state, cx| state.toggle_call_camera(cx));
1190 }))
1191 .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| {
1192 this.state
1193 .update(cx, |state, cx| state.toggle_call_share(cx));
1194 }))
1195 .on_action(cx.listener(|this, _: &ToggleCallHand, _, cx| {
1196 this.state
1197 .update(cx, |state, cx| state.toggle_call_hand(cx));
1198 }))
1199 .on_action(cx.listener(|this, _: &OpenLobbyList, _, cx| {
1200 this.state
1201 .update(cx, |state, cx| state.open_call_lobby_list(cx));
1202 }))
1203 .on_action(cx.listener(|this, _: &StartAudioCall, _, cx| {
1204 this.start_call_in_open_chat(false, cx);
1205 }))
1206 .on_action(cx.listener(|this, _: &StartVideoCall, _, cx| {
1207 this.start_call_in_open_chat(true, cx);
1208 }))
1209 .on_action(
1210 cx.listener(|this, _: &ZoomIn, _, cx| change_zoom(&this.state, WindowZoom::In, cx)),
1211 )
1212 .on_action(
1213 cx.listener(|this, _: &ZoomOut, _, cx| {
1214 change_zoom(&this.state, WindowZoom::Out, cx)
1215 }),
1216 )
1217 .on_action(cx.listener(|this, _: &ZoomReset, _, cx| {
1218 change_zoom(&this.state, WindowZoom::Reset, cx)
1219 }))
1220 .on_action(zoom_listener::<ZoomShareIn>(&self.state, ZoomStep::In))
1221 .on_action(zoom_listener::<ZoomShareOut>(&self.state, ZoomStep::Out))
1222 .on_action(zoom_listener::<ZoomShareReset>(
1223 &self.state,
1224 ZoomStep::Reset,
1225 ))
1226 .on_action(cx.listener(|this, _: &JumpToLastReadOrNewest, _, cx| {
1227 if this.image_viewer.is_some() {
1228 cx.propagate();
1229 return;
1230 }
1231 this.conversation.update(cx, |conversation, cx| {
1232 conversation.jump_to_last_read_or_newest(cx)
1233 });
1234 }))
1235 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
1236 this.close_image_viewer(window, cx);
1237 this.conversation.update(cx, |conversation, cx| {
1238 conversation.reply_to_hovered(window, cx)
1239 });
1240 }))
1241 .relative()
1242 .size_full()
1243 .bg(theme::background())
1244 .text_color(theme::text())
1245 .text_size(px(14.))
1246 .child(crate::frame_log::probe("first"))
1247 .child(user_activity::input_probe(self.user_input.clone()))
1248 .child(app_ui_kit::long_press::long_press_right_click())
1249 .child(
1250 v_flex()
1251 .debug_selector(|| "shell".into())
1252 .size_full()
1253 .child(title_bar)
1254 .children(banner)
1255 .child(rising_in(
1256 self.islands(
1257 show_sidebar,
1258 calendar_open,
1259 quick_view_open,
1260 calls_open,
1261 manage_team_open,
1262 ),
1263 &self.splash,
1264 cx,
1265 )),
1266 )
1267 .children(splash)
1268 .children(self.image_viewer.clone())
1269 .children(self.switcher.clone())
1270 .children(render_task_dialog(self.state.read(cx), cx))
1271 .children(self.forward_dialog.clone())
1272 .children(self.status_dialog.clone())
1273 .children(self.shortcuts_dialog.clone())
1274 .children(self.agent_dialog.clone())
1275 .children(self.settings_dialog.clone())
1276 .children(self.profile_card.clone())
1277 .child(crate::frame_log::probe("last"))
1278 }
1279}
1280
1281fn rising_in(islands: Div, splash: &Splash, cx: &App) -> AnyElement {
1282 if splash.content_rising().is_none() || cx.reduce_motion() {
1283 return islands.into_any_element();
1284 }
1285 islands
1286 .relative()
1287 .with_animation(
1288 "startup-content-rise",
1289 Animation::new(splash::CONTENT_RISE).with_easing(ease_out_quint()),
1290 |islands, delta| {
1291 islands
1292 .top(px(splash::CONTENT_RISE_OFFSET * (1. - delta)))
1293 .opacity(delta)
1294 },
1295 )
1296 .into_any_element()
1297}
1298
1299#[cfg(test)]
1300mod opening_link_tests {
1301 use super::opening_link_text;
1302
1303 #[test]
1304 fn the_notice_names_the_host() {
1305 assert_eq!(
1306 opening_link_text("https://example.com/a?b#c"),
1307 "Opening example.com..."
1308 );
1309 assert_eq!(opening_link_text("mailto:"), "Opening mailto:...");
1310 assert_eq!(opening_link_text(""), "Opening link...");
1311 }
1312}