BlueGit

ict rusty-teams

Files

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

main.rs

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