BlueGit

ict rusty-teams

Files

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

shell.rs

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