use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::{ AnimationExt as _, App, Context, Div, Entity, FontWeight, IntoElement, ParentElement, Styled, Window, assets::IconName, component::input::{Input, InputEvent, InputState}, div, px, }; use super::{ Route, Yumush, account_list::account_list, clickable, connection_notice, new_input, notify_error, parts, settings_page::{segment, segment_track}, style::{Tokens, radius, size, space, text}, widget::{field, frame}, }; use crate::lang::w; pub struct Auth { server_input: Entity, any_cert: bool, server_open: bool, username_input: Entity, password_input: Entity, invite_input: Entity, } impl Auth { pub fn new(server: &crate::Server, window: &mut Window, cx: &mut Context) -> Self { let server_input = new_input(w().auth.server, window, cx); server_input.update(cx, |input, cx| input.set_value(&server.address, window, cx)); cx.subscribe_in(&server_input, window, |this, _, event, _, cx| { if matches!(event, InputEvent::PressEnter { .. }) { apply_server(this, cx); } }) .detach(); let username_input = new_input(w().auth.username_placeholder, window, cx); let password_input = new_input(w().auth.password_placeholder, window, cx); let invite_input = new_input(w().auth.invite_placeholder, window, cx); password_input.update(cx, |input, cx| input.set_masked(true, window, cx)); username_input.update(cx, |input, cx| input.focus(window, cx)); for input in [&username_input, &password_input, &invite_input] { cx.subscribe_in(input, window, |this, _, event, window, cx| { if matches!(event, InputEvent::PressEnter { .. }) { submit(this, window, cx); } }) .detach(); } Self { server_input, any_cert: server.any_cert, server_open: false, username_input, password_input, invite_input, } } pub fn focus(&self, window: &mut Window, cx: &mut Context) { self.username_input .update(cx, |input, cx| input.focus(window, cx)); } } fn apply_server(this: &mut Yumush, cx: &mut Context) { let address = this.auth.server_input.read(cx).text().to_string(); let server = crate::Server { address: address.trim().to_string(), any_cert: this.auth.any_cert, }; if server.address.is_empty() || !this.network.set_server(server.clone()) { return; } server.save(); this.accounts = crate::accounts::load(&server.address); this.server = None; this.connection = super::Connection::Connecting; cx.notify(); } fn submit(this: &mut Yumush, window: &mut Window, cx: &mut Context) { apply_server(this, cx); let username = this.auth.username_input.read(cx).text().to_string(); let password = this.auth.password_input.read(cx).text().to_string(); let invite = this.auth.invite_input.read(cx).text().to_string(); let invite = invite.trim().to_string(); let route = this.current_route; let network = this.network.clone(); cx.spawn_in(window, async move |this, cx| { let result = if route == Route::Register { let invite = (!invite.is_empty()).then_some(invite); network .register(&username, &password, invite.as_deref()) .await } else { network.login(&username, &password).await }; match result { Ok((user, token)) => { let _ = this.update_in(cx, |yumush, window, cx| { yumush .auth .password_input .update(cx, |input, cx| input.set_value("", window, cx)); yumush.enter_session(user, token, window, cx); }); } Err(error) => notify_error(&this, cx, error), } }) .detach(); } fn server_toggle( entity: &Entity, address: &str, open: bool, t: Tokens, ) -> impl IntoElement { let entity = entity.clone(); clickable("server_toggle", t, move |_, cx| { entity.update(cx, |yumush, cx| { yumush.auth.server_open = !yumush.auth.server_open; cx.notify(); }) }) .self_center() .px(px(space::SM)) .py(px(space::XXS)) .rounded(px(radius::MD)) .flex() .items_center() .gap(px(space::XS)) .text_size(t.text(text::CAPTION)) .text_color(t.dim()) .child(host(address).to_string()) .child( super::parts::small_icon(IconName::ChevronRight).with_animation( ("server_chevron", open as u64), super::parts::motion(), move |icon, delta| { let turn = if open { delta } else { 1. - delta }; icon.rotate(gpui_kit::radians(std::f32::consts::FRAC_PI_2 * turn)) }, ), ) } fn host(address: &str) -> &str { match address.rsplit_once(':') { Some((host, port)) if port.chars().all(|c| c.is_ascii_digit()) => { host.trim_start_matches('[').trim_end_matches(']') } _ => address, } } fn password_field(t: Tokens, input: &Entity) -> Div { frame(t, Input::new(input).mask_toggle()) } impl Yumush { pub(super) fn auth_page(&self, route: Route, cx: &mut Context) -> impl IntoElement { let t = Tokens::of(cx); let action_label = if route == Route::Register { w().auth.register } else { w().auth.log_in }; let entity = cx.entity(); let submit_entity = entity.clone(); let action_button = parts::button( "auth_button", action_label, parts::Press::Primary, t, move |window, cx| { submit_entity.update(cx, |this, cx| submit(this, window, cx)); }, ) .justify_center(); let server = self.server.as_ref(); let registration_open = server.is_none_or(|settings| settings.registration_open); let maintenance = server.is_some_and(|settings| settings.maintenance); let title = server.map_or_else(|| "yumush".to_string(), |settings| settings.name.clone()); let tabs = segment_track(t) .child(tab(t, &entity, Route::Login, w().auth.log_in, route)) .child(tab(t, &entity, Route::Register, w().auth.register, route)); let invite = (route == Route::Register && !registration_open) .then(|| field(t, &self.auth.invite_input)); div() .flex() .flex_col() .items_center() .justify_center() .size_full() .child( div() .w(px(size::POPOVER)) .p(px(space::XL)) .flex() .flex_col() .gap(px(space::MD)) .child( div() .text_size(t.text(text::BODY)) .font_weight(FontWeight::SEMIBOLD) .child(title), ) .children(maintenance.then(|| { div() .text_size(t.text(text::CAPTION)) .text_color(t.dim()) .child(w().auth.maintenance) })) .children(self.saved_accounts(&entity, t, cx)) .child(tabs) .child(field(t, &self.auth.username_input)) .child(password_field(t, &self.auth.password_input)) .children(invite) .child(action_button) .children(connection_notice(&self.connection, t)) .child(server_toggle( &entity, &self.network.server().address, self.auth.server_open, t, )) .child( div() .relative() .when(!self.auth.server_open, |this| this.invisible()) .flex() .flex_col() .gap(px(space::SM)) .child(field(t, &self.auth.server_input)) .child( div() .flex() .items_center() .justify_between() .text_size(t.text(text::CAPTION)) .text_color(t.dim()) .child(w().auth.accept_any_cert) .child(super::parts::toggle( "any_cert_switch", self.auth.any_cert, t, { let entity = entity.clone(); move |_, cx| { entity.update(cx, |yumush, cx| { yumush.auth.any_cert = !yumush.auth.any_cert; apply_server(yumush, cx); cx.notify(); }) } }, )), ) .with_animation( ("server_fields", self.auth.server_open as u64), super::parts::motion(), |fields, delta| { fields.opacity(delta).top(px(space::SM * (delta - 1.))) }, ), ), ) } fn saved_accounts(&self, entity: &Entity, t: Tokens, cx: &App) -> Option
{ (!self.accounts.list.is_empty()).then(|| { div() .flex() .flex_col() .gap(px(space::XS)) .pb(px(space::LG)) .child(super::parts::caption_head(w().auth.saved, t)) .child(account_list(self, entity, None, t, cx)) }) } } fn tab( t: Tokens, entity: &Entity, target: Route, label: &'static str, route: Route, ) -> impl IntoElement { let entity = entity.clone(); segment( clickable(label, t, move |window, cx| { entity.update(cx, |this, cx| this.change_page(target, window, cx)); }), route == target, t, ) .flex_1() .text_size(t.text(text::CAPTION)) .child(label) }