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