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 app_state;
8mod assets;
9mod avatar_image;
10mod backend;
11mod call;
12mod card_actions;
13mod card_inputs;
14mod chat_actions;
15mod crash_log;
16mod diagnostics;
17mod card_refresh;
18mod card_state;
19mod channel_files;
20mod data;
21mod demo;
22mod demo_cards;
23mod demo_chart_cards;
24mod demo_gifs;
25mod demo_input_cards;
26mod demo_library;
27mod demo_profiles;
28mod downloads;
29mod embedded_web;
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 embedded_web::configure(user_data_folder.clone());
162 let transport = webview::start(webview::HostConfig {
163 user_data_folder,
164 window_title: format!("{APP_NAME} - Sign in"),
165 });
166 task_dialog::install_host(transport.clone());
167 return transport;
168 }
169 #[cfg(not(windows))]
170 let _ = database;
171 Arc::new(session::CdpTransport::new(
172 endpoint.unwrap_or(session::DEFAULT_ENDPOINT),
173 ))
174}
175
176const STORE_OPEN_ATTEMPTS: u32 = 20;
177const STORE_OPEN_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);
178
179fn open_store(path: &std::path::Path) -> Store {
180 for _ in 1..STORE_OPEN_ATTEMPTS {
181 match Store::open(path) {
182 Ok(store) => return store,
183 Err(_) => std::thread::sleep(STORE_OPEN_INTERVAL),
184 }
185 }
186 Store::open(path).expect("cache database")
187}
188
189fn main() {
190 crash_log::install(data_path(crash_log::CRASH_LOG_FILE));
191 diagnostics::install(data_path(diagnostics::DIAGNOSTICS_FILE));
192 let arguments = parse_arguments();
193 if !arguments.demo && arguments.database.is_none() {
194 let relaunch_value = std::env::var(updater::RELAUNCH_ENV).ok();
195 if let Some(value) = &relaunch_value {
196 updater::wait_for_previous_exit(value);
197 }
198 let relaunched = relaunch_value.is_some();
199 if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched)
200 == single_instance::Claim::Forwarded
201 {
202 return;
203 }
204 }
205 #[cfg(windows)]
206 updater::clean_up_old_binary();
207 let mode = Mode {
208 demo: arguments.demo,
209 read_only: arguments.read_only,
210 demo_sync: arguments.demo_sync,
211 };
212 let store = if arguments.demo {
213 let store = Store::open_in_memory().expect("in-memory store");
214 demo::seed(&store);
215 store
216 } else {
217 let path = arguments
218 .database
219 .clone()
220 .unwrap_or_else(|| data_path(DATABASE_FILE));
221 open_store(&path)
222 };
223 let store = Arc::new(store);
224 let user_input = user_activity::UserInput::new();
225 let receiver = (!arguments.demo)
226 .then(|| {
227 let transport = transport(arguments.endpoint.as_deref(), arguments.database.as_deref());
228 let activity = user_activity::track(user_input.clone());
229 backend::start(store.clone(), transport, !arguments.read_only, activity)
230 });
231
232 let mut application = gpui_kit::application();
233 if let Ok(http_client) = reqwest_client::ReqwestClient::user_agent(HTTP_USER_AGENT) {
234 application = application.with_http_client(Arc::new(http_client));
235 }
236
237 application
238 .with_assets(assets::AppAssets)
239 .run(move |cx| {
240 gpui_kit::init(cx);
241 cx.on_app_quit(|_| {
242 updater::exit_after_grace();
243 async {}
244 })
245 .detach();
246 theme::load_fonts(cx);
247 theme::apply(cx);
248 bind_keys(cx);
249 let state = cx.new(|_| {
250 let mut state = AppState::new(store, mode);
251 state.start_on_channels = arguments.channels_tab;
252 state
253 });
254 cx.set_global(app_state::AppHandle(state.clone()));
255 cx.set_global(views::shell::UserInputHandle(user_input));
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}