-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathpopover.cpp
131 lines (103 loc) · 3.24 KB
/
popover.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
#include "popover.h"
#include "dialog.h"
#include "funcs.h"
namespace Karm::Ui {
struct ShowPopoverEvent {
Math::Vec2i at;
Child child;
};
struct ClosePopoverEvent {};
void showPopover(Node& n, Math::Vec2i at, Child child) {
bubble<ShowPopoverEvent>(n, at, child);
}
void closePopover(Node& n) {
bubble<ClosePopoverEvent>(n);
}
struct PopoverLayer : public ProxyNode<PopoverLayer> {
Opt<Child> _popover;
Opt<Child> _shouldPopover;
bool _shouldPopoverClose = false;
Math::Vec2i _popoverAt;
using ProxyNode<PopoverLayer>::ProxyNode;
~PopoverLayer() {
if (_popover)
(*_popover)->detach(this);
}
void _showPopover(Child child, Math::Vec2i at) {
// We need to defer showing the dialog until the next frame,
// otherwise replacing the dialog might cause some use after free down the tree
_shouldPopover = child;
_popoverAt = at;
shouldLayout(*this);
}
void _closePopover() {
// We need to defer closing the dialog until the next frame,
// otherwise we might cause some use after free down the tree
_shouldPopoverClose = true;
shouldLayout(*this);
}
Math::Recti _positionPopover(Math::Recti r) {
// Position the popover at the given point, but make sure it fits in the screen
auto size = (*_popover)->size(r.size(), Hint::MIN);
auto pos = _popoverAt;
pos.y = clamp(pos.y, 0, r.size().y - size.y);
if (pos.x + size.x > r.end())
pos.x = pos.x - size.x;
return {pos, size};
}
void paint(Gfx::Canvas& g, Math::Recti r) override {
ProxyNode::paint(g, r);
if (_popover)
(*_popover)->paint(g, r);
}
void event(App::Event& e) override {
if (e.accepted())
return;
bool allowToGoDown = e.is<ShowDialogEvent>();
if (_popover and not allowToGoDown) {
(*_popover)->event(e);
if (e.accepted())
return;
auto me = e.is<App::MouseEvent>();
if (me and me->type == App::MouseEvent::PRESS) {
_closePopover();
e.accept();
}
} else {
ProxyNode::event(e);
}
}
void bubble(App::Event& event) override {
if (auto e = event.is<ShowPopoverEvent>()) {
_showPopover(e->child, e->at);
event.accept();
} else if (event.is<ClosePopoverEvent>()) {
_closePopover();
event.accept();
}
LeafNode<PopoverLayer>::bubble(event);
}
void layout(Math::Recti r) override {
ProxyNode::layout(r);
if (_shouldPopoverClose) {
if (_popover) {
(*_popover)->detach(this);
_popover = NONE;
}
_shouldPopoverClose = false;
}
if (_shouldPopover) {
if (_popover)
(*_popover)->detach(this);
_popover = _shouldPopover;
(*_popover)->attach(this);
_shouldPopover = NONE;
}
if (_popover)
(*_popover)->layout(_positionPopover(r));
}
};
Child popoverLayer(Child child) {
return makeRc<PopoverLayer>(child);
}
} // namespace Karm::Ui