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