-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathdialog.cpp
158 lines (126 loc) · 3.87 KB
/
dialog.cpp
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
#include "dialog.h"
#include "anim.h"
#include "funcs.h"
namespace Karm::Ui {
void showDialog(Node& n, Child child) {
bubble<ShowDialogEvent>(n, child);
}
struct CloseDialogEvent {
};
void closeDialog(Node& n) {
bubble<CloseDialogEvent>(n);
}
struct DialogLayer : public LeafNode<DialogLayer> {
Easedf _visibility{};
Child _child;
Opt<Child> _dialog;
Opt<Child> _shouldShow;
bool _shouldDialogClose = false;
DialogLayer(Child child) : _child(child) {
_child->attach(this);
}
~DialogLayer() {
if (_dialog)
(*_dialog)->detach(this);
_child->detach(this);
}
void _showDialog(Child child) {
// We need to defer showing the dialog until the next frame,
// otherwise replacing the dialog might cause some use after free down the tree
_shouldShow = child;
mouseLeave(*_child);
shouldLayout(*this);
_visibility.animate(*this, 1, 0.3, Math::Easing::exponentialOut);
}
void _closeDialog() {
// We need to defer closing the dialog until the next frame,
// otherwise we might cause some use after free down the tree
_shouldDialogClose = true;
shouldLayout(*this);
_visibility.animate(*this, 0, 0.1);
}
void reconcile(DialogLayer& o) override {
_child = _child->reconcile(o._child).unwrapOr(_child);
_child->attach(this);
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
_child->paint(g, r);
if (_visibility.value() > 0.001) {
g.push();
g.fillStyle(Ui::GRAY950.withOpacity(0.8 * _visibility.value()));
g.fill(bound());
g.pop();
}
if (_dialog) {
g.push();
// change the orgin to the center of the screen
g.translate(bound().center().cast<f64>());
auto scale = Math::lerp(0.9, 1.0, _visibility.value());
g.scale(scale);
g.translate(-bound().center().cast<f64>());
(*_dialog)->paint(g, r);
g.pop();
}
}
void event(App::Event& e) override {
if (e.accepted())
return;
if (_visibility.needRepaint(*this, e))
Ui::shouldRepaint(*this);
auto ke = e.is<App::KeyboardEvent>();
if (ke and ke->type == App::KeyboardEvent::PRESS and ke->key == App::Key::ESC) {
_closeDialog();
e.accept();
} else if (auto se = e.is<ShowDialogEvent>()) {
_showDialog(se->child);
e.accept();
} else if (e.is<CloseDialogEvent>()) {
_closeDialog();
e.accept();
} else if (_dialog) {
(*_dialog)->event(e);
} else {
_child->event(e);
}
}
void bubble(App::Event& e) override {
if (auto se = e.is<ShowDialogEvent>()) {
_showDialog(se->child);
e.accept();
} else if (e.is<CloseDialogEvent>()) {
_closeDialog();
e.accept();
}
LeafNode<DialogLayer>::bubble(e);
}
void layout(Math::Recti r) override {
if (_shouldDialogClose) {
if (_dialog) {
(*_dialog)->detach(this);
_dialog = NONE;
}
_shouldDialogClose = false;
}
if (_shouldShow) {
if (_dialog) {
(*_dialog)->detach(this);
}
_dialog = _shouldShow;
(*_dialog)->attach(this);
_shouldShow = NONE;
}
_child->layout(r);
if (_dialog)
(*_dialog)->layout(r);
}
Math::Vec2i size(Math::Vec2i s, Hint hint) override {
return _child->size(s, hint);
}
Math::Recti bound() override {
return _child->bound();
}
};
Child dialogLayer(Child child) {
return makeRc<DialogLayer>(child);
}
} // namespace Karm::Ui