BlueGit

ict rusty-teams

Files

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

main.rs

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