BlueGit

ict rusty-teams

Files

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

main.rs

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