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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
#include "cru/ui/render/RenderObject.h"
#include "cru/common/Logger.h"
#include "cru/platform/GraphicsBase.h"
#include "cru/platform/graphics/util/Painter.h"
#include "cru/ui/Base.h"
#include "cru/ui/DebugFlags.h"
#include "cru/ui/host/WindowHost.h"
#include <algorithm>
#include <string>
#include <string_view>
#include <vector>
namespace cru::ui::render {
void RenderObject::SetAttachedControl(controls::Control* new_control) {
control_ = new_control;
OnAttachedControlChanged(new_control);
}
void RenderObject::AddChild(RenderObject* render_object, const Index position) {
Expects(child_mode_ != ChildMode::None);
Expects(!(child_mode_ == ChildMode::Single && children_.size() > 0));
Expects(render_object->GetParent() ==
nullptr); // Render object already has a parent.
Expects(position >= 0); // Position index is less than 0.
Expects(
position <=
static_cast<Index>(children_.size())); // Position index is out of bound.
children_.insert(children_.cbegin() + position, render_object);
render_object->SetParent(this);
render_object->SetWindowHostRecursive(GetWindowHost());
OnAddChild(render_object, position);
}
void RenderObject::RemoveChild(const Index position) {
Expects(position >= 0); // Position index is less than 0.
Expects(position < static_cast<Index>(
children_.size())); // Position index is out of bound.
const auto i = children_.cbegin() + position;
const auto removed_child = *i;
children_.erase(i);
removed_child->SetParent(nullptr);
removed_child->SetWindowHostRecursive(nullptr);
OnRemoveChild(removed_child, position);
}
RenderObject* RenderObject::GetFirstChild() const {
const auto& children = GetChildren();
if (children.empty()) {
return nullptr;
} else {
return children.front();
}
}
void RenderObject::TraverseDescendants(
const std::function<void(RenderObject*)>& action) {
action(this);
for (auto child : children_) child->TraverseDescendants(action);
}
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::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) {
log::Debug(u"{} Measure begins :\nrequirement: {}\npreferred size: {}",
this->GetDebugPathInTree(), requirement.ToDebugString(),
preferred_size.ToDebugString());
}
size_ = OnMeasureCore(merged_requirement, merged_preferred_size);
if constexpr (cru::ui::debug_flags::layout) {
log::Debug(u"{} Measure ends :\nresult size: {}",
this->GetDebugPathInTree(), size_);
}
Ensures(size_.width >= 0);
Ensures(size_.height >= 0);
}
void RenderObject::Layout(const Point& offset) {
if constexpr (cru::ui::debug_flags::layout) {
log::Debug(u"{} Layout :\noffset: {} size: {}", this->GetDebugPathInTree(),
offset, GetSize());
}
offset_ = offset;
OnLayoutCore();
}
Thickness RenderObject::GetOuterSpaceThickness() const {
return margin_ + padding_;
}
void RenderObject::Draw(platform::graphics::IPainter* painter) {
OnDrawCore(painter);
}
RenderObject* RenderObject::GetSingleChild() const {
Expects(child_mode_ == ChildMode::Single);
const auto& children = GetChildren();
if (children.empty())
return nullptr;
else
return children.front();
}
void RenderObject::OnParentChanged(RenderObject* old_parent,
RenderObject* new_parent) {
CRU_UNUSED(old_parent)
CRU_UNUSED(new_parent)
}
void RenderObject::OnAddChild(RenderObject* new_child, Index position) {
CRU_UNUSED(new_child)
CRU_UNUSED(position)
InvalidateLayout();
InvalidatePaint();
}
void RenderObject::OnRemoveChild(RenderObject* removed_child, Index position) {
CRU_UNUSED(removed_child)
CRU_UNUSED(position)
InvalidateLayout();
InvalidatePaint();
}
void RenderObject::DefaultDrawChildren(platform::graphics::IPainter* painter) {
for (const auto child : GetChildren()) {
auto offset = child->GetOffset();
platform::graphics::util::WithTransform(
painter, platform::Matrix::Translation(offset.x, offset.y),
[child](auto p) { child->Draw(p); });
}
}
void RenderObject::DefaultDrawContent(platform::graphics::IPainter* painter) {
const auto content_rect = GetContentRect();
platform::graphics::util::WithTransform(
painter, Matrix::Translation(content_rect.left, content_rect.top),
[this](auto p) { this->OnDrawContent(p); });
}
void RenderObject::OnDrawCore(platform::graphics::IPainter* painter) {
DefaultDrawContent(painter);
DefaultDrawChildren(painter);
}
void RenderObject::OnDrawContent(platform::graphics::IPainter* painter) {
CRU_UNUSED(painter);
}
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 = GetSize();
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);
}
void RenderObject::OnAfterLayout() {}
Rect RenderObject::GetPaddingRect() const {
const auto size = GetSize();
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 = GetSize();
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;
}
void RenderObject::SetParent(RenderObject* new_parent) {
const auto old_parent = parent_;
parent_ = new_parent;
OnParentChanged(old_parent, new_parent);
}
void RenderObject::InvalidateLayout() {
if (window_host_ != nullptr) window_host_->InvalidateLayout();
}
void RenderObject::InvalidatePaint() {
if (window_host_ != nullptr) window_host_->InvalidatePaint();
}
constexpr std::u16string_view kUnamedName(u"UNNAMED");
std::u16string_view RenderObject::GetName() const { return kUnamedName; }
std::u16string RenderObject::GetDebugPathInTree() const {
std::vector<std::u16string_view> chain;
const RenderObject* parent = this;
while (parent != nullptr) {
chain.push_back(parent->GetName());
parent = parent->GetParent();
}
std::u16string result(chain.back());
for (auto iter = chain.crbegin() + 1; iter != chain.crend(); ++iter) {
result += u" -> ";
result += *iter;
}
return result;
}
void RenderObject::SetWindowHostRecursive(host::WindowHost* host) {
if (window_host_ != nullptr) {
detach_from_host_event_.Raise(nullptr);
}
window_host_ = host;
if (host != nullptr) {
attach_to_host_event_.Raise(nullptr);
}
for (const auto child : GetChildren()) {
child->SetWindowHostRecursive(host);
}
}
} // namespace cru::ui::render
|