1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use rui::*;

// XXX: WIP

fn digit_button(title: &str, state: StateHandle<String>) -> impl View {
    let t = String::from(title);
    zstack((
        rectangle()
            .corner_radius(10.0)
            .color(RED_HIGHLIGHT)
            .tap(move |cx| cx[state].push_str(&t)),
        text(title).color(BLACK).offset([10.0, 10.0]),
    ))
    .padding(Auto)
}

fn calc_button(title: &str, callback: impl Fn(&mut Context) + 'static) -> impl View {
    zstack((
        rectangle()
            .corner_radius(10.0)
            .color(GREEN_HIGHLIGHT)
            .tap(callback),
        text(title).color(BLACK).offset([10.0, 10.0]),
    ))
    .padding(Auto)
}

fn main() {
    rui(state(
        || String::from("0"),
        |s, cx| {
            vstack((
                text(&cx[s].to_string()),
                hstack((
                    calc_button("AC", move |cx| cx[s] = "0".into()),
                    calc_button("+/-", |_| ()),
                    calc_button("%", |_| ()),
                    calc_button("/", |_| ()),
                )),
                hstack((
                    digit_button("7", s),
                    digit_button("8", s),
                    digit_button("9", s),
                    calc_button("*", |_| ()),
                )),
                hstack((
                    digit_button("4", s),
                    digit_button("5", s),
                    digit_button("6", s),
                    calc_button("-", |_| ()),
                )),
                hstack((
                    digit_button("1", s),
                    digit_button("2", s),
                    digit_button("3", s),
                    calc_button("+", |_| ()),
                )),
                hstack((
                    digit_button("0", s),
                    calc_button(".", move |cx| cx[s].push_str(".")),
                    calc_button("=", |_| ()),
                )),
            ))
        },
    ))
}