BlueGit

ict rusty-teams

Files

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

main.rs

cdded127 History
main.rs 8.2 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
170fn main() {
171 crash_log::install(data_path(crash_log::CRASH_LOG_FILE));
172 let arguments = parse_arguments();
173 if !arguments.demo && arguments.database.is_none() {
174 let relaunched = std::env::var_os(updater::RELAUNCH_ENV).is_some();
175 if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched)
176 == single_instance::Claim::Forwarded
177 {
178 return;
179 }
180 }
181 #[cfg(windows)]
182 updater::clean_up_old_binary();
183 let mode = Mode {
184 demo: arguments.demo,
185 read_only: arguments.read_only,
186 demo_sync: arguments.demo_sync,
187 };
188 let store = if arguments.demo {
189 let store = Store::open_in_memory().expect("in-memory store");
190 demo::seed(&store);
191 store
192 } else {
193 let path = arguments
194 .database
195 .clone()
196 .unwrap_or_else(|| data_path(DATABASE_FILE));
197 Store::open(&path).expect("cache database")
198 };
199 let store = Arc::new(store);
200 let receiver = (!arguments.demo)
201 .then(|| {
202 let transport = transport(arguments.endpoint.as_deref(), arguments.database.as_deref());
203 backend::start(store.clone(), transport, !arguments.read_only)
204 });
205
206 let mut application = gpui_kit::application();
207 if let Ok(http_client) = reqwest_client::ReqwestClient::user_agent(HTTP_USER_AGENT) {
208 application = application.with_http_client(Arc::new(http_client));
209 }
210
211 application
212 .with_assets(assets::AppAssets)
213 .run(move |cx| {
214 gpui_kit::init(cx);
215 theme::load_fonts(cx);
216 theme::apply(cx);
217 bind_keys(cx);
218 let state = cx.new(|_| {
219 let mut state = AppState::new(store, mode);
220 state.start_on_channels = arguments.channels_tab;
221 state
222 });
223 cx.set_global(app_state::AppHandle(state.clone()));
224 if arguments.demo {
225 state.update(cx, |state, cx| {
226 demo::seed_directory(state);
227 state.demo_live_meetings();
228 state.select(demo::first_selection(), cx);
229 });
230 }
231 if let Some(mut receiver) = receiver {
232 let state = state.clone();
233 cx.spawn(async move |cx| {
234 while let Some(event) = receiver.recv().await {
235 state.update(cx, |state, cx| state.apply(event, cx));
236 }
237 })
238 .detach();
239 }
240
241 let bounds = Bounds::centered(None, size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), cx);
242 let mut options = TitleBar::window_options();
243 options.window_bounds = Some(WindowBounds::Windowed(bounds));
244 options.window_min_size = Some(size(px(WINDOW_MIN_WIDTH), px(WINDOW_MIN_HEIGHT)));
245 if let Some(titlebar) = options.titlebar.as_mut() {
246 titlebar.title = Some(APP_NAME.into());
247 }
248 let open_target = arguments.open_target;
249 let startup = Startup {
250 switcher_query: arguments.switcher_query,
251 composer_text: arguments.composer_text,
252 reply_to_last: arguments.reply_to_last,
253 };
254 let (main_window, _) = gpui_kit::open_window(options, cx, move |window, cx| {
255 cx.new(|cx| AppShell::new(state, open_target, startup, window, cx))
256 })
257 .expect("failed to open window");
258 let main_window_id = main_window.window_id();
259 cx.on_window_closed(move |cx, closed| {
260 if closed == main_window_id {
261 cx.quit();
262 }
263 })
264 .detach();
265 cx.activate(true);
266 });
267}