1use rui::*;
2
3fn digit_button(title: &str, state: StateHandle<String>) -> impl View {
6 let t = String::from(title);
7 zstack((
8 rectangle()
9 .corner_radius(10.0)
10 .color(RED_HIGHLIGHT)
11 .tap(move |cx| cx[state].push_str(&t)),
12 text(title).color(BLACK).offset([10.0, 10.0]),
13 ))
14 .padding(Auto)
15}
16
17fn calc_button(title: &str, callback: impl Fn(&mut Context) + 'static) -> impl View {
18 zstack((
19 rectangle()
20 .corner_radius(10.0)
21 .color(GREEN_HIGHLIGHT)
22 .tap(callback),
23 text(title).color(BLACK).offset([10.0, 10.0]),
24 ))
25 .padding(Auto)
26}
27
28fn main() {
29 rui(state(
30 || String::from("0"),
31 |s, cx| {
32 vstack((
33 text(&cx[s].to_string()),
34 hstack((
35 calc_button("AC", move |cx| cx[s] = "0".into()),
36 calc_button("+/-", |_| ()),
37 calc_button("%", |_| ()),
38 calc_button("/", |_| ()),
39 )),
40 hstack((
41 digit_button("7", s),
42 digit_button("8", s),
43 digit_button("9", s),
44 calc_button("*", |_| ()),
45 )),
46 hstack((
47 digit_button("4", s),
48 digit_button("5", s),
49 digit_button("6", s),
50 calc_button("-", |_| ()),
51 )),
52 hstack((
53 digit_button("1", s),
54 digit_button("2", s),
55 digit_button("3", s),
56 calc_button("+", |_| ()),
57 )),
58 hstack((
59 digit_button("0", s),
60 calc_button(".", move |cx| cx[s].push_str(".")),
61 calc_button("=", |_| ()),
62 )),
63 ))
64 },
65 ))
66}