BlueGit

ict rusty-teams

Files

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

main.rs

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