BlueGit

ict rusty-teams

Files

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

main.rs

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