forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoga.cpp
More file actions
3864 lines (3460 loc) · 159 KB
/
Yoga.cpp
File metadata and controls
3864 lines (3460 loc) · 159 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "Yoga.h"
#include <float.h>
#include <string.h>
#include <algorithm>
#include <atomic>
#include <memory>
#include "Utils.h"
#include "YGNode.h"
#include "YGNodePrint.h"
#include "Yoga-internal.h"
#include "event/event.h"
#include "log.h"
#ifdef _MSC_VER
#include <float.h>
/* define fmaxf if < VC12 */
#if _MSC_VER < 1800
__forceinline const float fmaxf(const float a, const float b) {
return (a > b) ? a : b;
}
#endif
#endif
using namespace facebook::yoga;
using detail::Log;
#ifdef ANDROID
static int YGAndroidLog(const YGConfigRef config, const YGNodeRef node,
YGLogLevel level, const char* format, va_list args);
#else
static int YGDefaultLog(const YGConfigRef config, const YGNodeRef node,
YGLogLevel level, const char* format, va_list args);
#endif
#ifdef ANDROID
#include <android/log.h>
static int YGAndroidLog(const YGConfigRef config, const YGNodeRef node,
YGLogLevel level, const char* format, va_list args) {
int androidLevel = YGLogLevelDebug;
switch (level) {
case YGLogLevelFatal:
androidLevel = ANDROID_LOG_FATAL;
break;
case YGLogLevelError:
androidLevel = ANDROID_LOG_ERROR;
break;
case YGLogLevelWarn:
androidLevel = ANDROID_LOG_WARN;
break;
case YGLogLevelInfo:
androidLevel = ANDROID_LOG_INFO;
break;
case YGLogLevelDebug:
androidLevel = ANDROID_LOG_DEBUG;
break;
case YGLogLevelVerbose:
androidLevel = ANDROID_LOG_VERBOSE;
break;
}
const int result = __android_log_vprint(androidLevel, "yoga", format, args);
return result;
}
#else
#define YG_UNUSED(x) (void)(x);
static int YGDefaultLog(const YGConfigRef config, const YGNodeRef node,
YGLogLevel level, const char* format, va_list args) {
YG_UNUSED(config);
YG_UNUSED(node);
switch (level) {
case YGLogLevelError:
case YGLogLevelFatal:
return vfprintf(stderr, format, args);
case YGLogLevelWarn:
case YGLogLevelInfo:
case YGLogLevelDebug:
case YGLogLevelVerbose:
default:
return vprintf(format, args);
}
}
#undef YG_UNUSED
#endif
YOGA_EXPORT bool YGFloatIsUndefined(const float value) {
return facebook::yoga::isUndefined(value);
}
detail::CompactValue YGComputedEdgeValue(const YGStyle::Edges& edges,
YGEdge edge,
detail::CompactValue defaultValue) {
if (!edges[edge].isUndefined()) {
return edges[edge];
}
if ((edge == YGEdgeTop || edge == YGEdgeBottom) &&
!edges[YGEdgeVertical].isUndefined()) {
return edges[YGEdgeVertical];
}
if ((edge == YGEdgeLeft || edge == YGEdgeRight || edge == YGEdgeStart ||
edge == YGEdgeEnd) &&
!edges[YGEdgeHorizontal].isUndefined()) {
return edges[YGEdgeHorizontal];
}
if (!edges[YGEdgeAll].isUndefined()) {
return edges[YGEdgeAll];
}
if (edge == YGEdgeStart || edge == YGEdgeEnd) {
return detail::CompactValue::ofUndefined();
}
return defaultValue;
}
YOGA_EXPORT void* YGNodeGetContext(YGNodeRef node) {
return node->getContext();
}
YOGA_EXPORT void YGNodeSetContext(YGNodeRef node, void* context) {
return node->setContext(context);
}
YOGA_EXPORT bool YGNodeHasMeasureFunc(YGNodeRef node) {
return node->hasMeasureFunc();
}
YOGA_EXPORT void YGNodeSetMeasureFunc(YGNodeRef node,
YGMeasureFunc measureFunc) {
node->setMeasureFunc(measureFunc);
}
YOGA_EXPORT bool YGNodeHasBaselineFunc(YGNodeRef node) {
return node->hasBaselineFunc();
}
YOGA_EXPORT void YGNodeSetBaselineFunc(YGNodeRef node,
YGBaselineFunc baselineFunc) {
node->setBaselineFunc(baselineFunc);
}
YOGA_EXPORT YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeRef node) {
return node->getDirtied();
}
YOGA_EXPORT void YGNodeSetDirtiedFunc(YGNodeRef node,
YGDirtiedFunc dirtiedFunc) {
node->setDirtiedFunc(dirtiedFunc);
}
YOGA_EXPORT void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc) {
node->setPrintFunc(printFunc);
}
YOGA_EXPORT bool YGNodeGetHasNewLayout(YGNodeRef node) {
return node->getHasNewLayout();
}
YOGA_EXPORT void YGConfigSetPrintTreeFlag(YGConfigRef config, bool enabled) {
config->printTree = enabled;
}
YOGA_EXPORT void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout) {
node->setHasNewLayout(hasNewLayout);
}
YOGA_EXPORT YGNodeType YGNodeGetNodeType(YGNodeRef node) {
return node->getNodeType();
}
YOGA_EXPORT void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType) {
return node->setNodeType(nodeType);
}
YOGA_EXPORT bool YGNodeIsDirty(YGNodeRef node) { return node->isDirty(); }
YOGA_EXPORT bool YGNodeLayoutGetDidUseLegacyFlag(const YGNodeRef node) {
return node->didUseLegacyFlag();
}
YOGA_EXPORT void YGNodeMarkDirtyAndPropogateToDescendants(
const YGNodeRef node) {
return node->markDirtyAndPropogateDownwards();
}
int32_t gConfigInstanceCount = 0;
YOGA_EXPORT WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
const YGNodeRef node = new YGNode{config};
YGAssertWithConfig(config, node != nullptr,
"Could not allocate memory for node");
Event::publish<Event::NodeAllocation>(node, {config});
return node;
}
YOGA_EXPORT YGConfigRef YGConfigGetDefault() {
static YGConfigRef defaultConfig = YGConfigNew();
return defaultConfig;
}
YOGA_EXPORT YGNodeRef YGNodeNew(void) {
return YGNodeNewWithConfig(YGConfigGetDefault());
}
YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNode) {
YGNodeRef node = new YGNode(*oldNode);
YGAssertWithConfig(oldNode->getConfig(), node != nullptr,
"Could not allocate memory for node");
Event::publish<Event::NodeAllocation>(node, {node->getConfig()});
node->setOwner(nullptr);
return node;
}
static YGConfigRef YGConfigClone(const YGConfig& oldConfig) {
const YGConfigRef config = new YGConfig(oldConfig);
YGAssert(config != nullptr, "Could not allocate memory for config");
gConfigInstanceCount++;
return config;
}
static YGNodeRef YGNodeDeepClone(YGNodeRef oldNode) {
auto config = YGConfigClone(*oldNode->getConfig());
auto node = new YGNode{*oldNode, config};
node->setOwner(nullptr);
Event::publish<Event::NodeAllocation>(node, {node->getConfig()});
YGVector vec = YGVector();
vec.reserve(oldNode->getChildren().size());
YGNodeRef childNode = nullptr;
for (auto* item : oldNode->getChildren()) {
childNode = YGNodeDeepClone(item);
childNode->setOwner(node);
vec.push_back(childNode);
}
node->setChildren(vec);
return node;
}
YOGA_EXPORT void YGNodeFree(const YGNodeRef node) {
if (YGNodeRef owner = node->getOwner()) {
owner->removeChild(node);
node->setOwner(nullptr);
}
const uint32_t childCount = YGNodeGetChildCount(node);
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef child = YGNodeGetChild(node, i);
child->setOwner(nullptr);
}
node->clearChildren();
Event::publish<Event::NodeDeallocation>(node, {node->getConfig()});
delete node;
}
static void YGConfigFreeRecursive(const YGNodeRef root) {
if (root->getConfig() != nullptr) {
gConfigInstanceCount--;
delete root->getConfig();
}
// Delete configs recursively for childrens
for (auto* child : root->getChildren()) {
YGConfigFreeRecursive(child);
}
}
YOGA_EXPORT void YGNodeFreeRecursiveWithCleanupFunc(const YGNodeRef root,
YGNodeCleanupFunc cleanup) {
uint32_t skipped = 0;
while (YGNodeGetChildCount(root) > skipped) {
const YGNodeRef child = YGNodeGetChild(root, skipped);
if (child->getOwner() != root) {
// Don't free shared nodes that we don't own.
skipped += 1;
} else {
YGNodeRemoveChild(root, child);
YGNodeFreeRecursive(child);
}
}
if (cleanup != nullptr) {
cleanup(root);
}
YGNodeFree(root);
}
YOGA_EXPORT void YGNodeFreeRecursive(const YGNodeRef root) {
return YGNodeFreeRecursiveWithCleanupFunc(root, nullptr);
}
YOGA_EXPORT void YGNodeReset(YGNodeRef node) { node->reset(); }
int32_t YGConfigGetInstanceCount(void) { return gConfigInstanceCount; }
YOGA_EXPORT YGConfigRef YGConfigNew(void) {
#ifdef ANDROID
const YGConfigRef config = new YGConfig(YGAndroidLog);
#else
const YGConfigRef config = new YGConfig(YGDefaultLog);
#endif
gConfigInstanceCount++;
return config;
}
YOGA_EXPORT void YGConfigFree(const YGConfigRef config) {
delete config;
gConfigInstanceCount--;
}
void YGConfigCopy(const YGConfigRef dest, const YGConfigRef src) {
memcpy(dest, src, sizeof(YGConfig));
}
YOGA_EXPORT void YGNodeSetIsReferenceBaseline(YGNodeRef node,
bool isReferenceBaseline) {
if (node->isReferenceBaseline() != isReferenceBaseline) {
node->setIsReferenceBaseline(isReferenceBaseline);
node->markDirtyAndPropogate();
}
}
YOGA_EXPORT bool YGNodeIsReferenceBaseline(YGNodeRef node) {
return node->isReferenceBaseline();
}
YOGA_EXPORT void YGNodeInsertChild(const YGNodeRef owner, const YGNodeRef child,
const uint32_t index) {
YGAssertWithNode(owner, child->getOwner() == nullptr,
"Child already has a owner, it must be removed first.");
YGAssertWithNode(
owner, !owner->hasMeasureFunc(),
"Cannot add child: Nodes with measure functions cannot have children.");
owner->insertChild(child, index);
child->setOwner(owner);
owner->markDirtyAndPropogate();
}
YOGA_EXPORT void YGNodeSwapChild(const YGNodeRef owner, const YGNodeRef child,
const uint32_t index) {
owner->replaceChild(child, index);
child->setOwner(owner);
}
YOGA_EXPORT void YGNodeRemoveChild(const YGNodeRef owner,
const YGNodeRef excludedChild) {
if (YGNodeGetChildCount(owner) == 0) {
// This is an empty set. Nothing to remove.
return;
}
// Children may be shared between parents, which is indicated by not having an
// owner. We only want to reset the child completely if it is owned
// exclusively by one node.
auto childOwner = excludedChild->getOwner();
if (owner->removeChild(excludedChild)) {
if (owner == childOwner) {
excludedChild->setLayout({}); // layout is no longer valid
excludedChild->setOwner(nullptr);
}
owner->markDirtyAndPropogate();
}
}
YOGA_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef owner) {
const uint32_t childCount = YGNodeGetChildCount(owner);
if (childCount == 0) {
// This is an empty set already. Nothing to do.
return;
}
const YGNodeRef firstChild = YGNodeGetChild(owner, 0);
if (firstChild->getOwner() == owner) {
// If the first child has this node as its owner, we assume that this child
// set is unique.
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef oldChild = YGNodeGetChild(owner, i);
oldChild->setLayout(YGNode().getLayout()); // layout is no longer valid
oldChild->setOwner(nullptr);
}
owner->clearChildren();
owner->markDirtyAndPropogate();
return;
}
// Otherwise, we are not the owner of the child set. We don't have to do
// anything to clear it.
owner->setChildren(YGVector());
owner->markDirtyAndPropogate();
}
static void YGNodeSetChildrenInternal(YGNodeRef const owner,
const std::vector<YGNodeRef>& children) {
if (!owner) {
return;
}
if (children.size() == 0) {
if (YGNodeGetChildCount(owner) > 0) {
for (YGNodeRef const child : owner->getChildren()) {
child->setLayout(YGLayout());
child->setOwner(nullptr);
}
owner->setChildren(YGVector());
owner->markDirtyAndPropogate();
}
} else {
if (YGNodeGetChildCount(owner) > 0) {
for (YGNodeRef const oldChild : owner->getChildren()) {
// Our new children may have nodes in common with the old children. We
// don't reset these common nodes.
if (std::find(children.begin(), children.end(), oldChild) ==
children.end()) {
oldChild->setLayout(YGLayout());
oldChild->setOwner(nullptr);
}
}
}
owner->setChildren(children);
for (YGNodeRef child : children) {
child->setOwner(owner);
}
owner->markDirtyAndPropogate();
}
}
YOGA_EXPORT void YGNodeSetChildren(const YGNodeRef owner, const YGNodeRef c[],
const uint32_t count) {
const YGVector children = {c, c + count};
YGNodeSetChildrenInternal(owner, children);
}
YOGA_EXPORT void YGNodeSetChildren(YGNodeRef const owner,
const std::vector<YGNodeRef>& children) {
YGNodeSetChildrenInternal(owner, children);
}
YOGA_EXPORT YGNodeRef YGNodeGetChild(const YGNodeRef node,
const uint32_t index) {
if (index < node->getChildren().size()) {
return node->getChild(index);
}
return nullptr;
}
YOGA_EXPORT uint32_t YGNodeGetChildCount(const YGNodeRef node) {
return static_cast<uint32_t>(node->getChildren().size());
}
YOGA_EXPORT YGNodeRef YGNodeGetOwner(const YGNodeRef node) {
return node->getOwner();
}
YOGA_EXPORT YGNodeRef YGNodeGetParent(const YGNodeRef node) {
return node->getOwner();
}
YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef node) {
YGAssertWithNode(node, node->hasMeasureFunc(),
"Only leaf nodes with custom measure functions"
"should manually mark themselves as dirty");
node->markDirtyAndPropogate();
}
YOGA_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode,
const YGNodeRef srcNode) {
if (!(dstNode->getStyle() == srcNode->getStyle())) {
dstNode->setStyle(srcNode->getStyle());
dstNode->markDirtyAndPropogate();
}
}
YOGA_EXPORT float YGNodeStyleGetFlexGrow(const YGNodeConstRef node) {
return node->getStyle().flexGrow().isUndefined()
? kDefaultFlexGrow
: node->getStyle().flexGrow().unwrap();
}
YOGA_EXPORT float YGNodeStyleGetFlexShrink(const YGNodeConstRef node) {
return node->getStyle().flexShrink().isUndefined()
? (node->getConfig()->useWebDefaults ? kWebDefaultFlexShrink
: kDefaultFlexShrink)
: node->getStyle().flexShrink().unwrap();
}
namespace {
template <typename T, typename NeedsUpdate, typename Update>
void updateStyle(YGNode* node, T value, NeedsUpdate&& needsUpdate,
Update&& update) {
if (needsUpdate(node->getStyle(), value)) {
update(node->getStyle(), value);
node->markDirtyAndPropogate();
}
}
template <typename Ref, typename T>
void updateStyle(YGNode* node, Ref (YGStyle::*prop)(), T value) {
updateStyle(
node, value, [prop](YGStyle& s, T x) { return (s.*prop)() != x; },
[prop](YGStyle& s, T x) { (s.*prop)() = x; });
}
template <typename Ref, typename Idx>
void updateIndexedStyleProp(YGNode* node, Ref (YGStyle::*prop)(), Idx idx,
detail::CompactValue value) {
using detail::CompactValue;
updateStyle(
node, value,
[idx, prop](YGStyle& s, CompactValue x) { return (s.*prop)()[idx] != x; },
[idx, prop](YGStyle& s, CompactValue x) { (s.*prop)()[idx] = x; });
}
} // namespace
// MSVC has trouble inferring the return type of pointer to member functions
// with const and non-const overloads, instead of preferring the non-const
// overload like clang and GCC. For the purposes of updateStyle(), we can help
// MSVC by specifying that return type explicitely. In combination with
// decltype, MSVC will prefer the non-const version.
#define MSVC_HINT(PROP) decltype(YGStyle{}.PROP())
YOGA_EXPORT void YGNodeStyleSetDirection(const YGNodeRef node,
const YGDirection value) {
updateStyle<MSVC_HINT(direction)>(node, &YGStyle::direction, value);
}
YOGA_EXPORT YGDirection YGNodeStyleGetDirection(const YGNodeConstRef node) {
return node->getStyle().direction();
}
YOGA_EXPORT void YGNodeStyleSetFlexDirection(
const YGNodeRef node, const YGFlexDirection flexDirection) {
updateStyle<MSVC_HINT(flexDirection)>(node, &YGStyle::flexDirection,
flexDirection);
}
YOGA_EXPORT YGFlexDirection
YGNodeStyleGetFlexDirection(const YGNodeConstRef node) {
return node->getStyle().flexDirection();
}
YOGA_EXPORT void YGNodeStyleSetJustifyContent(const YGNodeRef node,
const YGJustify justifyContent) {
updateStyle<MSVC_HINT(justifyContent)>(node, &YGStyle::justifyContent,
justifyContent);
}
YOGA_EXPORT YGJustify YGNodeStyleGetJustifyContent(const YGNodeConstRef node) {
return node->getStyle().justifyContent();
}
YOGA_EXPORT void YGNodeStyleSetAlignContent(const YGNodeRef node,
const YGAlign alignContent) {
updateStyle<MSVC_HINT(alignContent)>(node, &YGStyle::alignContent,
alignContent);
}
YOGA_EXPORT YGAlign YGNodeStyleGetAlignContent(const YGNodeConstRef node) {
return node->getStyle().alignContent();
}
YOGA_EXPORT void YGNodeStyleSetAlignItems(const YGNodeRef node,
const YGAlign alignItems) {
updateStyle<MSVC_HINT(alignItems)>(node, &YGStyle::alignItems, alignItems);
}
YOGA_EXPORT YGAlign YGNodeStyleGetAlignItems(const YGNodeConstRef node) {
return node->getStyle().alignItems();
}
YOGA_EXPORT void YGNodeStyleSetAlignSelf(const YGNodeRef node,
const YGAlign alignSelf) {
updateStyle<MSVC_HINT(alignSelf)>(node, &YGStyle::alignSelf, alignSelf);
}
YOGA_EXPORT YGAlign YGNodeStyleGetAlignSelf(const YGNodeConstRef node) {
return node->getStyle().alignSelf();
}
YOGA_EXPORT void YGNodeStyleSetPositionType(const YGNodeRef node,
const YGPositionType positionType) {
updateStyle<MSVC_HINT(positionType)>(node, &YGStyle::positionType,
positionType);
}
YOGA_EXPORT YGPositionType
YGNodeStyleGetPositionType(const YGNodeConstRef node) {
return node->getStyle().positionType();
}
YOGA_EXPORT void YGNodeStyleSetFlexWrap(const YGNodeRef node,
const YGWrap flexWrap) {
updateStyle<MSVC_HINT(flexWrap)>(node, &YGStyle::flexWrap, flexWrap);
}
YOGA_EXPORT YGWrap YGNodeStyleGetFlexWrap(const YGNodeConstRef node) {
return node->getStyle().flexWrap();
}
YOGA_EXPORT void YGNodeStyleSetOverflow(const YGNodeRef node,
const YGOverflow overflow) {
updateStyle<MSVC_HINT(overflow)>(node, &YGStyle::overflow, overflow);
}
YOGA_EXPORT YGOverflow YGNodeStyleGetOverflow(const YGNodeConstRef node) {
return node->getStyle().overflow();
}
YOGA_EXPORT void YGNodeStyleSetDisplay(const YGNodeRef node,
const YGDisplay display) {
updateStyle<MSVC_HINT(display)>(node, &YGStyle::display, display);
}
YOGA_EXPORT YGDisplay YGNodeStyleGetDisplay(const YGNodeConstRef node) {
return node->getStyle().display();
}
// TODO(T26792433): Change the API to accept YGFloatOptional.
YOGA_EXPORT void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) {
updateStyle<MSVC_HINT(flex)>(node, &YGStyle::flex, YGFloatOptional{flex});
}
// TODO(T26792433): Change the API to accept YGFloatOptional.
YOGA_EXPORT float YGNodeStyleGetFlex(const YGNodeConstRef node) {
return node->getStyle().flex().isUndefined()
? YGUndefined
: node->getStyle().flex().unwrap();
}
// TODO(T26792433): Change the API to accept YGFloatOptional.
YOGA_EXPORT void YGNodeStyleSetFlexGrow(const YGNodeRef node,
const float flexGrow) {
updateStyle<MSVC_HINT(flexGrow)>(node, &YGStyle::flexGrow,
YGFloatOptional{flexGrow});
}
// TODO(T26792433): Change the API to accept YGFloatOptional.
YOGA_EXPORT void YGNodeStyleSetFlexShrink(const YGNodeRef node,
const float flexShrink) {
updateStyle<MSVC_HINT(flexShrink)>(node, &YGStyle::flexShrink,
YGFloatOptional{flexShrink});
}
YOGA_EXPORT YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {
YGValue flexBasis = node->getStyle().flexBasis();
if (flexBasis.unit == YGUnitUndefined || flexBasis.unit == YGUnitAuto) {
// TODO(T26792433): Get rid off the use of YGUndefined at client side
flexBasis.value = YGUndefined;
}
return flexBasis;
}
YOGA_EXPORT void YGNodeStyleSetFlexBasis(const YGNodeRef node,
const float flexBasis) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(flexBasis);
updateStyle<MSVC_HINT(flexBasis)>(node, &YGStyle::flexBasis, value);
}
YOGA_EXPORT void YGNodeStyleSetFlexBasisPercent(const YGNodeRef node,
const float flexBasisPercent) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(flexBasisPercent);
updateStyle<MSVC_HINT(flexBasis)>(node, &YGStyle::flexBasis, value);
}
YOGA_EXPORT void YGNodeStyleSetFlexBasisAuto(const YGNodeRef node) {
updateStyle<MSVC_HINT(flexBasis)>(node, &YGStyle::flexBasis,
detail::CompactValue::ofAuto());
}
YOGA_EXPORT void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge,
float points) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(position)>(node, &YGStyle::position, edge,
value);
}
YOGA_EXPORT void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge,
float percent) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(position)>(node, &YGStyle::position, edge,
value);
}
YOGA_EXPORT YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) {
return node->getStyle().position()[edge];
}
YOGA_EXPORT void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge,
float points) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &YGStyle::margin, edge,
value);
}
YOGA_EXPORT void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge,
float percent) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &YGStyle::margin, edge,
value);
}
YOGA_EXPORT void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) {
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &YGStyle::margin, edge,
detail::CompactValue::ofAuto());
}
YOGA_EXPORT YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) {
return node->getStyle().margin()[edge];
}
YOGA_EXPORT void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge,
float points) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(padding)>(node, &YGStyle::padding, edge,
value);
}
YOGA_EXPORT void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge,
float percent) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(padding)>(node, &YGStyle::padding, edge,
value);
}
YOGA_EXPORT YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) {
return node->getStyle().padding()[edge];
}
// TODO(T26792433): Change the API to accept YGFloatOptional.
YOGA_EXPORT void YGNodeStyleSetBorder(const YGNodeRef node, const YGEdge edge,
const float border) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(border);
updateIndexedStyleProp<MSVC_HINT(border)>(node, &YGStyle::border, edge,
value);
}
YOGA_EXPORT float YGNodeStyleGetBorder(const YGNodeConstRef node,
const YGEdge edge) {
auto border = node->getStyle().border()[edge];
if (border.isUndefined() || border.isAuto()) {
// TODO(T26792433): Rather than returning YGUndefined, change the api to
// return YGFloatOptional.
return YGUndefined;
}
return static_cast<YGValue>(border).value;
}
// Yoga specific properties, not compatible with flexbox specification
// TODO(T26792433): Change the API to accept YGFloatOptional.
YOGA_EXPORT float YGNodeStyleGetAspectRatio(const YGNodeConstRef node) {
const YGFloatOptional op = node->getStyle().aspectRatio();
return op.isUndefined() ? YGUndefined : op.unwrap();
}
// TODO(T26792433): Change the API to accept YGFloatOptional.
YOGA_EXPORT void YGNodeStyleSetAspectRatio(const YGNodeRef node,
const float aspectRatio) {
updateStyle<MSVC_HINT(aspectRatio)>(node, &YGStyle::aspectRatio,
YGFloatOptional{aspectRatio});
}
YOGA_EXPORT void YGNodeStyleSetWidth(YGNodeRef node, float points) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(dimensions)>(node, &YGStyle::dimensions,
YGDimensionWidth, value);
}
YOGA_EXPORT void YGNodeStyleSetWidthPercent(YGNodeRef node, float percent) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(dimensions)>(node, &YGStyle::dimensions,
YGDimensionWidth, value);
}
YOGA_EXPORT void YGNodeStyleSetWidthAuto(YGNodeRef node) {
updateIndexedStyleProp<MSVC_HINT(dimensions)>(node, &YGStyle::dimensions,
YGDimensionWidth,
detail::CompactValue::ofAuto());
}
YOGA_EXPORT YGValue YGNodeStyleGetWidth(YGNodeConstRef node) {
return node->getStyle().dimensions()[YGDimensionWidth];
}
YOGA_EXPORT void YGNodeStyleSetHeight(YGNodeRef node, float points) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(dimensions)>(node, &YGStyle::dimensions,
YGDimensionHeight, value);
}
YOGA_EXPORT void YGNodeStyleSetHeightPercent(YGNodeRef node, float percent) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(dimensions)>(node, &YGStyle::dimensions,
YGDimensionHeight, value);
}
YOGA_EXPORT void YGNodeStyleSetHeightAuto(YGNodeRef node) {
updateIndexedStyleProp<MSVC_HINT(dimensions)>(node, &YGStyle::dimensions,
YGDimensionHeight,
detail::CompactValue::ofAuto());
}
YOGA_EXPORT YGValue YGNodeStyleGetHeight(YGNodeConstRef node) {
return node->getStyle().dimensions()[YGDimensionHeight];
}
YOGA_EXPORT void YGNodeStyleSetMinWidth(const YGNodeRef node,
const float minWidth) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(minWidth);
updateIndexedStyleProp<MSVC_HINT(minDimensions)>(
node, &YGStyle::minDimensions, YGDimensionWidth, value);
}
YOGA_EXPORT void YGNodeStyleSetMinWidthPercent(const YGNodeRef node,
const float minWidth) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(minWidth);
updateIndexedStyleProp<MSVC_HINT(minDimensions)>(
node, &YGStyle::minDimensions, YGDimensionWidth, value);
}
YOGA_EXPORT YGValue YGNodeStyleGetMinWidth(const YGNodeConstRef node) {
return node->getStyle().minDimensions()[YGDimensionWidth];
};
YOGA_EXPORT void YGNodeStyleSetMinHeight(const YGNodeRef node,
const float minHeight) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(minHeight);
updateIndexedStyleProp<MSVC_HINT(minDimensions)>(
node, &YGStyle::minDimensions, YGDimensionHeight, value);
}
YOGA_EXPORT void YGNodeStyleSetMinHeightPercent(const YGNodeRef node,
const float minHeight) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(minHeight);
updateIndexedStyleProp<MSVC_HINT(minDimensions)>(
node, &YGStyle::minDimensions, YGDimensionHeight, value);
}
YOGA_EXPORT YGValue YGNodeStyleGetMinHeight(const YGNodeConstRef node) {
return node->getStyle().minDimensions()[YGDimensionHeight];
};
YOGA_EXPORT void YGNodeStyleSetMaxWidth(const YGNodeRef node,
const float maxWidth) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(maxWidth);
updateIndexedStyleProp<MSVC_HINT(maxDimensions)>(
node, &YGStyle::maxDimensions, YGDimensionWidth, value);
}
YOGA_EXPORT void YGNodeStyleSetMaxWidthPercent(const YGNodeRef node,
const float maxWidth) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(maxWidth);
updateIndexedStyleProp<MSVC_HINT(maxDimensions)>(
node, &YGStyle::maxDimensions, YGDimensionWidth, value);
}
YOGA_EXPORT YGValue YGNodeStyleGetMaxWidth(const YGNodeConstRef node) {
return node->getStyle().maxDimensions()[YGDimensionWidth];
};
YOGA_EXPORT void YGNodeStyleSetMaxHeight(const YGNodeRef node,
const float maxHeight) {
auto value = detail::CompactValue::ofMaybe<YGUnitPoint>(maxHeight);
updateIndexedStyleProp<MSVC_HINT(maxDimensions)>(
node, &YGStyle::maxDimensions, YGDimensionHeight, value);
}
YOGA_EXPORT void YGNodeStyleSetMaxHeightPercent(const YGNodeRef node,
const float maxHeight) {
auto value = detail::CompactValue::ofMaybe<YGUnitPercent>(maxHeight);
updateIndexedStyleProp<MSVC_HINT(maxDimensions)>(
node, &YGStyle::maxDimensions, YGDimensionHeight, value);
}
YOGA_EXPORT YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) {
return node->getStyle().maxDimensions()[YGDimensionHeight];
};
#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \
YOGA_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node) { \
return node->getLayout().instanceName; \
}
#define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \
YOGA_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node, \
const YGEdge edge) { \
YGAssertWithNode(node, edge <= YGEdgeEnd, \
"Cannot get layout properties of multi-edge shorthands"); \
\
if (edge == YGEdgeStart) { \
if (node->getLayout().direction() == YGDirectionRTL) { \
return node->getLayout().instanceName[YGEdgeRight]; \
} else { \
return node->getLayout().instanceName[YGEdgeLeft]; \
} \
} \
\
if (edge == YGEdgeEnd) { \
if (node->getLayout().direction() == YGDirectionRTL) { \
return node->getLayout().instanceName[YGEdgeLeft]; \
} else { \
return node->getLayout().instanceName[YGEdgeRight]; \
} \
} \
\
return node->getLayout().instanceName[edge]; \
}
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction());
YG_NODE_LAYOUT_PROPERTY_IMPL(bool, HadOverflow, hadOverflow());
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Margin, margin);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Border, border);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding);
YOGA_EXPORT bool YGNodeLayoutGetDidLegacyStretchFlagAffectLayout(
const YGNodeRef node) {
return node->getLayout().doesLegacyStretchFlagAffectsLayout();
}
std::atomic<uint32_t> gCurrentGenerationCount(0);
bool YGLayoutNodeInternal(
const YGNodeRef node, const float availableWidth,
const float availableHeight, const YGDirection ownerDirection,
const YGMeasureMode widthMeasureMode, const YGMeasureMode heightMeasureMode,
const float ownerWidth, const float ownerHeight, const bool performLayout,
const LayoutPassReason reason, const YGConfigRef config,
LayoutData& layoutMarkerData, void* const layoutContext,
const uint32_t depth, const uint32_t generationCount);
#ifdef DEBUG
static void YGNodePrintInternal(const YGNodeRef node,
const YGPrintOptions options) {
std::string str;
facebook::yoga::YGNodeToString(str, node, options, 0);
Log::log(node, YGLogLevelDebug, nullptr, str.c_str());
}
YOGA_EXPORT void YGNodePrint(const YGNodeRef node,
const YGPrintOptions options) {
YGNodePrintInternal(node, options);
}
#endif
const std::array<YGEdge, 4> leading = {
{YGEdgeTop, YGEdgeBottom, YGEdgeLeft, YGEdgeRight}};
const std::array<YGEdge, 4> trailing = {
{YGEdgeBottom, YGEdgeTop, YGEdgeRight, YGEdgeLeft}};
static const std::array<YGEdge, 4> pos = {{
YGEdgeTop,
YGEdgeBottom,
YGEdgeLeft,
YGEdgeRight,
}};
static const std::array<YGDimension, 4> dim = {
{YGDimensionHeight, YGDimensionHeight, YGDimensionWidth, YGDimensionWidth}};
static inline float YGNodePaddingAndBorderForAxis(const YGNodeConstRef node,
const YGFlexDirection axis,
const float widthSize) {
return (node->getLeadingPaddingAndBorder(axis, widthSize) +
node->getTrailingPaddingAndBorder(axis, widthSize))
.unwrap();
}
static inline YGAlign YGNodeAlignItem(const YGNode* node, const YGNode* child) {
const YGAlign align = child->getStyle().alignSelf() == YGAlignAuto
? node->getStyle().alignItems()
: child->getStyle().alignSelf();
if (align == YGAlignBaseline &&
YGFlexDirectionIsColumn(node->getStyle().flexDirection())) {
return YGAlignFlexStart;
}
return align;
}
static float YGBaseline(const YGNodeRef node, void* layoutContext) {
if (node->hasBaselineFunc()) {
Event::publish<Event::NodeBaselineStart>(node);
const float baseline = node->baseline(
node->getLayout().measuredDimensions[YGDimensionWidth],
node->getLayout().measuredDimensions[YGDimensionHeight], layoutContext);
Event::publish<Event::NodeBaselineEnd>(node);
YGAssertWithNode(node, !YGFloatIsUndefined(baseline),
"Expect custom baseline function to not return NaN");
return baseline;
}
YGNodeRef baselineChild = nullptr;
const uint32_t childCount = YGNodeGetChildCount(node);
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef child = YGNodeGetChild(node, i);
if (child->getLineIndex() > 0) {
break;
}
if (child->getStyle().positionType() == YGPositionTypeAbsolute) {
continue;
}
if (YGNodeAlignItem(node, child) == YGAlignBaseline ||
child->isReferenceBaseline()) {
baselineChild = child;
break;
}