-
Notifications
You must be signed in to change notification settings - Fork 6
/
todomvc.rs
150 lines (142 loc) · 6.08 KB
/
todomvc.rs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use bevy::prelude::*;
use derive_more::{Deref, DerefMut};
use std::sync::Arc;
use ui4::{prelude::*, widgets::vscroll_view};
#[derive(Default, Deref, Lens)]
struct EditedText(String);
#[derive(Default, Deref, DerefMut, Lens)]
struct TodoList(TrackedVec<Todo>);
#[derive(Clone)]
struct Todo {
text: Arc<str>,
done: bool,
}
fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
.add_plugin(Ui4Plugin)
.add_plugin(Ui4Root(root))
.init_resource::<EditedText>()
.init_resource::<TodoList>();
app.world.spawn().insert_bundle(UiCameraBundle::default());
app.run()
}
fn root(ctx: Ctx) -> Ctx {
ctx.with(UiColor(Color::BLACK))
.with(Right(Units::Stretch(1.)))
.with(Left(Units::Stretch(1.)))
.with(Width(Units::Pixels(400.)))
.with(Height(Units::Pixels(600.)))
.child(
text("Todos")
.with(TextSize(80.))
.with(Height(Units::Pixels(120.)))
.with(TextAlign(TextAlignment {
vertical: VerticalAlign::Top,
horizontal: HorizontalAlign::Center,
})),
)
.child(|ctx: Ctx| {
ctx.with(Height(Units::Auto))
.with(LayoutType::Row)
.with(Left(Units::Pixels(5.)))
.with(Right(Units::Pixels(5.)))
.with(ColBetween(Units::Pixels(5.)))
.with(Bottom(Units::Pixels(5.)))
.child(
textbox(res().lens(EditedText::F0))
.with(Width(Units::Stretch(9.)))
.with(Height(Units::Pixels(30.))),
)
.child(
button("Add")
.with(OnClick::new(|world: &mut World| {
let text = std::mem::take(
&mut world.get_resource_mut::<EditedText>().unwrap().0,
);
world.get_resource_mut::<TodoList>().unwrap().push(Todo {
text: text.into(),
done: false,
});
}))
.with(Width(Units::Stretch(1.)))
.with(Height(Units::Pixels(30.))),
)
})
.child(
vscroll_view(res::<TodoList>().lens(TodoList::F0).each(|item, index| {
move |ctx: &mut McCtx| {
ctx.c(todo(item, index));
}
}))
.with(Left(Units::Pixels(5.)))
.with(Right(Units::Pixels(5.))),
)
}
fn todo(item: impl WorldLens<Out = Todo>, index: IndexObserver) -> impl FnOnce(Ctx) -> Ctx {
move |ctx: Ctx| {
ctx.with(Height(Units::Pixels(30.)))
.with(LayoutType::Row)
.with(Left(Units::Pixels(5.)))
.with(Right(Units::Pixels(5.)))
.with(Bottom(Units::Pixels(2.)))
.with(
item.map(|t: &Todo| t.done)
.dedup()
.map(|&done: &bool| if done { Color::GREEN } else { Color::NONE })
.map(UiColor),
)
.child(
text(item.map(|t: &Todo| t.text.to_string()))
.with(TextSize(28.))
.with(TextAlign(TextAlignment {
vertical: VerticalAlign::Top,
horizontal: HorizontalAlign::Left,
})),
)
.children(
item.map(|todo: &Todo| todo.done)
.map_child(move |done: bool| {
move |ctx: &mut McCtx| {
if done {
ctx.c(|ctx: Ctx| {
ctx.with(Width(Units::Pixels(150.)))
.with(Height(Units::Pixels(30.)))
.with(LayoutType::Row)
.with(ColBetween(Units::Pixels(5.)))
.child(
button("Unmark")
.with(Width(Units::Stretch(1.)))
.with(Height(Units::Pixels(30.)))
.with(OnClick::new(move |world| {
item.get_mut(world).done = false;
})),
)
.child(
button("Remove")
.with(button::HoverColor(Color::RED))
.with(Width(Units::Stretch(1.)))
.with(Height(Units::Pixels(30.)))
.with(index.dedup().map(|&i: &usize| {
OnClick::new(move |world| {
let mut list = world
.get_resource_mut::<TodoList>()
.unwrap();
list.remove(i);
})
})),
)
});
} else {
ctx.c(button("Mark Complete")
.with(Width(Units::Pixels(150.)))
.with(Height(Units::Pixels(30.)))
.with(OnClick::new(move |world| {
item.get_mut(world).done = true;
})));
}
}
}),
)
}
}