BlueGit

ict rusty-teams

Files

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

main.rs

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