| 1 | #![cfg_attr( |
| 2 | all(not(debug_assertions), target_os = "windows"), |
| 3 | windows_subsystem = "windows" |
| 4 | )] |
| 5 | |
| 6 | mod activity; |
| 7 | mod agent_api; |
| 8 | mod agents; |
| 9 | mod app_state; |
| 10 | mod assets; |
| 11 | mod avatar_image; |
| 12 | mod backend; |
| 13 | mod call; |
| 14 | mod card_actions; |
| 15 | mod card_inputs; |
| 16 | mod card_refresh; |
| 17 | mod card_state; |
| 18 | mod channel_files; |
| 19 | mod chat_actions; |
| 20 | mod crash_log; |
| 21 | mod data; |
| 22 | mod demo; |
| 23 | mod demo_cards; |
| 24 | mod demo_chart_cards; |
| 25 | mod demo_gifs; |
| 26 | mod demo_input_cards; |
| 27 | mod demo_library; |
| 28 | mod demo_profiles; |
| 29 | mod demo_recap; |
| 30 | mod diagnostics; |
| 31 | mod downloads; |
| 32 | mod emoji; |
| 33 | mod format; |
| 34 | mod frame_log; |
| 35 | mod fuzzy; |
| 36 | mod gifs; |
| 37 | mod image_view_gallery; |
| 38 | mod image_view_math; |
| 39 | #[cfg(test)] |
| 40 | mod layout_guard; |
| 41 | mod local_previews; |
| 42 | mod message_actions; |
| 43 | mod notice; |
| 44 | mod notify; |
| 45 | mod outbox; |
| 46 | mod own_status; |
| 47 | mod pending_rows; |
| 48 | mod people; |
| 49 | mod presence_link; |
| 50 | mod profile_model; |
| 51 | mod profile_state; |
| 52 | mod reaction_model; |
| 53 | mod read_state; |
| 54 | mod recap; |
| 55 | mod remote_image; |
| 56 | mod render; |
| 57 | mod rows; |
| 58 | mod runtime; |
| 59 | mod schedule_time; |
| 60 | mod scheduled; |
| 61 | mod scheduled_rows; |
| 62 | mod section_actions; |
| 63 | mod sidebar_model; |
| 64 | mod single_instance; |
| 65 | mod stickers; |
| 66 | mod stored_outgoing; |
| 67 | mod task_dialog; |
| 68 | mod theme; |
| 69 | mod translation; |
| 70 | mod translation_actions; |
| 71 | mod typing; |
| 72 | mod updater; |
| 73 | mod user_activity; |
| 74 | mod views; |
| 75 | |
| 76 | use std::path::PathBuf; |
| 77 | use std::sync::Arc; |
| 78 | use std::time::Duration; |
| 79 | |
| 80 | use directories::ProjectDirs; |
| 81 | use gpui_kit::component::TitleBar; |
| 82 | use gpui_kit::*; |
| 83 | use store::Store; |
| 84 | |
| 85 | use app_state::{AppState, Mode}; |
| 86 | use views::shell::{AppShell, OpenTarget, Startup, bind_keys}; |
| 87 | |
| 88 | pub const APP_NAME: &str = "Rusty Teams"; |
| 89 | const HTTP_USER_AGENT: &str = "teams-linux"; |
| 90 | const WINDOW_WIDTH: f32 = 1240.; |
| 91 | const WINDOW_HEIGHT: f32 = 820.; |
| 92 | const WINDOW_MIN_WIDTH: f32 = 640.; |
| 93 | const WINDOW_MIN_HEIGHT: f32 = 480.; |
| 94 | const ENDPOINT_UNREGISTER_TIMEOUT: Duration = Duration::from_millis(800); |
| 95 | const SINGLE_INSTANCE_NAME: &str = "rusty-teams"; |
| 96 | const DATABASE_FILE: &str = "cache.sqlite3"; |
| 97 | #[cfg(windows)] |
| 98 | const WEBVIEW_FOLDER: &str = "WebView2"; |
| 99 | |
| 100 | #[derive(Default)] |
| 101 | struct Arguments { |
| 102 | demo: bool, |
| 103 | read_only: bool, |
| 104 | database: Option<PathBuf>, |
| 105 | endpoint: Option<String>, |
| 106 | open_target: Option<OpenTarget>, |
| 107 | switcher_query: Option<String>, |
| 108 | composer_text: Option<String>, |
| 109 | reply_to_last: bool, |
| 110 | channels_tab: bool, |
| 111 | demo_sync: Option<Duration>, |
| 112 | } |
| 113 | |
| 114 | fn parse_arguments() -> Arguments { |
| 115 | let mut arguments = Arguments::default(); |
| 116 | let mut input = std::env::args().skip(1); |
| 117 | while let Some(flag) = input.next() { |
| 118 | match flag.as_str() { |
| 119 | "--demo" => arguments.demo = true, |
| 120 | "--read-only" => arguments.read_only = true, |
| 121 | "--switcher" => { |
| 122 | arguments.switcher_query.get_or_insert_with(String::new); |
| 123 | } |
| 124 | "--query" => arguments.switcher_query = input.next(), |
| 125 | "--type" => arguments.composer_text = input.next(), |
| 126 | "--reply-last" => arguments.reply_to_last = true, |
| 127 | "--channels-tab" => arguments.channels_tab = true, |
| 128 | "--demo-sync" => { |
| 129 | arguments.demo_sync = input |
| 130 | .next() |
| 131 | .and_then(|value| value.parse().ok()) |
| 132 | .map(Duration::from_secs_f32) |
| 133 | } |
| 134 | "--database" => arguments.database = input.next().map(PathBuf::from), |
| 135 | "--endpoint" => arguments.endpoint = input.next(), |
| 136 | "--open" => { |
| 137 | arguments.open_target = input |
| 138 | .next() |
| 139 | .and_then(|value| value.parse().ok()) |
| 140 | .map(OpenTarget::Chat) |
| 141 | } |
| 142 | "--open-channel" => { |
| 143 | arguments.open_target = input |
| 144 | .next() |
| 145 | .and_then(|value| value.parse().ok()) |
| 146 | .map(OpenTarget::Channel) |
| 147 | } |
| 148 | _ => {} |
| 149 | } |
| 150 | } |
| 151 | arguments |
| 152 | } |
| 153 | |
| 154 | fn data_path(name: &str) -> PathBuf { |
| 155 | ProjectDirs::from("", "", store::DATA_DIR_NAME) |
| 156 | .map(|directories| directories.data_local_dir().join(name)) |
| 157 | .unwrap_or_else(|| PathBuf::from(name)) |
| 158 | } |
| 159 | |
| 160 | fn transport( |
| 161 | endpoint: Option<&str>, |
| 162 | database: Option<&std::path::Path>, |
| 163 | ) -> Arc<dyn session::Transport> { |
| 164 | #[cfg(windows)] |
| 165 | if endpoint.is_none() { |
| 166 | let user_data_folder = match database.and_then(std::path::Path::parent) { |
| 167 | Some(directory) => directory.join(WEBVIEW_FOLDER), |
| 168 | None => data_path(WEBVIEW_FOLDER), |
| 169 | }; |
| 170 | let transport = webview::start(webview::HostConfig { |
| 171 | user_data_folder, |
| 172 | window_title: format!("{APP_NAME} - Sign in"), |
| 173 | }); |
| 174 | task_dialog::install_host(transport.clone()); |
| 175 | return transport; |
| 176 | } |
| 177 | #[cfg(not(windows))] |
| 178 | let _ = database; |
| 179 | Arc::new(session::CdpTransport::new( |
| 180 | endpoint.unwrap_or(session::DEFAULT_ENDPOINT), |
| 181 | )) |
| 182 | } |
| 183 | |
| 184 | const STORE_OPEN_ATTEMPTS: u32 = 20; |
| 185 | const STORE_OPEN_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500); |
| 186 | |
| 187 | fn open_store(path: &std::path::Path) -> Store { |
| 188 | for _ in 1..STORE_OPEN_ATTEMPTS { |
| 189 | match Store::open(path) { |
| 190 | Ok(store) => return store, |
| 191 | Err(_) => std::thread::sleep(STORE_OPEN_INTERVAL), |
| 192 | } |
| 193 | } |
| 194 | Store::open(path).expect("cache database") |
| 195 | } |
| 196 | |
| 197 | fn main() { |
| 198 | crash_log::install(data_path(crash_log::CRASH_LOG_FILE)); |
| 199 | diagnostics::install(data_path(diagnostics::DIAGNOSTICS_FILE)); |
| 200 | let arguments = parse_arguments(); |
| 201 | if !arguments.demo && arguments.database.is_none() { |
| 202 | let relaunch_value = std::env::var(updater::RELAUNCH_ENV).ok(); |
| 203 | if let Some(value) = &relaunch_value { |
| 204 | updater::wait_for_previous_exit(value); |
| 205 | } |
| 206 | let relaunched = relaunch_value.is_some(); |
| 207 | if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched) |
| 208 | == single_instance::Claim::Forwarded |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | #[cfg(windows)] |
| 214 | updater::clean_up_old_binary(); |
| 215 | let mode = Mode { |
| 216 | demo: arguments.demo, |
| 217 | read_only: arguments.read_only, |
| 218 | demo_sync: arguments.demo_sync, |
| 219 | }; |
| 220 | let store = if arguments.demo { |
| 221 | let store = Store::open_in_memory().expect("in-memory store"); |
| 222 | demo::seed(&store); |
| 223 | store |
| 224 | } else { |
| 225 | let path = arguments |
| 226 | .database |
| 227 | .clone() |
| 228 | .unwrap_or_else(|| data_path(DATABASE_FILE)); |
| 229 | open_store(&path) |
| 230 | }; |
| 231 | let store = Arc::new(store); |
| 232 | let user_input = user_activity::UserInput::new(); |
| 233 | let receiver = (!arguments.demo).then(|| { |
| 234 | let transport = transport(arguments.endpoint.as_deref(), arguments.database.as_deref()); |
| 235 | let activity = user_activity::track(user_input.clone()); |
| 236 | backend::start(store.clone(), transport, !arguments.read_only, activity) |
| 237 | }); |
| 238 | |
| 239 | let mut application = gpui_kit::application(); |
| 240 | if let Ok(http_client) = reqwest_client::ReqwestClient::user_agent(HTTP_USER_AGENT) { |
| 241 | application = application.with_http_client(Arc::new(http_client)); |
| 242 | } |
| 243 | |
| 244 | application.with_assets(assets::AppAssets).run(move |cx| { |
| 245 | gpui_kit::init(cx); |
| 246 | cx.on_app_quit(|_| { |
| 247 | updater::exit_after_grace(); |
| 248 | async {} |
| 249 | }) |
| 250 | .detach(); |
| 251 | theme::load_fonts(cx); |
| 252 | theme::apply(cx); |
| 253 | bind_keys(cx); |
| 254 | let state = cx.new(|_| { |
| 255 | let mut state = AppState::new(store, mode); |
| 256 | state.start_on_channels = arguments.channels_tab; |
| 257 | state |
| 258 | }); |
| 259 | views::shell::install_globals(&state, user_input, cx); |
| 260 | agent_api::init(state.read(cx).store.clone(), arguments.demo, cx); |
| 261 | agents::install(state.clone(), cx); |
| 262 | if arguments.demo { |
| 263 | demo::start_session(&state, cx); |
| 264 | } |
| 265 | if let Some(mut receiver) = receiver { |
| 266 | let state = state.clone(); |
| 267 | cx.spawn(async move |cx| { |
| 268 | while let Some(event) = receiver.recv().await { |
| 269 | state.update(cx, |state, cx| state.apply(event, cx)); |
| 270 | } |
| 271 | }) |
| 272 | .detach(); |
| 273 | } |
| 274 | |
| 275 | let bounds = Bounds::centered(None, size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), cx); |
| 276 | let mut options = TitleBar::window_options(); |
| 277 | options.window_bounds = Some(WindowBounds::Windowed(bounds)); |
| 278 | options.window_min_size = Some(size(px(WINDOW_MIN_WIDTH), px(WINDOW_MIN_HEIGHT))); |
| 279 | if let Some(titlebar) = options.titlebar.as_mut() { |
| 280 | titlebar.title = Some(APP_NAME.into()); |
| 281 | } |
| 282 | let open_target = arguments.open_target; |
| 283 | let startup = Startup { |
| 284 | switcher_query: arguments.switcher_query, |
| 285 | composer_text: arguments.composer_text, |
| 286 | reply_to_last: arguments.reply_to_last, |
| 287 | }; |
| 288 | let (main_window, _) = gpui_kit::open_window(options, cx, move |window, cx| { |
| 289 | cx.new(|cx| AppShell::new(state, open_target, startup, window, cx)) |
| 290 | }) |
| 291 | .expect("failed to open window"); |
| 292 | let main_window_id = main_window.window_id(); |
| 293 | cx.on_window_closed(move |cx, closed| { |
| 294 | if closed == main_window_id { |
| 295 | cx.quit(); |
| 296 | } |
| 297 | }) |
| 298 | .detach(); |
| 299 | cx.activate(true); |
| 300 | }); |
| 301 | presence_link::unregister_on_exit(ENDPOINT_UNREGISTER_TIMEOUT); |
| 302 | #[cfg(windows)] |
| 303 | updater::exit_now(); |
| 304 | } |