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