BlueGit

ict rusty-teams

Files

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

main.rs

396af7fa History
main.rs 6.1 KiB Download Raw
1#![cfg_attr(
2 all(not(debug_assertions), target_os = "windows"),
3 windows_subsystem = "windows"
4)]
5
6mod app_state;
7mod avatar_image;
8mod backend;
9mod data;
10mod demo;
11mod emoji;
12mod format;
13mod assets;
14mod frame_log;
15mod fuzzy;
16mod render;
17mod rows;
18mod runtime;
19mod sidebar_model;
20mod theme;
21mod updater;
22mod views;
23
24use std::path::PathBuf;
25use std::sync::Arc;
26use std::time::Duration;
27
28use directories::ProjectDirs;
29use gpui_kit::component::TitleBar;
30use gpui_kit::*;
31use store::Store;
32
33use app_state::{AppState, Mode};
34use views::shell::{AppShell, OpenTarget, Startup, bind_keys};
35
36pub const APP_NAME: &str = "Rusty Teams";
37const WINDOW_WIDTH: f32 = 1240.;
38const WINDOW_HEIGHT: f32 = 820.;
39const WINDOW_MIN_WIDTH: f32 = 640.;
40const WINDOW_MIN_HEIGHT: f32 = 480.;
41const DATABASE_FILE: &str = "cache.sqlite3";
42#[cfg(windows)]
43const WEBVIEW_FOLDER: &str = "WebView2";
44
45#[derive(Default)]
46struct Arguments {
47 demo: bool,
48 read_only: bool,
49 database: Option<PathBuf>,
50 endpoint: Option<String>,
51 open_target: Option<OpenTarget>,
52 switcher_query: Option<String>,
53 composer_text: Option<String>,
54 reply_to_last: bool,
55 channels_tab: bool,
56 demo_sync: Option<Duration>,
57}
58
59fn parse_arguments() -> Arguments {
60 let mut arguments = Arguments::default();
61 let mut input = std::env::args().skip(1);
62 while let Some(flag) = input.next() {
63 match flag.as_str() {
64 "--demo" => arguments.demo = true,
65 "--read-only" => arguments.read_only = true,
66 "--switcher" => {
67 arguments.switcher_query.get_or_insert_with(String::new);
68 }
69 "--query" => arguments.switcher_query = input.next(),
70 "--type" => arguments.composer_text = input.next(),
71 "--reply-last" => arguments.reply_to_last = true,
72 "--channels-tab" => arguments.channels_tab = true,
73 "--demo-sync" => {
74 arguments.demo_sync = input
75 .next()
76 .and_then(|value| value.parse().ok())
77 .map(Duration::from_secs_f32)
78 }
79 "--database" => arguments.database = input.next().map(PathBuf::from),
80 "--endpoint" => arguments.endpoint = input.next(),
81 "--open" => {
82 arguments.open_target = input
83 .next()
84 .and_then(|value| value.parse().ok())
85 .map(OpenTarget::Chat)
86 }
87 "--open-channel" => {
88 arguments.open_target = input
89 .next()
90 .and_then(|value| value.parse().ok())
91 .map(OpenTarget::Channel)
92 }
93 _ => {}
94 }
95 }
96 arguments
97}
98
99fn data_path(name: &str) -> PathBuf {
100 ProjectDirs::from("", "", store::DATA_DIR_NAME)
101 .map(|directories| directories.data_local_dir().join(name))
102 .unwrap_or_else(|| PathBuf::from(name))
103}
104
105fn transport(endpoint: Option<&str>) -> Arc<dyn session::Transport> {
106 #[cfg(windows)]
107 if endpoint.is_none() {
108 return webview::start(webview::HostConfig {
109 user_data_folder: data_path(WEBVIEW_FOLDER),
110 window_title: format!("{APP_NAME} - Sign in"),
111 });
112 }
113 Arc::new(session::CdpTransport::new(endpoint.unwrap_or(session::DEFAULT_ENDPOINT)))
114}
115
116fn main() {
117 let arguments = parse_arguments();
118 #[cfg(windows)]
119 updater::clean_up_old_binary();
120 let mode = Mode {
121 demo: arguments.demo,
122 read_only: arguments.read_only,
123 demo_sync: arguments.demo_sync,
124 };
125 let store = if arguments.demo {
126 let store = Store::open_in_memory().expect("in-memory store");
127 demo::seed(&store);
128 store
129 } else {
130 let path = arguments.database.clone().unwrap_or_else(|| data_path(DATABASE_FILE));
131 Store::open(&path).expect("cache database")
132 };
133 let store = Arc::new(store);
134 let receiver = (!arguments.demo)
135 .then(|| backend::start(store.clone(), transport(arguments.endpoint.as_deref())));
136
137 gpui_kit::application()
138 .with_assets(assets::AppAssets)
139 .run(move |cx| {
140 gpui_kit::init(cx);
141 theme::load_fonts(cx);
142 theme::apply(cx);
143 bind_keys(cx);
144 cx.on_window_closed(|cx, _| {
145 if cx.windows().is_empty() {
146 cx.quit();
147 }
148 })
149 .detach();
150
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 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 cx.activate(true);
190 });
191}