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
|
#include "cru/ui/render/RenderObject.h"
#include "cru/base/Base.h"
#include "cru/platform/GraphicsBase.h"
#include "cru/ui/controls/Control.h"
#include "cru/ui/controls/ControlHost.h"
namespace cru::ui::render {
const BoxConstraint BoxConstraint::kNotLimit{Size::kMax, Size::kZero};
RenderObject::RenderObject(std::string name)
: name_(std::move(name)),
control_(nullptr),
parent_(nullptr),
layout_valid_(false) {}
RenderObject::~RenderObject() { DestroyEvent_.Raise(this); }
void RenderObject::SetParent(RenderObject* new_parent) {
#ifdef CRU_DEBUG
// In case there is a cycle.
auto parent = new_parent;
while (parent) {
assert(parent != this);
parent = parent->GetParent();
}
#endif
parent_ = new_parent;
}
void RenderObject::SetAttachedControl(controls::Control* new_control) {
auto old_control = control_;
control_ = new_control;
OnAttachedControlChanged(old_control, new_control);
}
Point RenderObject::GetTotalOffset() {
Point result{};
RenderObject* render_object = this;
while (render_object != nullptr) {
const auto o = render_object->GetOffset();
result.x += o.x;
result.y += o.y;
render_object = render_object->GetParent();
}
return result;
}
Point RenderObject::FromRootToContent(const Point& point) {
const auto offset = GetTotalOffset();
const auto rect = GetContentRect();
return Point{point.x - (offset.x + rect.left),
point.y - (offset.y + rect.top)};
}
void RenderObject::SetMargin(const Thickness& margin) {
margin_ = margin;
InvalidateLayout();
}
void RenderObject::SetPadding(const Thickness& padding) {
padding_ = padding;
InvalidateLayout();
}
void RenderObject::SetSuggestSize(const MeasureSize& suggest_size) {
custom_measure_requirement_.suggest = suggest_size;
InvalidateLayout();
}
void RenderObject::SetMinSize(const MeasureSize& min_size) {
custom_measure_requirement_.min = min_size;
InvalidateLayout();
}
void RenderObject::SetMaxSize(const MeasureSize& max_size) {
custom_measure_requirement_.max = max_size;
InvalidateLayout();
}
void RenderObject::Measure(const MeasureRequirement& requirement) {
if (layout_valid_ && requirement == last_measure_requirement_) {
return;
}
measure_result_size_ = OnMeasureCore(requirement);
if (measure_result_size_.width < 0 || measure_result_size_.height < 0) {
throw Exception("Measure result size is invalid.");
}
last_measure_requirement_ = requirement;
}
void RenderObject::Layout(const Point& offset) {
Layout({offset, GetMeasureResultSize()});
}
void RenderObject::Layout(const Rect& rect) {
offset_ = rect.GetLeftTop();
auto new_size = rect.GetSize();
if (size_ != new_size) {
size_ = new_size;
OnResize(new_size);
}
OnLayoutCore(rect);
layout_valid_ = true;
}
Thickness RenderObject::GetTotalSpaceThickness() { return margin_ + padding_; }
Thickness RenderObject::GetInnerSpaceThickness() { return padding_; }
Size RenderObject::OnMeasureCore(const MeasureRequirement& requirement) {
auto space_size = GetTotalSpaceThickness().GetTotalSize();
auto content_requirement =
requirement.Merge(custom_measure_requirement_).Minus(space_size);
auto content_size = OnMeasureContent(content_requirement);
return space_size + content_size;
}
void RenderObject::OnLayoutCore(const Rect& rect) {
auto total_size = rect.GetSize();
auto outer_space = GetTotalSpaceThickness();
auto content_size = (total_size - outer_space.GetTotalSize()).AtLeast0();
Point lefttop{outer_space.left, outer_space.top};
lefttop.x = std::min(lefttop.x, total_size.width);
lefttop.y = std::min(lefttop.y, total_size.height);
const Rect content_rect{lefttop, content_size};
OnLayoutContent(content_rect);
}
Rect RenderObject::GetPaddingRect() {
const auto size = GetMeasureResultSize();
Rect rect{Point{}, size};
rect = rect.Shrink(GetMargin());
rect.left = std::min(rect.left, size.width);
rect.top = std::min(rect.top, size.height);
rect.width = std::max(rect.width, 0.0f);
rect.height = std::max(rect.height, 0.0f);
return rect;
}
Rect RenderObject::GetContentRect() {
const auto size = GetMeasureResultSize();
Rect rect{Point{}, size};
rect = rect.Shrink(GetMargin());
rect = rect.Shrink(GetPadding());
rect.left = std::min(rect.left, size.width);
rect.top = std::min(rect.top, size.height);
rect.width = std::max(rect.width, 0.0f);
rect.height = std::max(rect.height, 0.0f);
return rect;
}
controls::ControlHost* RenderObject::GetControlHost() {
if (control_) {
return control_->GetControlHost();
}
return nullptr;
}
void RenderObject::InvalidateLayout() {
WalkUp([](RenderObject* ro) { ro->layout_valid_ = false; });
if (auto host = GetControlHost()) {
host->ScheduleRelayout();
}
}
void RenderObject::InvalidatePaint() {
if (auto window = GetControlHost()) {
window->ScheduleRepaint();
}
}
std::string RenderObject::GetName() { return name_; }
std::string RenderObject::GetDebugPathInTree() {
std::vector<std::string> chain;
RenderObject* parent = this;
while (parent != nullptr) {
chain.push_back(parent->GetName());
parent = parent->GetParent();
}
std::string result(chain.back());
for (auto iter = chain.crbegin() + 1; iter != chain.crend(); ++iter) {
result += " -> ";
result += *iter;
}
return result;
}
} // namespace cru::ui::render
|