BlueGit

ict rusty-teams

Files

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

main.rs

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