BlueGit

ict rusty-teams

Files

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

main.rs

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