BlueGit

ict rusty-teams

Files

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

shell.rs

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