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