state

Function state 

Source
pub fn state<S: 'static, V: View, D: Fn() -> S + 'static, F: Fn(StateHandle<S>, &Context) -> V + 'static>(
    initial: D,
    f: F,
) -> impl View
Expand description

State allows you to associate some state with a view. This is what you’ll use for a data model, as well as per-view state. Your state should be efficiently clonable. Use Rc as necessary.

initial is the initial value for your state.

f callback which is passed a State<S>

Examples found in repository?
examples/toggle.rs (line 4)
3fn main() {
4    rui(state(|| false, |s, _| toggle(s)));
5}
More examples
Hide additional examples
examples/gallery.rs (line 11)
10fn slider_example() -> impl View {
11    hstack((caption("slider"), state(|| 0.5, |s, _| hslider(s))))
12}
13
14fn caption(s: &'static str) -> impl View {
15    s.font_size(12).padding(Auto)
16}
17
18fn knob_example() -> impl View {
19    hstack((
20        caption("knob"),
21        state(|| 0.5, |s, _| knob(s).size([30.0, 30.0]).padding(Auto)),
22    ))
23}
24
25fn toggle_example() -> impl View {
26    hstack((
27        caption("toggle"),
28        state(|| false, |s, _| toggle(s).size([30.0, 30.0]).padding(Auto)),
29    ))
30}
31
32fn text_editor_example() -> impl View {
33    hstack((
34        caption("text_editor"),
35        state(
36            || "edit me".to_string(),
37            |txt, _| text_editor(txt).padding(Auto),
38        ),
39    ))
40}
examples/slider.rs (lines 19-25)
18fn main() {
19    rui(state(MyState::default, |state_handle, cx| {
20        map(
21            cx[state_handle].value,
22            move |v, cx| cx[state_handle].value = v,
23            |s, _| my_slider(s),
24        )
25    }));
26}
examples/lens.rs (lines 11-18)
10fn main() {
11    rui(state(MyState::default, |state, cx| {
12        vstack((
13            cx[state].value.font_size(10).padding(Auto),
14            hslider(bind(state, ValueLens {}))
15                .thumb_color(RED_HIGHLIGHT)
16                .padding(Auto),
17        ))
18    }));
19}
examples/font_size.rs (lines 4-14)
3fn main() {
4    rui(state(
5        || 0.0,
6        |size, cx| {
7            let s = (cx[size] * 100.0) as u32;
8            vstack((
9                "58".font_size(s),
10                format!("font size: {}", s),
11                hslider(size),
12            ))
13        },
14    ));
15}
examples/counter.rs (lines 4-15)
3fn main() {
4    rui(state(
5        || 1,
6        |count, cx| {
7            vstack((
8                cx[count].padding(Auto),
9                button("increment", move |cx| {
10                    cx[count] += 1;
11                })
12                .padding(Auto),
13            ))
14        },
15    ));
16}