BlueGit

ict rusty-teams

Files

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

main.rs

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