BlueGit

ict rusty-teams

Files

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

shell.rs

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