BlueGit

ict rusty-teams

Files

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

main.rs

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