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