BlueGit

ict rusty-teams

Files

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

main.rs

main.rs 9.2 KiB Download Raw
1#![cfg_attr(
2 all(not(debug_assertions), target_os = "windows"),
3 windows_subsystem = "windows"
4)]
5
6mod activity;
7mod agent_api;
8mod agents;
9mod app_state;
10mod backend;
11mod call;
12mod card_actions;
13mod card_inputs;
14mod card_refresh;
15mod card_state;
16mod chat_actions;
17mod data;
18mod demo;
19mod demo_profiles;
20mod emoji;
21mod gifs;
22mod image_view_gallery;
23#[cfg(test)]
24mod layout_guard;
25mod local_previews;
26mod message_actions;
27mod notice;
28mod notify;
29mod outbox;
30mod outgoing;
31mod own_status;
32mod pending_rows;
33mod people;
34mod profile_model;
35mod profile_state;
36mod reaction_model;
37mod rows;
38mod scheduled;
39mod scheduled_rows;
40mod section_actions;
41mod sidebar_model;
42mod stored_outgoing;
43mod task_dialog;
44mod translation_actions;
45mod typing;
46mod views;
47
48pub use app_ui_kit::render;
49pub use app_base::{
50 APP_NAME, assets, avatar_image, channel_files, crash_log, demo_cards, demo_chart_cards, demo_gifs,
51 demo_input_cards, demo_library, demo_recap, diagnostics, downloads, format, frame_log, fuzzy,
52 image_view_math, inline_image, presence_link, read_state, recap, remote_image, runtime, schedule_time,
53 single_instance, stickers, theme, translation, updater, user_activity,
54};
55
56use std::path::PathBuf;
57use std::sync::Arc;
58use std::time::Duration;
59
60use directories::ProjectDirs;
61use gpui_kit::component::TitleBar;
62use gpui_kit::*;
63use store::Store;
64
65use app_state::{AppState, Mode};
66use views::shell::{AppShell, OpenTarget, Startup, bind_keys};
67
68const VERSION_MARKER: &str = concat!("teams-build:", env!("TEAMS_BUILD_VERSION"));
69const HTTP_USER_AGENT: &str = "teams-linux";
70const WINDOW_WIDTH: f32 = 1240.;
71const WINDOW_HEIGHT: f32 = 820.;
72const WINDOW_MIN_WIDTH: f32 = 640.;
73const WINDOW_MIN_HEIGHT: f32 = 480.;
74const ENDPOINT_UNREGISTER_TIMEOUT: Duration = Duration::from_millis(800);
75const SINGLE_INSTANCE_NAME: &str = "rusty-teams";
76const DATABASE_FILE: &str = "cache.sqlite3";
77#[cfg(windows)]
78const WEBVIEW_FOLDER: &str = "WebView2";
79
80#[derive(Default)]
81struct Arguments {
82 demo: bool,
83 read_only: bool,
84 database: Option<PathBuf>,
85 endpoint: Option<String>,
86 open_target: Option<OpenTarget>,
87 switcher_query: Option<String>,
88 composer_text: Option<String>,
89 reply_to_last: bool,
90 channels_tab: bool,
91 demo_sync: Option<Duration>,
92}
93
94fn parse_arguments() -> Arguments {
95 let mut arguments = Arguments::default();
96 let mut input = std::env::args().skip(1);
97 while let Some(flag) = input.next() {
98 match flag.as_str() {
99 "--demo" => arguments.demo = true,
100 "--read-only" => arguments.read_only = true,
101 "--switcher" => {
102 arguments.switcher_query.get_or_insert_with(String::new);
103 }
104 "--query" => arguments.switcher_query = input.next(),
105 "--type" => arguments.composer_text = input.next(),
106 "--reply-last" => arguments.reply_to_last = true,
107 "--channels-tab" => arguments.channels_tab = true,
108 "--demo-sync" => {
109 arguments.demo_sync = input
110 .next()
111 .and_then(|value| value.parse().ok())
112 .map(Duration::from_secs_f32)
113 }
114 "--database" => arguments.database = input.next().map(PathBuf::from),
115 "--endpoint" => arguments.endpoint = input.next(),
116 "--open" => {
117 arguments.open_target = input
118 .next()
119 .and_then(|value| value.parse().ok())
120 .map(OpenTarget::Chat)
121 }
122 "--open-channel" => {
123 arguments.open_target = input
124 .next()
125 .and_then(|value| value.parse().ok())
126 .map(OpenTarget::Channel)
127 }
128 _ => {}
129 }
130 }
131 arguments
132}
133
134fn data_path(name: &str) -> PathBuf {
135 ProjectDirs::from("", "", store::DATA_DIR_NAME)
136 .map(|directories| directories.data_local_dir().join(name))
137 .unwrap_or_else(|| PathBuf::from(name))
138}
139
140fn transport(
141 endpoint: Option<&str>,
142 database: Option<&std::path::Path>,
143) -> Arc<dyn session::Transport> {
144 #[cfg(windows)]
145 if endpoint.is_none() {
146 let user_data_folder = match database.and_then(std::path::Path::parent) {
147 Some(directory) => directory.join(WEBVIEW_FOLDER),
148 None => data_path(WEBVIEW_FOLDER),
149 };
150 let transport = webview::start(webview::HostConfig {
151 user_data_folder,
152 window_title: format!("{APP_NAME} - Sign in"),
153 });
154 task_dialog::install_host(transport.clone());
155 return transport;
156 }
157 #[cfg(not(windows))]
158 let _ = database;
159 Arc::new(session::CdpTransport::new(
160 endpoint.unwrap_or(session::DEFAULT_ENDPOINT),
161 ))
162}
163
164const STORE_OPEN_ATTEMPTS: u32 = 20;
165const STORE_OPEN_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);
166
167fn open_store(path: &std::path::Path) -> Store {
168 for _ in 1..STORE_OPEN_ATTEMPTS {
169 match Store::open(path) {
170 Ok(store) => return store,
171 Err(_) => std::thread::sleep(STORE_OPEN_INTERVAL),
172 }
173 }
174 Store::open(path).expect("cache database")
175}
176
177fn main() {
178 app_base::version::set_build_version(VERSION_MARKER.trim_start_matches("teams-build:"));
179 crash_log::install(data_path(crash_log::CRASH_LOG_FILE));
180 diagnostics::install(data_path(diagnostics::DIAGNOSTICS_FILE));
181 let arguments = parse_arguments();
182 if !arguments.demo && arguments.database.is_none() {
183 let relaunch_value = std::env::var(updater::RELAUNCH_ENV).ok();
184 if let Some(value) = &relaunch_value {
185 updater::wait_for_previous_exit(value);
186 }
187 let relaunched = relaunch_value.is_some();
188 if single_instance::claim(SINGLE_INSTANCE_NAME, relaunched)
189 == single_instance::Claim::Forwarded
190 {
191 return;
192 }
193 }
194 #[cfg(windows)]
195 updater::clean_up_old_binary();
196 let mode = Mode {
197 demo: arguments.demo,
198 read_only: arguments.read_only,
199 demo_sync: arguments.demo_sync,
200 };
201 let store = if arguments.demo {
202 let store = Store::open_in_memory().expect("in-memory store");
203 demo::seed(&store);
204 store
205 } else {
206 let path = arguments
207 .database
208 .clone()
209 .unwrap_or_else(|| data_path(DATABASE_FILE));
210 open_store(&path)
211 };
212 let store = Arc::new(store);
213 let user_input = user_activity::UserInput::new();
214 let receiver = (!arguments.demo).then(|| {
215 let transport = transport(arguments.endpoint.as_deref(), arguments.database.as_deref());
216 let activity = user_activity::track(user_input.clone());
217 backend::start(store.clone(), transport, !arguments.read_only, activity)
218 });
219
220 let mut application = gpui_kit::application();
221 if let Ok(http_client) = reqwest_client::ReqwestClient::user_agent(HTTP_USER_AGENT) {
222 application = application.with_http_client(Arc::new(http_client));
223 }
224
225 application.with_assets(assets::AppAssets).run(move |cx| {
226 gpui_kit::init(cx);
227 cx.on_app_quit(|_| {
228 updater::exit_after_grace();
229 async {}
230 })
231 .detach();
232 theme::load_fonts(cx);
233 theme::apply(cx);
234 bind_keys(cx);
235 let state = cx.new(|_| {
236 let mut state = AppState::new(store, mode);
237 state.start_on_channels = arguments.channels_tab;
238 state
239 });
240 views::shell::install_globals(&state, user_input, cx);
241 agent_api::init(state.read(cx).store.clone(), arguments.demo, cx);
242 agents::install(state.clone(), cx);
243 if arguments.demo {
244 demo::start_session(&state, cx);
245 }
246 if let Some(mut receiver) = receiver {
247 let state = state.clone();
248 cx.spawn(async move |cx| {
249 while let Some(event) = receiver.recv().await {
250 state.update(cx, |state, cx| state.apply(event, cx));
251 }
252 })
253 .detach();
254 }
255
256 let bounds = Bounds::centered(None, size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), cx);
257 let mut options = TitleBar::window_options();
258 options.window_bounds = Some(WindowBounds::Windowed(bounds));
259 options.window_min_size = Some(size(px(WINDOW_MIN_WIDTH), px(WINDOW_MIN_HEIGHT)));
260 if let Some(titlebar) = options.titlebar.as_mut() {
261 titlebar.title = Some(APP_NAME.into());
262 }
263 let open_target = arguments.open_target;
264 let startup = Startup {
265 switcher_query: arguments.switcher_query,
266 composer_text: arguments.composer_text,
267 reply_to_last: arguments.reply_to_last,
268 };
269 let (main_window, _) = gpui_kit::open_window(options, cx, move |window, cx| {
270 cx.new(|cx| AppShell::new(state, open_target, startup, window, cx))
271 })
272 .expect("failed to open window");
273 let main_window_id = main_window.window_id();
274 cx.on_window_closed(move |cx, closed| {
275 if closed == main_window_id {
276 cx.quit();
277 }
278 })
279 .detach();
280 cx.activate(true);
281 });
282 presence_link::unregister_on_exit(ENDPOINT_UNREGISTER_TIMEOUT);
283 #[cfg(windows)]
284 updater::exit_now();
285}