BlueGit

ict rusty-teams

Files

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

main.rs

main.rs 6.3 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>) -> Arc<dyn session::Transport> {
108 #[cfg(windows)]
109 if endpoint.is_none() {
110 return webview::start(webview::HostConfig {
111 user_data_folder: data_path(WEBVIEW_FOLDER),
112 window_title: format!("{APP_NAME} - Sign in"),
113 });
114 }
115 Arc::new(session::CdpTransport::new(
116 endpoint.unwrap_or(session::DEFAULT_ENDPOINT),
117 ))
118}
119
120fn main() {
121 let arguments = parse_arguments();
122 #[cfg(windows)]
123 updater::clean_up_old_binary();
124 let mode = Mode {
125 demo: arguments.demo,
126 read_only: arguments.read_only,
127 demo_sync: arguments.demo_sync,
128 };
129 let store = if arguments.demo {
130 let store = Store::open_in_memory().expect("in-memory store");
131 demo::seed(&store);
132 store
133 } else {
134 let path = arguments
135 .database
136 .clone()
137 .unwrap_or_else(|| data_path(DATABASE_FILE));
138 Store::open(&path).expect("cache database")
139 };
140 let store = Arc::new(store);
141 let receiver = (!arguments.demo)
142 .then(|| backend::start(store.clone(), transport(arguments.endpoint.as_deref())));
143
144 gpui_kit::application()
145 .with_assets(assets::AppAssets)
146 .run(move |cx| {
147 gpui_kit::init(cx);
148 theme::load_fonts(cx);
149 theme::apply(cx);
150 bind_keys(cx);
151 let state = cx.new(|_| {
152 let mut state = AppState::new(store, mode);
153 state.start_on_channels = arguments.channels_tab;
154 state
155 });
156 if arguments.demo {
157 state.update(cx, |state, cx| {
158 demo::seed_directory(state);
159 state.select(demo::first_selection(), cx);
160 });
161 }
162 if let Some(mut receiver) = receiver {
163 let state = state.clone();
164 cx.spawn(async move |cx| {
165 while let Some(event) = receiver.recv().await {
166 state.update(cx, |state, cx| state.apply(event, cx));
167 }
168 })
169 .detach();
170 }
171
172 let bounds = Bounds::centered(None, size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), cx);
173 let mut options = TitleBar::window_options();
174 options.window_bounds = Some(WindowBounds::Windowed(bounds));
175 options.window_min_size = Some(size(px(WINDOW_MIN_WIDTH), px(WINDOW_MIN_HEIGHT)));
176 if let Some(titlebar) = options.titlebar.as_mut() {
177 titlebar.title = Some(APP_NAME.into());
178 }
179 let open_target = arguments.open_target;
180 let startup = Startup {
181 switcher_query: arguments.switcher_query,
182 composer_text: arguments.composer_text,
183 reply_to_last: arguments.reply_to_last,
184 };
185 let (main_window, _) = gpui_kit::open_window(options, cx, move |window, cx| {
186 cx.new(|cx| AppShell::new(state, open_target, startup, window, cx))
187 })
188 .expect("failed to open window");
189 let main_window_id = main_window.window_id();
190 cx.on_window_closed(move |cx, closed| {
191 if closed == main_window_id {
192 cx.quit();
193 }
194 })
195 .detach();
196 cx.activate(true);
197 });
198}