BlueGit

ict rusty-teams

Files

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

shell.rs

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