BlueGit

ict rusty-teams

Files

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

shell.rs

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