BlueGit

ict rusty-teams

Files

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

shell.rs

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