BlueGit

ict rusty-teams

Files

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

shell.rs

shell.rs 48.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::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::PickPerson { user_id } => {
597 let user_id = user_id.clone();
598 this.state
599 .update(cx, |state, cx| state.open_chat_with(&user_id, cx));
600 }
601 SwitcherEvent::Close => {}
602 }
603 this.switcher = None;
604 this.restore_focus(window, cx);
605 cx.notify();
606 },
607 )
608 .detach();
609 self.switcher = Some(switcher);
610 cx.notify();
611 }
612
613 fn open_message(
614 &mut self,
615 conversation_id: &str,
616 message_id: &str,
617 cx: &mut Context<Self>,
618 ) -> bool {
619 let Some(selection) = selection_for(&self.state.read(cx).sidebar, conversation_id) else {
620 return false;
621 };
622 let message_id = message_id.to_owned();
623 self.state.update(cx, |state, cx| {
624 if message_id.is_empty() {
625 state.select(selection, cx)
626 } else {
627 state.jump_to_message(selection, message_id, cx)
628 }
629 });
630 true
631 }
632
633 fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
634 if !self
635 .state
636 .update(cx, |state, _| state.take_status_message_request())
637 {
638 return;
639 }
640 self.close_image_viewer(window, cx);
641 self.switcher = None;
642 let app = self.state.clone();
643 let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx));
644 cx.subscribe_in(
645 &dialog,
646 window,
647 |this, _, _: &StatusMessageDialogEvent, window, cx| {
648 this.status_dialog = None;
649 this.restore_focus(window, cx);
650 cx.notify();
651 },
652 )
653 .detach();
654 self.status_dialog = Some(dialog);
655 cx.notify();
656 }
657
658 fn open_agent_dialog(
659 &mut self,
660 request: StartRequest,
661 window: &mut Window,
662 cx: &mut Context<Self>,
663 ) {
664 let Some(runs) = crate::agents::runs_handle(cx) else {
665 return;
666 };
667 let settings = runs.read(cx).settings();
668 let Some(action) = settings
669 .actions
670 .iter()
671 .find(|action| action.id == request.action_id)
672 .cloned()
673 else {
674 return;
675 };
676 let agent_name = settings
677 .agents
678 .iter()
679 .find(|agent| agent.id == action.agent_id)
680 .map(|agent| agent.name.clone())
681 .unwrap_or_default();
682 self.switcher = None;
683 let app = self.state.clone();
684 let dialog = cx.new(|cx| StartDialog::new(app, request, action, &agent_name, window, cx));
685 cx.subscribe_in(
686 &dialog,
687 window,
688 |this, _, _: &StartDialogEvent, window, cx| {
689 this.agent_dialog = None;
690 window.focus(&this.focus_handle, cx);
691 cx.notify();
692 },
693 )
694 .detach();
695 self.agent_dialog = Some(dialog);
696 cx.notify();
697 }
698
699 fn open_settings(
700 &mut self,
701 section: SettingsSection,
702 window: &mut Window,
703 cx: &mut Context<Self>,
704 ) {
705 if let Some(dialog) = &self.settings_dialog {
706 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx));
707 return;
708 }
709 self.close_image_viewer(window, cx);
710 self.switcher = None;
711 let (app, center) = (self.state.clone(), self.notifications.clone());
712 let dialog = cx.new(|cx| SettingsDialog::new(app, center, section, window, cx));
713 cx.subscribe_in(
714 &dialog,
715 window,
716 |this, _, _: &SettingsDialogEvent, window, cx| {
717 this.settings_dialog = None;
718 this.restore_focus(window, cx);
719 cx.notify();
720 },
721 )
722 .detach();
723 self.settings_dialog = Some(dialog);
724 cx.notify();
725 }
726
727 fn toggle_shortcuts_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
728 if self.shortcuts_dialog.take().is_some() {
729 self.restore_focus(window, cx);
730 cx.notify();
731 return;
732 }
733 self.close_image_viewer(window, cx);
734 self.switcher = None;
735 let dialog = cx.new(|cx| ShortcutsDialog::new(window, cx));
736 cx.subscribe_in(
737 &dialog,
738 window,
739 |this, _, _: &ShortcutsDialogEvent, window, cx| {
740 this.shortcuts_dialog = None;
741 this.restore_focus(window, cx);
742 cx.notify();
743 },
744 )
745 .detach();
746 self.shortcuts_dialog = Some(dialog);
747 cx.notify();
748 }
749
750 fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) {
751 let Some(request) = self
752 .state
753 .update(cx, |state, _| state.take_profile_request())
754 else {
755 return;
756 };
757 self.close_image_viewer(window, cx);
758 if let Some(card) = &self.profile_card {
759 card.update(cx, |card, cx| {
760 card.move_to(request.user_id, request.anchor, cx)
761 });
762 return;
763 }
764 let app = self.state.clone();
765 let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx));
766 cx.subscribe_in(
767 &card,
768 window,
769 |this, _, _: &ProfileCardEvent, window, cx| {
770 this.profile_card = None;
771 this.restore_focus(window, cx);
772 cx.notify();
773 },
774 )
775 .detach();
776 self.profile_card = Some(card);
777 cx.notify();
778 }
779
780 fn open_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
781 let Some(request) = self
782 .state
783 .update(cx, |state, _| state.take_image_viewer_request())
784 else {
785 return;
786 };
787 self.switcher = None;
788 let app = self.state.clone();
789 let viewer = cx.new(|cx| ImageViewer::new(app, request, window, cx));
790 cx.subscribe_in(
791 &viewer,
792 window,
793 |this, _, _: &ImageViewerEvent, window, cx| {
794 this.image_viewer = None;
795 window.focus(&this.focus_handle, cx);
796 cx.notify();
797 },
798 )
799 .detach();
800 self.image_viewer = Some(viewer);
801 cx.notify();
802 }
803
804 fn start_call_in_open_chat(&mut self, with_video: bool, cx: &mut Context<Self>) {
805 self.state.update(cx, |state, cx| {
806 if let Some(Selection::Chat(chat_id)) = state.selection.clone() {
807 if with_video {
808 state.start_chat_video_call(&chat_id, cx);
809 } else {
810 state.start_chat_call(&chat_id, cx);
811 }
812 }
813 });
814 }
815
816 fn sync_window_fullscreen(&mut self, wanted: bool, window: &mut Window) {
817 if wanted == self.call_window_fullscreen {
818 return;
819 }
820 self.call_window_fullscreen = wanted;
821 if wanted {
822 self.window_was_fullscreen = window.is_fullscreen();
823 }
824 if wanted != window.is_fullscreen() && (wanted || !self.window_was_fullscreen) {
825 window.toggle_fullscreen();
826 }
827 }
828
829 fn close_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
830 if self.image_viewer.take().is_some() {
831 window.focus(&self.focus_handle, cx);
832 cx.notify();
833 }
834 }
835
836 fn restore_focus(&self, window: &mut Window, cx: &mut Context<Self>) {
837 match &self.image_viewer {
838 Some(viewer) => {
839 let handle = viewer.read(cx).focus_handle();
840 window.focus(&handle, cx)
841 }
842 None => window.focus(&self.focus_handle, cx),
843 }
844 }
845
846 fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
847 let Some(source) = self
848 .state
849 .update(cx, |state, _| state.take_forward_request())
850 else {
851 return;
852 };
853 self.switcher = None;
854 let candidates = candidates_from(self.state.read(cx));
855 let app = self.state.clone();
856 let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx));
857 cx.subscribe_in(
858 &dialog,
859 window,
860 |this, _, event: &ForwardDialogEvent, window, cx| {
861 this.forward_dialog = None;
862 this.restore_focus(window, cx);
863 if let ForwardDialogEvent::Sent { target, title } = event {
864 let target = target.clone();
865 let open = NoticeAction::new("Open", move |state, cx| {
866 state.select(target.clone(), cx)
867 });
868 this.state.update(cx, |state, cx| {
869 state.cancel_forward_selection(cx);
870 state.raise_notice(format!("Forwarded to {title}"), Some(open), cx)
871 });
872 }
873 cx.notify();
874 },
875 )
876 .detach();
877 self.forward_dialog = Some(dialog);
878 cx.notify();
879 }
880}
881
882impl AppShell {
883 fn islands(
884 &self,
885 call_view: Option<AnyElement>,
886 show_sidebar: bool,
887 calendar_open: bool,
888 ) -> impl IntoElement {
889 h_flex()
890 .debug_selector(|| "shell/islands".into())
891 .flex_1()
892 .min_h_0()
893 .w_full()
894 .when(show_sidebar, |islands| islands.gap(px(ISLAND_GAP)))
895 .pr(px(ISLAND_GAP))
896 .pb(px(ISLAND_GAP))
897 .child(
898 h_flex()
899 .h_full()
900 .flex_none()
901 .child(self.rail.clone())
902 .when(show_sidebar, |group| {
903 group.child(
904 self.sidebar.clone().cached(
905 StyleRefinement::default()
906 .w(px(SIDEBAR_WIDTH))
907 .h_full()
908 .flex_none(),
909 ),
910 )
911 }),
912 )
913 .child(match (call_view, self.prejoin.clone()) {
914 (Some(call_view), _) => call_view,
915 (None, Some(prejoin)) => prejoin
916 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
917 .into_any_element(),
918 (None, None) if calendar_open => self
919 .calendar
920 .clone()
921 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
922 .into_any_element(),
923 (None, None) => self
924 .conversation
925 .clone()
926 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
927 .into_any_element(),
928 })
929 }
930}
931
932impl Render for AppShell {
933 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
934 let viewed_call = self
935 .state
936 .read(cx)
937 .call
938 .as_ref()
939 .filter(|call| call.viewing);
940 let open_dialogs = [
941 self.state.read(cx).task_dialog.is_some(),
942 self.forward_dialog.is_some(),
943 self.status_dialog.is_some(),
944 self.shortcuts_dialog.is_some(),
945 self.agent_dialog.is_some(),
946 self.settings_dialog.is_some(),
947 viewed_call.is_some() && self.state.read(cx).transfer_picker.is_some(),
948 viewed_call.is_some_and(|call| call.options_dialog.is_some()),
949 viewed_call.is_some_and(|call| call.model.consent_required),
950 ]
951 .into_iter()
952 .filter(|open| *open)
953 .count();
954 elevation::set_app_modal_layers(window, open_dialogs, cx);
955 if open_dialogs > 0 {
956 crate::inline_video::stop(cx);
957 }
958 if let Some(dialog) = self
959 .state
960 .read(cx)
961 .task_dialog
962 .as_ref()
963 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
964 {
965 self.state.update(cx, |state, cx| {
966 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
967 });
968 }
969 if let Some(me) = self.state.read(cx).directory.me.clone() {
970 let app = self.state.clone();
971 cx.defer(move |cx| {
972 app.update(cx, |state, cx| {
973 state.request_avatars(vec![me.user_id.clone()], cx);
974 state.request_presence(vec![me.user_id], cx);
975 });
976 });
977 }
978 let chat_thread = self.state.read(cx).call_chat_thread();
979 self.sync_call_chat(chat_thread, window, cx);
980 self.sync_prejoin(window, cx);
981 self.leave_calendar_on_selection(cx);
982 let notice = self.state.read(cx).notice.clone();
983 self.toasts.sync(&self.state, notice.as_ref(), window, cx);
984 let update_failure = match &self.update {
985 UpdateStatus::Failed(message) => Some(message.clone()),
986 _ => None,
987 };
988 self.toasts.update_failed(update_failure.as_deref(), window, cx);
989 let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone().into());
990 let calendar_tab = self.sidebar.read(cx).tab() == SidebarTab::Calendar;
991 let state = self.state.read(cx);
992 let call_fullscreen = state.call_fullscreen();
993 let window_width = f32::from(window.viewport_size().width);
994 let panel_open = state
995 .call
996 .as_ref()
997 .is_some_and(|call| call.chat_open || call.people_open || call.transcript_open);
998 let calendar_open = calendar_tab && !state.viewing_call();
999 let show_sidebar = !calendar_open
1000 && !(state.viewing_call()
1001 && !call_fullscreen
1002 && sidebar_hidden_in_call(window_width, panel_open));
1003 let island_width = window_width
1004 - RAIL_WIDTH
1005 - if show_sidebar { SIDEBAR_WIDTH + ISLAND_GAP } else { 0. }
1006 - ISLAND_GAP;
1007 let call_view = render_call_view(&self.state, state, chat_panel, call_fullscreen, island_width)
1008 .filter(|_| state.viewing_call())
1009 .or_else(|| render_call_end(&self.state, state));
1010 let (call_view, call_overlay) = match call_view {
1011 Some(view) if call_fullscreen => (None, Some(render_call_overlay(view))),
1012 view => (view, None),
1013 };
1014 self.sync_window_fullscreen(call_fullscreen, window);
1015 let call_mini = state
1016 .call
1017 .as_ref()
1018 .is_some_and(|call| !call.viewing)
1019 .then(|| self.call_mini.clone());
1020 let stage_overlay = render_stage_overlay(&self.state, state);
1021 let banner = state.connection_banner_text().map(|banner| {
1022 render_connection_banner(
1023 banner,
1024 cx.listener(|this, _, _, cx| {
1025 this.state.update(cx, |state, _| state.retry_live())
1026 }),
1027 )
1028 });
1029 let title_bar = render_title_bar(
1030 crate::active_appearance::current(cx).app_icon,
1031 mode_label(state.mode),
1032 [
1033 update_button(
1034 &self.update,
1035 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
1036 ),
1037 own_status_button(&self.state, state),
1038 ]
1039 .into_iter()
1040 .flatten(),
1041 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
1042 );
1043 let shell = div()
1044 .id("app-shell")
1045 .key_context("AppShell")
1046 .track_focus(&self.focus_handle);
1047 self.team_keys(shell, cx)
1048 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
1049 this.toggle_switcher("", window, cx);
1050 }))
1051 .on_action(cx.listener(|this, _: &NewChat, window, cx| {
1052 this.close_image_viewer(window, cx);
1053 this.state.update(cx, |state, cx| state.start_new_chat(cx));
1054 }))
1055 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
1056 this.state
1057 .update(cx, |state, cx| state.toggle_call_mute(cx));
1058 }))
1059 .on_action(cx.listener(|this, _: &PushToTalk, _, cx| {
1060 this.state
1061 .update(cx, |state, cx| state.press_push_to_talk(cx));
1062 }))
1063 .on_key_up(cx.listener(|this, event: &KeyUpEvent, _, cx| {
1064 if event.keystroke.key == "space" {
1065 this.state
1066 .update(cx, |state, cx| state.release_push_to_talk(cx));
1067 }
1068 }))
1069 .on_modifiers_changed(cx.listener(|this, event: &ModifiersChangedEvent, _, cx| {
1070 if !event.modifiers.control {
1071 this.state
1072 .update(cx, |state, cx| state.release_push_to_talk(cx));
1073 }
1074 }))
1075 .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
1076 this.open_settings(SettingsSection::General, window, cx);
1077 }))
1078 .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| {
1079 this.toggle_shortcuts_dialog(window, cx);
1080 }))
1081 .on_action(cx.listener(|this, _: &OpenActivity, window, cx| {
1082 this.close_image_viewer(window, cx);
1083 this.sidebar
1084 .update(cx, |sidebar, cx| sidebar.show_activity(cx));
1085 }))
1086 .on_action(cx.listener(|this, _: &OpenSaved, window, cx| {
1087 this.close_image_viewer(window, cx);
1088 this.sidebar
1089 .update(cx, |sidebar, cx| sidebar.show_saved(cx));
1090 }))
1091 .on_action(cx.listener(|this, _: &OpenCalendar, window, cx| {
1092 this.close_image_viewer(window, cx);
1093 this.sidebar
1094 .update(cx, |sidebar, cx| sidebar.show_calendar(cx));
1095 }))
1096 .on_action(cx.listener(|this, _: &OpenAgents, window, cx| {
1097 this.close_image_viewer(window, cx);
1098 this.sidebar
1099 .update(cx, |sidebar, cx| sidebar.show_agents(cx));
1100 }))
1101 .on_action(cx.listener(|this, _: &OpenChatsTab, window, cx| {
1102 this.close_image_viewer(window, cx);
1103 this.sidebar
1104 .update(cx, |sidebar, cx| sidebar.show_chats(cx));
1105 }))
1106 .on_action(cx.listener(|this, _: &FindInChat, window, cx| {
1107 this.close_image_viewer(window, cx);
1108 this.conversation
1109 .update(cx, |conversation, cx| conversation.open_find(window, cx));
1110 }))
1111 .on_action(cx.listener(|this, _: &PopOutSelectedChat, _, cx| {
1112 let selected = this.state.read(cx).selection.clone();
1113 if let Some(Selection::Chat(chat_id)) = selected {
1114 chat_pop_out::pop_out_chat(&this.state, &chat_id, cx);
1115 }
1116 }))
1117 .on_action(cx.listener(|this, _: &OpenChatFilter, window, cx| {
1118 this.close_image_viewer(window, cx);
1119 this.sidebar
1120 .update(cx, |sidebar, cx| sidebar.toggle_filter_box(window, cx));
1121 }))
1122 .on_action(cx.listener(|this, _: &FilterUnreadChats, _, cx| {
1123 this.sidebar
1124 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Unread, cx));
1125 }))
1126 .on_action(cx.listener(|this, _: &FilterNonMeetingChats, _, cx| {
1127 this.sidebar
1128 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Chats, cx));
1129 }))
1130 .on_action(cx.listener(|this, _: &FilterMeetingChats, _, cx| {
1131 this.sidebar
1132 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Meetings, cx));
1133 }))
1134 .on_action(cx.listener(|this, _: &FilterMutedChats, _, cx| {
1135 this.sidebar
1136 .update(cx, |sidebar, cx| sidebar.toggle_pill(ChatFilterPill::Muted, cx));
1137 }))
1138 .on_action(cx.listener(|this, _: &ClearChatFilters, window, cx| {
1139 this.sidebar
1140 .update(cx, |sidebar, cx| sidebar.clear_filters(window, cx));
1141 }))
1142 .on_action(cx.listener(|this, _: &OpenChannelsTab, window, cx| {
1143 this.close_image_viewer(window, cx);
1144 this.sidebar
1145 .update(cx, |sidebar, cx| sidebar.show_channels(cx));
1146 }))
1147 .on_action(cx.listener(|this, _: &OpenCallsTab, window, cx| {
1148 this.close_image_viewer(window, cx);
1149 this.sidebar
1150 .update(cx, |sidebar, cx| sidebar.show_calls(cx));
1151 }))
1152 .on_action(cx.listener(|this, _: &PreviousConversation, window, cx| {
1153 this.close_image_viewer(window, cx);
1154 this.sidebar.update(cx, |sidebar, cx| {
1155 sidebar.select_adjacent(Step::Previous, cx)
1156 });
1157 }))
1158 .on_action(cx.listener(|this, _: &NextConversation, window, cx| {
1159 this.close_image_viewer(window, cx);
1160 this.sidebar
1161 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx));
1162 }))
1163 .on_action(cx.listener(|this, _: &DeclineCall, _, cx| {
1164 this.state
1165 .update(cx, |state, cx| state.decline_ringing_call(cx));
1166 }))
1167 .on_action(cx.listener(|this, _: &JoinMeetingFromToast, _, cx| {
1168 this.state
1169 .update(cx, |state, cx| state.join_latest_meeting_toast(cx));
1170 }))
1171 .on_action(cx.listener(|this, _: &HangUpCall, _, cx| {
1172 this.state.update(cx, |state, cx| state.leave_call(cx));
1173 }))
1174 .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| {
1175 this.state
1176 .update(cx, |state, cx| state.toggle_call_camera(cx));
1177 }))
1178 .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| {
1179 this.state
1180 .update(cx, |state, cx| state.toggle_call_share(cx));
1181 }))
1182 .on_action(cx.listener(|this, _: &ToggleCallHand, _, cx| {
1183 this.state
1184 .update(cx, |state, cx| state.toggle_call_hand(cx));
1185 }))
1186 .on_action(cx.listener(|this, _: &OpenLobbyList, _, cx| {
1187 this.state
1188 .update(cx, |state, cx| state.open_call_lobby_list(cx));
1189 }))
1190 .on_action(cx.listener(|this, _: &StartAudioCall, _, cx| {
1191 this.start_call_in_open_chat(false, cx);
1192 }))
1193 .on_action(cx.listener(|this, _: &StartVideoCall, _, cx| {
1194 this.start_call_in_open_chat(true, cx);
1195 }))
1196 .on_action(cx.listener(|this, _: &ToggleCallFullscreen, _, cx| {
1197 if this.image_viewer.is_some() || !this.state.read(cx).viewing_call() {
1198 cx.propagate();
1199 return;
1200 }
1201 this.state
1202 .update(cx, |state, cx| state.toggle_call_fullscreen(cx));
1203 }))
1204 .on_action(cx.listener(|this, _: &ZoomIn, _, cx| change_zoom(&this.state, WindowZoom::In, cx)))
1205 .on_action(cx.listener(|this, _: &ZoomOut, _, cx| change_zoom(&this.state, WindowZoom::Out, cx)))
1206 .on_action(cx.listener(|this, _: &ZoomReset, _, cx| change_zoom(&this.state, WindowZoom::Reset, cx)))
1207 .on_action(zoom_listener::<ZoomShareIn>(&self.state, ZoomStep::In))
1208 .on_action(zoom_listener::<ZoomShareOut>(&self.state, ZoomStep::Out))
1209 .on_action(zoom_listener::<ZoomShareReset>(&self.state, ZoomStep::Reset))
1210 .on_action(cx.listener(|this, _: &JumpToLastReadOrNewest, _, cx| {
1211 if this.image_viewer.is_some() || this.state.read(cx).viewing_call() {
1212 cx.propagate();
1213 return;
1214 }
1215 this.conversation
1216 .update(cx, |conversation, cx| conversation.jump_to_last_read_or_newest(cx));
1217 }))
1218 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
1219 this.close_image_viewer(window, cx);
1220 this.conversation.update(cx, |conversation, cx| {
1221 conversation.reply_to_hovered(window, cx)
1222 });
1223 }))
1224 .relative()
1225 .size_full()
1226 .bg(theme::background())
1227 .text_color(theme::text())
1228 .text_size(px(14.))
1229 .child(crate::frame_log::probe("first"))
1230 .child(user_activity::input_probe(self.user_input.clone()))
1231 .child(app_ui_kit::long_press::long_press_right_click())
1232 .child(
1233 v_flex()
1234 .debug_selector(|| "shell".into())
1235 .size_full()
1236 .child(title_bar)
1237 .children(banner)
1238 .child(self.islands(call_view, show_sidebar, calendar_open)),
1239 )
1240 .children(call_mini)
1241 .children(call_overlay)
1242 .children(stage_overlay)
1243 .children(self.image_viewer.clone())
1244 .children(self.switcher.clone())
1245 .children(render_task_dialog(self.state.read(cx), cx))
1246 .children(self.forward_dialog.clone())
1247 .children(self.status_dialog.clone())
1248 .children(self.shortcuts_dialog.clone())
1249 .children(self.agent_dialog.clone())
1250 .children(self.settings_dialog.clone())
1251 .children(self.profile_card.clone())
1252 .child(crate::frame_log::probe("last"))
1253 }
1254}