BlueGit

ict rusty-teams

Files

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

shell.rs

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