BlueGit

ict rusty-teams

Files

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

main.rs

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