BlueGit

ict rusty-teams

Files

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

main.rs

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