BlueGit

ict rusty-teams

Files

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

shell.rs

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