use std::rc::Rc; use cursive::view::{Nameable, Resizable}; use cursive::views::{Button, Dialog, EditView, LinearLayout, TextView}; use crate::screen::{LoginScreen, Screen}; use crate::{get_content, quit, secret_input, signup, ClientBox}; pub struct SignupScreen { client: ClientBox, } impl Screen for SignupScreen { const NAME: &'static str = "signup"; fn construct(client: ClientBox) -> Self { Self { client } } fn render_dialog(&self) -> Dialog { let username_input = EditView::new() .on_submit(|s, _| { s.focus_name("password").unwrap(); }) .content(Rc::clone(&self.client).borrow().username()) .with_name("username") .fixed_width(20); let password_input = secret_input() .on_submit(|s, _| { s.focus_name("confirm").unwrap(); }) .with_name("password") .fixed_width(20); let cl = Rc::clone(&self.client); let confirm_input = secret_input() .on_submit(move |s, confirm| { let username = &s.call_on_name("username", get_content).unwrap(); let password = &s.call_on_name("password", get_content).unwrap(); signup(s, &cl, username, password, confirm); }) .with_name("confirm") .fixed_width(20); let cl = Rc::clone(&self.client); let cl2 = Rc::clone(&self.client); Dialog::around( LinearLayout::vertical() .child(TextView::new("username:")) .child(username_input) .child(TextView::new("password:")) .child(password_input) .child(TextView::new("confirm password:")) .child(confirm_input) .child(Button::new("Sign Up", move |s| { let username = &s.call_on_name("username", get_content).unwrap(); let password = &s.call_on_name("password", get_content).unwrap(); let confirm = &s.call_on_name("confirm", get_content).unwrap(); signup(s, &cl, username, password, confirm); })) .child(Button::new("Previously registered?", move |s| { LoginScreen::new(&cl2).replace(s) })) .child(Button::new("Quit", quit)), ) .title("Sign Up") } }