BlueGit

ict rusty-teams

Files

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

shell.rs

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