BlueGit

ict rusty-teams

Files

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

shell.rs

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