BlueGit

ict rusty-teams

Files

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

shell.rs

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