1#![feature(type_alias_impl_trait, impl_trait_in_assoc_type)]
2
3use nuit::{Text, VStack, View, Bind, Button, State};
4
5#[derive(Bind)]
6struct IncrementView<F> {
7 action: F,
8}
9
10impl<F> IncrementView<F> {
11 pub fn new(action: F) -> Self {
12 Self { action }
13 }
14}
15
16impl<F> View for IncrementView<F> where F: Fn() + Clone + 'static {
17 type Body = impl View;
18
19 fn body(&self) -> Self::Body {
20 let action = self.action.clone();
21 Button::with_text("Increment", move || {
22 action()
23 })
24 }
25}
26
27#[derive(Bind, Default)]
28struct CounterView {
29 count: State<i32>,
30}
31
32impl View for CounterView {
33 type Body = impl View;
34
35 fn body(&self) -> Self::Body {
36 let count = self.count.clone();
37 VStack::new((
38 Text::new(format!("Count: {}", count.get())),
39 IncrementView::new(move || {
40 count.set(count.get() + 1);
41 })
42 ))
43 }
44}
45
46fn main() {
47 nuit::run_app(CounterView::default());
48}