BlueGit

ict rusty-teams

Files

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

main.rs

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