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