BlueGit

ict rusty-teams

Files

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

shell.rs

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