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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
#include "cru/ui/render/FlexLayoutRenderObject.h"
#include "cru/common/Logger.h"
#include "cru/platform/graphics/util/Painter.h"
#include "cru/ui/Base.h"
#include "cru/ui/render/LayoutHelper.h"
#include "cru/ui/render/MeasureRequirement.h"
#include <algorithm>
#include <functional>
#include <type_traits>
namespace cru::ui::render {
std::u16string_view FlexLayoutRenderObject::GetName() const {
return u"FlexLayoutRenderObject";
}
struct tag_horizontal_t {};
struct tag_vertical_t {};
template <typename TSize>
constexpr auto GetMain(const TSize& size, tag_horizontal_t) {
return size.width;
}
template <typename TSize>
constexpr auto GetCross(const TSize& size, tag_horizontal_t) {
return size.height;
}
template <typename TSize>
constexpr auto GetMain(const TSize& size, tag_vertical_t) {
return size.height;
}
template <typename TSize>
constexpr auto GetCross(const TSize& size, tag_vertical_t) {
return size.width;
}
template <typename TSize>
constexpr auto& GetMain(TSize& size, tag_horizontal_t) {
return size.width;
}
template <typename TSize>
constexpr auto& GetCross(TSize& size, tag_horizontal_t) {
return size.height;
}
template <typename TSize>
constexpr auto& GetMain(TSize& size, tag_vertical_t) {
return size.height;
}
template <typename TSize>
constexpr auto& GetCross(TSize& size, tag_vertical_t) {
return size.width;
}
template <typename TSize>
constexpr TSize CreateTSize(decltype(std::declval<TSize>().width) main,
decltype(std::declval<TSize>().height) cross,
tag_horizontal_t) {
return TSize{main, cross};
}
template <typename TSize>
constexpr TSize CreateTSize(decltype(std::declval<TSize>().width) main,
decltype(std::declval<TSize>().height) cross,
tag_vertical_t) {
return TSize{cross, main};
}
enum class FlexLayoutAdjustType { None, Expand, Shrink };
namespace {
void Remove(std::vector<Index>& v, const std::vector<Index>& to_remove_v) {
Index current = 0;
for (auto to_remove : to_remove_v) {
while (v[current] != to_remove) {
current++;
}
v.erase(v.cbegin() + current);
}
}
template <typename direction_tag_t,
typename = std::enable_if_t<
std::is_same_v<direction_tag_t, tag_horizontal_t> ||
std::is_same_v<direction_tag_t, tag_vertical_t>>>
Size FlexLayoutMeasureContentImpl(
const MeasureRequirement& requirement, const MeasureSize& preferred_size,
const std::vector<RenderObject*>& children,
const std::vector<FlexChildLayoutData>& layout_data,
Alignment item_cross_align, StringView log_tag) {
Expects(children.size() == layout_data.size());
direction_tag_t direction_tag;
const Index child_count = children.size();
MeasureLength preferred_main_length = GetMain(preferred_size, direction_tag);
MeasureLength preferred_cross_length =
GetCross(preferred_size, direction_tag);
MeasureLength max_main_length = GetMain(requirement.max, direction_tag);
MeasureLength max_cross_length = GetCross(requirement.max, direction_tag);
MeasureLength min_main_length = GetMain(requirement.min, direction_tag);
MeasureLength min_cross_length = GetCross(requirement.min, direction_tag);
// step 1.
for (Index i = 0; i < child_count; i++) {
const auto child = children[i];
child->Measure(MeasureRequirement{CreateTSize<MeasureSize>(
MeasureLength::NotSpecified(),
max_cross_length, direction_tag),
MeasureSize::NotSpecified()},
MeasureSize::NotSpecified());
}
float total_length = 0.f;
for (auto child : children) {
total_length += GetMain(child->GetSize(), direction_tag);
}
// step 2.
FlexLayoutAdjustType adjust_type = FlexLayoutAdjustType::None;
float target_length =
-1.f; // Use a strange value to indicate error. This value should be
// assigned before usage. Or program has a bug.
if (preferred_main_length.IsSpecified()) {
const float preferred_main_length_value =
preferred_main_length.GetLengthOrUndefined();
target_length = preferred_main_length_value;
if (total_length > preferred_main_length_value) {
adjust_type = FlexLayoutAdjustType::Shrink;
} else if (total_length < preferred_main_length_value) {
adjust_type = FlexLayoutAdjustType::Expand;
}
} else {
if (max_main_length.IsSpecified()) {
const float max_main_length_value =
max_main_length.GetLengthOrUndefined();
if (max_main_length_value < total_length) {
adjust_type = FlexLayoutAdjustType::Shrink;
target_length = max_main_length_value;
} else if (max_main_length_value > total_length) {
for (auto data : layout_data) {
if (data.expand_factor > 0) {
adjust_type = FlexLayoutAdjustType::Expand;
target_length = max_main_length_value;
break;
}
}
}
}
// Do not use else here.
if (adjust_type == FlexLayoutAdjustType::None &&
min_main_length.IsSpecified() &&
min_main_length.GetLengthOrUndefined() > total_length) {
adjust_type = FlexLayoutAdjustType::Expand;
target_length = min_main_length.GetLengthOrUndefined();
}
}
// step 3.
if (adjust_type == FlexLayoutAdjustType::Shrink) {
std::vector<Index> shrink_list;
for (Index i = 0; i < child_count; i++) {
if (layout_data[i].shrink_factor > 0) {
shrink_list.push_back(i);
}
}
while (!shrink_list.empty()) {
const float total_shrink_length = total_length - target_length;
float total_shrink_factor = 0.f;
std::vector<Index> to_remove;
for (Index i : shrink_list) {
total_shrink_factor += layout_data[i].shrink_factor;
}
for (Index i : shrink_list) {
const auto child = children[i];
const float shrink_length = layout_data[i].shrink_factor /
total_shrink_factor * total_shrink_length;
float new_measure_length =
GetMain(child->GetSize(), direction_tag) - shrink_length;
MeasureLength child_min_main_length =
GetMain(child->GetMinSize(), direction_tag);
if (child_min_main_length.IsSpecified() &&
new_measure_length < child_min_main_length.GetLengthOrUndefined()) {
new_measure_length = child_min_main_length.GetLengthOrUndefined();
} else if (new_measure_length < 0.f) {
new_measure_length = 0.f;
}
child->Measure(MeasureRequirement{CreateTSize<MeasureSize>(
new_measure_length,
max_cross_length, direction_tag),
MeasureSize::NotSpecified()},
CreateTSize<MeasureSize>(new_measure_length,
MeasureLength::NotSpecified(),
direction_tag));
const Size new_size = child->GetSize();
const float new_main_length = GetMain(new_size, direction_tag);
if (new_main_length > new_measure_length) {
to_remove.push_back(i);
}
}
total_length = 0.f;
for (auto child : children) {
total_length += GetMain(child->GetSize(), direction_tag);
}
if (total_length <= target_length) break;
Remove(shrink_list, to_remove);
}
} else if (adjust_type == FlexLayoutAdjustType::Expand) {
std::vector<Index> expand_list;
for (Index i = 0; i < child_count; i++) {
if (layout_data[i].expand_factor > 0) {
expand_list.push_back(i);
}
}
while (!expand_list.empty()) {
const float total_expand_length = target_length - total_length;
float total_expand_factor = 0.f;
std::vector<Index> to_remove;
for (Index i : expand_list) {
total_expand_factor += layout_data[i].expand_factor;
}
for (Index i : expand_list) {
const auto child = children[i];
const float expand_length = layout_data[i].expand_factor /
total_expand_factor * total_expand_length;
float new_measure_length =
GetMain(child->GetSize(), direction_tag) + expand_length;
MeasureLength child_max_main_length =
GetMain(child->GetMaxSize(), direction_tag);
if (child_max_main_length.IsSpecified() &&
new_measure_length > child_max_main_length.GetLengthOrUndefined()) {
new_measure_length = child_max_main_length.GetLengthOrUndefined();
}
child->Measure(
MeasureRequirement{
CreateTSize<MeasureSize>(MeasureLength::NotSpecified(),
max_cross_length, direction_tag),
CreateTSize<MeasureSize>(new_measure_length,
MeasureLength::NotSpecified(),
direction_tag)},
CreateTSize<MeasureSize>(new_measure_length,
MeasureLength::NotSpecified(),
direction_tag));
const Size new_size = child->GetSize();
const float new_main_length = GetMain(new_size, direction_tag);
if (new_main_length < new_measure_length) {
to_remove.push_back(i);
}
}
total_length = 0.f;
for (auto child : children) {
total_length += GetMain(child->GetSize(), direction_tag);
}
if (total_length >= target_length) break;
Remove(expand_list, to_remove);
}
}
float child_max_cross_length = 0.f;
for (auto child : children) {
const float cross_length = GetCross(child->GetSize(), direction_tag);
if (cross_length > child_max_cross_length) {
child_max_cross_length = cross_length;
}
}
if (max_main_length.IsSpecified() &&
total_length > max_main_length.GetLengthOrUndefined()) {
log::TagWarn(
log_tag,
u"(Measure) Children's main axis length exceeds required max length.");
total_length = max_main_length.GetLengthOrUndefined();
} else if (min_main_length.IsSpecified() &&
total_length < min_main_length.GetLengthOrUndefined()) {
total_length = min_main_length.GetLengthOrUndefined();
}
child_max_cross_length =
std::max(preferred_cross_length.GetLengthOr0(), child_max_cross_length);
child_max_cross_length =
std::max(min_cross_length.GetLengthOr0(), child_max_cross_length);
for (Index i = 0; i < child_count; i++) {
auto child_layout_data = layout_data[i];
auto child = children[i];
if (child_layout_data.cross_alignment.value_or(item_cross_align) ==
Alignment::Stretch) {
auto size = child->GetSize();
GetCross(size, direction_tag) = child_max_cross_length;
child->Measure({size, size}, MeasureSize::NotSpecified());
}
}
return CreateTSize<Size>(total_length, child_max_cross_length, direction_tag);
}
} // namespace
Size FlexLayoutRenderObject::OnMeasureContent(
const MeasureRequirement& requirement, const MeasureSize& preferred_size) {
const bool horizontal = (direction_ == FlexDirection::Horizontal ||
direction_ == FlexDirection::HorizontalReverse);
if (horizontal) {
return FlexLayoutMeasureContentImpl<tag_horizontal_t>(
requirement, preferred_size, GetChildren(), GetChildLayoutDataList(),
item_cross_align_, log_tag);
} else {
return FlexLayoutMeasureContentImpl<tag_vertical_t>(
requirement, preferred_size, GetChildren(), GetChildLayoutDataList(),
item_cross_align_, log_tag);
}
}
void FlexLayoutRenderObject::OnLayoutContent(const Rect& content_rect) {
const auto& children = GetChildren();
const Index child_count = children.size();
if (direction_ == FlexDirection::Horizontal) {
float current_main_offset = 0;
for (Index i = 0; i < child_count; i++) {
const auto child = children[i];
const auto size = child->GetSize();
const auto cross_align =
GetChildLayoutDataList()[i].cross_alignment.value_or(
GetItemCrossAlign());
child->Layout(
Point{content_rect.left + current_main_offset,
CalculateAnchorByAlignment(cross_align, content_rect.top,
content_rect.height, size.height)});
current_main_offset += size.width;
}
} else if (direction_ == FlexDirection::HorizontalReverse) {
float current_main_offset = 0;
for (Index i = 0; i < child_count; i++) {
const auto child = children[i];
const auto size = child->GetSize();
const auto cross_align =
GetChildLayoutDataList()[i].cross_alignment.value_or(
GetItemCrossAlign());
child->Layout(
Point{content_rect.GetRight() - current_main_offset,
CalculateAnchorByAlignment(cross_align, content_rect.top,
content_rect.height, size.height)});
current_main_offset += size.width;
}
} else if (direction_ == FlexDirection::Vertical) {
float current_main_offset = 0;
for (Index i = 0; i < child_count; i++) {
const auto child = children[i];
const auto size = child->GetSize();
const auto cross_align =
GetChildLayoutDataList()[i].cross_alignment.value_or(
GetItemCrossAlign());
child->Layout(Point{
CalculateAnchorByAlignment(cross_align, content_rect.left,
content_rect.width, size.width),
content_rect.top + current_main_offset,
});
current_main_offset += size.height;
}
} else {
float current_main_offset = 0;
for (Index i = 0; i < child_count; i++) {
const auto child = children[i];
const auto size = child->GetSize();
const auto cross_align =
GetChildLayoutDataList()[i].cross_alignment.value_or(
GetItemCrossAlign());
child->Layout(
Point{CalculateAnchorByAlignment(cross_align, content_rect.left,
content_rect.width, size.width),
content_rect.GetBottom() - current_main_offset});
current_main_offset += size.height;
}
}
}
} // namespace cru::ui::render
|