BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / main.rs

main.rs

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