BlueGit

ict rusty-teams

Files

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

shell.rs

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