BlueGit

ict rusty-teams

Files

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

shell.rs

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