-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
171 lines (139 loc) · 4.42 KB
/
menu.cpp
File metadata and controls
171 lines (139 loc) · 4.42 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <kapalaframe-app/event.h>
#include <kapalaframe-app/inputs.h>
#include <kapalaframe-math/align.h>
#include "loader.h"
import Mdi;
import Kapalaframe.Ui;
import Kapalaframe.Kira;
namespace ShoolLoader {
// MARK: Model -----------------------------------------------------------------
struct State {
Configs configs;
Opt<String> error = NONE;
usize selected = 0;
};
struct MoveSelectionAction {
isize delta;
};
struct SelectAction {};
using Action = Union<MoveSelectionAction, SelectAction>;
static Ui::Task<Action> reduce(State& s, Action a) {
a.visit(Visitor{
[&](MoveSelectionAction a) {
if (s.selected == 0 and a.delta < 0)
s.selected = s.configs.entries.len() - 1;
else if (s.selected == s.configs.entries.len() - 1 and a.delta > 0)
s.selected = 0;
else
s.selected += a.delta;
},
[&](SelectAction) {
if (s.error)
s.error = NONE;
auto res = ShoolLoader::loadEntry(s.configs.entries[s.selected]);
if (not res)
s.error = String{res.none().msg()};
},
});
return NONE;
}
using Model = Ui::Model<State, Action, reduce>;
// MARK: Views -----------------------------------------------------------------
Ui::Child icon(Entry const& e) {
return e.icon.visit(Visitor{
[&](Gfx::Icon i) {
return Ui::icon(i, 64);
},
[&](Image::Picture i) {
return Ui::pinSize(64, Ui::image(i));
},
[&](None) {
return Ui::empty();
},
});
}
Ui::Child entry(State const& s, Entry const& e, usize i) {
auto style =
s.selected == i
? Ui::ButtonStyle::regular()
: Ui::ButtonStyle::secondary();
auto tile = Ui::vflow(
12,
Math::Align::CENTER,
icon(e),
Ui::titleMedium(e.name)
);
return tile |
Ui::center() |
Ui::pinSize({192, 128}) |
Ui::insets(12) |
Ui::button(Ui::SINK<>, style);
}
Ui::Child list(State const& s) {
auto items =
iter(s.configs.entries)
.mapi([&](Entry const& e, usize i) {
return entry(s, e, i);
})
.collect<Vec<Ui::Child>>();
return Ui::hflow(16, items);
}
Ui::Child alert(String title, String subtitle) {
return Ui::vflow(
16,
Math::Align::CENTER,
Kr::errorPage(Mdi::ALERT_DECAGRAM, title, subtitle) | Ui::grow(),
Ui::bodyMedium("Press [ENTER] to continue.")
) |
Ui::insets(64);
}
void intent(Ui::Node& n, App::Event& e) {
if (auto k = e.is<App::KeyboardEvent>()) {
if (k->key == App::Key::LEFT) {
Ui::bubble<Action>(n, MoveSelectionAction{-1});
e.accept();
} else if (k->key == App::Key::RIGHT) {
Ui::bubble<Action>(n, MoveSelectionAction{1});
e.accept();
} else if (k->key == App::Key::ENTER) {
Ui::bubble<Action>(n, SelectAction{});
e.accept();
}
}
}
Ui::Child menu(Configs const& c) {
if (c.entries.len() == 0)
return alert(
"No entries found."s,
"Please add an entry to the configuration file."s
);
return Ui::reducer<Model>(
{c},
[](State const& s) {
if (s.error)
return alert(
"System startup failed."s,
s.error.unwrap()
);
Ui::Children children;
if (s.configs.title)
children.pushBack(Ui::headlineLarge(*s.configs.title));
if (s.configs.subtitle)
children.pushBack(Ui::titleLarge(Ui::GRAY400, *s.configs.subtitle));
children.pushBack(list(s) | Ui::grow());
children.pushBack(Ui::labelMedium("Use ← and → to navigate, and press ENTER to select an entry."));
children.pushBack(Ui::labelSmall(Ui::GRAY500, "Powered by ShoolLoader ►"));
return Ui::vflow(
16,
Math::Align::CENTER,
children
) |
Ui::insets(64) |
Ui::intent(intent);
}
);
}
Async::Task<> showMenuAsync(Sys::Context& ctx, Configs const& c) {
co_return co_await Ui::runAsync(ctx, ShoolLoader::menu(c));
}
} // namespace Shoolloader