BlueGit

ict rusty-teams

Files

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

shell.rs

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