BlueGit

ict rusty-teams

Files

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

shell.rs

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