BlueGit

ict rusty-teams

Files

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

shell.rs

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