use super::input_field::SecureInputField;
use super::virtual_keyboard::VirtualKeyboard;
use egui::*;
use secure_types::SecureString;
pub struct CredentialsForm {
open: bool,
confrim_password: bool,
username: SecureInputField,
password: SecureInputField,
confirm_password: SecureInputField,
y_spacing: f32,
x_spacing: f32,
last_focused: &'static str,
virtual_keyboard: VirtualKeyboard,
}
impl CredentialsForm {
pub fn new() -> Self {
let username = SecureInputField::new("Username", false, true);
let password = SecureInputField::new("Password", true, true);
let confirm_password = SecureInputField::new("Confirm Password", true, true);
Self {
open: false,
confrim_password: false,
username,
password,
confirm_password,
y_spacing: 15.0,
x_spacing: 10.0,
last_focused: "Username",
virtual_keyboard: VirtualKeyboard::new(false),
}
}
pub fn is_open(&self) -> bool {
self.open
}
pub fn open(&mut self) {
self.open = true;
}
pub fn close(&mut self) {
self.open = false;
}
pub fn erase(&mut self) {
self.username.erase();
self.password.erase();
self.confirm_password.erase();
}
pub fn reset(&mut self) {
*self = Self::new();
}
pub fn is_confirm_password(&self) -> bool {
self.confrim_password
}
fn copy_passwd_to_confirm(&mut self) {
let new_text = self.password.text.clone();
self.confirm_password.set_text(new_text);
}
pub fn username(&self) -> SecureString {
self.username.text.clone()
}
pub fn password(&self) -> SecureString {
self.password.text.clone()
}
pub fn confirm_password(&self) -> SecureString {
self.confirm_password.text.clone()
}
pub fn with_open(mut self, open: bool) -> Self {
self.open = open;
self
}
pub fn with_enabled_virtual_keyboard(mut self) -> Self {
self.virtual_keyboard.open();
self
}
pub fn with_icon_size(mut self, size: Vec2) -> Self {
self.username.set_icon_size(size);
self.password.set_icon_size(size);
self.confirm_password.set_icon_size(size);
self
}
pub fn enable_virtual_keyboard(&mut self) {
self.virtual_keyboard.open();
}
pub fn disable_virtual_keyboard(&mut self) {
self.virtual_keyboard.close();
}
pub fn set_confirm_password(&mut self, confirm_password: bool) {
self.confrim_password = confirm_password;
}
pub fn set_min_size(&mut self, size: Vec2) {
self.username.set_min_size(size);
self.password.set_min_size(size);
self.confirm_password.set_min_size(size);
}
pub fn set_icon_size(&mut self, size: Vec2) {
self.username.set_icon_size(size);
self.password.set_icon_size(size);
self.confirm_password.set_icon_size(size);
}
pub fn set_username_text(&mut self, text: SecureString) {
self.username.set_text(text);
}
pub fn set_password_text(&mut self, text: SecureString) {
self.password.set_text(text);
}
pub fn set_confirm_password_text(&mut self, text: SecureString) {
self.confirm_password.set_text(text);
}
pub fn with_y_spacing(mut self, y_spacing: f32) -> Self {
self.y_spacing = y_spacing;
self
}
pub fn with_x_spacing(mut self, x_spacing: f32) -> Self {
self.x_spacing = x_spacing;
self
}
pub fn with_confirm_password(mut self, confirm_password: bool) -> Self {
self.confrim_password = confirm_password;
self
}
pub fn with_min_size(mut self, size: Vec2) -> Self {
let username = self.username.clone();
let password = self.password.clone();
let confirm_password = self.confirm_password.clone();
let username = username.min_size(size);
let password = password.min_size(size);
let confirm_password = confirm_password.min_size(size);
self.username = username;
self.password = password;
self.confirm_password = confirm_password;
self
}
pub fn show(&mut self, ui: &mut Ui) {
if !self.open {
return;
}
ui.vertical_centered(|ui| {
ui.spacing_mut().item_spacing.y = self.y_spacing;
ui.spacing_mut().item_spacing.x = self.x_spacing;
let user_res = self.username.show(ui);
let pass_res = self.password.show(ui);
let confirm_password_focused = if self.confrim_password {
let res = self.confirm_password.show(ui);
res.map(|res| res.response.gained_focus()).unwrap_or(false)
} else {
self.copy_passwd_to_confirm();
false
};
let username_focused = user_res.map(|res| res.response.gained_focus()).unwrap_or(false);
let password_focused = pass_res.map(|res| res.response.gained_focus()).unwrap_or(false);
if username_focused {
self.last_focused = "Username";
}
if password_focused {
self.last_focused = "Password";
}
if confirm_password_focused {
self.last_focused = "Confirm Password";
}
let target_text = match self.last_focused {
"Username" => &mut self.username.text,
"Password" => &mut self.password.text,
"Confirm Password" => &mut self.confirm_password.text,
_ => &mut self.username.text,
};
ui.add_space(10.0);
self.virtual_keyboard.show(target_text, ui);
});
}
}