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