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