BlueGit

ict rusty-teams

Files

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

shell.rs

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