BlueGit

ict rusty-teams

Files

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

shell.rs

shell.rs 48.3 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::call_mini::CallMiniView;
10use super::call_stage::{render_call_overlay, render_stage_overlay};
11use super::call_end_view::render_call_end;
12use super::call_view::{CHAT_PANEL_WIDTH, OVERLAY_BELOW as CALL_COLUMN_MIN, render_call_view};
13use super::actions::{NewChat, OpenSwitcher, ReplyToHovered, ToggleCallMute};
14use super::calendar_view::CalendarView;
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::prejoin_view::PrejoinView;
20use super::profile_card::{ProfileCard, ProfileCardEvent};
21use gpui_kit::prelude::FluentBuilder as _;
22use super::rail::RailView;
23use crate::chat_filter::ChatFilterPill;
24use super::saved_panel::{SavedPanel, SavedPanelEvent};
25use super::shortcuts::{
26 ClearChatFilters, DeclineCall, FilterMeetingChats, FilterMutedChats,
27 FilterNonMeetingChats, FilterUnreadChats, FindInChat, HangUpCall, JoinMeetingFromToast, JumpToLastReadOrNewest, NextConversation, OpenActivity,
28 OpenAgents, OpenCallsTab, OpenSettings, OpenChannelsTab, OpenChatFilter, OpenChatsTab, PopOutSelectedChat, OpenLobbyList, OpenSaved, OpenCalendar, PreviousConversation, PushToTalk,
29 Scope, ShowShortcuts, StartAudioCall, StartVideoCall, ToggleCallCamera, ToggleCallFullscreen,
30 ToggleCallHand, ToggleCallShare, ZoomIn, ZoomOut, ZoomReset, ZoomShareIn, ZoomShareOut,
31 ZoomShareReset,
32};
33use super::window_zoom::{WindowZoom, change_zoom};
34use super::call_pop_out::zoom_listener;
35use super::chat_pop_out;
36use super::shortcuts_dialog::{ShortcutsDialog, ShortcutsDialogEvent};
37use super::sidebar::{SidebarEvent, SidebarTab, SidebarView};
38use app_ui_kit::render::hooks::RenderHooks;
39use app_ui_kit::layout::{ISLAND_GAP, RAIL_WIDTH, SIDEBAR_WIDTH};
40use super::connection_banner::render_connection_banner;
41use super::notice_toast::NoticeToasts;
42use super::status_menu::own_status_button;
43use super::status_message_dialog::{StatusMessageDialog, StatusMessageDialogEvent};
44use super::switcher::{Switcher, SwitcherEvent, candidates_from};
45use super::title_bar::{mode_label, render_title_bar, update_button};
46use crate::activity::ActivityCenter;
47use crate::agents::{AgentRunsEvent, StartDialog, StartDialogEvent, StartRequest};
48use crate::app_state::{AppEvent, AppState, Selection, selection_for};
49use crate::call::{CallMedia, CallWindows, ZoomStep};
50use crate::notice::NoticeAction;
51use crate::notify::NotificationCenter;
52use crate::settings::{SettingsDialog, SettingsDialogEvent};
53use crate::settings_section::SettingsSection;
54use crate::sidebar_model::Step;
55use crate::theme;
56use crate::updater::{self, IdleInputs, UpdateStatus};
57use crate::user_activity::{self, UserInput};
58
59pub fn bind_keys(cx: &mut App) {
60 super::composer::bind_keys(cx);
61 super::shortcuts::bind_scopes(
62 cx,
63 &[
64 Scope::Global,
65 Scope::OutsideComposer,
66 Scope::GlobalOverTextFields,
67 Scope::OutsideTextFields,
68 ],
69 );
70}
71
72#[derive(Debug, Clone, Copy)]
73pub enum OpenTarget {
74 Chat(usize),
75 Channel(usize),
76}
77
78#[derive(Default)]
79pub struct Startup {
80 pub switcher_query: Option<String>,
81 pub composer_text: Option<String>,
82 pub reply_to_last: bool,
83 pub launched_by_autostart: bool,
84}
85
86pub const SIDEBAR_HIDDEN_BELOW: f32 = 960.;
87
88pub fn sidebar_hidden_in_call(window_width: f32, panel_open: bool) -> bool {
89 let panel_width = if panel_open { CHAT_PANEL_WIDTH + ISLAND_GAP } else { 0. };
90 window_width < SIDEBAR_HIDDEN_BELOW
91 || window_width - RAIL_WIDTH - SIDEBAR_WIDTH - 2. * ISLAND_GAP - panel_width < CALL_COLUMN_MIN
92}
93
94pub struct UserInputHandle(pub Arc<UserInput>);
95
96impl Global for UserInputHandle {}
97
98pub fn install_globals(state: &Entity<AppState>, user_input: Arc<UserInput>, cx: &mut App) {
99 cx.set_global(crate::app_state::AppHandle(state.clone()));
100 cx.set_global(UserInputHandle(user_input));
101 cx.set_global(RenderHooks {
102 open_profile_link: super::profile_card::open_profile_link,
103 jump_to_quoted,
104 inline_image,
105 });
106}
107
108fn inline_image(url: &str, cx: &App) -> Option<std::path::PathBuf> {
109 let handle = cx.try_global::<crate::app_state::AppHandle>()?;
110 let entry = handle.0.read(cx).directory.image(url)?;
111 Some(entry.path.clone())
112}
113
114fn jump_to_quoted(message_id: String, cx: &mut App) {
115 let Some(handle) = cx.try_global::<crate::app_state::AppHandle>() else {
116 return;
117 };
118 handle.0.clone().update(cx, |state, cx| {
119 if let Some(selection) = state.selection.clone() {
120 state.jump_to_message(selection, message_id, cx);
121 }
122 });
123}
124
125#[cfg(windows)]
126fn watch_releases(running_tag: &'static str, cx: &mut App) {
127 use app_base::release_feed;
128 cx.spawn(async move |cx| {
129 cx.background_executor()
130 .timer(release_feed::FIRST_CHECK_DELAY)
131 .await;
132 loop {
133 let Some(paths) = updater::current_paths() else {
134 return;
135 };
136 let fetch = cx.update(|cx| release_feed::fetch_update(running_tag, paths, cx));
137 match fetch.await {
138 Ok(Some(tag)) => log::info!("release {tag} downloaded for the next restart"),
139 Ok(None) => {}
140 Err(error) => log::warn!("release check failed: {error}"),
141 }
142 cx.background_executor()
143 .timer(release_feed::CHECK_INTERVAL)
144 .await;
145 }
146 })
147 .detach();
148}
149
150const DEMO_UPDATE_VARIABLE: &str = "TEAMS_DEMO_UPDATE";
151
152pub struct AppShell {
153 state: Entity<AppState>,
154 rail: Entity<RailView>,
155 sidebar: Entity<SidebarView>,
156 calendar: Entity<CalendarView>,
157 shown_selection: Option<Selection>,
158 conversation: Entity<ConversationView>,
159 call_chat: Option<(String, Entity<ConversationView>)>,
160 prejoin: Option<Entity<PrejoinView>>,
161 switcher: Option<Entity<Switcher>>,
162 notifications: Entity<NotificationCenter>,
163 _call_windows: Entity<CallWindows>,
164 forward_dialog: Option<Entity<ForwardDialog>>,
165 image_viewer: Option<Entity<ImageViewer>>,
166 status_dialog: Option<Entity<StatusMessageDialog>>,
167 shortcuts_dialog: Option<Entity<ShortcutsDialog>>,
168 agent_dialog: Option<Entity<StartDialog>>,
169 settings_dialog: Option<Entity<SettingsDialog>>,
170 profile_card: Option<Entity<ProfileCard>>,
171 focus_handle: FocusHandle,
172 call_window_fullscreen: bool,
173 window_was_fullscreen: bool,
174 open_target: Option<OpenTarget>,
175 update: UpdateStatus,
176 toasts: NoticeToasts,
177 user_input: Arc<UserInput>,
178 _subscription: Subscription,
179 call_mini: Entity<CallMiniView>,
180 _call_media_observation: Subscription,
181 _panel_subscriptions: [Subscription; 2],
182 _sidebar_subscription: Subscription,
183 _agents_subscription: Option<Subscription>,
184 _agent_subscription: Option<Subscription>,
185 _escape_observation: Subscription,
186 _activation_observation: Subscription,
187 _appearance_observation: Subscription,
188}
189
190impl AppShell {
191 #[cfg(test)]
192 pub(crate) fn set_update_status(&mut self, update: UpdateStatus, cx: &mut Context<Self>) {
193 self.update = update;
194 cx.notify();
195 }
196
197 fn sync_call_chat(
198 &mut self,
199 thread: Option<String>,
200 window: &mut Window,
201 cx: &mut Context<Self>,
202 ) {
203 match (thread, &self.call_chat) {
204 (Some(wanted), Some((open, _))) if &wanted == open => {}
205 (Some(wanted), _) => {
206 let app = self.state.clone();
207 let selection = Selection::Chat(wanted.clone());
208 let view = cx.new(|cx| ConversationView::pinned_to(app, selection, window, cx));
209 self.call_chat = Some((wanted, view));
210 }
211 (None, _) => self.call_chat = None,
212 }
213 }
214
215 fn leave_calendar_on_selection(&mut self, cx: &mut Context<Self>) {
216 let selection = self.state.read(cx).selection.clone();
217 if selection == self.shown_selection {
218 return;
219 }
220 self.shown_selection = selection.clone();
221 if self.sidebar.read(cx).tab() != SidebarTab::Calendar {
222 return;
223 }
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 call_media_observation = cx.observe(&call_media, |this, _, cx| {
257 if this.state.read(cx).viewing_call() {
258 cx.notify();
259 }
260 });
261 let activity = cx.new(|cx| ActivityCenter::new(state.clone(), window, cx));
262 let activity_panel = cx.new(|cx| ActivityPanel::new(state.clone(), activity.clone(), cx));
263 let saved_panel = cx.new(|cx| SavedPanel::new(state.clone(), cx));
264 let agents_panel = crate::agents::runs_handle(cx)
265 .map(|runs| cx.new(|cx| crate::agents::AgentsPanel::new(runs, cx)));
266 let agents_subscription = agents_panel.as_ref().map(|panel| {
267 cx.subscribe(
268 panel,
269 |this, _, event: &crate::agents::AgentsPanelEvent, cx| {
270 let crate::agents::AgentsPanelEvent::Open {
271 conversation_id,
272 message_id,
273 } = event;
274 this.open_message(conversation_id, message_id, cx);
275 },
276 )
277 });
278 let panel_subscriptions = [
279 cx.subscribe(
280 &activity_panel,
281 |this, _, event: &ActivityPanelEvent, cx| match event {
282 ActivityPanelEvent::OpenSettings => this.state.update(cx, |state, cx| {
283 state.open_settings(SettingsSection::Notifications, cx)
284 }),
285 ActivityPanelEvent::Open {
286 conversation_id,
287 message_id,
288 link,
289 } => {
290 if !this.open_message(conversation_id, message_id, cx)
291 && let Some(link) = link
292 {
293 cx.open_url(link);
294 }
295 }
296 },
297 ),
298 cx.subscribe(&saved_panel, |this, _, event: &SavedPanelEvent, cx| {
299 let SavedPanelEvent::Open {
300 conversation_id,
301 message_id,
302 } = event;
303 this.open_message(conversation_id, message_id, cx);
304 }),
305 ];
306 let agent_subscription = crate::agents::runs_handle(cx).map(|runs| {
307 cx.subscribe_in(
308 &runs,
309 window,
310 |this, _, event: &AgentRunsEvent, window, cx| match event {
311 AgentRunsEvent::StartRequested(request) => {
312 this.open_agent_dialog(request.clone(), window, cx)
313 }
314 AgentRunsEvent::OpenSettings => {
315 this.open_settings(SettingsSection::Agents, window, cx)
316 }
317 AgentRunsEvent::Finished(_) => {}
318 },
319 )
320 });
321 let sidebar = cx.new(|cx| SidebarView::new(state.clone(), activity_panel, saved_panel, agents_panel, cx));
322 let sidebar_subscription = cx.subscribe_in(&sidebar, window, |this, _, event: &SidebarEvent, window, cx| {
323 match event {
324 SidebarEvent::RestoreFocus => this.restore_focus(window, cx),
325 SidebarEvent::TabChanged => {
326 if this.sidebar.read(cx).tab() == SidebarTab::Calendar {
327 this.restore_focus(window, cx);
328 }
329 cx.notify();
330 }
331 }
332 });
333 let calendar = cx.new(|cx| CalendarView::new(state.clone(), cx));
334 let rail = cx.new(|cx| RailView::new(state.clone(), activity.clone(), sidebar.clone(), cx));
335 let conversation = cx.new(|cx| ConversationView::new(state.clone(), window, cx));
336 let timeline = conversation.read(cx).timeline_bounds();
337 let call_mini = cx.new(|cx| CallMiniView::new(state.clone(), timeline, 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::Forward) {
344 this.open_forward_dialog(window, cx);
345 }
346 if matches!(event, AppEvent::ImageViewer) {
347 this.open_image_viewer(window, cx);
348 }
349 if matches!(event, AppEvent::StatusMessage) {
350 this.open_status_dialog(window, cx);
351 }
352 if let AppEvent::Settings(section) = event {
353 this.open_settings(*section, window, cx);
354 }
355 if matches!(event, AppEvent::Profile) {
356 this.open_profile_card(window, cx);
357 }
358 cx.notify();
359 });
360 let focus_handle = cx.focus_handle();
361 window.focus(&focus_handle, cx);
362 let notifications =
363 cx.new(|cx| NotificationCenter::new(state.clone(), activity.clone(), window, cx));
364 let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx));
365 let user_input = cx.global::<UserInputHandle>().0.clone();
366 let escape_observation = cx.observe_keystrokes(|this, event, _, cx| {
367 this.user_input.touch();
368 if event.keystroke.key == "escape" && event.action.is_none() {
369 this.state
370 .update(cx, |state, cx| state.escape_call_fullscreen(cx));
371 }
372 });
373 let appearance_observation = cx.observe_window_appearance(window, |_, window, cx| {
374 crate::active_appearance::system_appearance_changed(window.appearance(), cx);
375 });
376 let activation_observation = cx.observe_window_activation(window, |this, window, cx| {
377 if window.is_window_active() {
378 this.state
379 .update(cx, |state, cx| state.refresh_calendar(cx));
380 } else {
381 this.state
382 .update(cx, |state, cx| state.release_push_to_talk(cx));
383 }
384 });
385 let closing = notifications.clone();
386 window.on_window_should_close(cx, move |_, cx| !closing.read(cx).intercept_close(cx));
387 let mut shell = AppShell {
388 state,
389 rail,
390 sidebar,
391 calendar,
392 shown_selection: None,
393 conversation,
394 call_chat: None,
395 prejoin: None,
396 switcher: None,
397 notifications,
398 _call_windows: call_windows,
399 forward_dialog: None,
400 image_viewer: None,
401 status_dialog: None,
402 shortcuts_dialog: None,
403 agent_dialog: None,
404 settings_dialog: None,
405 profile_card: None,
406 focus_handle,
407 call_window_fullscreen: false,
408 window_was_fullscreen: false,
409 open_target,
410 update: UpdateStatus::UpToDate,
411 toasts: NoticeToasts::default(),
412 user_input,
413 _subscription: subscription,
414 call_mini,
415 _call_media_observation: call_media_observation,
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::Close => {}
597 }
598 this.switcher = None;
599 this.restore_focus(window, cx);
600 cx.notify();
601 },
602 )
603 .detach();
604 self.switcher = Some(switcher);
605 cx.notify();
606 }
607
608 fn open_message(
609 &mut self,
610 conversation_id: &str,
611 message_id: &str,
612 cx: &mut Context<Self>,
613 ) -> bool {
614 let Some(selection) = selection_for(&self.state.read(cx).sidebar, conversation_id) else {
615 return false;
616 };
617 let message_id = message_id.to_owned();
618 self.state.update(cx, |state, cx| {
619 if message_id.is_empty() {
620 state.select(selection, cx)
621 } else {
622 state.jump_to_message(selection, message_id, cx)
623 }
624 });
625 true
626 }
627
628 fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
629 if !self
630 .state
631 .update(cx, |state, _| state.take_status_message_request())
632 {
633 return;
634 }
635 self.close_image_viewer(window, cx);
636 self.switcher = None;
637 let app = self.state.clone();
638 let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx));
639 cx.subscribe_in(
640 &dialog,
641 window,
642 |this, _, _: &StatusMessageDialogEvent, window, cx| {
643 this.status_dialog = None;
644 this.restore_focus(window, cx);
645 cx.notify();
646 },
647 )
648 .detach();
649 self.status_dialog = Some(dialog);
650 cx.notify();
651 }
652
653 fn open_agent_dialog(
654 &mut self,
655 request: StartRequest,
656 window: &mut Window,
657 cx: &mut Context<Self>,
658 ) {
659 let Some(runs) = crate::agents::runs_handle(cx) else {
660 return;
661 };
662 let settings = runs.read(cx).settings();
663 let Some(action) = settings
664 .actions
665 .iter()
666 .find(|action| action.id == request.action_id)
667 .cloned()
668 else {
669 return;
670 };
671 let agent_name = settings
672 .agents
673 .iter()
674 .find(|agent| agent.id == action.agent_id)
675 .map(|agent| agent.name.clone())
676 .unwrap_or_default();
677 self.switcher = None;
678 let app = self.state.clone();
679 let dialog = cx.new(|cx| StartDialog::new(app, request, action, &agent_name, window, cx));
680 cx.subscribe_in(
681 &dialog,
682 window,
683 |this, _, _: &StartDialogEvent, window, cx| {
684 this.agent_dialog = None;
685 window.focus(&this.focus_handle, cx);
686 cx.notify();
687 },
688 )
689 .detach();
690 self.agent_dialog = Some(dialog);
691 cx.notify();
692 }
693
694 fn open_settings(
695 &mut self,
696 section: SettingsSection,
697 window: &mut Window,
698 cx: &mut Context<Self>,
699 ) {
700 if let Some(dialog) = &self.settings_dialog {
701 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx));
702 return;
703 }
704 self.close_image_viewer(window, cx);
705 self.switcher = None;
706 let (app, center) = (self.state.clone(), self.notifications.clone());
707 let dialog = cx.new(|cx| SettingsDialog::new(app, center, section, window, cx));
708 cx.subscribe_in(
709 &dialog,
710 window,
711 |this, _, _: &SettingsDialogEvent, window, cx| {
712 this.settings_dialog = None;
713 this.restore_focus(window, cx);
714 cx.notify();
715 },
716 )
717 .detach();
718 self.settings_dialog = Some(dialog);
719 cx.notify();
720 }
721
722 fn toggle_shortcuts_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
723 if self.shortcuts_dialog.take().is_some() {
724 self.restore_focus(window, cx);
725 cx.notify();
726 return;
727 }
728 self.close_image_viewer(window, cx);
729 self.switcher = None;
730 let dialog = cx.new(|cx| ShortcutsDialog::new(window, cx));
731 cx.subscribe_in(
732 &dialog,
733 window,
734 |this, _, _: &ShortcutsDialogEvent, window, cx| {
735 this.shortcuts_dialog = None;
736 this.restore_focus(window, cx);
737 cx.notify();
738 },
739 )
740 .detach();
741 self.shortcuts_dialog = Some(dialog);
742 cx.notify();
743 }
744
745 fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) {
746 let Some(request) = self
747 .state
748 .update(cx, |state, _| state.take_profile_request())
749 else {
750 return;
751 };
752 self.close_image_viewer(window, cx);
753 if let Some(card) = &self.profile_card {
754 card.update(cx, |card, cx| {
755 card.move_to(request.user_id, request.anchor, cx)
756 });
757 return;
758 }
759 let app = self.state.clone();
760 let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx));
761 cx.subscribe_in(
762 &card,
763 window,
764 |this, _, _: &ProfileCardEvent, window, cx| {
765 this.profile_card = None;
766 this.restore_focus(window, cx);
767 cx.notify();
768 },
769 )
770 .detach();
771 self.profile_card = Some(card);
772 cx.notify();
773 }
774
775 fn open_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
776 let Some(request) = self
777 .state
778 .update(cx, |state, _| state.take_image_viewer_request())
779 else {
780 return;
781 };
782 self.switcher = None;
783 let app = self.state.clone();
784 let viewer = cx.new(|cx| ImageViewer::new(app, request, window, cx));
785 cx.subscribe_in(
786 &viewer,
787 window,
788 |this, _, _: &ImageViewerEvent, window, cx| {
789 this.image_viewer = None;
790 window.focus(&this.focus_handle, cx);
791 cx.notify();
792 },
793 )
794 .detach();
795 self.image_viewer = Some(viewer);
796 cx.notify();
797 }
798
799 fn start_call_in_open_chat(&mut self, with_video: bool, cx: &mut Context<Self>) {
800 self.state.update(cx, |state, cx| {
801 if let Some(Selection::Chat(chat_id)) = state.selection.clone() {
802 if with_video {
803 state.start_chat_video_call(&chat_id, cx);
804 } else {
805 state.start_chat_call(&chat_id, cx);
806 }
807 }
808 });
809 }
810
811 fn sync_window_fullscreen(&mut self, wanted: bool, window: &mut Window) {
812 if wanted == self.call_window_fullscreen {
813 return;
814 }
815 self.call_window_fullscreen = wanted;
816 if wanted {
817 self.window_was_fullscreen = window.is_fullscreen();
818 }
819 if wanted != window.is_fullscreen() && (wanted || !self.window_was_fullscreen) {
820 window.toggle_fullscreen();
821 }
822 }
823
824 fn close_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
825 if self.image_viewer.take().is_some() {
826 window.focus(&self.focus_handle, cx);
827 cx.notify();
828 }
829 }
830
831 fn restore_focus(&self, window: &mut Window, cx: &mut Context<Self>) {
832 match &self.image_viewer {
833 Some(viewer) => {
834 let handle = viewer.read(cx).focus_handle();
835 window.focus(&handle, cx)
836 }
837 None => window.focus(&self.focus_handle, cx),
838 }
839 }
840
841 fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
842 let Some(source) = self
843 .state
844 .update(cx, |state, _| state.take_forward_request())
845 else {
846 return;
847 };
848 self.switcher = None;
849 let candidates = candidates_from(self.state.read(cx));
850 let app = self.state.clone();
851 let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx));
852 cx.subscribe_in(
853 &dialog,
854 window,
855 |this, _, event: &ForwardDialogEvent, window, cx| {
856 this.forward_dialog = None;
857 this.restore_focus(window, cx);
858 if let ForwardDialogEvent::Sent { target, title } = event {
859 let target = target.clone();
860 let open = NoticeAction::new("Open", move |state, cx| {
861 state.select(target.clone(), cx)
862 });
863 this.state.update(cx, |state, cx| {
864 state.cancel_forward_selection(cx);
865 state.raise_notice(format!("Forwarded to {title}"), Some(open), cx)
866 });
867 }
868 cx.notify();
869 },
870 )
871 .detach();
872 self.forward_dialog = Some(dialog);
873 cx.notify();
874 }
875}
876
877impl AppShell {
878 fn islands(
879 &self,
880 call_view: Option<AnyElement>,
881 show_sidebar: bool,
882 calendar_open: bool,
883 ) -> impl IntoElement {
884 h_flex()
885 .debug_selector(|| "shell/islands".into())
886 .flex_1()
887 .min_h_0()
888 .w_full()
889 .gap(px(ISLAND_GAP))
890 .pr(px(ISLAND_GAP))
891 .pb(px(ISLAND_GAP))
892 .child(
893 h_flex()
894 .h_full()
895 .flex_none()
896 .child(self.rail.clone())
897 .when(show_sidebar, |group| {
898 group.child(
899 self.sidebar.clone().cached(
900 StyleRefinement::default()
901 .w(px(SIDEBAR_WIDTH))
902 .h_full()
903 .flex_none(),
904 ),
905 )
906 }),
907 )
908 .child(match (call_view, self.prejoin.clone()) {
909 (Some(call_view), _) => call_view,
910 (None, Some(prejoin)) => prejoin
911 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
912 .into_any_element(),
913 (None, 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, 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 viewed_call = self
930 .state
931 .read(cx)
932 .call
933 .as_ref()
934 .filter(|call| call.viewing);
935 let open_dialogs = [
936 self.state.read(cx).task_dialog.is_some(),
937 self.forward_dialog.is_some(),
938 self.status_dialog.is_some(),
939 self.shortcuts_dialog.is_some(),
940 self.agent_dialog.is_some(),
941 self.settings_dialog.is_some(),
942 viewed_call.is_some() && self.state.read(cx).transfer_picker.is_some(),
943 viewed_call.is_some_and(|call| call.options_dialog.is_some()),
944 viewed_call.is_some_and(|call| call.model.consent_required),
945 ]
946 .into_iter()
947 .filter(|open| *open)
948 .count();
949 elevation::set_app_modal_layers(window, open_dialogs, cx);
950 if open_dialogs > 0 {
951 crate::inline_video::stop(cx);
952 }
953 if let Some(dialog) = self
954 .state
955 .read(cx)
956 .task_dialog
957 .as_ref()
958 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
959 {
960 self.state.update(cx, |state, cx| {
961 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
962 });
963 }
964 if let Some(me) = self.state.read(cx).directory.me.clone() {
965 let app = self.state.clone();
966 cx.defer(move |cx| {
967 app.update(cx, |state, cx| {
968 state.request_avatars(vec![me.user_id.clone()], cx);
969 state.request_presence(vec![me.user_id], cx);
970 });
971 });
972 }
973 let chat_thread = self.state.read(cx).call_chat_thread();
974 self.sync_call_chat(chat_thread, window, cx);
975 self.sync_prejoin(window, cx);
976 self.leave_calendar_on_selection(cx);
977 let notice = self.state.read(cx).notice.clone();
978 self.toasts.sync(&self.state, notice.as_ref(), window, cx);
979 let update_failure = match &self.update {
980 UpdateStatus::Failed(message) => Some(message.clone()),
981 _ => None,
982 };
983 self.toasts.update_failed(update_failure.as_deref(), window, cx);
984 let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone().into());
985 let calendar_tab = self.sidebar.read(cx).tab() == SidebarTab::Calendar;
986 let state = self.state.read(cx);
987 let call_fullscreen = state.call_fullscreen();
988 let window_width = f32::from(window.viewport_size().width);
989 let panel_open = state
990 .call
991 .as_ref()
992 .is_some_and(|call| call.chat_open || call.people_open || call.transcript_open);
993 let calendar_open = calendar_tab && !state.viewing_call();
994 let show_sidebar = !calendar_open
995 && !(state.viewing_call()
996 && !call_fullscreen
997 && sidebar_hidden_in_call(window_width, panel_open));
998 let island_width = window_width
999 - RAIL_WIDTH
1000 - if show_sidebar { SIDEBAR_WIDTH } else { 0. }
1001 - 2. * ISLAND_GAP;
1002 let call_view = render_call_view(&self.state, state, chat_panel, call_fullscreen, island_width)
1003 .filter(|_| state.viewing_call())
1004 .or_else(|| render_call_end(&self.state, state));
1005 let (call_view, call_overlay) = match call_view {
1006 Some(view) if call_fullscreen => (None, Some(render_call_overlay(view))),
1007 view => (view, None),
1008 };
1009 self.sync_window_fullscreen(call_fullscreen, window);
1010 let call_mini = state
1011 .call
1012 .as_ref()
1013 .is_some_and(|call| !call.viewing)
1014 .then(|| self.call_mini.clone());
1015 let stage_overlay = render_stage_overlay(&self.state, state);
1016 let banner = state.connection_banner_text().map(|banner| {
1017 render_connection_banner(
1018 banner,
1019 cx.listener(|this, _, _, cx| {
1020 this.state.update(cx, |state, _| state.retry_live())
1021 }),
1022 )
1023 });
1024 let title_bar = render_title_bar(
1025 crate::active_appearance::current(cx).app_icon,
1026 mode_label(state.mode),
1027 [
1028 update_button(
1029 &self.update,
1030 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
1031 ),
1032 own_status_button(&self.state, state),
1033 ]
1034 .into_iter()
1035 .flatten(),
1036 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
1037 );
1038 let shell = div()
1039 .id("app-shell")
1040 .key_context("AppShell")
1041 .track_focus(&self.focus_handle);
1042 self.team_keys(shell, cx)
1043 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
1044 this.toggle_switcher("", window, cx);
1045 }))
1046 .on_action(cx.listener(|this, _: &NewChat, window, cx| {
1047 this.close_image_viewer(window, cx);
1048 this.state.update(cx, |state, cx| state.start_new_chat(cx));
1049 }))
1050 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
1051 this.state
1052 .update(cx, |state, cx| state.toggle_call_mute(cx));
1053 }))
1054 .on_action(cx.listener(|this, _: &PushToTalk, _, cx| {
1055 this.state
1056 .update(cx, |state, cx| state.press_push_to_talk(cx));
1057 }))
1058 .on_key_up(cx.listener(|this, event: &KeyUpEvent, _, cx| {
1059 if event.keystroke.key == "space" {
1060 this.state
1061 .update(cx, |state, cx| state.release_push_to_talk(cx));
1062 }
1063 }))
1064 .on_modifiers_changed(cx.listener(|this, event: &ModifiersChangedEvent, _, cx| {
1065 if !event.modifiers.control {
1066 this.state
1067 .update(cx, |state, cx| state.release_push_to_talk(cx));
1068 }
1069 }))
1070 .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
1071 this.open_settings(SettingsSection::General, window, cx);
1072 }))
1073 .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| {
1074 this.toggle_shortcuts_dialog(window, cx);
1075 }))
1076 .on_action(cx.listener(|this, _: &OpenActivity, window, cx| {
1077 this.close_image_viewer(window, cx);
1078 this.sidebar
1079 .update(cx, |sidebar, cx| sidebar.show_activity(cx));
1080 }))
1081 .on_action(cx.listener(|this, _: &OpenSaved, window, cx| {
1082 this.close_image_viewer(window, cx);
1083 this.sidebar
1084 .update(cx, |sidebar, cx| sidebar.show_saved(cx));
1085 }))
1086 .on_action(cx.listener(|this, _: &OpenCalendar, window, cx| {
1087 this.close_image_viewer(window, cx);
1088 this.sidebar
1089 .update(cx, |sidebar, cx| sidebar.show_calendar(cx));
1090 }))
1091 .on_action(cx.listener(|this, _: &OpenAgents, window, cx| {
1092 this.close_image_viewer(window, cx);
1093 this.sidebar
1094 .update(cx, |sidebar, cx| sidebar.show_agents(cx));
1095 }))
1096 .on_action(cx.listener(|this, _: &OpenChatsTab, window, cx| {
1097 this.close_image_viewer(window, cx);
1098 this.sidebar
1099 .update(cx, |sidebar, cx| sidebar.show_chats(cx));
1100 }))
1101 .on_action(cx.listener(|this, _: &FindInChat, window, cx| {
1102 this.close_image_viewer(window, cx);
1103 this.conversation
1104 .update(cx, |conversation, cx| conversation.open_find(window, cx));
1105 }))
1106 .on_action(cx.listener(|this, _: &PopOutSelectedChat, _, cx| {
1107 let selected = this.state.read(cx).selection.clone();
1108 if let Some(Selection::Chat(chat_id)) = selected {
1109 chat_pop_out::pop_out_chat(&this.state, &chat_id, cx);
1110 }
1111 }))
1112 .on_action(cx.listener(|this, _: &OpenChatFilter, window, cx| {
1113 this.close_image_viewer(window, cx);
1114 this.sidebar
1115 .update(cx, |sidebar, cx| sidebar.toggle_filter_box(window, cx));
1116 }))
1117 .on_action(cx.listener(|this, _: &FilterUnreadChats, _, cx| {
1118 this.sidebar
1119 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Unread, cx));
1120 }))
1121 .on_action(cx.listener(|this, _: &FilterNonMeetingChats, _, cx| {
1122 this.sidebar
1123 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Chats, cx));
1124 }))
1125 .on_action(cx.listener(|this, _: &FilterMeetingChats, _, cx| {
1126 this.sidebar
1127 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Meetings, cx));
1128 }))
1129 .on_action(cx.listener(|this, _: &FilterMutedChats, _, cx| {
1130 this.sidebar
1131 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Muted, cx));
1132 }))
1133 .on_action(cx.listener(|this, _: &ClearChatFilters, window, cx| {
1134 this.sidebar
1135 .update(cx, |sidebar, cx| sidebar.clear_filters(window, cx));
1136 }))
1137 .on_action(cx.listener(|this, _: &OpenChannelsTab, window, cx| {
1138 this.close_image_viewer(window, cx);
1139 this.sidebar
1140 .update(cx, |sidebar, cx| sidebar.show_channels(cx));
1141 }))
1142 .on_action(cx.listener(|this, _: &OpenCallsTab, window, cx| {
1143 this.close_image_viewer(window, cx);
1144 this.sidebar
1145 .update(cx, |sidebar, cx| sidebar.show_calls(cx));
1146 }))
1147 .on_action(cx.listener(|this, _: &PreviousConversation, window, cx| {
1148 this.close_image_viewer(window, cx);
1149 this.sidebar.update(cx, |sidebar, cx| {
1150 sidebar.select_adjacent(Step::Previous, cx)
1151 });
1152 }))
1153 .on_action(cx.listener(|this, _: &NextConversation, window, cx| {
1154 this.close_image_viewer(window, cx);
1155 this.sidebar
1156 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx));
1157 }))
1158 .on_action(cx.listener(|this, _: &DeclineCall, _, cx| {
1159 this.state
1160 .update(cx, |state, cx| state.decline_ringing_call(cx));
1161 }))
1162 .on_action(cx.listener(|this, _: &JoinMeetingFromToast, _, cx| {
1163 this.state
1164 .update(cx, |state, cx| state.join_latest_meeting_toast(cx));
1165 }))
1166 .on_action(cx.listener(|this, _: &HangUpCall, _, cx| {
1167 this.state.update(cx, |state, cx| state.leave_call(cx));
1168 }))
1169 .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| {
1170 this.state
1171 .update(cx, |state, cx| state.toggle_call_camera(cx));
1172 }))
1173 .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| {
1174 this.state
1175 .update(cx, |state, cx| state.toggle_call_share(cx));
1176 }))
1177 .on_action(cx.listener(|this, _: &ToggleCallHand, _, cx| {
1178 this.state
1179 .update(cx, |state, cx| state.toggle_call_hand(cx));
1180 }))
1181 .on_action(cx.listener(|this, _: &OpenLobbyList, _, cx| {
1182 this.state
1183 .update(cx, |state, cx| state.open_call_lobby_list(cx));
1184 }))
1185 .on_action(cx.listener(|this, _: &StartAudioCall, _, cx| {
1186 this.start_call_in_open_chat(false, cx);
1187 }))
1188 .on_action(cx.listener(|this, _: &StartVideoCall, _, cx| {
1189 this.start_call_in_open_chat(true, cx);
1190 }))
1191 .on_action(cx.listener(|this, _: &ToggleCallFullscreen, _, cx| {
1192 if this.image_viewer.is_some() || !this.state.read(cx).viewing_call() {
1193 cx.propagate();
1194 return;
1195 }
1196 this.state
1197 .update(cx, |state, cx| state.toggle_call_fullscreen(cx));
1198 }))
1199 .on_action(cx.listener(|this, _: &ZoomIn, _, cx| change_zoom(&this.state, WindowZoom::In, cx)))
1200 .on_action(cx.listener(|this, _: &ZoomOut, _, cx| change_zoom(&this.state, WindowZoom::Out, cx)))
1201 .on_action(cx.listener(|this, _: &ZoomReset, _, cx| change_zoom(&this.state, WindowZoom::Reset, cx)))
1202 .on_action(zoom_listener::<ZoomShareIn>(&self.state, ZoomStep::In))
1203 .on_action(zoom_listener::<ZoomShareOut>(&self.state, ZoomStep::Out))
1204 .on_action(zoom_listener::<ZoomShareReset>(&self.state, ZoomStep::Reset))
1205 .on_action(cx.listener(|this, _: &JumpToLastReadOrNewest, _, cx| {
1206 if this.image_viewer.is_some() || this.state.read(cx).viewing_call() {
1207 cx.propagate();
1208 return;
1209 }
1210 this.conversation
1211 .update(cx, |conversation, cx| conversation.jump_to_last_read_or_newest(cx));
1212 }))
1213 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
1214 this.close_image_viewer(window, cx);
1215 this.conversation.update(cx, |conversation, cx| {
1216 conversation.reply_to_hovered(window, cx)
1217 });
1218 }))
1219 .relative()
1220 .size_full()
1221 .bg(theme::background())
1222 .text_color(theme::text())
1223 .text_size(px(14.))
1224 .child(crate::frame_log::probe("first"))
1225 .child(user_activity::input_probe(self.user_input.clone()))
1226 .child(app_ui_kit::long_press::long_press_right_click())
1227 .child(
1228 v_flex()
1229 .debug_selector(|| "shell".into())
1230 .size_full()
1231 .child(title_bar)
1232 .children(banner)
1233 .child(self.islands(call_view, show_sidebar, calendar_open)),
1234 )
1235 .children(call_mini)
1236 .children(call_overlay)
1237 .children(stage_overlay)
1238 .children(self.image_viewer.clone())
1239 .children(self.switcher.clone())
1240 .children(render_task_dialog(self.state.read(cx), cx))
1241 .children(self.forward_dialog.clone())
1242 .children(self.status_dialog.clone())
1243 .children(self.shortcuts_dialog.clone())
1244 .children(self.agent_dialog.clone())
1245 .children(self.settings_dialog.clone())
1246 .children(self.profile_card.clone())
1247 .child(crate::frame_log::probe("last"))
1248 }
1249}