BlueGit

ict rusty-teams

Files

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

main.rs

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