-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathnode.h
276 lines (204 loc) · 5.85 KB
/
node.h
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#pragma once
#include <karm-app/event.h>
#include <karm-base/checked.h>
#include <karm-base/func.h>
#include <karm-base/hash.h>
#include <karm-gfx/canvas.h>
#include <karm-logger/logger.h>
#include <karm-sys/async.h>
#include "atoms.h"
#include "macros.h"
namespace Karm::Ui {
enum struct Hint {
MIN,
PREFERRED,
MAX,
};
struct Node;
using Child = Rc<Node>;
using Children = Vec<Child>;
using Visitor = Func<void(Node&)>;
// MARK: Node ------------------------------------------------------------------
using Key = Opt<Hash>;
struct Node : public App::Dispatch {
Key _key = NONE;
bool _consumed = false;
struct PaintEvent {
Math::Recti bound;
};
struct LayoutEvent {
};
struct AnimateEvent {
f64 dt;
};
Key key() const {
return _key;
}
virtual Opt<Child> reconcile(Child other) { return other; }
virtual void paint(Gfx::Canvas&, Math::Recti) {}
virtual void layout(Math::Recti) {}
virtual Math::Vec2i size(Math::Vec2i s, Hint) { return s; }
virtual Math::Recti bound() { panic("bound() not implemented"); }
virtual Node* parent() { return nullptr; }
virtual void attach(Node*) {}
virtual void detach(Node*) {}
};
inline auto key(Hashable auto const& key) {
return [key](Child child) {
child->_key = hash(key);
return child;
};
}
template <typename T>
concept Decorator = requires(T& t, Child& c) {
{ t(c) } -> Meta::Same<Child>;
};
always_inline Child operator|(Child child, Decorator auto decorator) {
return decorator(child);
}
always_inline Child& operator|=(Child& child, Decorator auto decorator) {
return child = decorator(child);
}
always_inline auto operator|(Decorator auto decorator, Decorator auto decorator2) {
return [=](Child child) {
return decorator2(decorator(child));
};
}
// MARK: LeafNode --------------------------------------------------------------
template <typename Crtp>
struct LeafNode : public Node {
Node* _parent = nullptr;
virtual void reconcile(Crtp&) {}
Opt<Child> reconcile(Child other) override {
// NOTE: Nodes should never be part of a state, to
// ensure this we check that nodes are not
// reused accross rebuilds
if (other->_consumed)
panic("reconcile() called on consumed node, did you forget to wrap the node in a slot?");
if (this == &other.unwrap())
panic("reconcile() called on self, did you forget to wrap the node in a slot?");
if (not other.is<Crtp>() or _key != other->key())
return other;
reconcile(other.unwrap<Crtp>());
other->_consumed = true;
return NONE;
}
void bubble(App::Event& e) override {
if (_parent and not e.accepted())
_parent->bubble(e);
}
void event(App::Event&) override {
}
Node* parent() override {
return _parent;
}
void attach(Node* parent) override {
_parent = parent;
}
void detach(Node* parent) override {
if (_parent == parent)
_parent = nullptr;
}
};
// MARK: GroupNode -------------------------------------------------------------
template <typename Crtp>
struct GroupNode : public LeafNode<Crtp> {
Children _children;
Math::Recti _bound{};
GroupNode() = default;
GroupNode(Children children) : _children(children) {
for (auto& c : _children) {
c->attach(this);
}
}
~GroupNode() {
for (auto& c : _children) {
c->detach(this);
}
}
Children& children() {
return _children;
}
Children const& children() const {
return _children;
}
void reconcile(Crtp& o) override {
auto& us = children();
auto& them = o.children();
for (usize i = 0; i < them.len(); i++) {
if (i < us.len()) {
us.replace(i, us[i]->reconcile(them[i]).unwrapOr(us[i]));
} else {
us.insert(i, them[i]);
}
us[i]->attach(this);
}
us.trunc(them.len());
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
for (auto& child : children()) {
if (not child->bound().colide(r))
continue;
child->paint(g, r);
}
}
void event(App::Event& e) override {
if (e.accepted())
return;
for (auto& child : children()) {
child->event(e);
if (e.accepted())
return;
}
}
void layout(Math::Recti r) override {
_bound = r;
for (auto& child : children())
child->layout(r);
}
Math::Recti bound() override {
return _bound;
}
};
// MARK: ProxyNode -------------------------------------------------------------
template <typename Crtp>
struct ProxyNode : public LeafNode<Crtp> {
Child _child;
ProxyNode(Child child) : _child(child) {
_child->attach(this);
}
~ProxyNode() {
_child->detach(this);
}
Node& child() {
return *_child;
}
Node const& child() const {
return *_child;
}
void reconcile(Crtp& o) override {
_child = _child->reconcile(o._child).unwrapOr(_child);
_child->attach(this);
LeafNode<Crtp>::reconcile(o);
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
child().paint(g, r);
}
void event(App::Event& e) override {
if (e.accepted())
return;
child().event(e);
}
void layout(Math::Recti r) override {
child().layout(r);
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
return child().size(s, hint);
}
Math::Recti bound() override {
return _child->bound();
}
};
using Slot = Func<Child()>;
using Slots = Func<Children()>;
} // namespace Karm::Ui