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