BlueGit

ict rusty-teams

Files

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

main.rs

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