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