BlueGit

ict rusty-teams

Files

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

main.rs

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