BlueGit

ict rusty-teams

Files

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

main.rs

main.rs 8.1 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 typing;
60mod updater;
61mod views;
62
63use std::path::PathBuf;
64use std::sync::Arc;
65use std::time::Duration;
66
67use directories::ProjectDirs;
68use gpui_kit::component::TitleBar;
69use gpui_kit::*;
70use store::Store;
71
72use app_state::{AppState, Mode};
73use views::shell::{AppShell, OpenTarget, Startup, bind_keys};
74
75pub const APP_NAME: &str = "Rusty Teams";
76const HTTP_USER_AGENT: &str = "teams-linux";
77const WINDOW_WIDTH: f32 = 1240.;
78const WINDOW_HEIGHT: f32 = 820.;
79const WINDOW_MIN_WIDTH: f32 = 640.;
80const WINDOW_MIN_HEIGHT: f32 = 480.;
81const SINGLE_INSTANCE_NAME: &str = "rusty-teams";
82const DATABASE_FILE: &str = "cache.sqlite3";
83#[cfg(windows)]
84const WEBVIEW_FOLDER: &str = "WebView2";
85
86#[derive(Default)]
87struct Arguments {
88 demo: bool,
89 read_only: bool,
90 database: Option<PathBuf>,
91 endpoint: Option<String>,
92 open_target: Option<OpenTarget>,
93 switcher_query: Option<String>,
94 composer_text: Option<String>,
95 reply_to_last: bool,
96 channels_tab: bool,
97 demo_sync: Option<Duration>,
98}
99
100fn parse_arguments() -> Arguments {
101 let mut arguments = Arguments::default();
102 let mut input = std::env::args().skip(1);
103 while let Some(flag) = input.next() {
104 match flag.as_str() {
105 "--demo" => arguments.demo = true,
106 "--read-only" => arguments.read_only = true,
107 "--switcher" => {
108 arguments.switcher_query.get_or_insert_with(String::new);
109 }
110 "--query" => arguments.switcher_query = input.next(),
111 "--type" => arguments.composer_text = input.next(),
112 "--reply-last" => arguments.reply_to_last = true,
113 "--channels-tab" => arguments.channels_tab = true,
114 "--demo-sync" => {
115 arguments.demo_sync = input
116 .next()
117 .and_then(|value| value.parse().ok())
118 .map(Duration::from_secs_f32)
119 }
120 "--database" => arguments.database = input.next().map(PathBuf::from),
121 "--endpoint" => arguments.endpoint = input.next(),
122 "--open" => {
123 arguments.open_target = input
124 .next()
125 .and_then(|value| value.parse().ok())
126 .map(OpenTarget::Chat)
127 }
128 "--open-channel" => {
129 arguments.open_target = input
130 .next()
131 .and_then(|value| value.parse().ok())
132 .map(OpenTarget::Channel)
133 }
134 _ => {}
135 }
136 }
137 arguments
138}
139
140fn data_path(name: &str) -> PathBuf {
141 ProjectDirs::from("", "", store::DATA_DIR_NAME)
142 .map(|directories| directories.data_local_dir().join(name))
143 .unwrap_or_else(|| PathBuf::from(name))
144}
145
146fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<dyn session::Transport> {
147 #[cfg(windows)]
148 if endpoint.is_none() {
149 let user_data_folder = match database.and_then(std::path::Path::parent) {
150 Some(directory) => directory.join(WEBVIEW_FOLDER),
151 None => data_path(WEBVIEW_FOLDER),
152 };
153 embedded_web::configure(user_data_folder.clone());
154 let transport = webview::start(webview::HostConfig {
155 user_data_folder,
156 window_title: format!("{APP_NAME} - Sign in"),
157 });
158 task_dialog::install_host(transport.clone());
159 return transport;
160 }
161 #[cfg(not(windows))]
162 let _ = database;
163 Arc::new(session::CdpTransport::new(
164 endpoint.unwrap_or(session::DEFAULT_ENDPOINT),
165 ))
166}
167
168fn main() {
169 crash_log::install(data_path(crash_log::CRASH_LOG_FILE));
170 let arguments = parse_arguments();
171 if !arguments.demo && arguments.database.is_none() {
172 let relaunched = std::env::var_os(updater::RELAUNCH_ENV).is_some();
173 if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched)
174 == single_instance::Claim::Forwarded
175 {
176 return;
177 }
178 }
179 #[cfg(windows)]
180 updater::clean_up_old_binary();
181 let mode = Mode {
182 demo: arguments.demo,
183 read_only: arguments.read_only,
184 demo_sync: arguments.demo_sync,
185 };
186 let store = if arguments.demo {
187 let store = Store::open_in_memory().expect("in-memory store");
188 demo::seed(&store);
189 store
190 } else {
191 let path = arguments
192 .database
193 .clone()
194 .unwrap_or_else(|| data_path(DATABASE_FILE));
195 Store::open(&path).expect("cache database")
196 };
197 let store = Arc::new(store);
198 let receiver = (!arguments.demo)
199 .then(|| {
200 let transport = transport(arguments.endpoint.as_deref(), arguments.database.as_deref());
201 backend::start(store.clone(), transport, !arguments.read_only)
202 });
203
204 let mut application = gpui_kit::application();
205 if let Ok(http_client) = reqwest_client::ReqwestClient::user_agent(HTTP_USER_AGENT) {
206 application = application.with_http_client(Arc::new(http_client));
207 }
208
209 application
210 .with_assets(assets::AppAssets)
211 .run(move |cx| {
212 gpui_kit::init(cx);
213 theme::load_fonts(cx);
214 theme::apply(cx);
215 bind_keys(cx);
216 let state = cx.new(|_| {
217 let mut state = AppState::new(store, mode);
218 state.start_on_channels = arguments.channels_tab;
219 state
220 });
221 cx.set_global(app_state::AppHandle(state.clone()));
222 if arguments.demo {
223 state.update(cx, |state, cx| {
224 demo::seed_directory(state);
225 state.demo_live_meetings();
226 state.select(demo::first_selection(), cx);
227 });
228 }
229 if let Some(mut receiver) = receiver {
230 let state = state.clone();
231 cx.spawn(async move |cx| {
232 while let Some(event) = receiver.recv().await {
233 state.update(cx, |state, cx| state.apply(event, cx));
234 }
235 })
236 .detach();
237 }
238
239 let bounds = Bounds::centered(None, size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), cx);
240 let mut options = TitleBar::window_options();
241 options.window_bounds = Some(WindowBounds::Windowed(bounds));
242 options.window_min_size = Some(size(px(WINDOW_MIN_WIDTH), px(WINDOW_MIN_HEIGHT)));
243 if let Some(titlebar) = options.titlebar.as_mut() {
244 titlebar.title = Some(APP_NAME.into());
245 }
246 let open_target = arguments.open_target;
247 let startup = Startup {
248 switcher_query: arguments.switcher_query,
249 composer_text: arguments.composer_text,
250 reply_to_last: arguments.reply_to_last,
251 };
252 let (main_window, _) = gpui_kit::open_window(options, cx, move |window, cx| {
253 cx.new(|cx| AppShell::new(state, open_target, startup, window, cx))
254 })
255 .expect("failed to open window");
256 let main_window_id = main_window.window_id();
257 cx.on_window_closed(move |cx, closed| {
258 if closed == main_window_id {
259 cx.quit();
260 }
261 })
262 .detach();
263 cx.activate(true);
264 });
265}