BlueGit

ict rusty-teams

Files

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

shell.rs

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