BlueGit

ict rusty-teams

Files

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

shell.rs

shell.rs 20.8 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::sidebar::SidebarView;
14use super::status_bar::render_status_bar;
15use super::status_menu::own_status_button;
16use super::status_message_dialog::{StatusMessageDialog, StatusMessageDialogEvent};
17use super::switcher::{Switcher, SwitcherEvent, candidates_from};
18use super::title_bar::render_title_bar;
19use crate::activity::ActivityCenter;
20use crate::app_state::{AppEvent, AppState, Selection};
21use crate::embedded_web::{Overlays, RootFocus};
22use crate::notice::NoticeAction;
23use crate::notify::{NotificationCenter, selection_for};
24use crate::theme;
25use crate::updater::{self, IdleInputs, UpdateStatus};
26
27actions!(teams, [OpenSwitcher, NewChat, ToggleCallMute]);
28
29pub fn bind_keys(cx: &mut App) {
30 super::composer::bind_keys(cx);
31 cx.bind_keys([
32 KeyBinding::new("ctrl-k", OpenSwitcher, None),
33 KeyBinding::new("ctrl-n", NewChat, None),
34 KeyBinding::new("alt-r", ReplyToHovered, None),
35 KeyBinding::new("ctrl-shift-m", ToggleCallMute, None),
36 #[cfg(target_os = "macos")]
37 KeyBinding::new("cmd-k", OpenSwitcher, None),
38 #[cfg(target_os = "macos")]
39 KeyBinding::new("cmd-n", NewChat, None),
40 ]);
41}
42
43#[derive(Debug, Clone, Copy)]
44pub enum OpenTarget {
45 Chat(usize),
46 Channel(usize),
47}
48
49#[derive(Default)]
50pub struct Startup {
51 pub switcher_query: Option<String>,
52 pub composer_text: Option<String>,
53 pub reply_to_last: bool,
54}
55
56pub struct AppShell {
57 state: Entity<AppState>,
58 sidebar: Entity<SidebarView>,
59 conversation: 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 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 pub fn new(
78 state: Entity<AppState>,
79 open_target: Option<OpenTarget>,
80 startup: Startup,
81 window: &mut Window,
82 cx: &mut Context<Self>,
83 ) -> Self {
84 let sidebar = cx.new(|cx| SidebarView::new(state.clone(), cx));
85 let conversation = cx.new(|cx| ConversationView::new(state.clone(), window, cx));
86 let subscription =
87 cx.subscribe_in(&state, window, |this, _, event: &AppEvent, window, cx| {
88 if matches!(event, AppEvent::Sidebar | AppEvent::Status) {
89 this.apply_open_target(cx);
90 }
91 if matches!(event, AppEvent::Forward) {
92 this.open_forward_dialog(window, cx);
93 }
94 if matches!(event, AppEvent::StatusMessage) {
95 this.open_status_dialog(window, cx);
96 }
97 if matches!(event, AppEvent::NotificationSettings) {
98 this.notifications
99 .update(cx, |center, cx| center.open_settings(cx));
100 }
101 if matches!(event, AppEvent::Profile) {
102 this.open_profile_card(window, cx);
103 }
104 cx.notify();
105 });
106 let focus_handle = cx.focus_handle();
107 window.focus(&focus_handle, cx);
108 cx.set_global(RootFocus(focus_handle.clone()));
109 let notifications = cx.new(|cx| NotificationCenter::new(state.clone(), window, cx));
110 let activity = cx.new(|cx| ActivityCenter::new(state.clone(), window, cx));
111 let activity_observation = cx.observe(&activity, |_, _, cx| cx.notify());
112 let escape_observation = cx.observe_keystrokes(|this, event, _, cx| {
113 if event.keystroke.key == "escape" {
114 this.state.update(cx, |state, cx| state.leave_stage_fullscreen(cx));
115 }
116 });
117 let closing = notifications.clone();
118 window.on_window_should_close(cx, move |_, cx| !closing.read(cx).intercept_close(cx));
119 let mut shell = AppShell {
120 state,
121 sidebar,
122 conversation,
123 switcher: None,
124 notifications,
125 activity,
126 activity_panel: None,
127 saved_panel: None,
128 forward_dialog: None,
129 status_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 open_profile_card(&mut self, window: &mut Window, cx: &mut Context<Self>) {
393 let Some(request) = self
394 .state
395 .update(cx, |state, _| state.take_profile_request())
396 else {
397 return;
398 };
399 if let Some(card) = &self.profile_card {
400 card.update(cx, |card, cx| {
401 card.move_to(request.user_id, request.anchor, cx)
402 });
403 return;
404 }
405 let app = self.state.clone();
406 let card = cx.new(|cx| ProfileCard::new(app, request.user_id, request.anchor, window, cx));
407 cx.subscribe_in(
408 &card,
409 window,
410 |this, _, _: &ProfileCardEvent, window, cx| {
411 this.profile_card = None;
412 window.focus(&this.focus_handle, cx);
413 cx.notify();
414 },
415 )
416 .detach();
417 self.profile_card = Some(card);
418 cx.notify();
419 }
420
421 fn open_forward_dialog(&mut self, window: &mut Window, cx: &mut Context<Self>) {
422 let Some(source) = self
423 .state
424 .update(cx, |state, _| state.take_forward_request())
425 else {
426 return;
427 };
428 self.switcher = None;
429 self.activity_panel = None;
430 self.saved_panel = None;
431 let candidates = candidates_from(self.state.read(cx));
432 let app = self.state.clone();
433 let dialog = cx.new(|cx| ForwardDialog::new(app, source, candidates, window, cx));
434 cx.subscribe_in(
435 &dialog,
436 window,
437 |this, _, event: &ForwardDialogEvent, window, cx| {
438 this.forward_dialog = None;
439 window.focus(&this.focus_handle, cx);
440 if let ForwardDialogEvent::Sent { target, title } = event {
441 let target = target.clone();
442 let open = NoticeAction::new("Open", move |state, cx| {
443 state.select(target.clone(), cx)
444 });
445 this.state.update(cx, |state, cx| {
446 state.raise_notice(format!("Forwarded to {title}"), Some(open), cx)
447 });
448 }
449 cx.notify();
450 },
451 )
452 .detach();
453 self.forward_dialog = Some(dialog);
454 cx.notify();
455 }
456}
457
458impl Render for AppShell {
459 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
460 if let Some(dialog) = self
461 .state
462 .read(cx)
463 .task_dialog
464 .as_ref()
465 .map(|dialog| (dialog.scope.clone(), dialog.card.clone()))
466 {
467 self.state.update(cx, |state, cx| {
468 state.ensure_card_inputs(&dialog.0, &dialog.1, window, cx)
469 });
470 }
471 if let Some(me) = self.state.read(cx).directory.me.clone() {
472 let app = self.state.clone();
473 cx.defer(move |cx| {
474 app.update(cx, |state, cx| {
475 state.request_avatars(vec![me.user_id.clone()], cx);
476 state.request_presence(vec![me.user_id], cx);
477 });
478 });
479 }
480 let panels = self.switcher.is_some()
481 || self.activity_panel.is_some()
482 || self.saved_panel.is_some()
483 || self.forward_dialog.is_some()
484 || self.status_dialog.is_some()
485 || self.profile_card.is_some()
486 || {
487 let state = self.state.read(cx);
488 state.task_dialog.is_some() || state.status_menu_open
489 };
490 Overlays::update(cx, |overlays| overlays.panels = panels);
491 let state = self.state.read(cx);
492 let call_view = render_call_view(&self.state, state).filter(|_| state.viewing_call());
493 let call_mini = render_call_mini(&self.state, state);
494 let stage_overlay = render_stage_overlay(&self.state, state);
495 let status = render_status_bar(
496 state,
497 &self.update,
498 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
499 cx.listener(|this, _, _, cx| {
500 this.state
501 .update(cx, |state, cx| state.run_notice_action(cx))
502 }),
503 );
504 let title_bar = render_title_bar(
505 own_status_button(&self.state, state),
506 self.activity.read(cx).feed().unread_count(),
507 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
508 cx.listener(|this, _, window, cx| this.toggle_saved_panel(window, cx)),
509 cx.listener(|this, _, window, cx| this.toggle_activity_panel(window, cx)),
510 );
511 div()
512 .id("app-shell")
513 .key_context("AppShell")
514 .track_focus(&self.focus_handle)
515 .on_action(cx.listener(|this, _: &OpenSwitcher, window, cx| {
516 this.toggle_switcher("", window, cx);
517 }))
518 .on_action(cx.listener(|this, _: &NewChat, _, cx| {
519 this.state.update(cx, |state, cx| state.start_new_chat(cx));
520 }))
521 .on_action(cx.listener(|this, _: &ToggleCallMute, _, cx| {
522 this.state.update(cx, |state, cx| state.toggle_call_mute(cx));
523 }))
524 .on_action(cx.listener(|this, _: &ReplyToHovered, window, cx| {
525 this.conversation.update(cx, |conversation, cx| {
526 conversation.reply_to_hovered(window, cx)
527 });
528 }))
529 .relative()
530 .size_full()
531 .bg(theme::background())
532 .text_color(theme::text())
533 .text_size(px(14.))
534 .child(crate::frame_log::probe("first"))
535 .child(
536 v_flex()
537 .size_full()
538 .child(title_bar)
539 .child(
540 h_flex()
541 .flex_1()
542 .min_h_0()
543 .w_full()
544 .child(self.sidebar.clone())
545 .child(match call_view {
546 Some(call_view) => call_view,
547 None => self.conversation.clone().into_any_element(),
548 }),
549 )
550 .child(status),
551 )
552 .children(call_mini)
553 .children(stage_overlay)
554 .children(self.activity_panel.clone())
555 .children(self.saved_panel.clone())
556 .children(self.switcher.clone())
557 .children(render_task_dialog(self.state.read(cx), cx))
558 .children(self.forward_dialog.clone())
559 .children(self.status_dialog.clone())
560 .children(self.profile_card.clone())
561 .child(crate::frame_log::probe("last"))
562 }
563}