BlueGit

ict rusty-teams

Files

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

shell.rs

shell.rs 40.4 KiB Download Raw
1use std::sync::Arc;
2
3use gpui_kit::component::{elevation, h_flex, v_flex};
4use gpui_kit::*;
5
6use super::activity_panel::{ActivityPanel, ActivityPanelEvent};
7use super::call_mini::CallMiniView;
8use super::call_stage::{render_call_overlay, render_stage_overlay};
9use super::call_end_view::render_call_end;
10use super::call_view::{CHAT_PANEL_WIDTH, OVERLAY_BELOW as CALL_COLUMN_MIN, render_call_view};
11use super::actions::{NewChat, OpenSwitcher, ReplyToHovered, ToggleCallMute};
12use super::conversation::ConversationView;
13use super::dialog_overlay::render_task_dialog;
14use super::forward_dialog::{ForwardDialog, ForwardDialogEvent};
15use super::image_viewer::{ImageViewer, ImageViewerEvent};
16use super::prejoin_view::PrejoinView;
17use super::profile_card::{ProfileCard, ProfileCardEvent};
18use gpui_kit::prelude::FluentBuilder as _;
19use super::rail::RailView;
20use super::saved_panel::{SavedPanel, SavedPanelEvent};
21use super::shortcuts::{
22 AcceptCall, DeclineCall, HangUpCall, JoinMeetingFromToast, NextConversation, OpenActivity,
23 OpenAgents, OpenCallsTab, OpenSettings, OpenChannelsTab, OpenChatsTab, OpenLobbyList, OpenSaved, PreviousConversation, PushToTalk,
24 Scope, ShowShortcuts, StartAudioCall, StartVideoCall, ToggleCallCamera, ToggleCallFullscreen,
25 ToggleCallHand, ToggleCallShare, ZoomShareIn, ZoomShareOut, ZoomShareReset,
26};
27use super::call_pop_out::zoom_listener;
28use super::shortcuts_dialog::{ShortcutsDialog, ShortcutsDialogEvent};
29use super::sidebar::SidebarView;
30use app_ui_kit::render::hooks::RenderHooks;
31use app_ui_kit::layout::{ISLAND_GAP, RAIL_WIDTH, SIDEBAR_WIDTH};
32use super::connection_banner::render_connection_banner;
33use super::notice_toast::NoticeToasts;
34use super::status_menu::own_status_button;
35use super::status_message_dialog::{StatusMessageDialog, StatusMessageDialogEvent};
36use super::switcher::{Switcher, SwitcherEvent, candidates_from};
37use super::title_bar::{mode_label, render_title_bar, update_button};
38use crate::activity::ActivityCenter;
39use crate::agents::{AgentRunsEvent, StartDialog, StartDialogEvent, StartRequest};
40use crate::app_state::{AppEvent, AppState, Selection, selection_for};
41use crate::call::{CallMedia, CallWindows, ZoomStep};
42use crate::notice::NoticeAction;
43use crate::notify::NotificationCenter;
44use crate::settings::{SettingsDialog, SettingsDialogEvent};
45use crate::settings_section::SettingsSection;
46use crate::sidebar_model::Step;
47use crate::theme;
48use crate::updater::{self, IdleInputs, UpdateStatus};
49use crate::user_activity::{self, UserInput};
50
51pub fn bind_keys(cx: &mut App) {
52 super::composer::bind_keys(cx);
53 super::shortcuts::bind_scopes(
54 cx,
55 &[
56 Scope::Global,
57 Scope::OutsideComposer,
58 Scope::GlobalOverTextFields,
59 Scope::OutsideTextFields,
60 ],
61 );
62}
63
64#[derive(Debug, Clone, Copy)]
65pub enum OpenTarget {
66 Chat(usize),
67 Channel(usize),
68}
69
70#[derive(Default)]
71pub struct Startup {
72 pub switcher_query: Option<String>,
73 pub composer_text: Option<String>,
74 pub reply_to_last: bool,
75}
76
77pub const SIDEBAR_HIDDEN_BELOW: f32 = 960.;
78
79pub fn sidebar_hidden_in_call(window_width: f32, panel_open: bool) -> bool {
80 let panel_width = if panel_open { CHAT_PANEL_WIDTH + ISLAND_GAP } else { 0. };
81 window_width < SIDEBAR_HIDDEN_BELOW
82 || window_width - RAIL_WIDTH - SIDEBAR_WIDTH - 2. * ISLAND_GAP - panel_width < CALL_COLUMN_MIN
83}
84
85pub struct UserInputHandle(pub Arc<UserInput>);
86
87impl Global for UserInputHandle {}
88
89pub fn install_globals(state: &Entity<AppState>, user_input: Arc<UserInput>, cx: &mut App) {
90 cx.set_global(crate::app_state::AppHandle(state.clone()));
91 cx.set_global(UserInputHandle(user_input));
92 cx.set_global(RenderHooks {
93 open_profile_link: super::profile_card::open_profile_link,
94 jump_to_quoted,
95 });
96}
97
98fn jump_to_quoted(message_id: String, cx: &mut App) {
99 let Some(handle) = cx.try_global::<crate::app_state::AppHandle>() else {
100 return;
101 };
102 handle.0.clone().update(cx, |state, cx| {
103 if let Some(selection) = state.selection.clone() {
104 state.jump_to_message(selection, message_id, cx);
105 }
106 });
107}
108
109const DEMO_UPDATE_VARIABLE: &str = "TEAMS_DEMO_UPDATE";
110
111pub struct AppShell {
112 state: Entity<AppState>,
113 rail: Entity<RailView>,
114 sidebar: Entity<SidebarView>,
115 conversation: Entity<ConversationView>,
116 call_chat: Option<(String, Entity<ConversationView>)>,
117 prejoin: Option<Entity<PrejoinView>>,
118 switcher: Option<Entity<Switcher>>,
119 notifications: Entity<NotificationCenter>,
120 _call_windows: Entity<CallWindows>,
121 forward_dialog: Option<Entity<ForwardDialog>>,
122 image_viewer: Option<Entity<ImageViewer>>,
123 status_dialog: Option<Entity<StatusMessageDialog>>,
124 shortcuts_dialog: Option<Entity<ShortcutsDialog>>,
125 agent_dialog: Option<Entity<StartDialog>>,
126 settings_dialog: Option<Entity<SettingsDialog>>,
127 profile_card: Option<Entity<ProfileCard>>,
128 focus_handle: FocusHandle,
129 call_window_fullscreen: bool,
130 window_was_fullscreen: bool,
131 open_target: Option<OpenTarget>,
132 update: UpdateStatus,
133 toasts: NoticeToasts,
134 user_input: Arc<UserInput>,
135 _subscription: Subscription,
136 call_mini: Entity<CallMiniView>,
137 _call_media_observation: Subscription,
138 _panel_subscriptions: [Subscription; 2],
139 _agents_subscription: Option<Subscription>,
140 _agent_subscription: Option<Subscription>,
141 _escape_observation: Subscription,
142 _activation_observation: Subscription,
143}
144
145impl AppShell {
146 fn sync_call_chat(
147 &mut self,
148 thread: Option<String>,
149 window: &mut Window,
150 cx: &mut Context<Self>,
151 ) {
152 match (thread, &self.call_chat) {
153 (Some(wanted), Some((open, _))) if &wanted == open => {}
154 (Some(wanted), _) => {
155 let app = self.state.clone();
156 let selection = Selection::Chat(wanted.clone());
157 let view = cx.new(|cx| ConversationView::pinned_to(app, selection, window, cx));
158 self.call_chat = Some((wanted, view));
159 }
160 (None, _) => self.call_chat = None,
161 }
162 }
163
164 fn sync_prejoin(&mut self, window: &mut Window, cx: &mut Context<Self>) {
165 let wanted = self.state.read(cx).prejoin.clone();
166 let open = self.prejoin.as_ref().map(|view| view.read(cx).pending_id());
167 match (wanted, open) {
168 (Some(pending), Some(id)) if pending.id == id => {}
169 (Some(pending), _) => {
170 let app = self.state.clone();
171 self.prejoin = Some(cx.new(|cx| PrejoinView::new(app, &pending, window, cx)));
172 }
173 (None, _) => self.prejoin = None,
174 }
175 }
176
177 pub fn new(
178 state: Entity<AppState>,
179 open_target: Option<OpenTarget>,
180 startup: Startup,
181 window: &mut Window,
182 cx: &mut Context<Self>,
183 ) -> Self {
184 let call_media = cx.new(|_| CallMedia);
185 state.update(cx, |state, _| state.call_media = Some(call_media.clone()));
186 let call_media_observation = cx.observe(&call_media, |this, _, cx| {
187 if this.state.read(cx).viewing_call() {
188 cx.notify();
189 }
190 });
191 let activity = cx.new(|cx| ActivityCenter::new(state.clone(), window, cx));
192 let activity_panel = cx.new(|cx| ActivityPanel::new(state.clone(), activity.clone(), cx));
193 let saved_panel = cx.new(|cx| SavedPanel::new(state.clone(), cx));
194 let agents_panel = crate::agents::runs_handle(cx)
195 .map(|runs| cx.new(|cx| crate::agents::AgentsPanel::new(runs, cx)));
196 let agents_subscription = agents_panel.as_ref().map(|panel| {
197 cx.subscribe(
198 panel,
199 |this, _, event: &crate::agents::AgentsPanelEvent, cx| {
200 let crate::agents::AgentsPanelEvent::Open {
201 conversation_id,
202 message_id,
203 } = event;
204 this.open_message(conversation_id, message_id, cx);
205 },
206 )
207 });
208 let panel_subscriptions = [
209 cx.subscribe(
210 &activity_panel,
211 |this, _, event: &ActivityPanelEvent, cx| match event {
212 ActivityPanelEvent::OpenSettings => this.state.update(cx, |state, cx| {
213 state.open_settings(SettingsSection::Notifications, cx)
214 }),
215 ActivityPanelEvent::Open {
216 conversation_id,
217 message_id,
218 } => this.open_message(conversation_id, message_id, cx),
219 },
220 ),
221 cx.subscribe(&saved_panel, |this, _, event: &SavedPanelEvent, cx| {
222 let SavedPanelEvent::Open {
223 conversation_id,
224 message_id,
225 } = event;
226 this.open_message(conversation_id, message_id, cx);
227 }),
228 ];
229 let agent_subscription = crate::agents::runs_handle(cx).map(|runs| {
230 cx.subscribe_in(
231 &runs,
232 window,
233 |this, _, event: &AgentRunsEvent, window, cx| match event {
234 AgentRunsEvent::StartRequested(request) => {
235 this.open_agent_dialog(request.clone(), window, cx)
236 }
237 AgentRunsEvent::OpenSettings => {
238 this.open_settings(SettingsSection::Agents, window, cx)
239 }
240 AgentRunsEvent::Finished(_) => {}
241 },
242 )
243 });
244 let sidebar = cx.new(|cx| SidebarView::new(state.clone(), activity_panel, saved_panel, agents_panel, cx));
245 let rail = cx.new(|cx| RailView::new(state.clone(), activity.clone(), sidebar.clone(), cx));
246 let conversation = cx.new(|cx| ConversationView::new(state.clone(), window, cx));
247 let timeline = conversation.read(cx).timeline_bounds();
248 let call_mini = cx.new(|cx| CallMiniView::new(state.clone(), timeline, cx));
249 let subscription =
250 cx.subscribe_in(&state, window, |this, _, event: &AppEvent, window, cx| {
251 if matches!(event, AppEvent::Sidebar | AppEvent::Status) {
252 this.apply_open_target(cx);
253 }
254 if matches!(event, AppEvent::Forward) {
255 this.open_forward_dialog(window, cx);
256 }
257 if matches!(event, AppEvent::ImageViewer) {
258 this.open_image_viewer(window, cx);
259 }
260 if matches!(event, AppEvent::StatusMessage) {
261 this.open_status_dialog(window, cx);
262 }
263 if let AppEvent::Settings(section) = event {
264 this.open_settings(*section, window, cx);
265 }
266 if matches!(event, AppEvent::Profile) {
267 this.open_profile_card(window, cx);
268 }
269 cx.notify();
270 });
271 let focus_handle = cx.focus_handle();
272 window.focus(&focus_handle, cx);
273 let notifications =
274 cx.new(|cx| NotificationCenter::new(state.clone(), activity.clone(), window, cx));
275 let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx));
276 let user_input = cx.global::<UserInputHandle>().0.clone();
277 let escape_observation = cx.observe_keystrokes(|this, event, _, cx| {
278 this.user_input.touch();
279 if event.keystroke.key == "escape" && event.action.is_none() {
280 this.state
281 .update(cx, |state, cx| state.escape_call_fullscreen(cx));
282 }
283 });
284 let activation_observation = cx.observe_window_activation(window, |this, window, cx| {
285 if window.is_window_active() {
286 this.state
287 .update(cx, |state, cx| state.refresh_calendar(cx));
288 } else {
289 this.state
290 .update(cx, |state, cx| state.release_push_to_talk(cx));
291 }
292 });
293 let closing = notifications.clone();
294 window.on_window_should_close(cx, move |_, cx| !closing.read(cx).intercept_close(cx));
295 let mut shell = AppShell {
296 state,
297 rail,
298 sidebar,
299 conversation,
300 call_chat: None,
301 prejoin: None,
302 switcher: None,
303 notifications,
304 _call_windows: call_windows,
305 forward_dialog: None,
306 image_viewer: None,
307 status_dialog: None,
308 shortcuts_dialog: None,
309 agent_dialog: None,
310 settings_dialog: None,
311 profile_card: None,
312 focus_handle,
313 call_window_fullscreen: false,
314 window_was_fullscreen: false,
315 open_target,
316 update: UpdateStatus::UpToDate,
317 toasts: NoticeToasts::default(),
318 user_input,
319 _subscription: subscription,
320 call_mini,
321 _call_media_observation: call_media_observation,
322 _panel_subscriptions: panel_subscriptions,
323 _agents_subscription: agents_subscription,
324 _agent_subscription: agent_subscription,
325 _escape_observation: escape_observation,
326 _activation_observation: activation_observation,
327 };
328 shell.apply_open_target(cx);
329 shell
330 .state
331 .update(cx, |state, cx| state.refresh_connection_banner(cx));
332 if shell.state.read(cx).mode.demo && std::env::var_os(DEMO_UPDATE_VARIABLE).is_some() {
333 shell.update = UpdateStatus::Ready;
334 }
335 let Startup {
336 switcher_query,
337 composer_text,
338 reply_to_last,
339 } = startup;
340 if let Some(query) = switcher_query {
341 shell.toggle_switcher(&query, window, cx);
342 }
343 if composer_text.is_some() || reply_to_last {
344 cx.defer_in(window, move |this, window, cx| {
345 this.conversation.update(cx, |conversation, cx| {
346 if reply_to_last {
347 conversation.reply_to_latest_from_others(window, cx);
348 }
349 if let Some(text) = composer_text {
350 conversation.set_composer_text(&text, window, cx);
351 }
352 });
353 });
354 }
355 #[cfg(windows)]
356 cx.spawn_in(window, async move |this, cx| {
357 loop {
358 cx.background_executor().timer(updater::POLL_INTERVAL).await;
359 if this
360 .update_in(cx, |this, window, cx| this.poll_update(window, cx))
361 .is_err()
362 {
363 break;
364 }
365 }
366 })
367 .detach();
368 shell
369 }
370
371 #[cfg_attr(not(windows), allow(dead_code))]
372 fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {
373 if matches!(
374 self.update,
375 UpdateStatus::Failed(_) | UpdateStatus::Installing
376 ) {
377 return;
378 }
379 let Some(paths) = updater::current_paths() else {
380 return;
381 };
382 if self.update == UpdateStatus::UpToDate {
383 if !updater::check(&paths) {
384 return;
385 }
386 self.update = UpdateStatus::Ready;
387 let update_exe = paths.update_exe.clone();
388 cx.background_spawn(async move { updater::prescan(&update_exe) })
389 .detach();
390 }
391 let conversation = self.conversation.read(cx);
392 let idle = updater::is_idle(IdleInputs {
393 window_active: window.is_window_active(),
394 composer_empty: conversation.composer_is_empty(cx),
395 send_in_flight: conversation.send_in_flight(),
396 });
397 if idle {
398 self.restart_into_update(cx);
399 }
400 cx.notify();
401 }
402
403 fn restart_into_update(&mut self, cx: &mut Context<Self>) {
404 if self.update == UpdateStatus::Installing {
405 return;
406 }
407 let Some(paths) = updater::current_paths() else {
408 return;
409 };
410 self.update = UpdateStatus::Installing;
411 cx.notify();
412 cx.spawn(async move |this, cx| {
413 let result = cx
414 .background_spawn(async move { updater::install_and_relaunch(&paths) })
415 .await;
416 let _ = this.update(cx, |this, cx| {
417 match result {
418 Ok(()) => cx.quit(),
419 Err(message) => this.update = UpdateStatus::Failed(message),
420 }
421 cx.notify();
422 });
423 })
424 .detach();
425 }
426
427 fn apply_open_target(&mut self, cx: &mut Context<Self>) {
428 let Some(target) = self.open_target else {
429 return;
430 };
431 let sidebar = &self.state.read(cx).sidebar;
432 let selection = match target {
433 OpenTarget::Chat(index) => sidebar
434 .chats
435 .get(index)
436 .map(|chat| Selection::Chat(chat.id.clone())),
437 OpenTarget::Channel(index) => sidebar
438 .teams
439 .iter()
440 .flat_map(|team| team.channels.iter())
441 .nth(index)
442 .map(|channel| Selection::Channel(channel.id.clone())),
443 };
444 if let Some(selection) = selection {
445 self.open_target = None;
446 self.state
447 .update(cx, |state, cx| state.select(selection, cx));
448 }
449 }
450
451 fn toggle_switcher(
452 &mut self,
453 initial_query: &str,
454 window: &mut Window,
455 cx: &mut Context<Self>,
456 ) {
457 if self.switcher.take().is_some() {
458 self.restore_focus(window, cx);
459 cx.notify();
460 return;
461 }
462 self.close_image_viewer(window, cx);
463 let candidates = candidates_from(self.state.read(cx));
464 let app = self.state.clone();
465 let switcher = cx.new(|cx| Switcher::new(app, candidates, initial_query, window, cx));
466 cx.subscribe_in(
467 &switcher,
468 window,
469 |this, _, event: &SwitcherEvent, window, cx| {
470 match event {
471 SwitcherEvent::Pick(selection) => {
472 let selection = selection.clone();
473 this.state
474 .update(cx, |state, cx| state.select(selection, cx));
475 }
476 SwitcherEvent::PickMessage {
477 selection,
478 message_id,
479 } => {
480 let (selection, message_id) = (selection.clone(), message_id.clone());
481 this.state.update(cx, |state, cx| {
482 state.jump_to_message(selection, message_id, cx)
483 });
484 }
485 SwitcherEvent::Close => {}
486 }
487 this.switcher = None;
488 this.restore_focus(window, cx);
489 cx.notify();
490 },
491 )
492 .detach();
493 self.switcher = Some(switcher);
494 cx.notify();
495 }
496
497 fn open_message(&mut self, conversation_id: &str, message_id: &str, cx: &mut Context<Self>) {
498 let Some(selection) = selection_for(&self.state.read(cx).sidebar, conversation_id) else {
499 return;
500 };
501 let message_id = message_id.to_owned();
502 self.state.update(cx, |state, cx| {
503 if message_id.is_empty() {
504 state.select(selection, cx)
505 } else {
506 state.jump_to_message(selection, message_id, cx)
507 }
508 });
509 }
510
511 fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
512 if !self
513 .state
514 .update(cx, |state, _| state.take_status_message_request())
515 {
516 return;
517 }
518 self.close_image_viewer(window, cx);
519 self.switcher = None;
520 let app = self.state.clone();
521 let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx));
522 cx.subscribe_in(
523 &dialog,
524 window,
525 |this, _, _: &StatusMessageDialogEvent, window, cx| {
526 this.status_dialog = None;
527 this.restore_focus(window, cx);
528 cx.notify();
529 },
530 )
531 .detach();
532 self.status_dialog = Some(dialog);
533 cx.notify();
534 }
535
536 fn open_agent_dialog(
537 &mut self,
538 request: StartRequest,
539 window: &mut Window,
540 cx: &mut Context<Self>,
541 ) {
542 let Some(runs) = crate::agents::runs_handle(cx) else {
543 return;
544 };
545 let settings = runs.read(cx).settings();
546 let Some(action) = settings
547 .actions
548 .iter()
549 .find(|action| action.id == request.action_id)
550 .cloned()
551 else {
552 return;
553 };
554 let agent_name = settings
555 .agents
556 .iter()
557 .find(|agent| agent.id == action.agent_id)
558 .map(|agent| agent.name.clone())
559 .unwrap_or_default();
560 self.switcher = None;
561 let app = self.state.clone();
562 let dialog = cx.new(|cx| StartDialog::new(app, request, action, &agent_name, window, cx));
563 cx.subscribe_in(
564 &dialog,
565 window,
566 |this, _, _: &StartDialogEvent, window, cx| {
567 this.agent_dialog = None;
568 window.focus(&this.focus_handle, cx);
569 cx.notify();
570 },
571 )
572 .detach();
573 self.agent_dialog = Some(dialog);
574 cx.notify();
575 }
576
577 fn open_settings(
578 &mut self,
579 section: SettingsSection,
580 window: &mut Window,
581 cx: &mut Context<Self>,
582 ) {
583 if let Some(dialog) = &self.settings_dialog {
584 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx));
585 return;
586 }
587 self.close_image_viewer(window, cx);
588 self.switcher = None;
589 let (app, center) = (self.state.clone(), self.notifications.clone());
590 let dialog = cx.new(|cx| SettingsDialog::new(app, center, section, window, cx));
591 cx.subscribe_in(
592 &dialog,
593 window,
594 |this, _, _: &SettingsDialogEvent, window, cx| {
595 this.settings_dialog = None;
596 this.restore_focus(window, cx);
597 cx.notify();
598 },
599 )
600 .detach();
601 self.settings_dialog = Some(dialog);
602 cx.notify();
603 }
604
605 fn toggle_shortcuts_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
606 if self.shortcuts_dialog.take().is_some() {
607 self.restore_focus(window, cx);
608 cx.notify();
609 return;
610 }
611 self.close_image_viewer(window, cx);
612 self.switcher = None;
613 let dialog = cx.new(|cx| ShortcutsDialog::new(window, cx));
614 cx.subscribe_in(
615 &dialog,
616 window,
617 |this, _, _: &ShortcutsDialogEvent, window, cx| {
618 this.shortcuts_dialog = None;
619 this.restore_focus(window, cx);
620 cx.notify();
621 },
622 )
623 .detach();
624 self.shortcuts_dialog = Some(dialog);
625 cx.notify();
626 }
627
628 fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) {
629 let Some(request) = self
630 .state
631 .update(cx, |state, _| state.take_profile_request())
632 else {
633 return;
634 };
635 self.close_image_viewer(window, cx);
636 if let Some(card) = &self.profile_card {
637 card.update(cx, |card, cx| {
638 card.move_to(request.user_id, request.anchor, cx)
639 });
640 return;
641 }
642 let app = self.state.clone();
643 let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx));
644 cx.subscribe_in(
645 &card,
646 window,
647 |this, _, _: &ProfileCardEvent, window, cx| {
648 this.profile_card = None;
649 this.restore_focus(window, cx);
650 cx.notify();
651 },
652 )
653 .detach();
654 self.profile_card = Some(card);
655 cx.notify();
656 }
657
658 fn open_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
659 let Some(request) = self
660 .state
661 .update(cx, |state, _| state.take_image_viewer_request())
662 else {
663 return;
664 };
665 self.switcher = None;
666 let app = self.state.clone();
667 let viewer = cx.new(|cx| ImageViewer::new(app, request, window, cx));
668 cx.subscribe_in(
669 &viewer,
670 window,
671 |this, _, _: &ImageViewerEvent, window, cx| {
672 this.image_viewer = None;
673 window.focus(&this.focus_handle, cx);
674 cx.notify();
675 },
676 )
677 .detach();
678 self.image_viewer = Some(viewer);
679 cx.notify();
680 }
681
682 fn start_call_in_open_chat(&mut self, with_video: bool, cx: &mut Context<Self>) {
683 self.state.update(cx, |state, cx| {
684 if let Some(Selection::Chat(chat_id)) = state.selection.clone() {
685 if with_video {
686 state.start_chat_video_call(&chat_id, cx);
687 } else {
688 state.start_chat_call(&chat_id, cx);
689 }
690 }
691 });
692 }
693
694 fn sync_window_fullscreen(&mut self, wanted: bool, window: &mut Window) {
695 if wanted == self.call_window_fullscreen {
696 return;
697 }
698 self.call_window_fullscreen = wanted;
699 if wanted {
700 self.window_was_fullscreen = window.is_fullscreen();
701 }
702 if wanted != window.is_fullscreen() && (wanted || !self.window_was_fullscreen) {
703 window.toggle_fullscreen();
704 }
705 }
706
707 fn close_image_viewer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
708 if self.image_viewer.take().is_some() {
709 window.focus(&self.focus_handle, cx);
710 cx.notify();
711 }
712 }
713
714 fn restore_focus(&self, window: &mut Window, cx: &mut Context<Self>) {
715 match &self.image_viewer {
716 Some(viewer) => {
717 let handle = viewer.read(cx).focus_handle();
718 window.focus(&handle, cx)
719 }
720 None => window.focus(&self.focus_handle, cx),
721 }
722 }
723
724 fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
725 let Some(source) = self
726 .state
727 .update(cx, |state, _| state.take_forward_request())
728 else {
729 return;
730 };
731 self.switcher = None;
732 let candidates = candidates_from(self.state.read(cx));
733 let app = self.state.clone();
734 let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx));
735 cx.subscribe_in(
736 &dialog,
737 window,
738 |this, _, event: &ForwardDialogEvent, window, cx| {
739 this.forward_dialog = None;
740 this.restore_focus(window, cx);
741 if let ForwardDialogEvent::Sent { target, title } = event {
742 let target = target.clone();
743 let open = NoticeAction::new("Open", move |state, cx| {
744 state.select(target.clone(), cx)
745 });
746 this.state.update(cx, |state, cx| {
747 state.raise_notice(format!("Forwarded to {title}"), Some(open), cx)
748 });
749 }
750 cx.notify();
751 },
752 )
753 .detach();
754 self.forward_dialog = Some(dialog);
755 cx.notify();
756 }
757}
758
759impl AppShell {
760 fn islands(&self, call_view: Option<AnyElement>, show_sidebar: bool) -> impl IntoElement {
761 h_flex()
762 .debug_selector(|| "shell/islands".into())
763 .flex_1()
764 .min_h_0()
765 .w_full()
766 .gap(px(ISLAND_GAP))
767 .pr(px(ISLAND_GAP))
768 .child(
769 h_flex()
770 .h_full()
771 .flex_none()
772 .child(self.rail.clone())
773 .when(show_sidebar, |group| {
774 group.child(
775 self.sidebar.clone().cached(
776 StyleRefinement::default()
777 .w(px(SIDEBAR_WIDTH))
778 .h_full()
779 .flex_none(),
780 ),
781 )
782 }),
783 )
784 .child(match (call_view, self.prejoin.clone()) {
785 (Some(call_view), _) => call_view,
786 (None, Some(prejoin)) => prejoin
787 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
788 .into_any_element(),
789 (None, None) => self
790 .conversation
791 .clone()
792 .cached(
793 StyleRefinement::default()
794 .flex_1()
795 .min_w_0()
796 .h_full()
797 .rounded(px(10.))
798 .bg(theme::surface())
799 .overflow_hidden(),
800 )
801 .into_any_element(),
802 })
803 }
804}
805
806impl Render for AppShell {
807 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
808 let open_dialogs = [
809 self.state.read(cx).task_dialog.is_some(),
810 self.forward_dialog.is_some(),
811 self.status_dialog.is_some(),
812 self.shortcuts_dialog.is_some(),
813 self.agent_dialog.is_some(),
814 self.settings_dialog.is_some(),
815 ]
816 .into_iter()
817 .filter(|open| *open)
818 .count();
819 elevation::set_app_modal_layers(window, open_dialogs, cx);
820 if let Some(dialog) = self
821 .state
822 .read(cx)
823 .task_dialog
824 .as_ref()
825 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
826 {
827 self.state.update(cx, |state, cx| {
828 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
829 });
830 }
831 if let Some(me) = self.state.read(cx).directory.me.clone() {
832 let app = self.state.clone();
833 cx.defer(move |cx| {
834 app.update(cx, |state, cx| {
835 state.request_avatars(vec![me.user_id.clone()], cx);
836 state.request_presence(vec![me.user_id], cx);
837 });
838 });
839 }
840 let chat_thread = self.state.read(cx).call_chat_thread();
841 self.sync_call_chat(chat_thread, window, cx);
842 self.sync_prejoin(window, cx);
843 let notice = self.state.read(cx).notice.clone();
844 self.toasts.sync(&self.state, notice.as_ref(), window, cx);
845 let update_failure = match &self.update {
846 UpdateStatus::Failed(message) => Some(message.clone()),
847 _ => None,
848 };
849 self.toasts.update_failed(update_failure.as_deref(), window, cx);
850 let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone().into());
851 let state = self.state.read(cx);
852 let call_fullscreen = state.call_fullscreen();
853 let window_width = f32::from(window.viewport_size().width);
854 let panel_open = state
855 .call
856 .as_ref()
857 .is_some_and(|call| call.chat_open || call.people_open || call.transcript_open);
858 let show_sidebar = !(state.viewing_call()
859 && !call_fullscreen
860 && sidebar_hidden_in_call(window_width, panel_open));
861 let island_width = window_width
862 - RAIL_WIDTH
863 - if show_sidebar { SIDEBAR_WIDTH } else { 0. }
864 - 2. * ISLAND_GAP;
865 let call_view = render_call_view(&self.state, state, chat_panel, call_fullscreen, island_width)
866 .filter(|_| state.viewing_call())
867 .or_else(|| render_call_end(&self.state, state));
868 let (call_view, call_overlay) = match call_view {
869 Some(view) if call_fullscreen => (None, Some(render_call_overlay(view))),
870 view => (view, None),
871 };
872 self.sync_window_fullscreen(call_fullscreen, window);
873 let call_mini = state
874 .call
875 .as_ref()
876 .is_some_and(|call| !call.viewing)
877 .then(|| self.call_mini.clone());
878 let stage_overlay = render_stage_overlay(&self.state, state);
879 let banner = state.connection_banner_text().map(|banner| {
880 render_connection_banner(
881 banner,
882 cx.listener(|this, _, _, cx| {
883 this.state.update(cx, |state, _| state.retry_live())
884 }),
885 )
886 });
887 let title_bar = render_title_bar(
888 mode_label(state.mode),
889 update_button(
890 &self.update,
891 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
892 ),
893 own_status_button(&self.state, state),
894 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
895 );
896 div()
897 .id("app-shell")
898 .key_context("AppShell")
899 .track_focus(&self.focus_handle)
900 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
901 this.toggle_switcher("", window, cx);
902 }))
903 .on_action(cx.listener(|this, _: &NewChat, window, cx| {
904 this.close_image_viewer(window, cx);
905 this.state.update(cx, |state, cx| state.start_new_chat(cx));
906 }))
907 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
908 this.state
909 .update(cx, |state, cx| state.toggle_call_mute(cx));
910 }))
911 .on_action(cx.listener(|this, _: &PushToTalk, _, cx| {
912 this.state
913 .update(cx, |state, cx| state.press_push_to_talk(cx));
914 }))
915 .on_key_up(cx.listener(|this, event: &KeyUpEvent, _, cx| {
916 if event.keystroke.key == "space" {
917 this.state
918 .update(cx, |state, cx| state.release_push_to_talk(cx));
919 }
920 }))
921 .on_modifiers_changed(cx.listener(|this, event: &ModifiersChangedEvent, _, cx| {
922 if !event.modifiers.control {
923 this.state
924 .update(cx, |state, cx| state.release_push_to_talk(cx));
925 }
926 }))
927 .on_action(cx.listener(|this, _: &OpenSettings, window, cx| {
928 this.open_settings(SettingsSection::General, window, cx);
929 }))
930 .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| {
931 this.toggle_shortcuts_dialog(window, cx);
932 }))
933 .on_action(cx.listener(|this, _: &OpenActivity, window, cx| {
934 this.close_image_viewer(window, cx);
935 this.sidebar
936 .update(cx, |sidebar, cx| sidebar.show_activity(cx));
937 }))
938 .on_action(cx.listener(|this, _: &OpenSaved, window, cx| {
939 this.close_image_viewer(window, cx);
940 this.sidebar
941 .update(cx, |sidebar, cx| sidebar.show_saved(cx));
942 }))
943 .on_action(cx.listener(|this, _: &OpenAgents, window, cx| {
944 this.close_image_viewer(window, cx);
945 this.sidebar
946 .update(cx, |sidebar, cx| sidebar.show_agents(cx));
947 }))
948 .on_action(cx.listener(|this, _: &OpenChatsTab, window, cx| {
949 this.close_image_viewer(window, cx);
950 this.sidebar
951 .update(cx, |sidebar, cx| sidebar.show_chats(cx));
952 }))
953 .on_action(cx.listener(|this, _: &OpenChannelsTab, window, cx| {
954 this.close_image_viewer(window, cx);
955 this.sidebar
956 .update(cx, |sidebar, cx| sidebar.show_channels(cx));
957 }))
958 .on_action(cx.listener(|this, _: &OpenCallsTab, window, cx| {
959 this.close_image_viewer(window, cx);
960 this.sidebar
961 .update(cx, |sidebar, cx| sidebar.show_calls(cx));
962 }))
963 .on_action(cx.listener(|this, _: &PreviousConversation, window, cx| {
964 this.close_image_viewer(window, cx);
965 this.sidebar.update(cx, |sidebar, cx| {
966 sidebar.select_adjacent(Step::Previous, cx)
967 });
968 }))
969 .on_action(cx.listener(|this, _: &NextConversation, window, cx| {
970 this.close_image_viewer(window, cx);
971 this.sidebar
972 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx));
973 }))
974 .on_action(cx.listener(|this, _: &AcceptCall, _, cx| {
975 this.state
976 .update(cx, |state, cx| state.accept_ringing_call(cx));
977 }))
978 .on_action(cx.listener(|this, _: &DeclineCall, _, cx| {
979 this.state
980 .update(cx, |state, cx| state.decline_ringing_call(cx));
981 }))
982 .on_action(cx.listener(|this, _: &JoinMeetingFromToast, _, cx| {
983 this.state
984 .update(cx, |state, cx| state.join_latest_meeting_toast(cx));
985 }))
986 .on_action(cx.listener(|this, _: &HangUpCall, _, cx| {
987 this.state.update(cx, |state, cx| state.leave_call(cx));
988 }))
989 .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| {
990 this.state
991 .update(cx, |state, cx| state.toggle_call_camera(cx));
992 }))
993 .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| {
994 this.state
995 .update(cx, |state, cx| state.toggle_call_share(cx));
996 }))
997 .on_action(cx.listener(|this, _: &ToggleCallHand, _, cx| {
998 this.state
999 .update(cx, |state, cx| state.toggle_call_hand(cx));
1000 }))
1001 .on_action(cx.listener(|this, _: &OpenLobbyList, _, cx| {
1002 this.state
1003 .update(cx, |state, cx| state.open_call_lobby_list(cx));
1004 }))
1005 .on_action(cx.listener(|this, _: &StartAudioCall, _, cx| {
1006 this.start_call_in_open_chat(false, cx);
1007 }))
1008 .on_action(cx.listener(|this, _: &StartVideoCall, _, cx| {
1009 this.start_call_in_open_chat(true, cx);
1010 }))
1011 .on_action(cx.listener(|this, _: &ToggleCallFullscreen, _, cx| {
1012 if this.image_viewer.is_some() || !this.state.read(cx).viewing_call() {
1013 cx.propagate();
1014 return;
1015 }
1016 this.state
1017 .update(cx, |state, cx| state.toggle_call_fullscreen(cx));
1018 }))
1019 .on_action(zoom_listener::<ZoomShareIn>(&self.state, ZoomStep::In))
1020 .on_action(zoom_listener::<ZoomShareOut>(&self.state, ZoomStep::Out))
1021 .on_action(zoom_listener::<ZoomShareReset>(&self.state, ZoomStep::Reset))
1022 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
1023 this.close_image_viewer(window, cx);
1024 this.conversation.update(cx, |conversation, cx| {
1025 conversation.reply_to_hovered(window, cx)
1026 });
1027 }))
1028 .relative()
1029 .size_full()
1030 .bg(theme::background())
1031 .text_color(theme::text())
1032 .text_size(px(14.))
1033 .child(crate::frame_log::probe("first"))
1034 .child(user_activity::input_probe(self.user_input.clone()))
1035 .child(
1036 v_flex()
1037 .debug_selector(|| "shell".into())
1038 .size_full()
1039 .child(title_bar)
1040 .children(banner)
1041 .child(self.islands(call_view, show_sidebar)),
1042 )
1043 .children(call_mini)
1044 .children(call_overlay)
1045 .children(stage_overlay)
1046 .children(self.image_viewer.clone())
1047 .children(self.switcher.clone())
1048 .children(render_task_dialog(self.state.read(cx), cx))
1049 .children(self.forward_dialog.clone())
1050 .children(self.status_dialog.clone())
1051 .children(self.shortcuts_dialog.clone())
1052 .children(self.agent_dialog.clone())
1053 .children(self.settings_dialog.clone())
1054 .children(self.profile_card.clone())
1055 .child(crate::frame_log::probe("last"))
1056 }
1057}