BlueGit

ict rusty-teams

Files

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

shell.rs

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