yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use gpui_kit::prelude::FluentBuilder as _; |
| 2 | use gpui_kit::{ |
| 3 | AnimationExt as _, App, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, |
| 4 | Window, |
| 5 | assets::IconName, |
| 6 | component::input::{Input, InputEvent, InputState}, |
| 7 | div, px, |
| 8 | }; |
| 9 | |
| 10 | use super::{ |
| 11 | Palette, Route, Yumush, |
| 12 | account_list::account_list, |
| 13 | clickable, connection_notice, new_input, notify_error, palette, parts, |
| 14 | settings_page::{segment, segment_track}, |
| 15 | style::{radius, size, space, text}, |
| 16 | text, |
| 17 | widget::{field, frame}, |
| 18 | }; |
| 19 | use crate::lang::w; |
| 20 | |
| 21 | pub struct Auth { |
| 22 | server_input: Entity<InputState>, |
| 23 | any_cert: bool, |
| 24 | server_open: bool, |
| 25 | username_input: Entity<InputState>, |
| 26 | password_input: Entity<InputState>, |
| 27 | invite_input: Entity<InputState>, |
| 28 | } |
| 29 | |
| 30 | impl Auth { |
| 31 | pub fn new(server: &crate::Server, window: &mut Window, cx: &mut Context<Yumush>) -> Self { |
| 32 | let server_input = new_input(w().auth.server, window, cx); |
| 33 | server_input.update(cx, |input, cx| input.set_value(&server.address, window, cx)); |
| 34 | cx.subscribe_in(&server_input, window, |this, _, event, _, cx| { |
| 35 | if matches!(event, InputEvent::PressEnter { .. }) { |
| 36 | apply_server(this, cx); |
| 37 | } |
| 38 | }) |
| 39 | .detach(); |
| 40 | let username_input = new_input(w().auth.username_placeholder, window, cx); |
| 41 | let password_input = new_input(w().auth.password_placeholder, window, cx); |
| 42 | let invite_input = new_input(w().auth.invite_placeholder, window, cx); |
| 43 | password_input.update(cx, |input, cx| input.set_masked(true, window, cx)); |
| 44 | username_input.update(cx, |input, cx| input.focus(window, cx)); |
| 45 | |
| 46 | for input in [&username_input, &password_input, &invite_input] { |
| 47 | cx.subscribe_in(input, window, |this, _, event, window, cx| { |
| 48 | if matches!(event, InputEvent::PressEnter { .. }) { |
| 49 | submit(this, window, cx); |
| 50 | } |
| 51 | }) |
| 52 | .detach(); |
| 53 | } |
| 54 | |
| 55 | Self { |
| 56 | server_input, |
| 57 | any_cert: server.any_cert, |
| 58 | server_open: false, |
| 59 | username_input, |
| 60 | password_input, |
| 61 | invite_input, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn focus(&self, window: &mut Window, cx: &mut Context<Yumush>) { |
| 66 | self.username_input |
| 67 | .update(cx, |input, cx| input.focus(window, cx)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn apply_server(this: &mut Yumush, cx: &mut Context<Yumush>) { |
| 72 | let address = this.auth.server_input.read(cx).text().to_string(); |
| 73 | let server = crate::Server { |
| 74 | address: address.trim().to_string(), |
| 75 | any_cert: this.auth.any_cert, |
| 76 | }; |
| 77 | if server.address.is_empty() || !this.network.set_server(server.clone()) { |
| 78 | return; |
| 79 | } |
| 80 | server.save(); |
| 81 | this.accounts = crate::accounts::load(&server.address); |
| 82 | this.server = None; |
| 83 | this.connection = super::Connection::Connecting; |
| 84 | cx.notify(); |
| 85 | } |
| 86 | |
| 87 | fn submit(this: &mut Yumush, window: &mut Window, cx: &mut Context<Yumush>) { |
| 88 | apply_server(this, cx); |
| 89 | let username = this.auth.username_input.read(cx).text().to_string(); |
| 90 | let password = this.auth.password_input.read(cx).text().to_string(); |
| 91 | let invite = this.auth.invite_input.read(cx).text().to_string(); |
| 92 | let invite = invite.trim().to_string(); |
| 93 | let route = this.current_route; |
| 94 | let network = this.network.clone(); |
| 95 | |
| 96 | cx.spawn_in(window, async move |this, cx| { |
| 97 | let result = match route { |
| 98 | Route::Register => { |
| 99 | let invite = (!invite.is_empty()).then_some(invite); |
| 100 | network |
| 101 | .register(&username, &password, invite.as_deref()) |
| 102 | .await |
| 103 | } |
| 104 | Route::Login | Route::Chat => network.login(&username, &password).await, |
| 105 | }; |
| 106 | |
| 107 | match result { |
| 108 | Ok((user, token)) => { |
| 109 | let _ = this.update_in(cx, |yumush, window, cx| { |
| 110 | yumush |
| 111 | .auth |
| 112 | .password_input |
| 113 | .update(cx, |input, cx| input.set_value("", window, cx)); |
| 114 | yumush.enter_session(user, token, window, cx); |
| 115 | }); |
| 116 | } |
| 117 | Err(error) => notify_error(&this, cx, error), |
| 118 | } |
| 119 | }) |
| 120 | .detach(); |
| 121 | } |
| 122 | |
| 123 | fn server_toggle( |
| 124 | entity: &Entity<Yumush>, |
| 125 | address: &str, |
| 126 | open: bool, |
| 127 | palette: Palette, |
| 128 | ) -> impl IntoElement { |
| 129 | let entity = entity.clone(); |
| 130 | clickable("server_toggle", palette, move |_, cx| { |
| 131 | entity.update(cx, |yumush, cx| { |
| 132 | yumush.auth.server_open = !yumush.auth.server_open; |
| 133 | cx.notify(); |
| 134 | }) |
| 135 | }) |
| 136 | .self_center() |
| 137 | .px(px(space::SM)) |
| 138 | .py(px(space::XXS)) |
| 139 | .rounded(px(radius::MD)) |
| 140 | .flex() |
| 141 | .items_center() |
| 142 | .gap(px(space::XS)) |
| 143 | .text_size(text(text::CAPTION)) |
| 144 | .text_color(palette.muted) |
| 145 | .child(host(address).to_string()) |
| 146 | .child( |
| 147 | super::parts::small_icon(IconName::ChevronRight).with_animation( |
| 148 | ("server_chevron", open as u64), |
| 149 | super::parts::motion(), |
| 150 | move |icon, delta| { |
| 151 | let turn = if open { delta } else { 1. - delta }; |
| 152 | icon.rotate(gpui_kit::radians(std::f32::consts::FRAC_PI_2 * turn)) |
| 153 | }, |
| 154 | ), |
| 155 | ) |
| 156 | } |
| 157 | |
| 158 | fn host(address: &str) -> &str { |
| 159 | match address.rsplit_once(':') { |
| 160 | Some((host, port)) if port.chars().all(|c| c.is_ascii_digit()) => { |
| 161 | host.trim_start_matches('[').trim_end_matches(']') |
| 162 | } |
| 163 | _ => address, |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | fn password_field(palette: Palette, input: &Entity<InputState>) -> Div { |
| 168 | frame(palette, Input::new(input).mask_toggle()) |
| 169 | } |
| 170 | |
| 171 | impl Yumush { |
| 172 | pub(super) fn auth_page(&self, route: Route, cx: &mut Context<Self>) -> impl IntoElement { |
| 173 | let palette = palette(cx); |
| 174 | let action_label = match route { |
| 175 | Route::Register => w().auth.register, |
| 176 | Route::Login | Route::Chat => w().auth.log_in, |
| 177 | }; |
| 178 | |
| 179 | let entity = cx.entity(); |
| 180 | let submit_entity = entity.clone(); |
| 181 | let action_button = parts::button( |
| 182 | "auth_button", |
| 183 | action_label, |
| 184 | parts::Press::Primary, |
| 185 | palette.tokens, |
| 186 | move |window, cx| { |
| 187 | submit_entity.update(cx, |this, cx| submit(this, window, cx)); |
| 188 | }, |
| 189 | ) |
| 190 | .justify_center(); |
| 191 | |
| 192 | let server = self.server.as_ref(); |
| 193 | let registration_open = server.is_none_or(|settings| settings.registration_open); |
| 194 | let maintenance = server.is_some_and(|settings| settings.maintenance); |
| 195 | let title = server.map_or_else(|| "yumush".to_string(), |settings| settings.name.clone()); |
| 196 | |
| 197 | let tabs = segment_track(palette) |
| 198 | .child(tab(palette, &entity, Route::Login, w().auth.log_in, route)) |
| 199 | .child(tab( |
| 200 | palette, |
| 201 | &entity, |
| 202 | Route::Register, |
| 203 | w().auth.register, |
| 204 | route, |
| 205 | )); |
| 206 | let invite = (route == Route::Register && !registration_open) |
| 207 | .then(|| field(palette, &self.auth.invite_input)); |
| 208 | |
| 209 | div() |
| 210 | .flex() |
| 211 | .flex_col() |
| 212 | .items_center() |
| 213 | .justify_center() |
| 214 | .size_full() |
| 215 | .child( |
| 216 | div() |
| 217 | .w(px(size::POPOVER)) |
| 218 | .p(px(space::XL)) |
| 219 | .flex() |
| 220 | .flex_col() |
| 221 | .gap(px(space::MD)) |
| 222 | .child( |
| 223 | div() |
| 224 | .text_size(text(text::BODY)) |
| 225 | .font_weight(FontWeight::SEMIBOLD) |
| 226 | .child(title), |
| 227 | ) |
| 228 | .children(maintenance.then(|| { |
| 229 | div() |
| 230 | .text_size(text(text::CAPTION)) |
| 231 | .text_color(palette.muted) |
| 232 | .child(w().auth.maintenance) |
| 233 | })) |
| 234 | .children(self.saved_accounts(&entity, palette, cx)) |
| 235 | .child(tabs) |
| 236 | .child(field(palette, &self.auth.username_input)) |
| 237 | .child(password_field(palette, &self.auth.password_input)) |
| 238 | .children(invite) |
| 239 | .child(action_button) |
| 240 | .children(connection_notice(&self.connection, palette)) |
| 241 | .child(server_toggle( |
| 242 | &entity, |
| 243 | &self.network.server().address, |
| 244 | self.auth.server_open, |
| 245 | palette, |
| 246 | )) |
| 247 | .child( |
| 248 | div() |
| 249 | .relative() |
| 250 | .when(!self.auth.server_open, |this| this.invisible()) |
| 251 | .flex() |
| 252 | .flex_col() |
| 253 | .gap(px(space::SM)) |
| 254 | .child(field(palette, &self.auth.server_input)) |
| 255 | .child( |
| 256 | div() |
| 257 | .flex() |
| 258 | .items_center() |
| 259 | .justify_between() |
| 260 | .text_size(text(text::CAPTION)) |
| 261 | .text_color(palette.muted) |
| 262 | .child(w().auth.accept_any_cert) |
| 263 | .child(super::parts::toggle( |
| 264 | "any_cert_switch", |
| 265 | self.auth.any_cert, |
| 266 | palette.tokens, |
| 267 | { |
| 268 | let entity = entity.clone(); |
| 269 | move |_, cx| { |
| 270 | entity.update(cx, |yumush, cx| { |
| 271 | yumush.auth.any_cert = !yumush.auth.any_cert; |
| 272 | apply_server(yumush, cx); |
| 273 | cx.notify(); |
| 274 | }) |
| 275 | } |
| 276 | }, |
| 277 | )), |
| 278 | ) |
| 279 | .with_animation( |
| 280 | ("server_fields", self.auth.server_open as u64), |
| 281 | super::parts::motion(), |
| 282 | |fields, delta| { |
| 283 | fields.opacity(delta).top(px(space::SM * (delta - 1.))) |
| 284 | }, |
| 285 | ), |
| 286 | ), |
| 287 | ) |
| 288 | } |
| 289 | |
| 290 | fn saved_accounts(&self, entity: &Entity<Self>, palette: Palette, cx: &App) -> Option<Div> { |
| 291 | (!self.accounts.list.is_empty()).then(|| { |
| 292 | div() |
| 293 | .flex() |
| 294 | .flex_col() |
| 295 | .gap(px(space::XS)) |
| 296 | .pb(px(space::LG)) |
| 297 | .child(super::parts::caption_head(w().auth.saved, palette.tokens)) |
| 298 | .child(account_list(self, entity, false, None, palette, cx)) |
| 299 | }) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | fn tab( |
| 304 | palette: Palette, |
| 305 | entity: &Entity<Yumush>, |
| 306 | target: Route, |
| 307 | label: &'static str, |
| 308 | route: Route, |
| 309 | ) -> impl IntoElement { |
| 310 | let entity = entity.clone(); |
| 311 | segment( |
| 312 | clickable(label, palette, move |window, cx| { |
| 313 | entity.update(cx, |this, cx| this.change_page(target, window, cx)); |
| 314 | }), |
| 315 | route == target, |
| 316 | palette, |
| 317 | ) |
| 318 | .flex_1() |
| 319 | .text_size(text(text::CAPTION)) |
| 320 | .child(label) |
| 321 | } |