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
|
#include "cru/ui/render/RenderObject.h"
#include "cru/common/log/Logger.h"
#include "cru/ui/DebugFlags.h"
#include "cru/ui/controls/Control.h"
#include "cru/ui/host/WindowHost.h"
namespace cru::ui::render {
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() const {
Point result{};
const 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 {
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::SetPreferredSize(const MeasureSize& preferred_size) {
preferred_size_ = preferred_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,
const MeasureSize& preferred_size) {
MeasureRequirement merged_requirement =
MeasureRequirement::Merge(requirement, custom_measure_requirement_)
.Normalize();
MeasureSize merged_preferred_size =
preferred_size.OverrideBy(preferred_size_);
if constexpr (cru::ui::debug_flags::layout) {
CRU_LOG_DEBUG(u"{} Measure begins :\nrequirement: {}\npreferred size: {}",
this->GetDebugPathInTree(), requirement.ToDebugString(),
preferred_size.ToDebugString());
}
desired_size_ = OnMeasureCore(merged_requirement, merged_preferred_size);
if constexpr (cru::ui::debug_flags::layout) {
CRU_LOG_DEBUG(u"{} Measure ends :\nresult size: {}",
this->GetDebugPathInTree(), desired_size_);
}
Ensures(desired_size_.width >= 0);
Ensures(desired_size_.height >= 0);
}
void RenderObject::Layout(const Point& offset) {
if constexpr (cru::ui::debug_flags::layout) {
CRU_LOG_DEBUG(u"{} Layout :\noffset: {} size: {}",
this->GetDebugPathInTree(), offset, desired_size_);
}
offset_ = offset;
size_ = desired_size_;
OnResize(size_);
OnLayoutCore();
}
Thickness RenderObject::GetOuterSpaceThickness() const {
return margin_ + padding_;
}
Size RenderObject::OnMeasureCore(const MeasureRequirement& requirement,
const MeasureSize& preferred_size) {
const Thickness outer_space = GetOuterSpaceThickness();
const Size space_size{outer_space.GetHorizontalTotal(),
outer_space.GetVerticalTotal()};
MeasureRequirement content_requirement = requirement;
content_requirement.max = content_requirement.max.Minus(space_size);
content_requirement.min = content_requirement.min.Minus(space_size);
MeasureSize content_preferred_size =
content_requirement.Coerce(preferred_size.Minus(space_size));
const auto content_size =
OnMeasureContent(content_requirement, content_preferred_size);
return space_size + content_size;
}
void RenderObject::OnLayoutCore() {
Size total_size = GetDesiredSize();
const Thickness outer_space = GetOuterSpaceThickness();
const Size space_size{outer_space.GetHorizontalTotal(),
outer_space.GetVerticalTotal()};
auto content_size = total_size - space_size;
if (content_size.width < 0) {
content_size.width = 0;
}
if (content_size.height < 0) {
content_size.height = 0;
}
Point lefttop{outer_space.left, outer_space.top};
if (lefttop.x > total_size.width) {
lefttop.x = total_size.width;
}
if (lefttop.y > total_size.height) {
lefttop.y = total_size.height;
}
const Rect content_rect{lefttop, content_size};
OnLayoutContent(content_rect);
}
Rect RenderObject::GetPaddingRect() const {
const auto size = GetDesiredSize();
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 {
const auto size = GetDesiredSize();
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;
}
host::WindowHost* RenderObject::GetWindowHost() {
if (control_) {
return control_->GetWindowHost();
}
return nullptr;
}
void RenderObject::InvalidateLayout() {
if (auto window_host = GetWindowHost()) {
window_host->InvalidateLayout();
}
}
void RenderObject::InvalidatePaint() {
if (auto window_host = GetWindowHost()) {
window_host->InvalidatePaint();
}
}
String kUnamedName(u"UNNAMED");
String RenderObject::GetName() const { return kUnamedName; }
String RenderObject::GetDebugPathInTree() const {
std::vector<String> chain;
const RenderObject* parent = this;
while (parent != nullptr) {
chain.push_back(parent->GetName());
parent = parent->GetParent();
}
String result(chain.back());
for (auto iter = chain.crbegin() + 1; iter != chain.crend(); ++iter) {
result += u" -> ";
result += *iter;
}
return result;
}
} // namespace cru::ui::render
|