BlueGit

ict rusty-teams

Files

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

main.rs

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