BlueGit

ict rusty-teams

Files

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

main.rs

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