mirror of
https://github.com/danbulant/cushy
synced 2026-06-18 22:11:34 +00:00
Refs #98 This refactor overhauls the reactive system to move all the reactive methods to traits. The side effect of this change is that now DynamicReader's API is the same as Dynamic's API, but because it only implements Source<T>, DynamicReader does not offer any mutation functions. While it's unfortunate to have more traits to include to use Cushy, this seems like the best option, and it offers a path to try to integrate this into the tuple ForEach/MapEach traits. Unfortunately, my attempt at doing those in this set of changes led to issues specifying generic associated lifetimes for the DynamicGuard. But, I was also in the middle of this larger refactoring, so it might be that a fresh attempt will succeed.
88 lines
2.6 KiB
Rust
88 lines
2.6 KiB
Rust
use std::process::exit;
|
|
|
|
use cushy::value::{Dynamic, Source, Validations};
|
|
use cushy::widget::MakeWidget;
|
|
use cushy::widgets::input::{InputValue, MaskedString};
|
|
use cushy::widgets::layers::OverlayLayer;
|
|
use cushy::widgets::Expand;
|
|
use cushy::Run;
|
|
use figures::units::Lp;
|
|
|
|
fn main() -> cushy::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()
|
|
}
|