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