cushy/examples/focus.rs
Jonathan Johnson 2201f2c83b
Ranged sliders, advance_focus, allow_blur
Closes #60

Stepping in sliders is a compromise due to the flexibility of the
current slider implementation. I don't want to force types to implement
Add, and I don't like forcing types to require a Step (ie, what's the
appropriate value for f32 to specify as its next value?). Using a
percentage combined with lerp keeps the implementation fairly
straightfoward, although I remember experiencing this type of
configuration in another UI framework a long time ago and thinking it
was a little annoying to work with.

Ultimately, setting actual step boundaries can be done by customizing
the type that the slider is operating over. I feel like that's a much
more powerful design than I've experienced in previous frameworks, so
I'm hoping this percent step behavior is a reasonable compromise.
2023-11-20 19:44:03 -08:00

35 lines
1.1 KiB
Rust

use gooey::value::Dynamic;
use gooey::widget::MakeWidget;
use gooey::widgets::input::InputValue;
use gooey::widgets::slider::Slidable;
use gooey::widgets::{Checkbox, Custom};
use gooey::Run;
use kludgine::figures::units::Lp;
fn main() -> gooey::Result {
let allow_blur = Dynamic::new(true);
"Input Field"
.and(Dynamic::<String>::default().into_input())
.and("Range Slider")
.and(Dynamic::<u8>::default().slider_between(0_u8, 100_u8))
.and("Range Slider")
.and(Dynamic::new(10..=30).slider_between(0_u8, 100_u8))
.and(Checkbox::new(
allow_blur.clone(),
"Allow Custom Widget to Lose Focus",
))
.and(
Custom::empty()
.on_accept_focus(|_| true)
.on_redraw(|context| {
context.fill(context.theme().secondary.color);
if context.focused() {
context.draw_focus_ring();
}
})
.on_allow_blur(move |_| allow_blur.get())
.height(Lp::inches(1)),
)
.into_rows()
.run()
}