BlueGit

ict rusty-teams

Files

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

shell.rs

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