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::{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(_)) {
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 }
210 let conversation = self.conversation.read(cx);
211 let idle = updater::is_idle(IdleInputs {
212 window_active: window.is_window_active(),
213 composer_empty: conversation.composer_is_empty(cx),
214 send_in_flight: conversation.send_in_flight(),
215 });
216 if idle {
217 self.restart_into_update(cx);
218 }
219 cx.notify();
220 }
221
222 fn restart_into_update(&mut self, cx: &mut Context<Self>) {
223 let Some(paths) = updater::current_paths() else {
224 return;
225 };
226 match updater::install_and_relaunch(&paths) {
227 Ok(()) => cx.quit(),
228 Err(message) => self.update = UpdateStatus::Failed(message),
229 }
230 cx.notify();
231 }
232
233 fn apply_open_target(&mut self, cx: &mut Context<Self>) {
234 let Some(target) = self.open_target else {
235 return;
236 };
237 let sidebar = &self.state.read(cx).sidebar;
238 let selection = match target {
239 OpenTarget::Chat(index) => sidebar
240 .chats
241 .get(index)
242 .map(|chat| Selection::Chat(chat.id.clone())),
243 OpenTarget::Channel(index) => sidebar
244 .teams
245 .iter()
246 .flat_map(|team| team.channels.iter())
247 .nth(index)
248 .map(|channel| Selection::Channel(channel.id.clone())),
249 };
250 if let Some(selection) = selection {
251 self.open_target = None;
252 self.state
253 .update(cx, |state, cx| state.select(selection, cx));
254 }
255 }
256
257 fn toggle_switcher(
258 &mut self,
259 initial_query: &str,
260 window: &mut Window,
261 cx: &mut Context<Self>,
262 ) {
263 if self.switcher.take().is_some() {
264 window.focus(&self.focus_handle, cx);
265 cx.notify();
266 return;
267 }
268 self.activity_panel = None;
269 self.saved_panel = None;
270 let candidates = candidates_from(self.state.read(cx));
271 let app = self.state.clone();
272 let switcher = cx.new(|cx| Switcher::new(app, candidates, initial_query, window, cx));
273 cx.subscribe_in(
274 &switcher,
275 window,
276 |this, _, event: &SwitcherEvent, window, cx| {
277 match event {
278 SwitcherEvent::Pick(selection) => {
279 let selection = selection.clone();
280 this.state
281 .update(cx, |state, cx| state.select(selection, cx));
282 }
283 SwitcherEvent::PickMessage {
284 selection,
285 message_id,
286 } => {
287 let (selection, message_id) = (selection.clone(), message_id.clone());
288 this.state.update(cx, |state, cx| {
289 state.jump_to_message(selection, message_id, cx)
290 });
291 }
292 SwitcherEvent::Close => {}
293 }
294 this.switcher = None;
295 window.focus(&this.focus_handle, cx);
296 cx.notify();
297 },
298 )
299 .detach();
300 self.switcher = Some(switcher);
301 cx.notify();
302 }
303
304 fn toggle_activity_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) {
305 if self.activity_panel.take().is_some() {
306 window.focus(&self.focus_handle, cx);
307 cx.notify();
308 return;
309 }
310 self.switcher = None;
311 self.saved_panel = None;
312 let (app, activity) = (self.state.clone(), self.activity.clone());
313 let panel = cx.new(|cx| ActivityPanel::new(app, activity, window, cx));
314 cx.subscribe_in(
315 &panel,
316 window,
317 |this, _, event: &ActivityPanelEvent, window, cx| {
318 this.activity_panel = None;
319 window.focus(&this.focus_handle, cx);
320 match event {
321 ActivityPanelEvent::Close => {}
322 ActivityPanelEvent::OpenSettings => this
323 .notifications
324 .update(cx, |center, cx| center.open_settings(cx)),
325 ActivityPanelEvent::Open {
326 conversation_id,
327 message_id,
328 } => {
329 let selection =
330 selection_for(&this.state.read(cx).sidebar, conversation_id);
331 if let Some(selection) = selection {
332 let message_id = message_id.clone();
333 this.state.update(cx, |state, cx| {
334 if message_id.is_empty() {
335 state.select(selection, cx)
336 } else {
337 state.jump_to_message(selection, message_id, cx)
338 }
339 });
340 }
341 }
342 }
343 cx.notify();
344 },
345 )
346 .detach();
347 self.activity_panel = Some(panel);
348 cx.notify();
349 }
350 fn toggle_saved_panel(&mut self, window: &mut Window, cx: &mut Context<Self>) {
351 if self.saved_panel.take().is_some() {
352 window.focus(&self.focus_handle, cx);
353 cx.notify();
354 return;
355 }
356 self.switcher = None;
357 self.activity_panel = None;
358 let app = self.state.clone();
359 let panel = cx.new(|cx| SavedPanel::new(app, window, cx));
360 cx.subscribe_in(
361 &panel,
362 window,
363 |this, _, event: &SavedPanelEvent, window, cx| {
364 this.saved_panel = None;
365 window.focus(&this.focus_handle, cx);
366 if let SavedPanelEvent::Open {
367 conversation_id,
368 message_id,
369 } = event
370 {
371 let selection = selection_for(&this.state.read(cx).sidebar, conversation_id);
372 if let Some(selection) = selection {
373 let message_id = message_id.clone();
374 this.state.update(cx, |state, cx| {
375 state.jump_to_message(selection, message_id, cx)
376 });
377 }
378 }
379 cx.notify();
380 },
381 )
382 .detach();
383 self.saved_panel = Some(panel);
384 cx.notify();
385 }
386
387 fn open_status_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
388 if !self
389 .state
390 .update(cx, |state, _| state.take_status_message_request())
391 {
392 return;
393 }
394 self.switcher = None;
395 self.activity_panel = None;
396 self.saved_panel = None;
397 let app = self.state.clone();
398 let dialog = cx.new(|cx| StatusMessageDialog::new(app, window, cx));
399 cx.subscribe_in(
400 &dialog,
401 window,
402 |this, _, _: &StatusMessageDialogEvent, window, cx| {
403 this.status_dialog = None;
404 window.focus(&this.focus_handle, cx);
405 cx.notify();
406 },
407 )
408 .detach();
409 self.status_dialog = Some(dialog);
410 cx.notify();
411 }
412
413 fn toggle_shortcuts_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
414 if self.shortcuts_dialog.take().is_some() {
415 window.focus(&self.focus_handle, cx);
416 cx.notify();
417 return;
418 }
419 self.switcher = None;
420 self.activity_panel = None;
421 self.saved_panel = None;
422 let dialog = cx.new(|cx| ShortcutsDialog::new(window, cx));
423 cx.subscribe_in(
424 &dialog,
425 window,
426 |this, _, _: &ShortcutsDialogEvent, window, cx| {
427 this.shortcuts_dialog = None;
428 window.focus(&this.focus_handle, cx);
429 cx.notify();
430 },
431 )
432 .detach();
433 self.shortcuts_dialog = Some(dialog);
434 cx.notify();
435 }
436
437 fn open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) {
438 let Some(request) = self
439 .state
440 .update(cx, |state, _| state.take_profile_request())
441 else {
442 return;
443 };
444 if let Some(card) = &self.profile_card {
445 card.update(cx, |card, cx| {
446 card.move_to(request.user_id, request.anchor, cx)
447 });
448 return;
449 }
450 let app = self.state.clone();
451 let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx));
452 cx.subscribe_in(
453 &card,
454 window,
455 |this, _, _: &ProfileCardEvent, window, cx| {
456 this.profile_card = None;
457 window.focus(&this.focus_handle, cx);
458 cx.notify();
459 },
460 )
461 .detach();
462 self.profile_card = Some(card);
463 cx.notify();
464 }
465
466 fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
467 let Some(source) = self
468 .state
469 .update(cx, |state, _| state.take_forward_request())
470 else {
471 return;
472 };
473 self.switcher = None;
474 self.activity_panel = None;
475 self.saved_panel = None;
476 let candidates = candidates_from(self.state.read(cx));
477 let app = self.state.clone();
478 let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx));
479 cx.subscribe_in(
480 &dialog,
481 window,
482 |this, _, event: &ForwardDialogEvent, window, cx| {
483 this.forward_dialog = None;
484 window.focus(&this.focus_handle, cx);
485 if let ForwardDialogEvent::Sent { target, title } = event {
486 let target = target.clone();
487 let open = NoticeAction::new("Open", move |state, cx| {
488 state.select(target.clone(), cx)
489 });
490 this.state.update(cx, |state, cx| {
491 state.raise_notice(format!("Forwarded to {title}"), Some(open), cx)
492 });
493 }
494 cx.notify();
495 },
496 )
497 .detach();
498 self.forward_dialog = Some(dialog);
499 cx.notify();
500 }
501}
502
503impl Render for AppShell {
504 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
505 if let Some(dialog) = self
506 .state
507 .read(cx)
508 .task_dialog
509 .as_ref()
510 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
511 {
512 self.state.update(cx, |state, cx| {
513 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
514 });
515 }
516 if let Some(me) = self.state.read(cx).directory.me.clone() {
517 let app = self.state.clone();
518 cx.defer(move |cx| {
519 app.update(cx, |state, cx| {
520 state.request_avatars(vec![me.user_id.clone()], cx);
521 state.request_presence(vec![me.user_id], cx);
522 });
523 });
524 }
525 let panels = self.switcher.is_some()
526 || self.activity_panel.is_some()
527 || self.saved_panel.is_some()
528 || self.forward_dialog.is_some()
529 || self.status_dialog.is_some()
530 || self.shortcuts_dialog.is_some()
531 || self.profile_card.is_some()
532 || {
533 let state = self.state.read(cx);
534 state.task_dialog.is_some() || state.status_menu_open
535 };
536 Overlays::update(cx, |overlays| overlays.panels = panels);
537 let chat_thread = self.state.read(cx).call_chat_thread();
538 self.sync_call_chat(chat_thread, window, cx);
539 let chat_panel = self.call_chat.as_ref().map(|(_, view)| view.clone());
540 let state = self.state.read(cx);
541 let call_view = render_call_view(&self.state, state, chat_panel).filter(|_| state.viewing_call());
542 let call_mini = render_call_mini(&self.state, state);
543 let stage_overlay = render_stage_overlay(&self.state, state);
544 let status = render_status_bar(
545 state,
546 &self.update,
547 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
548 cx.listener(|this, _, _, cx| {
549 this.state
550 .update(cx, |state, cx| state.run_notice_action(cx))
551 }),
552 );
553 let title_bar = render_title_bar(
554 own_status_button(&self.state, state),
555 self.activity.read(cx).feed().unread_count(),
556 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
557 cx.listener(|this, _, window, cx| this.toggle_saved_panel(window, cx)),
558 cx.listener(|this, _, window, cx| this.toggle_activity_panel(window, cx)),
559 );
560 div()
561 .id("app-shell")
562 .key_context("AppShell")
563 .track_focus(&self.focus_handle)
564 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
565 this.toggle_switcher("", window, cx);
566 }))
567 .on_action(cx.listener(|this, _: &NewChat, _, cx| {
568 this.state.update(cx, |state, cx| state.start_new_chat(cx));
569 }))
570 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
571 this.state.update(cx, |state, cx| state.toggle_call_mute(cx));
572 }))
573 .on_action(cx.listener(|this, _: &ShowShortcuts, window, cx| {
574 this.toggle_shortcuts_dialog(window, cx);
575 }))
576 .on_action(cx.listener(|this, _: &OpenActivity, window, cx| {
577 this.toggle_activity_panel(window, cx);
578 }))
579 .on_action(cx.listener(|this, _: &OpenSaved, window, cx| {
580 this.toggle_saved_panel(window, cx);
581 }))
582 .on_action(cx.listener(|this, _: &OpenChatsTab, _, cx| {
583 this.sidebar.update(cx, |sidebar, cx| sidebar.show_chats(cx));
584 }))
585 .on_action(cx.listener(|this, _: &OpenChannelsTab, _, cx| {
586 this.sidebar.update(cx, |sidebar, cx| sidebar.show_channels(cx));
587 }))
588 .on_action(cx.listener(|this, _: &OpenCallsTab, _, cx| {
589 this.sidebar.update(cx, |sidebar, cx| sidebar.show_calls(cx));
590 }))
591 .on_action(cx.listener(|this, _: &PreviousConversation, _, cx| {
592 this.sidebar
593 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Previous, cx));
594 }))
595 .on_action(cx.listener(|this, _: &NextConversation, _, cx| {
596 this.sidebar
597 .update(cx, |sidebar, cx| sidebar.select_adjacent(Step::Next, cx));
598 }))
599 .on_action(cx.listener(|this, _: &AcceptCall, _, cx| {
600 this.state.update(cx, |state, cx| state.accept_ringing_call(cx));
601 }))
602 .on_action(cx.listener(|this, _: &DeclineCall, _, cx| {
603 this.state.update(cx, |state, cx| state.decline_ringing_call(cx));
604 }))
605 .on_action(cx.listener(|this, _: &HangUpCall, _, cx| {
606 this.state.update(cx, |state, cx| state.leave_call(cx));
607 }))
608 .on_action(cx.listener(|this, _: &ToggleCallCamera, _, cx| {
609 this.state.update(cx, |state, cx| state.toggle_call_camera(cx));
610 }))
611 .on_action(cx.listener(|this, _: &ToggleCallShare, _, cx| {
612 this.state.update(cx, |state, cx| state.toggle_call_share(cx));
613 }))
614 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
615 this.conversation.update(cx, |conversation, cx| {
616 conversation.reply_to_hovered(window, cx)
617 });
618 }))
619 .relative()
620 .size_full()
621 .bg(theme::background())
622 .text_color(theme::text())
623 .text_size(px(14.))
624 .child(crate::frame_log::probe("first"))
625 .child(
626 v_flex()
627 .size_full()
628 .child(title_bar)
629 .child(
630 h_flex()
631 .flex_1()
632 .min_h_0()
633 .w_full()
634 .child(self.sidebar.clone().cached(StyleRefinement::default().w(px(SIDEBAR_WIDTH)).h_full().flex_none()))
635 .child(match call_view {
636 Some(call_view) => call_view,
637 None => self
638 .conversation
639 .clone()
640 .cached(StyleRefinement::default().flex_1().min_w_0().h_full())
641 .into_any_element(),
642 }),
643 )
644 .child(status),
645 )
646 .children(call_mini)
647 .children(stage_overlay)
648 .children(self.activity_panel.clone())
649 .children(self.saved_panel.clone())
650 .children(self.switcher.clone())
651 .children(render_task_dialog(self.state.read(cx), cx))
652 .children(self.forward_dialog.clone())
653 .children(self.status_dialog.clone())
654 .children(self.shortcuts_dialog.clone())
655 .children(self.profile_card.clone())
656 .child(crate::frame_log::probe("last"))
657 }
658}