BlueGit

ict rusty-teams

Files

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

main.rs

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