BlueGit

ict rusty-teams

Files

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

shell.rs

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