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