/
login.rs
88 lines (81 loc) · 2.6 KB
/
login.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::process::exit;
use gooey::value::{Dynamic, Validations};
use gooey::widget::MakeWidget;
use gooey::widgets::input::{InputValue, MaskedString};
use gooey::widgets::layers::OverlayLayer;
use gooey::widgets::Expand;
use gooey::Run;
use kludgine::figures::units::Lp;
fn main() -> gooey::Result {
let tooltips = OverlayLayer::default();
let username = Dynamic::default();
let password = Dynamic::default();
let validations = Validations::default();
let username_field = "Username"
.align_left()
.and(
username
.clone()
.into_input()
.placeholder("Username")
.validation(validations.validate(&username, |u: &String| {
if u.is_empty() {
Err("usernames must contain at least one character")
} else {
Ok(())
}
}))
.hint("* required")
.tooltip(
&tooltips,
"If you can't remember your username, that's because this is a demo.",
),
)
.into_rows();
let password_field = "Password"
.align_left()
.and(
password
.clone()
.into_input()
.placeholder("Password")
.validation(
validations.validate(&password, |u: &MaskedString| match u.len() {
0..=7 => Err("passwords must be at least 8 characters long"),
_ => Ok(()),
}),
)
.hint("* required, 8 characters min")
.tooltip(&tooltips, "Passwords are always at least 8 bytes long."),
)
.into_rows();
let buttons = "Cancel"
.into_button()
.on_click(|_| {
eprintln!("Login cancelled");
exit(0)
})
.into_escape()
.tooltip(&tooltips, "This button quits the program")
.and(Expand::empty())
.and(
"Log In"
.into_button()
.on_click(validations.when_valid(move |()| {
println!("Welcome, {}", username.get());
exit(0);
}))
.into_default(),
)
.into_columns();
let ui = username_field
.and(password_field)
.and(buttons)
.into_rows()
.contain()
.width(Lp::inches(3)..Lp::inches(6))
.pad()
.scroll()
.centered();
ui.and(tooltips).into_layers().run()
}