Function rui::button

source ·
pub fn button<A: 'static, F: Fn(&mut Context) -> A + 'static + Clone>(
    view: impl View + Clone,
    f: F
) -> impl View
Expand description

Calls a function when the button is tapped.

Examples found in repository?
examples/gallery.rs (line 6)
3
4
5
6
7
8
fn button_example() -> impl View {
    hstack((
        caption("button"),
        button("press me", |_| println!("pressed")),
    ))
}
More examples
Hide additional examples
examples/counter.rs (lines 9-11)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    rui(state(
        || 1,
        |count, cx| {
            vstack((
                cx[count].padding(Auto),
                button("increment", move |cx| {
                    cx[count] += 1;
                })
                .padding(Auto),
            ))
        },
    ));
}
examples/counter2.rs (lines 9-11)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    rui(state(
        || 1,
        |count, cx| {
            vstack((
                format!("{}", cx[count]).padding(Auto),
                button("increment", move |cx| {
                    cx[count] += 1;
                })
                .padding(Auto),
                button("decrement", move |cx| {
                    cx[count] -= 1;
                })
                .padding(Auto),
            ))
        },
    ));
}
examples/todo_list.rs (lines 7-12)
3
4
5
6
7
8
9
10
11
12
13
14
15
fn add_button(todos: impl Binding<Vec<String>>) -> impl View {
    state(String::new, move |name, _| {
        hstack((
            text_editor(name),
            button(text("Add Item"), move |cx| {
                let name_str = cx[name].clone();
                todos.with_mut(cx, |todos| todos.push(name_str));
                // Gotta fix a bug in text_editor!
                // cx[name] = String::new();
            }),
        ))
    })
}
examples/async.rs (lines 12-18)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
    rui(state(
        || "task not started".to_string(),
        |s, cx| {
            hstack((
                button("press to begin", move |_| {
                    spawn(move || {
                        on_main(move |cx| cx[s] = "task started".into());
                        sleep(Duration::from_secs(2));
                        on_main(move |cx| cx[s] = "task complete".into());
                    });
                }),
                text(&cx[s]),
            ))
        },
    ));
}
examples/counter_list.rs (lines 11-13)
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
fn main() {
    rui(state(Counters::default, |counters, cx| {
        vstack((
            list(cx[counters].ids(), move |&i| {
                with_cx(move |cx| {
                    let count = bind(counters, CounterLens(i));
                    hstack((
                        format!("{}", count.get(cx)).padding(Auto),
                        button("increment", move |cx| {
                            *count.get_mut(cx) += 1;
                        })
                        .padding(Auto),
                        button("decrement", move |cx| {
                            *count.get_mut(cx) -= 1;
                        })
                        .padding(Auto),
                        button("remove", move |cx| cx[counters].remove_counter(i)).padding(Auto),
                    ))
                })
            }),
            format!("total: {}", cx[counters].sum_counters()).padding(Auto),
            button("add counter", move |cx| cx[counters].add_counter()).padding(Auto),
        ))
    }));
}