cushy/examples/focus-order.rs
Jonathan Johnson e70e92726c
Source<T> + Destination<T> (breaking)
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.
2024-01-02 09:00:29 -08:00

75 lines
2.2 KiB
Rust

use std::process::exit;
use cushy::value::{Dynamic, MapEach, Source};
use cushy::widget::{MakeWidget, MakeWidgetWithTag, WidgetTag};
use cushy::widgets::grid::{Grid, GridDimension, GridWidgets};
use cushy::widgets::input::{InputValue, MaskedString};
use cushy::widgets::Expand;
use cushy::Run;
use figures::units::Lp;
/// This example is the same as login, but it has an explicit tab order to
/// change from the default order (username, password, cancel, log in) to
/// username, password, log in, cancel.
fn main() -> cushy::Result {
let username = Dynamic::default();
let password = Dynamic::default();
let valid =
(&username, &password).map_each(|(username, password)| validate(username, password));
let (login_tag, login_id) = WidgetTag::new();
let (cancel_tag, cancel_id) = WidgetTag::new();
let (username_tag, username_id) = WidgetTag::new();
let username_row = (
"Username",
username.clone().into_input().make_with_tag(username_tag),
);
let password_row = (
"Password",
password.clone().into_input().with_next_focus(login_id),
);
let buttons = "Cancel"
.into_button()
.on_click(|_| {
eprintln!("Login cancelled");
exit(0)
})
.make_with_tag(cancel_tag)
.into_escape()
.with_next_focus(username_id)
.and(Expand::empty())
.and(
"Log In"
.into_button()
.on_click(move |_| {
println!("Welcome, {}", username.get());
exit(0);
})
.make_with_tag(login_tag)
.with_enabled(valid)
.into_default()
.with_next_focus(cancel_id),
)
.into_columns();
Grid::from_rows(GridWidgets::from(username_row).and(password_row))
.dimensions([
GridDimension::FitContent,
GridDimension::Fractional { weight: 1 },
])
.and(buttons)
.into_rows()
.contain()
.width(Lp::points(300)..Lp::points(600))
.scroll()
.centered()
.run()
}
fn validate(username: &String, password: &MaskedString) -> bool {
!username.is_empty() && !password.is_empty()
}