| 1 | #![cfg_attr( |
| 2 | all(not(debug_assertions), target_os = "windows"), |
| 3 | windows_subsystem = "windows" |
| 4 | )] |
| 5 | |
| 6 | mod active_appearance; |
| 7 | mod active_locale; |
| 8 | mod agent_direct; |
| 9 | pub mod agents; |
| 10 | pub mod call; |
| 11 | mod i18n; |
| 12 | mod inline_video; |
| 13 | #[cfg(test)] |
| 14 | mod layout_guard; |
| 15 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 16 | mod linux_browser; |
| 17 | #[cfg(target_os = "linux")] |
| 18 | mod linux_install; |
| 19 | #[cfg(target_os = "linux")] |
| 20 | mod linux_update; |
| 21 | mod main_window; |
| 22 | pub mod notify; |
| 23 | mod settings; |
| 24 | mod views; |
| 25 | |
| 26 | pub use state::*; |
| 27 | |
| 28 | use std::path::PathBuf; |
| 29 | use std::sync::Arc; |
| 30 | use std::time::Duration; |
| 31 | |
| 32 | use directories::ProjectDirs; |
| 33 | use gpui_kit::component::TitleBar; |
| 34 | use gpui_kit::*; |
| 35 | use store::Store; |
| 36 | |
| 37 | use crate::app_state::{AppState, Mode}; |
| 38 | use crate::call::remote_command::{ |
| 39 | ADMIT_ALL_FLAG, CALL_FLAG, HANG_UP_FLAG, JOIN_FLAG, RemoteCommand, UNMUTE_FLAG, |
| 40 | }; |
| 41 | use views::shell::{AppShell, OpenTarget, Startup, bind_keys}; |
| 42 | |
| 43 | const LINUX_APP_ID: &str = "rusty-teams"; |
| 44 | const VERSION_MARKER: &str = concat!("teams-build:", env!("TEAMS_BUILD_VERSION")); |
| 45 | const HTTP_USER_AGENT: &str = "teams-linux"; |
| 46 | const WINDOW_WIDTH: f32 = 1240.; |
| 47 | const WINDOW_HEIGHT: f32 = 820.; |
| 48 | const WINDOW_MIN_WIDTH: f32 = 640.; |
| 49 | const WINDOW_MIN_HEIGHT: f32 = 480.; |
| 50 | const ENDPOINT_UNREGISTER_TIMEOUT: Duration = Duration::from_millis(800); |
| 51 | const SINGLE_INSTANCE_NAME: &str = "rusty-teams"; |
| 52 | const DATABASE_FILE: &str = "cache.sqlite3"; |
| 53 | #[cfg(windows)] |
| 54 | const WEBVIEW_FOLDER: &str = "WebView2"; |
| 55 | #[cfg(windows)] |
| 56 | const DEMO_WEBVIEW_FOLDER: &str = "rusty-teams-demo-WebView2"; |
| 57 | |
| 58 | #[derive(Default)] |
| 59 | struct Arguments { |
| 60 | demo: bool, |
| 61 | read_only: bool, |
| 62 | database: Option<PathBuf>, |
| 63 | endpoint: Option<String>, |
| 64 | open_target: Option<OpenTarget>, |
| 65 | switcher_query: Option<String>, |
| 66 | composer_text: Option<String>, |
| 67 | reply_to_last: bool, |
| 68 | channels_tab: bool, |
| 69 | autostart: bool, |
| 70 | demo_sync: Option<Duration>, |
| 71 | remote_command: Option<RemoteCommand>, |
| 72 | } |
| 73 | |
| 74 | fn parse_arguments() -> Arguments { |
| 75 | let mut arguments = Arguments::default(); |
| 76 | let mut input = std::env::args().skip(1); |
| 77 | while let Some(flag) = input.next() { |
| 78 | match flag.as_str() { |
| 79 | "--demo" => arguments.demo = true, |
| 80 | "--read-only" => arguments.read_only = true, |
| 81 | "--switcher" => { |
| 82 | arguments.switcher_query.get_or_insert_with(String::new); |
| 83 | } |
| 84 | "--query" => arguments.switcher_query = input.next(), |
| 85 | "--type" => arguments.composer_text = input.next(), |
| 86 | "--reply-last" => arguments.reply_to_last = true, |
| 87 | "--channels-tab" => arguments.channels_tab = true, |
| 88 | autostart::AUTOSTART_FLAG => arguments.autostart = true, |
| 89 | "--demo-sync" => { |
| 90 | arguments.demo_sync = input |
| 91 | .next() |
| 92 | .and_then(|value| value.parse().ok()) |
| 93 | .map(Duration::from_secs_f32) |
| 94 | } |
| 95 | JOIN_FLAG => arguments.remote_command = input.next().map(RemoteCommand::Join), |
| 96 | HANG_UP_FLAG => arguments.remote_command = Some(RemoteCommand::HangUp), |
| 97 | ADMIT_ALL_FLAG => arguments.remote_command = Some(RemoteCommand::AdmitAll), |
| 98 | UNMUTE_FLAG => arguments.remote_command = Some(RemoteCommand::Unmute), |
| 99 | CALL_FLAG => { |
| 100 | arguments.remote_command = |
| 101 | input.next().and_then(|text| RemoteCommand::decode(&text)) |
| 102 | } |
| 103 | "--database" => arguments.database = input.next().map(PathBuf::from), |
| 104 | "--endpoint" => arguments.endpoint = input.next(), |
| 105 | "--open" => { |
| 106 | arguments.open_target = input |
| 107 | .next() |
| 108 | .and_then(|value| value.parse().ok()) |
| 109 | .map(OpenTarget::Chat) |
| 110 | } |
| 111 | "--open-channel" => { |
| 112 | arguments.open_target = input |
| 113 | .next() |
| 114 | .and_then(|value| value.parse().ok()) |
| 115 | .map(OpenTarget::Channel) |
| 116 | } |
| 117 | _ => {} |
| 118 | } |
| 119 | } |
| 120 | arguments |
| 121 | } |
| 122 | |
| 123 | const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(1); |
| 124 | |
| 125 | fn data_path(name: &str) -> PathBuf { |
| 126 | ProjectDirs::from("", "", store::DATA_DIR_NAME) |
| 127 | .map(|directories| directories.data_local_dir().join(name)) |
| 128 | .unwrap_or_else(|| PathBuf::from(name)) |
| 129 | } |
| 130 | |
| 131 | fn transport( |
| 132 | endpoint: Option<&str>, |
| 133 | database: Option<&std::path::Path>, |
| 134 | #[cfg(windows)] sign_in_title: String, |
| 135 | ) -> Arc<dyn session::Transport> { |
| 136 | #[cfg(windows)] |
| 137 | if endpoint.is_none() { |
| 138 | let user_data_folder = match database.and_then(std::path::Path::parent) { |
| 139 | Some(directory) => directory.join(WEBVIEW_FOLDER), |
| 140 | None => data_path(WEBVIEW_FOLDER), |
| 141 | }; |
| 142 | inline_video::configure(user_data_folder.clone()); |
| 143 | let transport = webview::start(webview::HostConfig { |
| 144 | user_data_folder, |
| 145 | window_title: sign_in_title, |
| 146 | log: |line| app_base::diagnostics::record(line), |
| 147 | }); |
| 148 | task_dialog::install_host(transport.clone()); |
| 149 | return transport; |
| 150 | } |
| 151 | #[cfg(not(windows))] |
| 152 | let _ = database; |
| 153 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 154 | if endpoint.is_none() { |
| 155 | return linux_browser::start(session::DEFAULT_ENDPOINT); |
| 156 | } |
| 157 | Arc::new(session::CdpTransport::new( |
| 158 | endpoint.unwrap_or(session::DEFAULT_ENDPOINT), |
| 159 | )) |
| 160 | } |
| 161 | |
| 162 | fn main_window_options(cx: &App) -> WindowOptions { |
| 163 | let bounds = Bounds::centered(None, size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), cx); |
| 164 | let mut options = TitleBar::window_options(); |
| 165 | options.window_bounds = Some(WindowBounds::Windowed(bounds)); |
| 166 | options.window_min_size = Some(size(px(WINDOW_MIN_WIDTH), px(WINDOW_MIN_HEIGHT))); |
| 167 | options.app_id = Some(LINUX_APP_ID.to_owned()); |
| 168 | if let Some(titlebar) = options.titlebar.as_mut() { |
| 169 | titlebar.title = Some(APP_NAME.into()); |
| 170 | } |
| 171 | options |
| 172 | } |
| 173 | |
| 174 | const STORE_OPEN_ATTEMPTS: u32 = 20; |
| 175 | const STORE_OPEN_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500); |
| 176 | |
| 177 | fn open_store(path: &std::path::Path) -> Store { |
| 178 | for _ in 1..STORE_OPEN_ATTEMPTS { |
| 179 | match Store::open(path) { |
| 180 | Ok(store) => return store, |
| 181 | Err(_) => std::thread::sleep(STORE_OPEN_INTERVAL), |
| 182 | } |
| 183 | } |
| 184 | Store::open(path).expect("cache database") |
| 185 | } |
| 186 | |
| 187 | fn main() { |
| 188 | session::ensure_crypto_provider(); |
| 189 | app_base::version::set_build_version(VERSION_MARKER.trim_start_matches("teams-build:")); |
| 190 | if let Some(tag) = option_env!("TEAMS_RELEASE_TAG") { |
| 191 | app_base::version::set_release_tag(tag); |
| 192 | } |
| 193 | crash_log::install( |
| 194 | app_base::support::crash_log_path().unwrap_or_else(|| data_path(crash_log::CRASH_LOG_FILE)), |
| 195 | ); |
| 196 | app_base::native_dumps::install( |
| 197 | data_path("native-dumps"), |
| 198 | app_base::support::crash_log_path().unwrap_or_else(|| data_path(crash_log::CRASH_LOG_FILE)), |
| 199 | ); |
| 200 | diagnostics::install( |
| 201 | app_base::support::diagnostics_log_path() |
| 202 | .unwrap_or_else(|| data_path(diagnostics::DIAGNOSTICS_FILE)), |
| 203 | ); |
| 204 | let _ = diagnostics::enable_from_override(); |
| 205 | #[cfg(target_os = "linux")] |
| 206 | app_base::linux_fonts::hide_system_color_emoji(); |
| 207 | let arguments = parse_arguments(); |
| 208 | if !arguments.demo && arguments.database.is_none() { |
| 209 | let relaunch_value = std::env::var(updater::RELAUNCH_ENV).ok(); |
| 210 | if let Some(value) = &relaunch_value { |
| 211 | updater::wait_for_previous_exit(value); |
| 212 | } |
| 213 | let relaunched = relaunch_value.is_some(); |
| 214 | let command = arguments |
| 215 | .remote_command |
| 216 | .as_ref() |
| 217 | .map(RemoteCommand::encode) |
| 218 | .unwrap_or_default(); |
| 219 | if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched, &command) |
| 220 | == single_instance::Claim::Forwarded |
| 221 | { |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | #[cfg(any(windows, target_os = "linux"))] |
| 226 | updater::clean_up_old_binary(); |
| 227 | let mode = Mode { |
| 228 | demo: arguments.demo, |
| 229 | read_only: arguments.read_only, |
| 230 | demo_sync: arguments.demo_sync, |
| 231 | }; |
| 232 | #[cfg(windows)] |
| 233 | if arguments.demo { |
| 234 | inline_video::configure(std::env::temp_dir().join(DEMO_WEBVIEW_FOLDER)); |
| 235 | } |
| 236 | let store = if arguments.demo { |
| 237 | let store = Store::open_in_memory().expect("in-memory store"); |
| 238 | demo::seed(&store); |
| 239 | store |
| 240 | } else { |
| 241 | let path = arguments |
| 242 | .database |
| 243 | .clone() |
| 244 | .unwrap_or_else(|| data_path(DATABASE_FILE)); |
| 245 | open_store(&path) |
| 246 | }; |
| 247 | let store = Arc::new(store); |
| 248 | let stored_diag = store |
| 249 | .meta(diagnostics::DIAGNOSTICS_SETTING_KEY) |
| 250 | .ok() |
| 251 | .flatten(); |
| 252 | diagnostics::load_enabled_from_store(stored_diag.as_deref()); |
| 253 | let ui_locale = app_base::ui_locale::effective_locale(app_base::ui_locale::load(&store)); |
| 254 | app_base::ui_locale::apply_locale(ui_locale); |
| 255 | let call_reports = app_call_model::report::load_enabled(&store); |
| 256 | app_base::support::record_startup_client_line( |
| 257 | env!("CARGO_PKG_VERSION"), |
| 258 | ui_locale, |
| 259 | crate::data::me(&store).is_some(), |
| 260 | call_reports, |
| 261 | ); |
| 262 | #[cfg(windows)] |
| 263 | let sign_in_title = format!( |
| 264 | "{} - {}", |
| 265 | app_base::APP_NAME, |
| 266 | app_base::ui_locale::tr("auth.sign_in") |
| 267 | ); |
| 268 | let user_input = user_activity::UserInput::new(); |
| 269 | let receiver = (!arguments.demo).then(|| { |
| 270 | let (ui_events, ui_receiver) = tokio::sync::mpsc::unbounded_channel(); |
| 271 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 272 | linux_browser::attach_ui_events(ui_events.clone()); |
| 273 | #[cfg(windows)] |
| 274 | let transport = transport( |
| 275 | arguments.endpoint.as_deref(), |
| 276 | arguments.database.as_deref(), |
| 277 | sign_in_title.clone(), |
| 278 | ); |
| 279 | #[cfg(not(windows))] |
| 280 | let transport = transport(arguments.endpoint.as_deref(), arguments.database.as_deref()); |
| 281 | let activity = user_activity::track(user_input.clone()); |
| 282 | let mut backend_events = |
| 283 | backend::start(store.clone(), transport, !arguments.read_only, activity); |
| 284 | app_base::runtime::handle().spawn(async move { |
| 285 | while let Some(event) = backend_events.recv().await { |
| 286 | if ui_events.send(event).is_err() { |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | }); |
| 291 | ui_receiver |
| 292 | }); |
| 293 | |
| 294 | let startup_zoom = appearance::load(&store).zoom; |
| 295 | let platform = zoom::wrap(gpui_kit::platform::current_platform(false), startup_zoom); |
| 296 | let mut application = Application::with_platform(platform); |
| 297 | if let Ok(http_client) = reqwest_client::ReqwestClient::user_agent(HTTP_USER_AGENT) { |
| 298 | application = application.with_http_client(Arc::new(http_client)); |
| 299 | } |
| 300 | |
| 301 | application |
| 302 | .with_assets(app_base::assets::AppAssets) |
| 303 | .run(move |cx| { |
| 304 | gpui_kit::init(cx); |
| 305 | cx.on_app_quit(|_| { |
| 306 | updater::exit_after_grace(); |
| 307 | async {} |
| 308 | }) |
| 309 | .detach(); |
| 310 | theme::load_fonts(cx); |
| 311 | active_appearance::install(&store, cx); |
| 312 | active_locale::install(&store, cx); |
| 313 | theme::apply(cx); |
| 314 | bind_keys(cx); |
| 315 | let state = cx.new(|_| { |
| 316 | let mut state = AppState::new(store, mode); |
| 317 | state.start_on_channels = arguments.channels_tab; |
| 318 | state |
| 319 | }); |
| 320 | views::shell::install_globals(&state, user_input, cx); |
| 321 | agent_api::init(state.read(cx).store.clone(), arguments.demo, cx); |
| 322 | agents::install(state.clone(), cx); |
| 323 | agent_direct::install(state.clone(), cx); |
| 324 | if arguments.demo { |
| 325 | demo::start_session(&state, cx); |
| 326 | } |
| 327 | if let Some(mut receiver) = receiver { |
| 328 | let state = state.clone(); |
| 329 | cx.spawn(async move |cx| { |
| 330 | while let Some(event) = receiver.recv().await { |
| 331 | let kind = event.kind(); |
| 332 | let started = std::time::Instant::now(); |
| 333 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 334 | linux_browser::note_backend_event(&event); |
| 335 | state.update(cx, |state, cx| state.apply(event, cx)); |
| 336 | app_base::diagnostics::record_if_slow(&kind, started.elapsed()); |
| 337 | } |
| 338 | }) |
| 339 | .detach(); |
| 340 | } |
| 341 | |
| 342 | // The main window decides when to quit (see `main_window`): closing it into |
| 343 | // the tray leaves the app running without windows. |
| 344 | cx.set_quit_mode(QuitMode::Explicit); |
| 345 | // The command line only applies to the first window, not to one reopened from the tray. |
| 346 | let first_open = std::cell::Cell::new(Some(( |
| 347 | arguments.open_target, |
| 348 | Startup { |
| 349 | switcher_query: arguments.switcher_query, |
| 350 | composer_text: arguments.composer_text, |
| 351 | reply_to_last: arguments.reply_to_last, |
| 352 | launched_by_autostart: arguments.autostart, |
| 353 | }, |
| 354 | ))); |
| 355 | main_window::install( |
| 356 | move |cx| { |
| 357 | let (open_target, startup) = first_open.take().unwrap_or_default(); |
| 358 | let state = state.clone(); |
| 359 | let opened = |
| 360 | gpui_kit::open_window(main_window_options(cx), cx, move |window, cx| { |
| 361 | cx.new(|cx| AppShell::new(state, open_target, startup, window, cx)) |
| 362 | }); |
| 363 | match opened { |
| 364 | Ok((handle, _)) => Some(handle.into()), |
| 365 | Err(error) => { |
| 366 | log::error!("failed to open the main window: {error}"); |
| 367 | None |
| 368 | } |
| 369 | } |
| 370 | }, |
| 371 | cx, |
| 372 | ) |
| 373 | .expect("failed to open window"); |
| 374 | cx.on_window_closed(|cx, closed| { |
| 375 | if main_window::note_closed(closed, cx) { |
| 376 | cx.quit(); |
| 377 | } |
| 378 | }) |
| 379 | .detach(); |
| 380 | #[cfg(target_os = "linux")] |
| 381 | if !arguments.demo { |
| 382 | linux_install::start(cx); |
| 383 | linux_update::start(cx); |
| 384 | } |
| 385 | if !arguments.autostart { |
| 386 | cx.activate(true); |
| 387 | } |
| 388 | cx.spawn(async move |cx| { |
| 389 | loop { |
| 390 | app_base::native_dumps::heartbeat(); |
| 391 | cx.background_executor().timer(HEARTBEAT_INTERVAL).await; |
| 392 | } |
| 393 | }) |
| 394 | .detach(); |
| 395 | }); |
| 396 | presence_link::unregister_on_exit(ENDPOINT_UNREGISTER_TIMEOUT); |
| 397 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 398 | linux_browser::stop(); |
| 399 | #[cfg(windows)] |
| 400 | updater::exit_now(); |
| 401 | } |