BlueGit

ict rusty-teams

Files

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

main.rs

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