BlueGit

ict rusty-teams

Files

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

shell.rs

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