BlueGit

ict rusty-teams

Files

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

shell.rs

shell.rs 24.6 KiB Download Raw
1use gpui_kit::component::{h_flex, v_flex};
2use gpui_kit::*;
3
4use super::activity_panel::{ActivityPanel, ActivityPanelEvent};
5use super::call_mini::render_call_mini;
6use super::call_stage::render_stage_overlay;
7use super::call_view::render_call_view;
8use super::conversation::{ConversationView, ReplyToHovered};
9use super::dialog_overlay::render_task_dialog;
10use super::forward_dialog::{ForwardDialog, ForwardDialogEvent};
11use super::profile_card::{ProfileCard, ProfileCardEvent};
12use super::rail::RailView;
13use super::saved_panel::{SavedPanel, SavedPanelEvent};
14use super::shortcuts::{
15 AcceptCall, DeclineCall, HangUpCall, NextConversation, OpenActivity, OpenChannelsTab,
16 OpenCallsTab, OpenChatsTab, OpenSaved, PreviousConversation, Scope, ShowShortcuts, ToggleCallCamera,
17 ToggleCallShare,
18};
19use super::shortcuts_dialog::{ShortcutsDialog, ShortcutsDialogEvent};
20use super::sidebar::{SIDEBAR_WIDTH, SidebarView};
21use super::status_bar::render_status_bar;
22use super::status_menu::own_status_button;
23use super::status_message_dialog::{StatusMessageDialog, StatusMessageDialogEvent};
24use super::switcher::{Switcher, SwitcherEvent, candidates_from};
25use super::title_bar::render_title_bar;
26use crate::activity::ActivityCenter;
27use crate::app_state::{AppEvent, AppState, Selection};
28use crate::call::{CallMedia, CallWindows};
29use crate::embedded_web::{Overlays, RootFocus};
30use crate::notice::NoticeAction;
31use crate::notify::{NotificationCenter, selection_for};
32use crate::sidebar_model::Step;
33use crate::theme;
34use crate::updater::{self, IdleInputs, UpdateStatus};
35
36actions!(teams, [OpenSwitcher, NewChat, ToggleCallMute]);
37
38pub fn bind_keys(cx: &mut App) {
39 super::composer::bind_keys(cx);
40 super::shortcuts::bind_scopes(cx, &[Scope::Global, Scope::OutsideComposer, Scope::GlobalOverTextFields]);
41}
42
43#[derive(Debug, Clone, Copy)]
44pub enum OpenTarget {
45 Chat(usize),
46 Channel(usize),
47}
48
49#[derive(Default)]
50pub struct Startup {
51 pub switcher_query: Option<String>,
52 pub composer_text: Option<String>,
53 pub reply_to_last: bool,
54}
55
56pub const ISLAND_GAP: f32 = 6.;
57
58pub struct AppShell {
59 state: Entity<AppState>,
60 rail: Entity<RailView>,
61 sidebar: Entity<SidebarView>,
62 conversation: Entity<ConversationView>,
63 call_chat: Option<(String, Entity<ConversationView>)>,
64 switcher: Option<Entity<Switcher>>,
65 notifications: Entity<NotificationCenter>,
66 _call_windows: Entity<CallWindows>,
67 forward_dialog: Option<Entity<ForwardDialog>>,
68 status_dialog: Option<Entity<StatusMessageDialog>>,
69 shortcuts_dialog: Option<Entity<ShortcutsDialog>>,
70 profile_card: Option<Entity<ProfileCard>>,
71 focus_handle: FocusHandle,
72 open_target: Option<OpenTarget>,
73 update: UpdateStatus,
74 _subscription: Subscription,
75 _call_media_observation: Subscription,
76 _panel_subscriptions: [Subscription; 2],
77 _escape_observation: Subscription,
78 _activation_observation: Subscription,
79}
80
81impl AppShell {
82 fn sync_call_chat(&mut self, thread: Option<String>, window: &mut Window, cx: &mut Context<Self>) {
83 match (thread, &self.call_chat) {
84 (Some(wanted), Some((open, _))) if &wanted == open => {}
85 (Some(wanted), _) => {
86 let app = self.state.clone();
87 let selection = Selection::Chat(wanted.clone());
88 let view = cx.new(|cx| ConversationView::pinned_to(app, selection, window, cx));
89 self.call_chat = Some((wanted, view));
90 }
91 (None, _) => self.call_chat = None,
92 }
93 }
94
95 pub fn new(
96 state: Entity<AppState>,
97 open_target: Option<OpenTarget>,
98 startup: Startup,
99 window: &mut Window,
100 cx: &mut Context<Self>,
101 ) -> Self {
102 let call_media = cx.new(|_| CallMedia);
103 state.update(cx, |state, _| state.call_media = Some(call_media.clone()));
104 let call_media_observation = cx.observe(&call_media, |_, _, cx| cx.notify());
105 let activity = cx.new(|cx| ActivityCenter::new(state.clone(), window, cx));
106 let activity_panel = cx.new(|cx| ActivityPanel::new(state.clone(), activity.clone(), cx));
107 let saved_panel = cx.new(|cx| SavedPanel::new(state.clone(), cx));
108 let panel_subscriptions = [
109 cx.subscribe(&activity_panel, |this, _, event: &ActivityPanelEvent, cx| match event {
110 ActivityPanelEvent::OpenSettings => this
111 .notifications
112 .update(cx, |center, cx| center.open_settings(cx)),
113 ActivityPanelEvent::Open {
114 conversation_id,
115 message_id,
116 } => this.open_message(conversation_id, message_id, cx),
117 }),
118 cx.subscribe(&saved_panel, |this, _, event: &SavedPanelEvent, cx| {
119 let SavedPanelEvent::Open {
120 conversation_id,
121 message_id,
122 } = event;
123 this.open_message(conversation_id, message_id, cx);
124 }),
125 ];
126 let sidebar = cx.new(|cx| SidebarView::new(state.clone(), activity_panel, saved_panel, cx));
127 let rail = cx.new(|cx| RailView::new(state.clone(), activity.clone(), sidebar.clone(), cx));
128 let conversation = cx.new(|cx| ConversationView::new(state.clone(), window, cx));
129 let subscription =
130 cx.subscribe_in(&state, window, |this, _, event: &AppEvent, window, cx| {
131 if matches!(event, AppEvent::Sidebar | AppEvent::Status) {
132 this.apply_open_target(cx);
133 }
134 if matches!(event, AppEvent::Forward) {
135 this.open_forward_dialog(window, cx);
136 }
137 if matches!(event, AppEvent::StatusMessage) {
138 this.open_status_dialog(window, cx);
139 }
140 if matches!(event, AppEvent::NotificationSettings) {
141 this.notifications
142 .update(cx, |center, cx| center.open_settings(cx));
143 }
144 if matches!(event, AppEvent::Profile) {
145 this.open_profile_card(window, cx);
146 }
147 cx.notify();
148 });
149 let focus_handle = cx.focus_handle();
150 window.focus(&focus_handle, cx);
151 cx.set_global(RootFocus(focus_handle.clone()));
152 let notifications = cx.new(|cx| NotificationCenter::new(state.clone(), window, cx));
153 let call_windows = cx.new(|cx| CallWindows::new(state.clone(), window, cx));
154 let escape_observation = cx.observe_keystrokes(|this, event, _, cx| {
155 if event.keystroke.key == "escape" {
156 this.state.update(cx, |state, cx| state.leave_stage_fullscreen(cx));
157 }
158 });
159 let activation_observation = cx.observe_window_activation(window, |this, window, cx| {
160 if window.is_window_active() {
161 this.state.update(cx, |state, cx| state.refresh_calendar(cx));
162 }
163 });
164 let closing = notifications.clone();
165 window.on_window_should_close(cx, move |_, cx| !closing.read(cx).intercept_close(cx));
166 let mut shell = AppShell {
167 state,
168 rail,
169 sidebar,
170 conversation,
171 call_chat: None,
172 switcher: None,
173 notifications,
174 _call_windows: call_windows,
175 forward_dialog: None,
176 status_dialog: None,
177 shortcuts_dialog: None,
178 profile_card: None,
179 focus_handle,
180 open_target,
181 update: UpdateStatus::UpToDate,
182 _subscription: subscription,
183 _call_media_observation: call_media_observation,
184 _panel_subscriptions: panel_subscriptions,
185 _escape_observation: escape_observation,
186 _activation_observation: activation_observation,
187 };
188 shell.apply_open_target(cx);
189 let Startup {
190 switcher_query,
191 composer_text,
192 reply_to_last,
193 } = startup;
194 if let Some(query) = switcher_query {
195 shell.toggle_switcher(&query, window, cx);
196 }
197 if composer_text.is_some() || reply_to_last {
198 cx.defer_in(window, move |this, window, cx| {
199 this.conversation.update(cx, |conversation, cx| {
200 if reply_to_last {
201 conversation.reply_to_latest_from_others(window, cx);
202 }
203 if let Some(text) = composer_text {
204 conversation.set_composer_text(&text, window, cx);
205 }
206 });
207 });
208 }
209 #[cfg(windows)]
210 cx.spawn_in(window, async move |this, cx| {
211 loop {
212 cx.background_executor().timer(updater::POLL_INTERVAL).await;
213 if this
214 .update_in(cx, |this, window, cx| this.poll_update(window, cx))
215 .is_err()
216 {
217 break;
218 }
219 }
220 })
221 .detach();
222 shell
223 }
224
225 #[cfg_attr(not(windows), allow(dead_code))]
226 fn poll_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {
227 if matches!(self.update, UpdateStatus::Failed(_) | UpdateStatus::Installing) {
228 return;
229 }
230 let Some(paths) = updater::current_paths() else {
231 return;
232 };
233 if self.update == UpdateStatus::UpToDate {
234 if !updater::check(&paths) {
235 return;
236 }
237 self.update = UpdateStatus::Ready;
238 let update_exe = paths.update_exe.clone();
239 cx.background_spawn(async move { updater::prescan(&update_exe) })
240 .detach();
241 }
242 let conversation = self.conversation.read(cx);
243 let idle = updater::is_idle(IdleInputs {
244 window_active: window.is_window_active(),
245 composer_empty: conversation.composer_is_empty(cx),
246 send_in_flight: conversation.send_in_flight(),
247 });
248 if idle {
249 self.restart_into_update(cx);
250 }
251 cx.notify();
252 }
253
254 fn restart_into_update(&mut self, cx: &mut Context<Self>) {
255 if self.update == UpdateStatus::Installing {
256 return;
257 }
258 let Some(paths) = updater::current_paths() else {
259 return;
260 };
261 self.update = UpdateStatus::Installing;
262 cx.notify();
263 cx.spawn(async move |this, cx| {
264 let result = cx
265 .background_spawn(async move { updater::install_and_relaunch(&paths) })
266 .await;
267 let _ = this.update(cx, |this, cx| {
268 match result {
269 Ok(()) => cx.quit(),
270 Err(message) => this.update = UpdateStatus::Failed(message),
271 }
272 cx.notify();
273 });
274 })
275 .detach();
276 }
277
278 fn apply_open_target(&mut self, cx: &mut Context<Self>) {
279 let Some(target) = self.open_target else {
280 return;
281 };
282 let sidebar = &self.state.read(cx).sidebar;
283 let selection = match target {
284 OpenTarget::Chat(index) => sidebar
285 .chats
286 .get(index)
287 .map(|chat| Selection::Chat(chat.id.clone())),
288 OpenTarget::Channel(index) => sidebar
289 .teams
290 .iter()
291 .flat_map(|team| team.channels.iter())
292 .nth(index)
293 .map(|channel| Selection::Channel(channel.id.clone())),
294 };
295 if let Some(selection) = selection {
296 self.open_target = None;
297 self.state
298 .update(cx, |state, cx| state.select(selection, cx));
299 }
300 }
301
302 fn toggle_switcher(
303 &mut self,
304 initial_query: &str,
305 window: &mut Window,
306 cx: &mut Context<Self>,
307 ) {
308 if self.switcher.take().is_some() {
309 window.focus(&self.focus_handle, cx);
310 cx.notify();
311 return;
312 }
313 let candidates = candidates_from(self.state.read(cx));
314 let app = self.state.clone();
315 let switcher = cx.new(|cx| Switcher::new(app, candidates, initial_query, window, cx));
316 cx.subscribe_in(
317 &switcher,
318 window,
319 |this, _, event: &SwitcherEvent, window, cx| {
320 match event {
321 SwitcherEvent::Pick(selection) => {
322 let selection = selection.clone();
323 this.state
324 .update(cx, |state, cx| state.select(selection, cx));
325 }
326 SwitcherEvent::PickMessage {
327 selection,
328 message_id,
329 } => {
330 let (selection, message_id) = (selection.clone(), message_id.clone());
331 this.state.update(cx, |state, cx| {
332 state.jump_to_message(selection, message_id, cx)
333 });
334 }
335 SwitcherEvent::Close => {}
336 }
337 this.switcher = None;
338 window.focus(&this.focus_handle, cx);
339 cx.notify();
340 },
341 )
342 .detach();
343 self.switcher = Some(switcher);
344 cx.notify();
345 }
346
347 fn open_message(&mut self, conversation_id: &str, message_id: &str, cx: &mut Context<Self>) {
348 let Some(selection) = selection_for(&self.state.read(cx).sidebar, conversation_id) else {
349 return;
350 };
351 let message_id = message_id.to_owned();
352 self.state.update(cx, |state, cx| {
353 if message_id.is_empty() {
354 state.select(selection, cx)
355 } else {
356 state.jump_to_message(selection, message_id, cx)
357 }
358 });
359 }
360
361 fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
362 if !self
363 .state
364 .update(cx, |state, _| state.take_status_message_request())
365 {
366 return;
367 }
368 self.switcher = None;
369 let app = self.state.clone();
370 let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx));
371 cx.subscribe_in(
372 &dialog,
373 window,
374 |this, _, _: &StatusMessageDialogEvent, window, cx| {
375 this.status_dialog = None;
376 window.focus(&this.focus_handle, cx);
377 cx.notify();
378 },
379 )
380 .detach();
381 self.status_dialog = Some(dialog);
382 cx.notify();
383 }
384
385 fn toggle_shortcuts_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
386 if self.shortcuts_dialog.take().is_some() {
387 window.focus(&self.focus_handle, cx);
388 cx.notify();
389 return;
390 }
391 self.switcher = None;
392 let dialog = cx.new(|cx| ShortcutsDialog::new(window, cx));
393 cx.subscribe_in(
394 &dialog,
395 window,
396 |this, _, _: &ShortcutsDialogEvent, window, cx| {
397 this.shortcuts_dialog = None;
398 window.focus(&this.focus_handle, cx);
399 cx.notify();
400 },
401 )
402 .detach();
403 self.shortcuts_dialog = Some(dialog);
404 cx.notify();
405 }
406
407 fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) {
408 let Some(request) = self
409 .state
410 .update(cx, |state, _| state.take_profile_request())
411 else {
412 return;
413 };
414 if let Some(card) = &self.profile_card {
415 card.update(cx, |card, cx| {
416 card.move_to(request.user_id, request.anchor, cx)
417 });
418 return;
419 }
420 let app = self.state.clone();
421 let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx));
422 cx.subscribe_in(
423 &card,
424 window,
425 |this, _, _: &ProfileCardEvent, window, cx| {
426 this.profile_card = None;
427 window.focus(&this.focus_handle, cx);
428 cx.notify();
429 },
430 )
431 .detach();
432 self.profile_card = Some(card);
433 cx.notify();
434 }
435
436 fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
437 let Some(source) = self
438 .state
439 .update(cx, |state, _| state.take_forward_request())
440 else {
441 return;
442 };
443 self.switcher = None;
444 let candidates = candidates_from(self.state.read(cx));
445 let app = self.state.clone();
446 let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx));
447 cx.subscribe_in(
448 &dialog,
449 window,
450 |this, _, event: &ForwardDialogEvent, window, cx| {
451 this.forward_dialog = None;
452 window.focus(&this.focus_handle, cx);
453 if let ForwardDialogEvent::Sent { target, title } = event {
454 let target = target.clone();
455 let open = NoticeAction::new("Open", move |state, cx| {
456 state.select(target.clone(), cx)
457 });
458 this.state.update(cx, |state, cx| {
459 state.raise_notice(format!("Forwarded to {title}"), Some(open), cx)
460 });
461 }
462 cx.notify();
463 },
464 )
465 .detach();
466 self.forward_dialog = Some(dialog);
467 cx.notify();
468 }
469}
470
471impl AppShell {
472 fn islands(&self, call_view: Option<AnyElement>) -> impl IntoElement {
473 h_flex()
474 .flex_1()
475 .min_h_0()
476 .w_full()
477 .gap(px(ISLAND_GAP))
478 .pr(px(ISLAND_GAP))
479 .py(px(ISLAND_GAP))
480 .child(
481 h_flex()
482 .h_full()
483 .flex_none()
484 .child(self.rail.clone())
485 .child(
486 self.sidebar
487 .clone()
488 .cached(StyleRefinement::default().w(px(SIDEBAR_WIDTH)).h_full().flex_none()),
489 ),
490 )
491 .child(match call_view {
492 Some(call_view) => call_view,
493 None => self
494 .conversation
495 .clone()
496 .cached(
497 StyleRefinement::default()
498 .flex_1()
499 .min_w_0()
500 .h_full()
501 .rounded(px(10.))
502 .bg(theme::surface())
503 .overflow_hidden(),
504 )
505 .into_any_element(),
506 })
507 }
508}
509
510impl Render for AppShell {
511 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
512 if let Some(dialog) = self
513 .state
514 .read(cx)
515 .task_dialog
516 .as_ref()
517 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
518 {
519 self.state.update(cx, |state, cx| {
520 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
521 });
522 }
523 if let Some(me) = self.state.read(cx).directory.me.clone() {
524 let app = self.state.clone();
525 cx.defer(move |cx| {
526 app.update(cx, |state, cx| {
527 state.request_avatars(vec![me.user_id.clone()], cx);
528 state.request_presence(vec![me.user_id], cx);
529 });
530 });
531 }
532 let panels = self.switcher.is_some()
533 || self.forward_dialog.is_some()
534 || self.status_dialog.is_some()
535 || self.shortcuts_dialog.is_some()
536 || self.profile_card.is_some()
537 || {
538 let state = self.state.read(cx);
539 state.task_dialog.is_some() || state.status_menu_open
540 };
541 Overlays::update(cx, |overlays| overlays.panels = panels);
542 let chat_thread = self.state.read(cx).call_chat_thread();
543 self.sync_call_chat(chat_thread, window, cx);
544 let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone());
545 let state = self.state.read(cx);
546 let call_view = render_call_view(&self.state, state, chat_panel).filter(|_| state.viewing_call());
547 let call_mini = render_call_mini(&self.state, state);
548 let stage_overlay = render_stage_overlay(&self.state, state);
549 let status = render_status_bar(
550 state,
551 &self.update,
552 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
553 cx.listener(|this, _, _, cx| {
554 this.state
555 .update(cx, |state, cx| state.run_notice_action(cx))
556 }),
557 );
558 let title_bar = render_title_bar(
559 own_status_button(&self.state, state),
560 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
561 );
562 div()
563 .id("app-shell")
564 .key_context("AppShell")
565 .track_focus(&self.focus_handle)
566 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
567 this.toggle_switcher("", window, cx);
568 }))
569 .on_action(cx.listener(|this, _: &NewChat, _, cx| {
570 this.state.update(cx, |state, cx| state.start_new_chat(cx));
571 }))
572 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
573 this.state.update(cx, |state, cx| state.toggle_call_mute(cx));
574 }))
575 .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| {
576 this.toggle_shortcuts_dialog(window, cx);
577 }))
578 .on_action(cx.listener(|this, _: &OpenActivity, _, cx| {
579 this.sidebar.update(cx, |sidebar, cx| sidebar.show_activity(cx));
580 }))
581 .on_action(cx.listener(|this, _: &OpenSaved, _, cx| {
582 this.sidebar.update(cx, |sidebar, cx| sidebar.show_saved(cx));
583 }))
584 .on_action(cx.listener(|this, _: &OpenChatsTab, _, cx| {
585 this.sidebar.update(cx, |sidebar, cx| sidebar.show_chats(cx));
586 }))
587 .on_action(cx.listener(|this, _: &OpenChannelsTab, _, cx| {
588 this.sidebar.update(cx, |sidebar, cx| sidebar.show_channels(cx));
589 }))
590 .on_action(cx.listener(|this, _: &OpenCallsTab, _, cx| {
591 this.sidebar.update(cx, |sidebar, cx| sidebar.show_calls(cx));
592 }))
593 .on_action(cx.listener(|this, _: &PreviousConversation, _, cx| {
594 this.sidebar
595 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Previous, cx));
596 }))
597 .on_action(cx.listener(|this, _: &NextConversation, _, cx| {
598 this.sidebar
599 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx));
600 }))
601 .on_action(cx.listener(|this, _: &AcceptCall, _, cx| {
602 this.state.update(cx, |state, cx| state.accept_ringing_call(cx));
603 }))
604 .on_action(cx.listener(|this, _: &DeclineCall, _, cx| {
605 this.state.update(cx, |state, cx| state.decline_ringing_call(cx));
606 }))
607 .on_action(cx.listener(|this, _: &HangUpCall, _, cx| {
608 this.state.update(cx, |state, cx| state.leave_call(cx));
609 }))
610 .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| {
611 this.state.update(cx, |state, cx| state.toggle_call_camera(cx));
612 }))
613 .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| {
614 this.state.update(cx, |state, cx| state.toggle_call_share(cx));
615 }))
616 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
617 this.conversation.update(cx, |conversation, cx| {
618 conversation.reply_to_hovered(window, cx)
619 });
620 }))
621 .relative()
622 .size_full()
623 .bg(theme::background())
624 .text_color(theme::text())
625 .text_size(px(14.))
626 .child(crate::frame_log::probe("first"))
627 .child(
628 v_flex()
629 .size_full()
630 .child(title_bar)
631 .child(self.islands(call_view))
632 .child(status),
633 )
634 .children(call_mini)
635 .children(stage_overlay)
636 .children(self.switcher.clone())
637 .children(render_task_dialog(self.state.read(cx), cx))
638 .children(self.forward_dialog.clone())
639 .children(self.status_dialog.clone())
640 .children(self.shortcuts_dialog.clone())
641 .children(self.profile_card.clone())
642 .child(crate::frame_log::probe("last"))
643 }
644}