BlueGit

ict rusty-teams

Files

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

main.rs

main.rs 9.5 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 chat_actions;
17mod crash_log;
18mod diagnostics;
19mod card_refresh;
20mod card_state;
21mod channel_files;
22mod data;
23mod demo;
24mod demo_cards;
25mod demo_chart_cards;
26mod demo_gifs;
27mod demo_input_cards;
28mod demo_library;
29mod demo_profiles;
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 own_status;
42mod notify;
43mod outbox;
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 single_instance;
59mod sidebar_model;
60mod stickers;
61mod stored_outgoing;
62mod task_dialog;
63mod theme;
64mod translation;
65mod translation_actions;
66mod typing;
67mod updater;
68mod user_activity;
69mod views;
70
71use std::path::PathBuf;
72use std::sync::Arc;
73use std::time::Duration;
74
75use directories::ProjectDirs;
76use gpui_kit::component::TitleBar;
77use gpui_kit::*;
78use store::Store;
79
80use app_state::{AppState, Mode};
81use views::shell::{AppShell, OpenTarget, Startup, bind_keys};
82
83pub const APP_NAME: &str = "Rusty Teams";
84const HTTP_USER_AGENT: &str = "teams-linux";
85const WINDOW_WIDTH: f32 = 1240.;
86const WINDOW_HEIGHT: f32 = 820.;
87const WINDOW_MIN_WIDTH: f32 = 640.;
88const WINDOW_MIN_HEIGHT: f32 = 480.;
89const ENDPOINT_UNREGISTER_TIMEOUT: Duration = Duration::from_millis(800);
90const SINGLE_INSTANCE_NAME: &str = "rusty-teams";
91const DATABASE_FILE: &str = "cache.sqlite3";
92#[cfg(windows)]
93const WEBVIEW_FOLDER: &str = "WebView2";
94
95#[derive(Default)]
96struct Arguments {
97 demo: bool,
98 read_only: bool,
99 database: Option<PathBuf>,
100 endpoint: Option<String>,
101 open_target: Option<OpenTarget>,
102 switcher_query: Option<String>,
103 composer_text: Option<String>,
104 reply_to_last: bool,
105 channels_tab: bool,
106 demo_sync: Option<Duration>,
107}
108
109fn parse_arguments() -> Arguments {
110 let mut arguments = Arguments::default();
111 let mut input = std::env::args().skip(1);
112 while let Some(flag) = input.next() {
113 match flag.as_str() {
114 "--demo" => arguments.demo = true,
115 "--read-only" => arguments.read_only = true,
116 "--switcher" => {
117 arguments.switcher_query.get_or_insert_with(String::new);
118 }
119 "--query" => arguments.switcher_query = input.next(),
120 "--type" => arguments.composer_text = input.next(),
121 "--reply-last" => arguments.reply_to_last = true,
122 "--channels-tab" => arguments.channels_tab = true,
123 "--demo-sync" => {
124 arguments.demo_sync = input
125 .next()
126 .and_then(|value| value.parse().ok())
127 .map(Duration::from_secs_f32)
128 }
129 "--database" => arguments.database = input.next().map(PathBuf::from),
130 "--endpoint" => arguments.endpoint = input.next(),
131 "--open" => {
132 arguments.open_target = input
133 .next()
134 .and_then(|value| value.parse().ok())
135 .map(OpenTarget::Chat)
136 }
137 "--open-channel" => {
138 arguments.open_target = input
139 .next()
140 .and_then(|value| value.parse().ok())
141 .map(OpenTarget::Channel)
142 }
143 _ => {}
144 }
145 }
146 arguments
147}
148
149fn data_path(name: &str) -> PathBuf {
150 ProjectDirs::from("", "", store::DATA_DIR_NAME)
151 .map(|directories| directories.data_local_dir().join(name))
152 .unwrap_or_else(|| PathBuf::from(name))
153}
154
155fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<dyn session::Transport> {
156 #[cfg(windows)]
157 if endpoint.is_none() {
158 let user_data_folder = match database.and_then(std::path::Path::parent) {
159 Some(directory) => directory.join(WEBVIEW_FOLDER),
160 None => data_path(WEBVIEW_FOLDER),
161 };
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 agent_api::init(state.read(cx).store.clone(), arguments.demo, cx);
257 agents::install(state.clone(), cx);
258 if arguments.demo {
259 state.update(cx, |state, cx| {
260 demo::seed_directory(state);
261 state.demo_live_meetings();
262 state.select(demo::first_selection(), cx);
263 });
264 }
265 if let Some(mut receiver) = receiver {
266 let state = state.clone();
267 cx.spawn(async move |cx| {
268 while let Some(event) = receiver.recv().await {
269 state.update(cx, |state, cx| state.apply(event, cx));
270 }
271 })
272 .detach();
273 }
274
275 let bounds = Bounds::centered(None, size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), cx);
276 let mut options = TitleBar::window_options();
277 options.window_bounds = Some(WindowBounds::Windowed(bounds));
278 options.window_min_size = Some(size(px(WINDOW_MIN_WIDTH), px(WINDOW_MIN_HEIGHT)));
279 if let Some(titlebar) = options.titlebar.as_mut() {
280 titlebar.title = Some(APP_NAME.into());
281 }
282 let open_target = arguments.open_target;
283 let startup = Startup {
284 switcher_query: arguments.switcher_query,
285 composer_text: arguments.composer_text,
286 reply_to_last: arguments.reply_to_last,
287 };
288 let (main_window, _) = gpui_kit::open_window(options, cx, move |window, cx| {
289 cx.new(|cx| AppShell::new(state, open_target, startup, window, cx))
290 })
291 .expect("failed to open window");
292 let main_window_id = main_window.window_id();
293 cx.on_window_closed(move |cx, closed| {
294 if closed == main_window_id {
295 cx.quit();
296 }
297 })
298 .detach();
299 cx.activate(true);
300 });
301 presence_link::unregister_on_exit(ENDPOINT_UNREGISTER_TIMEOUT);
302 #[cfg(windows)]
303 updater::exit_now();
304}