BlueGit

ict rusty-teams

Files

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

shell.rs

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