-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathhost.h
150 lines (113 loc) · 3.34 KB
/
host.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
#pragma once
#include <karm-app/host.h>
#include <karm-base/ring.h>
#include <karm-gfx/cpu/canvas.h>
#include <karm-sys/time.h>
#include <karm-text/loader.h>
#include "node.h"
namespace Karm::Ui {
static constexpr auto FRAME_RATE = 60;
static constexpr auto FRAME_TIME = 1.0 / FRAME_RATE;
struct Host : public Node {
Child _root;
Opt<Res<>> _res;
Gfx::CpuCanvas _g;
Vec<Math::Recti> _dirty;
bool _shouldLayout{};
bool _shouldAnimate{};
Host(Child root) : _root(root) {
_root->attach(this);
}
~Host() {
_root->detach(this);
}
virtual Gfx::MutPixels mutPixels() = 0;
Gfx::Pixels pixels() { return mutPixels(); }
virtual void flip(Slice<Math::Recti> regions) = 0;
virtual Res<> wait(Instant) = 0;
bool alive() {
return not _res;
}
Math::Recti bound() override {
return pixels().bound();
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
g.push();
g.clip(r);
g.clear(r, GRAY950);
g.fillStyle(GRAY50);
_root->paint(g, r);
g.pop();
}
void paint() {
_g.begin(mutPixels());
for (auto& d : _dirty) {
paint(_g, d);
}
_g.end();
flip(_dirty);
_dirty.clear();
}
void layout(Math::Recti r) override {
_root->layout(r);
}
void event(App::Event& event) override {
_root->event(event);
}
void bubble(App::Event& event) override {
if (auto e = event.is<Node::PaintEvent>()) {
_dirty.pushBack(e->bound);
event.accept();
} else if (auto e = event.is<Node::LayoutEvent>()) {
_shouldLayout = true;
event.accept();
} else if (auto e = event.is<Node::AnimateEvent>()) {
_shouldAnimate = true;
event.accept();
} else if (auto e = event.is<App::RequestExitEvent>()) {
_res = e->res;
event.accept();
}
if (not event.accepted()) {
logWarn("unhandled event, bouncing down");
_root->event(event);
}
}
Res<> run() {
_shouldLayout = true;
auto lastFrame = Sys::instant();
auto nextFrame = lastFrame;
bool nextFrameScheduled = false;
auto scheduleFrame = [&] {
auto instant = Sys::instant();
if (instant < nextFrame)
return false;
while (nextFrame < instant)
nextFrame += Duration::fromMSecs(FRAME_TIME * 1000);
lastFrame = nextFrame;
nextFrameScheduled = true;
return true;
};
while (not _res) {
if (_shouldAnimate and scheduleFrame()) {
_shouldAnimate = false;
auto e = App::makeEvent<Node::AnimateEvent>(FRAME_TIME);
event(*e);
}
if (_shouldLayout) {
layout(bound());
_shouldLayout = false;
_shouldAnimate = true;
_dirty.pushBack(bound());
}
if (_dirty.len() > 0) {
paint();
_dirty.clear();
}
try$(wait(nextFrameScheduled ? nextFrame : Instant::endOfTime()));
nextFrameScheduled = false;
}
return _res.unwrap();
}
};
} // namespace Karm::Ui