react-native-windows 0.78.9 → 0.78.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2430 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #include <algorithm>
9
+ #include <atomic>
10
+ #include <cfloat>
11
+ #include <cmath>
12
+ #include <cstring>
13
+
14
+ #include <yoga/Yoga.h>
15
+
16
+ #include <yoga/algorithm/AbsoluteLayout.h>
17
+ #include <yoga/algorithm/Align.h>
18
+ #include <yoga/algorithm/Baseline.h>
19
+ #include <yoga/algorithm/BoundAxis.h>
20
+ #include <yoga/algorithm/Cache.h>
21
+ #include <yoga/algorithm/CalculateLayout.h>
22
+ #include <yoga/algorithm/FlexDirection.h>
23
+ #include <yoga/algorithm/FlexLine.h>
24
+ #include <yoga/algorithm/PixelGrid.h>
25
+ #include <yoga/algorithm/SizingMode.h>
26
+ #include <yoga/algorithm/TrailingPosition.h>
27
+ #include <yoga/debug/AssertFatal.h>
28
+ #include <yoga/debug/Log.h>
29
+ #include <yoga/event/event.h>
30
+ #include <yoga/node/Node.h>
31
+ #include <yoga/numeric/Comparison.h>
32
+ #include <yoga/numeric/FloatOptional.h>
33
+
34
+ namespace facebook::yoga {
35
+
36
+ std::atomic<uint32_t> gCurrentGenerationCount(0);
37
+
38
+ static void constrainMaxSizeForMode(
39
+ const yoga::Node* node,
40
+ Direction direction,
41
+ FlexDirection axis,
42
+ float ownerAxisSize,
43
+ float ownerWidth,
44
+ /*in_out*/ SizingMode* mode,
45
+ /*in_out*/ float* size) {
46
+ const FloatOptional maxSize =
47
+ node->style().resolvedMaxDimension(
48
+ direction, dimension(axis), ownerAxisSize, ownerWidth) +
49
+ FloatOptional(node->style().computeMarginForAxis(axis, ownerWidth));
50
+ switch (*mode) {
51
+ case SizingMode::StretchFit:
52
+ case SizingMode::FitContent:
53
+ *size = (maxSize.isUndefined() || *size < maxSize.unwrap())
54
+ ? *size
55
+ : maxSize.unwrap();
56
+ break;
57
+ case SizingMode::MaxContent:
58
+ if (maxSize.isDefined()) {
59
+ *mode = SizingMode::FitContent;
60
+ *size = maxSize.unwrap();
61
+ }
62
+ break;
63
+ }
64
+ }
65
+
66
+ static void computeFlexBasisForChild(
67
+ const yoga::Node* const node,
68
+ yoga::Node* const child,
69
+ const float width,
70
+ const SizingMode widthMode,
71
+ const float height,
72
+ const float ownerWidth,
73
+ const float ownerHeight,
74
+ const SizingMode heightMode,
75
+ const Direction direction,
76
+ LayoutData& layoutMarkerData,
77
+ const uint32_t depth,
78
+ const uint32_t generationCount) {
79
+ const FlexDirection mainAxis =
80
+ resolveDirection(node->style().flexDirection(), direction);
81
+ const bool isMainAxisRow = isRow(mainAxis);
82
+ const float mainAxisSize = isMainAxisRow ? width : height;
83
+ const float mainAxisOwnerSize = isMainAxisRow ? ownerWidth : ownerHeight;
84
+
85
+ float childWidth = YGUndefined;
86
+ float childHeight = YGUndefined;
87
+ SizingMode childWidthSizingMode;
88
+ SizingMode childHeightSizingMode;
89
+
90
+ const FloatOptional resolvedFlexBasis = child->resolveFlexBasis(
91
+ direction, mainAxis, mainAxisOwnerSize, ownerWidth);
92
+ const bool isRowStyleDimDefined =
93
+ child->hasDefiniteLength(Dimension::Width, ownerWidth);
94
+ const bool isColumnStyleDimDefined =
95
+ child->hasDefiniteLength(Dimension::Height, ownerHeight);
96
+
97
+ if (resolvedFlexBasis.isDefined() && yoga::isDefined(mainAxisSize)) {
98
+ if (child->getLayout().computedFlexBasis.isUndefined() ||
99
+ (child->getConfig()->isExperimentalFeatureEnabled(
100
+ ExperimentalFeature::WebFlexBasis) &&
101
+ child->getLayout().computedFlexBasisGeneration != generationCount)) {
102
+ const FloatOptional paddingAndBorder = FloatOptional(
103
+ paddingAndBorderForAxis(child, mainAxis, direction, ownerWidth));
104
+ child->setLayoutComputedFlexBasis(
105
+ yoga::maxOrDefined(resolvedFlexBasis, paddingAndBorder));
106
+ }
107
+ } else if (isMainAxisRow && isRowStyleDimDefined) {
108
+ // The width is definite, so use that as the flex basis.
109
+ const FloatOptional paddingAndBorder =
110
+ FloatOptional(paddingAndBorderForAxis(
111
+ child, FlexDirection::Row, direction, ownerWidth));
112
+
113
+ child->setLayoutComputedFlexBasis(yoga::maxOrDefined(
114
+ child->getResolvedDimension(
115
+ direction, Dimension::Width, ownerWidth, ownerWidth),
116
+ paddingAndBorder));
117
+ } else if (!isMainAxisRow && isColumnStyleDimDefined) {
118
+ // The height is definite, so use that as the flex basis.
119
+ const FloatOptional paddingAndBorder =
120
+ FloatOptional(paddingAndBorderForAxis(
121
+ child, FlexDirection::Column, direction, ownerWidth));
122
+ child->setLayoutComputedFlexBasis(yoga::maxOrDefined(
123
+ child->getResolvedDimension(
124
+ direction, Dimension::Height, ownerHeight, ownerWidth),
125
+ paddingAndBorder));
126
+ } else {
127
+ // Compute the flex basis and hypothetical main size (i.e. the clamped flex
128
+ // basis).
129
+ childWidthSizingMode = SizingMode::MaxContent;
130
+ childHeightSizingMode = SizingMode::MaxContent;
131
+
132
+ auto marginRow =
133
+ child->style().computeMarginForAxis(FlexDirection::Row, ownerWidth);
134
+ auto marginColumn =
135
+ child->style().computeMarginForAxis(FlexDirection::Column, ownerWidth);
136
+
137
+ if (isRowStyleDimDefined) {
138
+ childWidth = child
139
+ ->getResolvedDimension(
140
+ direction, Dimension::Width, ownerWidth, ownerWidth)
141
+ .unwrap() +
142
+ marginRow;
143
+ childWidthSizingMode = SizingMode::StretchFit;
144
+ }
145
+ if (isColumnStyleDimDefined) {
146
+ childHeight =
147
+ child
148
+ ->getResolvedDimension(
149
+ direction, Dimension::Height, ownerHeight, ownerWidth)
150
+ .unwrap() +
151
+ marginColumn;
152
+ childHeightSizingMode = SizingMode::StretchFit;
153
+ }
154
+
155
+ // The W3C spec doesn't say anything about the 'overflow' property, but all
156
+ // major browsers appear to implement the following logic.
157
+ if ((!isMainAxisRow && node->style().overflow() == Overflow::Scroll) ||
158
+ node->style().overflow() != Overflow::Scroll) {
159
+ if (yoga::isUndefined(childWidth) && yoga::isDefined(width)) {
160
+ childWidth = width;
161
+ childWidthSizingMode = SizingMode::FitContent;
162
+ }
163
+ }
164
+
165
+ if ((isMainAxisRow && node->style().overflow() == Overflow::Scroll) ||
166
+ node->style().overflow() != Overflow::Scroll) {
167
+ if (yoga::isUndefined(childHeight) && yoga::isDefined(height)) {
168
+ childHeight = height;
169
+ childHeightSizingMode = SizingMode::FitContent;
170
+ }
171
+ }
172
+
173
+ const auto& childStyle = child->style();
174
+ if (childStyle.aspectRatio().isDefined()) {
175
+ if (!isMainAxisRow && childWidthSizingMode == SizingMode::StretchFit) {
176
+ childHeight = marginColumn +
177
+ (childWidth - marginRow) / childStyle.aspectRatio().unwrap();
178
+ childHeightSizingMode = SizingMode::StretchFit;
179
+ } else if (
180
+ isMainAxisRow && childHeightSizingMode == SizingMode::StretchFit) {
181
+ childWidth = marginRow +
182
+ (childHeight - marginColumn) * childStyle.aspectRatio().unwrap();
183
+ childWidthSizingMode = SizingMode::StretchFit;
184
+ }
185
+ }
186
+
187
+ // If child has no defined size in the cross axis and is set to stretch, set
188
+ // the cross axis to be measured exactly with the available inner width
189
+
190
+ const bool hasExactWidth =
191
+ yoga::isDefined(width) && widthMode == SizingMode::StretchFit;
192
+ const bool childWidthStretch =
193
+ resolveChildAlignment(node, child) == Align::Stretch &&
194
+ childWidthSizingMode != SizingMode::StretchFit;
195
+ if (!isMainAxisRow && !isRowStyleDimDefined && hasExactWidth &&
196
+ childWidthStretch) {
197
+ childWidth = width;
198
+ childWidthSizingMode = SizingMode::StretchFit;
199
+ if (childStyle.aspectRatio().isDefined()) {
200
+ childHeight =
201
+ (childWidth - marginRow) / childStyle.aspectRatio().unwrap();
202
+ childHeightSizingMode = SizingMode::StretchFit;
203
+ }
204
+ }
205
+
206
+ const bool hasExactHeight =
207
+ yoga::isDefined(height) && heightMode == SizingMode::StretchFit;
208
+ const bool childHeightStretch =
209
+ resolveChildAlignment(node, child) == Align::Stretch &&
210
+ childHeightSizingMode != SizingMode::StretchFit;
211
+ if (isMainAxisRow && !isColumnStyleDimDefined && hasExactHeight &&
212
+ childHeightStretch) {
213
+ childHeight = height;
214
+ childHeightSizingMode = SizingMode::StretchFit;
215
+
216
+ if (childStyle.aspectRatio().isDefined()) {
217
+ childWidth =
218
+ (childHeight - marginColumn) * childStyle.aspectRatio().unwrap();
219
+ childWidthSizingMode = SizingMode::StretchFit;
220
+ }
221
+ }
222
+
223
+ constrainMaxSizeForMode(
224
+ child,
225
+ direction,
226
+ FlexDirection::Row,
227
+ ownerWidth,
228
+ ownerWidth,
229
+ &childWidthSizingMode,
230
+ &childWidth);
231
+ constrainMaxSizeForMode(
232
+ child,
233
+ direction,
234
+ FlexDirection::Column,
235
+ ownerHeight,
236
+ ownerWidth,
237
+ &childHeightSizingMode,
238
+ &childHeight);
239
+
240
+ // Measure the child
241
+ calculateLayoutInternal(
242
+ child,
243
+ childWidth,
244
+ childHeight,
245
+ direction,
246
+ childWidthSizingMode,
247
+ childHeightSizingMode,
248
+ ownerWidth,
249
+ ownerHeight,
250
+ false,
251
+ LayoutPassReason::kMeasureChild,
252
+ layoutMarkerData,
253
+ depth,
254
+ generationCount);
255
+
256
+ child->setLayoutComputedFlexBasis(FloatOptional(yoga::maxOrDefined(
257
+ child->getLayout().measuredDimension(dimension(mainAxis)),
258
+ paddingAndBorderForAxis(child, mainAxis, direction, ownerWidth))));
259
+ }
260
+ child->setLayoutComputedFlexBasisGeneration(generationCount);
261
+ }
262
+
263
+ static void measureNodeWithMeasureFunc(
264
+ yoga::Node* const node,
265
+ const Direction direction,
266
+ float availableWidth,
267
+ float availableHeight,
268
+ const SizingMode widthSizingMode,
269
+ const SizingMode heightSizingMode,
270
+ const float ownerWidth,
271
+ const float ownerHeight,
272
+ LayoutData& layoutMarkerData,
273
+ const LayoutPassReason reason) {
274
+ yoga::assertFatalWithNode(
275
+ node,
276
+ node->hasMeasureFunc(),
277
+ "Expected node to have custom measure function");
278
+
279
+ if (widthSizingMode == SizingMode::MaxContent) {
280
+ availableWidth = YGUndefined;
281
+ }
282
+ if (heightSizingMode == SizingMode::MaxContent) {
283
+ availableHeight = YGUndefined;
284
+ }
285
+
286
+ const auto& layout = node->getLayout();
287
+ const float paddingAndBorderAxisRow = layout.padding(PhysicalEdge::Left) +
288
+ layout.padding(PhysicalEdge::Right) + layout.border(PhysicalEdge::Left) +
289
+ layout.border(PhysicalEdge::Right);
290
+ const float paddingAndBorderAxisColumn = layout.padding(PhysicalEdge::Top) +
291
+ layout.padding(PhysicalEdge::Bottom) + layout.border(PhysicalEdge::Top) +
292
+ layout.border(PhysicalEdge::Bottom);
293
+
294
+ // We want to make sure we don't call measure with negative size
295
+ const float innerWidth = yoga::isUndefined(availableWidth)
296
+ ? availableWidth
297
+ : yoga::maxOrDefined(0.0f, availableWidth - paddingAndBorderAxisRow);
298
+ const float innerHeight = yoga::isUndefined(availableHeight)
299
+ ? availableHeight
300
+ : yoga::maxOrDefined(0.0f, availableHeight - paddingAndBorderAxisColumn);
301
+
302
+ if (widthSizingMode == SizingMode::StretchFit &&
303
+ heightSizingMode == SizingMode::StretchFit &&
304
+ !YGConfigIsExperimentalFeatureEnabled( // [Win] YGExperimentalFeatureCallMeasureCallbackOnAllNodes check added for NetUI
305
+ node->getConfig(), YGExperimentalFeatureCallMeasureCallbackOnAllNodes)) {
306
+ // Don't bother sizing the text if both dimensions are already defined.
307
+ node->setLayoutMeasuredDimension(
308
+ boundAxis(
309
+ node,
310
+ FlexDirection::Row,
311
+ direction,
312
+ availableWidth,
313
+ ownerWidth,
314
+ ownerWidth),
315
+ Dimension::Width);
316
+ node->setLayoutMeasuredDimension(
317
+ boundAxis(
318
+ node,
319
+ FlexDirection::Column,
320
+ direction,
321
+ availableHeight,
322
+ ownerHeight,
323
+ ownerWidth),
324
+ Dimension::Height);
325
+ } else {
326
+ Event::publish<Event::MeasureCallbackStart>(node);
327
+
328
+ // Measure the text under the current constraints.
329
+ const YGSize measuredSize = node->measure(
330
+ innerWidth,
331
+ measureMode(widthSizingMode),
332
+ innerHeight,
333
+ measureMode(heightSizingMode));
334
+
335
+ layoutMarkerData.measureCallbacks += 1;
336
+ layoutMarkerData.measureCallbackReasonsCount[static_cast<size_t>(reason)] +=
337
+ 1;
338
+
339
+ Event::publish<Event::MeasureCallbackEnd>(
340
+ node,
341
+ {innerWidth,
342
+ unscopedEnum(measureMode(widthSizingMode)),
343
+ innerHeight,
344
+ unscopedEnum(measureMode(heightSizingMode)),
345
+ measuredSize.width,
346
+ measuredSize.height,
347
+ reason});
348
+
349
+ node->setLayoutMeasuredDimension(
350
+ boundAxis(
351
+ node,
352
+ FlexDirection::Row,
353
+ direction,
354
+ (widthSizingMode == SizingMode::MaxContent ||
355
+ widthSizingMode == SizingMode::FitContent)
356
+ ? measuredSize.width + paddingAndBorderAxisRow
357
+ : availableWidth,
358
+ ownerWidth,
359
+ ownerWidth),
360
+ Dimension::Width);
361
+
362
+ node->setLayoutMeasuredDimension(
363
+ boundAxis(
364
+ node,
365
+ FlexDirection::Column,
366
+ direction,
367
+ (heightSizingMode == SizingMode::MaxContent ||
368
+ heightSizingMode == SizingMode::FitContent)
369
+ ? measuredSize.height + paddingAndBorderAxisColumn
370
+ : availableHeight,
371
+ ownerHeight,
372
+ ownerWidth),
373
+ Dimension::Height);
374
+ }
375
+ }
376
+
377
+ // For nodes with no children, use the available values if they were provided,
378
+ // or the minimum size as indicated by the padding and border sizes.
379
+ static void measureNodeWithoutChildren(
380
+ yoga::Node* const node,
381
+ const Direction direction,
382
+ const float availableWidth,
383
+ const float availableHeight,
384
+ const SizingMode widthSizingMode,
385
+ const SizingMode heightSizingMode,
386
+ const float ownerWidth,
387
+ const float ownerHeight) {
388
+ const auto& layout = node->getLayout();
389
+
390
+ float width = availableWidth;
391
+ if (widthSizingMode == SizingMode::MaxContent ||
392
+ widthSizingMode == SizingMode::FitContent) {
393
+ width = layout.padding(PhysicalEdge::Left) +
394
+ layout.padding(PhysicalEdge::Right) +
395
+ layout.border(PhysicalEdge::Left) + layout.border(PhysicalEdge::Right);
396
+ }
397
+ node->setLayoutMeasuredDimension(
398
+ boundAxis(
399
+ node, FlexDirection::Row, direction, width, ownerWidth, ownerWidth),
400
+ Dimension::Width);
401
+
402
+ float height = availableHeight;
403
+ if (heightSizingMode == SizingMode::MaxContent ||
404
+ heightSizingMode == SizingMode::FitContent) {
405
+ height = layout.padding(PhysicalEdge::Top) +
406
+ layout.padding(PhysicalEdge::Bottom) +
407
+ layout.border(PhysicalEdge::Top) + layout.border(PhysicalEdge::Bottom);
408
+ }
409
+ node->setLayoutMeasuredDimension(
410
+ boundAxis(
411
+ node,
412
+ FlexDirection::Column,
413
+ direction,
414
+ height,
415
+ ownerHeight,
416
+ ownerWidth),
417
+ Dimension::Height);
418
+ }
419
+
420
+ static bool measureNodeWithFixedSize(
421
+ yoga::Node* const node,
422
+ const Direction direction,
423
+ const float availableWidth,
424
+ const float availableHeight,
425
+ const SizingMode widthSizingMode,
426
+ const SizingMode heightSizingMode,
427
+ const float ownerWidth,
428
+ const float ownerHeight) {
429
+ if ((yoga::isDefined(availableWidth) &&
430
+ widthSizingMode == SizingMode::FitContent && availableWidth <= 0.0f) ||
431
+ (yoga::isDefined(availableHeight) &&
432
+ heightSizingMode == SizingMode::FitContent && availableHeight <= 0.0f) ||
433
+ (widthSizingMode == SizingMode::StretchFit &&
434
+ heightSizingMode == SizingMode::StretchFit)) {
435
+ node->setLayoutMeasuredDimension(
436
+ boundAxis(
437
+ node,
438
+ FlexDirection::Row,
439
+ direction,
440
+ yoga::isUndefined(availableWidth) ||
441
+ (widthSizingMode == SizingMode::FitContent &&
442
+ availableWidth < 0.0f)
443
+ ? 0.0f
444
+ : availableWidth,
445
+ ownerWidth,
446
+ ownerWidth),
447
+ Dimension::Width);
448
+
449
+ node->setLayoutMeasuredDimension(
450
+ boundAxis(
451
+ node,
452
+ FlexDirection::Column,
453
+ direction,
454
+ yoga::isUndefined(availableHeight) ||
455
+ (heightSizingMode == SizingMode::FitContent &&
456
+ availableHeight < 0.0f)
457
+ ? 0.0f
458
+ : availableHeight,
459
+ ownerHeight,
460
+ ownerWidth),
461
+ Dimension::Height);
462
+ return true;
463
+ }
464
+
465
+ return false;
466
+ }
467
+
468
+ static void zeroOutLayoutRecursively(yoga::Node* const node) {
469
+ node->getLayout() = {};
470
+ node->setLayoutDimension(0, Dimension::Width);
471
+ node->setLayoutDimension(0, Dimension::Height);
472
+ node->setHasNewLayout(true);
473
+
474
+ node->cloneChildrenIfNeeded();
475
+ for (const auto child : node->getChildren()) {
476
+ zeroOutLayoutRecursively(child);
477
+ }
478
+ }
479
+
480
+ static void cleanupContentsNodesRecursively(yoga::Node* const node) {
481
+ for (auto child : node->getChildren()) {
482
+ if (child->style().display() == Display::Contents) {
483
+ child->getLayout() = {};
484
+ child->setLayoutDimension(0, Dimension::Width);
485
+ child->setLayoutDimension(0, Dimension::Height);
486
+ child->setHasNewLayout(true);
487
+ child->setDirty(false);
488
+ child->cloneChildrenIfNeeded();
489
+
490
+ cleanupContentsNodesRecursively(child);
491
+ }
492
+ }
493
+ }
494
+
495
+ static float calculateAvailableInnerDimension(
496
+ const yoga::Node* const node,
497
+ const Direction direction,
498
+ const Dimension dimension,
499
+ const float availableDim,
500
+ const float paddingAndBorder,
501
+ const float ownerDim,
502
+ const float ownerWidth) {
503
+ float availableInnerDim = availableDim - paddingAndBorder;
504
+ // Max dimension overrides predefined dimension value; Min dimension in turn
505
+ // overrides both of the above
506
+ if (yoga::isDefined(availableInnerDim)) {
507
+ // We want to make sure our available height does not violate min and max
508
+ // constraints
509
+ const FloatOptional minDimensionOptional =
510
+ node->style().resolvedMinDimension(
511
+ direction, dimension, ownerDim, ownerWidth);
512
+ const float minInnerDim = minDimensionOptional.isUndefined()
513
+ ? 0.0f
514
+ : minDimensionOptional.unwrap() - paddingAndBorder;
515
+
516
+ const FloatOptional maxDimensionOptional =
517
+ node->style().resolvedMaxDimension(
518
+ direction, dimension, ownerDim, ownerWidth);
519
+
520
+ const float maxInnerDim = maxDimensionOptional.isUndefined()
521
+ ? FLT_MAX
522
+ : maxDimensionOptional.unwrap() - paddingAndBorder;
523
+ availableInnerDim = yoga::maxOrDefined(
524
+ yoga::minOrDefined(availableInnerDim, maxInnerDim), minInnerDim);
525
+ }
526
+
527
+ return availableInnerDim;
528
+ }
529
+
530
+ static float computeFlexBasisForChildren(
531
+ yoga::Node* const node,
532
+ const float availableInnerWidth,
533
+ const float availableInnerHeight,
534
+ SizingMode widthSizingMode,
535
+ SizingMode heightSizingMode,
536
+ Direction direction,
537
+ FlexDirection mainAxis,
538
+ bool performLayout,
539
+ LayoutData& layoutMarkerData,
540
+ const uint32_t depth,
541
+ const uint32_t generationCount) {
542
+ float totalOuterFlexBasis = 0.0f;
543
+ YGNodeRef singleFlexChild = nullptr;
544
+ auto children = node->getLayoutChildren();
545
+ SizingMode sizingModeMainDim =
546
+ isRow(mainAxis) ? widthSizingMode : heightSizingMode;
547
+ // If there is only one child with flexGrow + flexShrink it means we can set
548
+ // the computedFlexBasis to 0 instead of measuring and shrinking / flexing the
549
+ // child to exactly match the remaining space
550
+ if (sizingModeMainDim == SizingMode::StretchFit) {
551
+ for (auto child : children) {
552
+ if (child->isNodeFlexible()) {
553
+ if (singleFlexChild != nullptr ||
554
+ yoga::inexactEquals(child->resolveFlexGrow(), 0.0f) ||
555
+ yoga::inexactEquals(child->resolveFlexShrink(), 0.0f)) {
556
+ // There is already a flexible child, or this flexible child doesn't
557
+ // have flexGrow and flexShrink, abort
558
+ singleFlexChild = nullptr;
559
+ break;
560
+ } else {
561
+ singleFlexChild = child;
562
+ }
563
+ }
564
+ }
565
+ }
566
+
567
+ for (auto child : children) {
568
+ child->processDimensions();
569
+ if (child->style().display() == Display::None) {
570
+ zeroOutLayoutRecursively(child);
571
+ child->setHasNewLayout(true);
572
+ child->setDirty(false);
573
+ continue;
574
+ }
575
+ if (performLayout) {
576
+ // Set the initial position (relative to the owner).
577
+ const Direction childDirection = child->resolveDirection(direction);
578
+ child->setPosition(
579
+ childDirection, availableInnerWidth, availableInnerHeight);
580
+ }
581
+
582
+ if (child->style().positionType() == PositionType::Absolute) {
583
+ continue;
584
+ }
585
+ if (child == singleFlexChild) {
586
+ child->setLayoutComputedFlexBasisGeneration(generationCount);
587
+ child->setLayoutComputedFlexBasis(FloatOptional(0));
588
+ } else {
589
+ computeFlexBasisForChild(
590
+ node,
591
+ child,
592
+ availableInnerWidth,
593
+ widthSizingMode,
594
+ availableInnerHeight,
595
+ availableInnerWidth,
596
+ availableInnerHeight,
597
+ heightSizingMode,
598
+ direction,
599
+ layoutMarkerData,
600
+ depth,
601
+ generationCount);
602
+ }
603
+
604
+ totalOuterFlexBasis +=
605
+ (child->getLayout().computedFlexBasis.unwrap() +
606
+ child->style().computeMarginForAxis(mainAxis, availableInnerWidth));
607
+ }
608
+
609
+ return totalOuterFlexBasis;
610
+ }
611
+
612
+ // It distributes the free space to the flexible items and ensures that the size
613
+ // of the flex items abide the min and max constraints. At the end of this
614
+ // function the child nodes would have proper size. Prior using this function
615
+ // please ensure that distributeFreeSpaceFirstPass is called.
616
+ static float distributeFreeSpaceSecondPass(
617
+ FlexLine& flexLine,
618
+ yoga::Node* const node,
619
+ const FlexDirection mainAxis,
620
+ const FlexDirection crossAxis,
621
+ const Direction direction,
622
+ const float ownerWidth,
623
+ const float mainAxisOwnerSize,
624
+ const float availableInnerMainDim,
625
+ const float availableInnerCrossDim,
626
+ const float availableInnerWidth,
627
+ const float availableInnerHeight,
628
+ const bool mainAxisOverflows,
629
+ const SizingMode sizingModeCrossDim,
630
+ const bool performLayout,
631
+ LayoutData& layoutMarkerData,
632
+ const uint32_t depth,
633
+ const uint32_t generationCount) {
634
+ float childFlexBasis = 0;
635
+ float flexShrinkScaledFactor = 0;
636
+ float flexGrowFactor = 0;
637
+ float deltaFreeSpace = 0;
638
+ const bool isMainAxisRow = isRow(mainAxis);
639
+ const bool isNodeFlexWrap = node->style().flexWrap() != Wrap::NoWrap;
640
+
641
+ for (auto currentLineChild : flexLine.itemsInFlow) {
642
+ childFlexBasis = boundAxisWithinMinAndMax(
643
+ currentLineChild,
644
+ direction,
645
+ mainAxis,
646
+ currentLineChild->getLayout().computedFlexBasis,
647
+ mainAxisOwnerSize,
648
+ ownerWidth)
649
+ .unwrap();
650
+ float updatedMainSize = childFlexBasis;
651
+
652
+ if (yoga::isDefined(flexLine.layout.remainingFreeSpace) &&
653
+ flexLine.layout.remainingFreeSpace < 0) {
654
+ flexShrinkScaledFactor =
655
+ -currentLineChild->resolveFlexShrink() * childFlexBasis;
656
+ // Is this child able to shrink?
657
+ if (flexShrinkScaledFactor != 0) {
658
+ float childSize = YGUndefined;
659
+
660
+ if (yoga::isDefined(flexLine.layout.totalFlexShrinkScaledFactors) &&
661
+ flexLine.layout.totalFlexShrinkScaledFactors == 0) {
662
+ childSize = childFlexBasis + flexShrinkScaledFactor;
663
+ } else {
664
+ childSize = childFlexBasis +
665
+ (flexLine.layout.remainingFreeSpace /
666
+ flexLine.layout.totalFlexShrinkScaledFactors) *
667
+ flexShrinkScaledFactor;
668
+ }
669
+
670
+ updatedMainSize = boundAxis(
671
+ currentLineChild,
672
+ mainAxis,
673
+ direction,
674
+ childSize,
675
+ availableInnerMainDim,
676
+ availableInnerWidth);
677
+ }
678
+ } else if (
679
+ yoga::isDefined(flexLine.layout.remainingFreeSpace) &&
680
+ flexLine.layout.remainingFreeSpace > 0) {
681
+ flexGrowFactor = currentLineChild->resolveFlexGrow();
682
+
683
+ // Is this child able to grow?
684
+ if (!std::isnan(flexGrowFactor) && flexGrowFactor != 0) {
685
+ updatedMainSize = boundAxis(
686
+ currentLineChild,
687
+ mainAxis,
688
+ direction,
689
+ childFlexBasis +
690
+ flexLine.layout.remainingFreeSpace /
691
+ flexLine.layout.totalFlexGrowFactors * flexGrowFactor,
692
+ availableInnerMainDim,
693
+ availableInnerWidth);
694
+ }
695
+ }
696
+
697
+ deltaFreeSpace += updatedMainSize - childFlexBasis;
698
+
699
+ const float marginMain = currentLineChild->style().computeMarginForAxis(
700
+ mainAxis, availableInnerWidth);
701
+ const float marginCross = currentLineChild->style().computeMarginForAxis(
702
+ crossAxis, availableInnerWidth);
703
+
704
+ float childCrossSize = YGUndefined;
705
+ float childMainSize = updatedMainSize + marginMain;
706
+ SizingMode childCrossSizingMode;
707
+ SizingMode childMainSizingMode = SizingMode::StretchFit;
708
+
709
+ const auto& childStyle = currentLineChild->style();
710
+ if (childStyle.aspectRatio().isDefined()) {
711
+ childCrossSize = isMainAxisRow
712
+ ? (childMainSize - marginMain) / childStyle.aspectRatio().unwrap()
713
+ : (childMainSize - marginMain) * childStyle.aspectRatio().unwrap();
714
+ childCrossSizingMode = SizingMode::StretchFit;
715
+
716
+ childCrossSize += marginCross;
717
+ } else if (
718
+ !std::isnan(availableInnerCrossDim) &&
719
+ !currentLineChild->hasDefiniteLength(
720
+ dimension(crossAxis), availableInnerCrossDim) &&
721
+ sizingModeCrossDim == SizingMode::StretchFit &&
722
+ !(isNodeFlexWrap && mainAxisOverflows) &&
723
+ resolveChildAlignment(node, currentLineChild) == Align::Stretch &&
724
+ !currentLineChild->style().flexStartMarginIsAuto(
725
+ crossAxis, direction) &&
726
+ !currentLineChild->style().flexEndMarginIsAuto(crossAxis, direction)) {
727
+ childCrossSize = availableInnerCrossDim;
728
+ childCrossSizingMode = SizingMode::StretchFit;
729
+ } else if (!currentLineChild->hasDefiniteLength(
730
+ dimension(crossAxis), availableInnerCrossDim)) {
731
+ childCrossSize = availableInnerCrossDim;
732
+ childCrossSizingMode = yoga::isUndefined(childCrossSize)
733
+ ? SizingMode::MaxContent
734
+ : SizingMode::FitContent;
735
+ } else {
736
+ childCrossSize = currentLineChild
737
+ ->getResolvedDimension(
738
+ direction,
739
+ dimension(crossAxis),
740
+ availableInnerCrossDim,
741
+ availableInnerWidth)
742
+ .unwrap() +
743
+ marginCross;
744
+ const bool isLoosePercentageMeasurement =
745
+ currentLineChild->getProcessedDimension(dimension(crossAxis))
746
+ .isPercent() &&
747
+ sizingModeCrossDim != SizingMode::StretchFit;
748
+ childCrossSizingMode =
749
+ yoga::isUndefined(childCrossSize) || isLoosePercentageMeasurement
750
+ ? SizingMode::MaxContent
751
+ : SizingMode::StretchFit;
752
+ }
753
+
754
+ constrainMaxSizeForMode(
755
+ currentLineChild,
756
+ direction,
757
+ mainAxis,
758
+ availableInnerMainDim,
759
+ availableInnerWidth,
760
+ &childMainSizingMode,
761
+ &childMainSize);
762
+ constrainMaxSizeForMode(
763
+ currentLineChild,
764
+ direction,
765
+ crossAxis,
766
+ availableInnerCrossDim,
767
+ availableInnerWidth,
768
+ &childCrossSizingMode,
769
+ &childCrossSize);
770
+
771
+ const bool requiresStretchLayout =
772
+ !currentLineChild->hasDefiniteLength(
773
+ dimension(crossAxis), availableInnerCrossDim) &&
774
+ resolveChildAlignment(node, currentLineChild) == Align::Stretch &&
775
+ !currentLineChild->style().flexStartMarginIsAuto(
776
+ crossAxis, direction) &&
777
+ !currentLineChild->style().flexEndMarginIsAuto(crossAxis, direction);
778
+
779
+ const float childWidth = isMainAxisRow ? childMainSize : childCrossSize;
780
+ const float childHeight = !isMainAxisRow ? childMainSize : childCrossSize;
781
+
782
+ const SizingMode childWidthSizingMode =
783
+ isMainAxisRow ? childMainSizingMode : childCrossSizingMode;
784
+ const SizingMode childHeightSizingMode =
785
+ !isMainAxisRow ? childMainSizingMode : childCrossSizingMode;
786
+
787
+ const bool isLayoutPass = performLayout && !requiresStretchLayout;
788
+ // Recursively call the layout algorithm for this child with the updated
789
+ // main size.
790
+ calculateLayoutInternal(
791
+ currentLineChild,
792
+ childWidth,
793
+ childHeight,
794
+ node->getLayout().direction(),
795
+ childWidthSizingMode,
796
+ childHeightSizingMode,
797
+ availableInnerWidth,
798
+ availableInnerHeight,
799
+ isLayoutPass,
800
+ isLayoutPass ? LayoutPassReason::kFlexLayout
801
+ : LayoutPassReason::kFlexMeasure,
802
+ layoutMarkerData,
803
+ depth,
804
+ generationCount);
805
+ node->setLayoutHadOverflow(
806
+ node->getLayout().hadOverflow() ||
807
+ currentLineChild->getLayout().hadOverflow());
808
+ }
809
+ return deltaFreeSpace;
810
+ }
811
+
812
+ // It distributes the free space to the flexible items.For those flexible items
813
+ // whose min and max constraints are triggered, those flex item's clamped size
814
+ // is removed from the remaingfreespace.
815
+ static void distributeFreeSpaceFirstPass(
816
+ FlexLine& flexLine,
817
+ const Direction direction,
818
+ const FlexDirection mainAxis,
819
+ const float ownerWidth,
820
+ const float mainAxisOwnerSize,
821
+ const float availableInnerMainDim,
822
+ const float availableInnerWidth) {
823
+ float flexShrinkScaledFactor = 0;
824
+ float flexGrowFactor = 0;
825
+ float baseMainSize = 0;
826
+ float boundMainSize = 0;
827
+ float deltaFreeSpace = 0;
828
+
829
+ for (auto currentLineChild : flexLine.itemsInFlow) {
830
+ float childFlexBasis = boundAxisWithinMinAndMax(
831
+ currentLineChild,
832
+ direction,
833
+ mainAxis,
834
+ currentLineChild->getLayout().computedFlexBasis,
835
+ mainAxisOwnerSize,
836
+ ownerWidth)
837
+ .unwrap();
838
+
839
+ if (flexLine.layout.remainingFreeSpace < 0) {
840
+ flexShrinkScaledFactor =
841
+ -currentLineChild->resolveFlexShrink() * childFlexBasis;
842
+
843
+ // Is this child able to shrink?
844
+ if (yoga::isDefined(flexShrinkScaledFactor) &&
845
+ flexShrinkScaledFactor != 0) {
846
+ baseMainSize = childFlexBasis +
847
+ flexLine.layout.remainingFreeSpace /
848
+ flexLine.layout.totalFlexShrinkScaledFactors *
849
+ flexShrinkScaledFactor;
850
+ boundMainSize = boundAxis(
851
+ currentLineChild,
852
+ mainAxis,
853
+ direction,
854
+ baseMainSize,
855
+ availableInnerMainDim,
856
+ availableInnerWidth);
857
+ if (yoga::isDefined(baseMainSize) && yoga::isDefined(boundMainSize) &&
858
+ baseMainSize != boundMainSize) {
859
+ // By excluding this item's size and flex factor from remaining, this
860
+ // item's min/max constraints should also trigger in the second pass
861
+ // resulting in the item's size calculation being identical in the
862
+ // first and second passes.
863
+ deltaFreeSpace += boundMainSize - childFlexBasis;
864
+ flexLine.layout.totalFlexShrinkScaledFactors -=
865
+ (-currentLineChild->resolveFlexShrink() *
866
+ currentLineChild->getLayout().computedFlexBasis.unwrap());
867
+ }
868
+ }
869
+ } else if (
870
+ yoga::isDefined(flexLine.layout.remainingFreeSpace) &&
871
+ flexLine.layout.remainingFreeSpace > 0) {
872
+ flexGrowFactor = currentLineChild->resolveFlexGrow();
873
+
874
+ // Is this child able to grow?
875
+ if (yoga::isDefined(flexGrowFactor) && flexGrowFactor != 0) {
876
+ baseMainSize = childFlexBasis +
877
+ flexLine.layout.remainingFreeSpace /
878
+ flexLine.layout.totalFlexGrowFactors * flexGrowFactor;
879
+ boundMainSize = boundAxis(
880
+ currentLineChild,
881
+ mainAxis,
882
+ direction,
883
+ baseMainSize,
884
+ availableInnerMainDim,
885
+ availableInnerWidth);
886
+
887
+ if (yoga::isDefined(baseMainSize) && yoga::isDefined(boundMainSize) &&
888
+ baseMainSize != boundMainSize) {
889
+ // By excluding this item's size and flex factor from remaining, this
890
+ // item's min/max constraints should also trigger in the second pass
891
+ // resulting in the item's size calculation being identical in the
892
+ // first and second passes.
893
+ deltaFreeSpace += boundMainSize - childFlexBasis;
894
+ flexLine.layout.totalFlexGrowFactors -= flexGrowFactor;
895
+ }
896
+ }
897
+ }
898
+ }
899
+ flexLine.layout.remainingFreeSpace -= deltaFreeSpace;
900
+ }
901
+
902
+ // Do two passes over the flex items to figure out how to distribute the
903
+ // remaining space.
904
+ //
905
+ // The first pass finds the items whose min/max constraints trigger, freezes
906
+ // them at those sizes, and excludes those sizes from the remaining space.
907
+ //
908
+ // The second pass sets the size of each flexible item. It distributes the
909
+ // remaining space amongst the items whose min/max constraints didn't trigger in
910
+ // the first pass. For the other items, it sets their sizes by forcing their
911
+ // min/max constraints to trigger again.
912
+ //
913
+ // This two pass approach for resolving min/max constraints deviates from the
914
+ // spec. The spec
915
+ // (https://www.w3.org/TR/CSS-flexbox-1/#resolve-flexible-lengths) describes a
916
+ // process that needs to be repeated a variable number of times. The algorithm
917
+ // implemented here won't handle all cases but it was simpler to implement and
918
+ // it mitigates performance concerns because we know exactly how many passes
919
+ // it'll do.
920
+ //
921
+ // At the end of this function the child nodes would have the proper size
922
+ // assigned to them.
923
+ //
924
+ static void resolveFlexibleLength(
925
+ yoga::Node* const node,
926
+ FlexLine& flexLine,
927
+ const FlexDirection mainAxis,
928
+ const FlexDirection crossAxis,
929
+ const Direction direction,
930
+ const float ownerWidth,
931
+ const float mainAxisOwnerSize,
932
+ const float availableInnerMainDim,
933
+ const float availableInnerCrossDim,
934
+ const float availableInnerWidth,
935
+ const float availableInnerHeight,
936
+ const bool mainAxisOverflows,
937
+ const SizingMode sizingModeCrossDim,
938
+ const bool performLayout,
939
+ LayoutData& layoutMarkerData,
940
+ const uint32_t depth,
941
+ const uint32_t generationCount) {
942
+ const float originalFreeSpace = flexLine.layout.remainingFreeSpace;
943
+ // First pass: detect the flex items whose min/max constraints trigger
944
+ distributeFreeSpaceFirstPass(
945
+ flexLine,
946
+ direction,
947
+ mainAxis,
948
+ ownerWidth,
949
+ mainAxisOwnerSize,
950
+ availableInnerMainDim,
951
+ availableInnerWidth);
952
+
953
+ // Second pass: resolve the sizes of the flexible items
954
+ const float distributedFreeSpace = distributeFreeSpaceSecondPass(
955
+ flexLine,
956
+ node,
957
+ mainAxis,
958
+ crossAxis,
959
+ direction,
960
+ ownerWidth,
961
+ mainAxisOwnerSize,
962
+ availableInnerMainDim,
963
+ availableInnerCrossDim,
964
+ availableInnerWidth,
965
+ availableInnerHeight,
966
+ mainAxisOverflows,
967
+ sizingModeCrossDim,
968
+ performLayout,
969
+ layoutMarkerData,
970
+ depth,
971
+ generationCount);
972
+
973
+ flexLine.layout.remainingFreeSpace = originalFreeSpace - distributedFreeSpace;
974
+ }
975
+
976
+ static void justifyMainAxis(
977
+ yoga::Node* const node,
978
+ FlexLine& flexLine,
979
+ const FlexDirection mainAxis,
980
+ const FlexDirection crossAxis,
981
+ const Direction direction,
982
+ const SizingMode sizingModeMainDim,
983
+ const SizingMode sizingModeCrossDim,
984
+ const float mainAxisOwnerSize,
985
+ const float ownerWidth,
986
+ const float availableInnerMainDim,
987
+ const float availableInnerCrossDim,
988
+ const float availableInnerWidth,
989
+ const bool performLayout) {
990
+ const auto& style = node->style();
991
+
992
+ const float leadingPaddingAndBorderMain =
993
+ node->style().computeFlexStartPaddingAndBorder(
994
+ mainAxis, direction, ownerWidth);
995
+ const float trailingPaddingAndBorderMain =
996
+ node->style().computeFlexEndPaddingAndBorder(
997
+ mainAxis, direction, ownerWidth);
998
+
999
+ const float gap =
1000
+ node->style().computeGapForAxis(mainAxis, availableInnerMainDim);
1001
+ // If we are using "at most" rules in the main axis, make sure that
1002
+ // remainingFreeSpace is 0 when min main dimension is not given
1003
+ if (sizingModeMainDim == SizingMode::FitContent &&
1004
+ flexLine.layout.remainingFreeSpace > 0) {
1005
+ if (style.minDimension(dimension(mainAxis)).isDefined() &&
1006
+ style
1007
+ .resolvedMinDimension(
1008
+ direction, dimension(mainAxis), mainAxisOwnerSize, ownerWidth)
1009
+ .isDefined()) {
1010
+ // This condition makes sure that if the size of main dimension(after
1011
+ // considering child nodes main dim, leading and trailing padding etc)
1012
+ // falls below min dimension, then the remainingFreeSpace is reassigned
1013
+ // considering the min dimension
1014
+
1015
+ // `minAvailableMainDim` denotes minimum available space in which child
1016
+ // can be laid out, it will exclude space consumed by padding and border.
1017
+ const float minAvailableMainDim =
1018
+ style
1019
+ .resolvedMinDimension(
1020
+ direction, dimension(mainAxis), mainAxisOwnerSize, ownerWidth)
1021
+ .unwrap() -
1022
+ leadingPaddingAndBorderMain - trailingPaddingAndBorderMain;
1023
+ const float occupiedSpaceByChildNodes =
1024
+ availableInnerMainDim - flexLine.layout.remainingFreeSpace;
1025
+ flexLine.layout.remainingFreeSpace = yoga::maxOrDefined(
1026
+ 0.0f, minAvailableMainDim - occupiedSpaceByChildNodes);
1027
+ } else {
1028
+ flexLine.layout.remainingFreeSpace = 0;
1029
+ }
1030
+ }
1031
+
1032
+ // In order to position the elements in the main axis, we have two controls.
1033
+ // The space between the beginning and the first element and the space between
1034
+ // each two elements.
1035
+ float leadingMainDim = 0;
1036
+ float betweenMainDim = gap;
1037
+ const Justify justifyContent = flexLine.layout.remainingFreeSpace >= 0
1038
+ ? node->style().justifyContent()
1039
+ : fallbackAlignment(node->style().justifyContent());
1040
+
1041
+ if (flexLine.numberOfAutoMargins == 0) {
1042
+ switch (justifyContent) {
1043
+ case Justify::Center:
1044
+ leadingMainDim = flexLine.layout.remainingFreeSpace / 2;
1045
+ break;
1046
+ case Justify::FlexEnd:
1047
+ leadingMainDim = flexLine.layout.remainingFreeSpace;
1048
+ break;
1049
+ case Justify::SpaceBetween:
1050
+ if (flexLine.itemsInFlow.size() > 1) {
1051
+ betweenMainDim += flexLine.layout.remainingFreeSpace /
1052
+ static_cast<float>(flexLine.itemsInFlow.size() - 1);
1053
+ }
1054
+ break;
1055
+ case Justify::SpaceEvenly:
1056
+ // Space is distributed evenly across all elements
1057
+ leadingMainDim = flexLine.layout.remainingFreeSpace /
1058
+ static_cast<float>(flexLine.itemsInFlow.size() + 1);
1059
+ betweenMainDim += leadingMainDim;
1060
+ break;
1061
+ case Justify::SpaceAround:
1062
+ // Space on the edges is half of the space between elements
1063
+ leadingMainDim = 0.5f * flexLine.layout.remainingFreeSpace /
1064
+ static_cast<float>(flexLine.itemsInFlow.size());
1065
+ betweenMainDim += leadingMainDim * 2;
1066
+ break;
1067
+ case Justify::FlexStart:
1068
+ break;
1069
+ }
1070
+ }
1071
+
1072
+ flexLine.layout.mainDim = leadingPaddingAndBorderMain + leadingMainDim;
1073
+ flexLine.layout.crossDim = 0;
1074
+
1075
+ float maxAscentForCurrentLine = 0;
1076
+ float maxDescentForCurrentLine = 0;
1077
+ bool isNodeBaselineLayout = isBaselineLayout(node);
1078
+ for (auto child : flexLine.itemsInFlow) {
1079
+ const LayoutResults& childLayout = child->getLayout();
1080
+ if (child->style().flexStartMarginIsAuto(mainAxis, direction) &&
1081
+ flexLine.layout.remainingFreeSpace > 0.0f) {
1082
+ flexLine.layout.mainDim += flexLine.layout.remainingFreeSpace /
1083
+ static_cast<float>(flexLine.numberOfAutoMargins);
1084
+ }
1085
+
1086
+ if (performLayout) {
1087
+ child->setLayoutPosition(
1088
+ childLayout.position(flexStartEdge(mainAxis)) +
1089
+ flexLine.layout.mainDim,
1090
+ flexStartEdge(mainAxis));
1091
+ }
1092
+
1093
+ if (child != flexLine.itemsInFlow.back()) {
1094
+ flexLine.layout.mainDim += betweenMainDim;
1095
+ }
1096
+
1097
+ if (child->style().flexEndMarginIsAuto(mainAxis, direction) &&
1098
+ flexLine.layout.remainingFreeSpace > 0.0f) {
1099
+ flexLine.layout.mainDim += flexLine.layout.remainingFreeSpace /
1100
+ static_cast<float>(flexLine.numberOfAutoMargins);
1101
+ }
1102
+ bool canSkipFlex =
1103
+ !performLayout && sizingModeCrossDim == SizingMode::StretchFit;
1104
+ if (canSkipFlex) {
1105
+ // If we skipped the flex step, then we can't rely on the measuredDims
1106
+ // because they weren't computed. This means we can't call
1107
+ // dimensionWithMargin.
1108
+ flexLine.layout.mainDim +=
1109
+ child->style().computeMarginForAxis(mainAxis, availableInnerWidth) +
1110
+ childLayout.computedFlexBasis.unwrap();
1111
+ flexLine.layout.crossDim = availableInnerCrossDim;
1112
+ } else {
1113
+ // The main dimension is the sum of all the elements dimension plus
1114
+ // the spacing.
1115
+ flexLine.layout.mainDim +=
1116
+ child->dimensionWithMargin(mainAxis, availableInnerWidth);
1117
+
1118
+ if (isNodeBaselineLayout) {
1119
+ // If the child is baseline aligned then the cross dimension is
1120
+ // calculated by adding maxAscent and maxDescent from the baseline.
1121
+ const float ascent = calculateBaseline(child) +
1122
+ child->style().computeFlexStartMargin(
1123
+ FlexDirection::Column, direction, availableInnerWidth);
1124
+ const float descent =
1125
+ child->getLayout().measuredDimension(Dimension::Height) +
1126
+ child->style().computeMarginForAxis(
1127
+ FlexDirection::Column, availableInnerWidth) -
1128
+ ascent;
1129
+
1130
+ maxAscentForCurrentLine =
1131
+ yoga::maxOrDefined(maxAscentForCurrentLine, ascent);
1132
+ maxDescentForCurrentLine =
1133
+ yoga::maxOrDefined(maxDescentForCurrentLine, descent);
1134
+ } else {
1135
+ // The cross dimension is the max of the elements dimension since
1136
+ // there can only be one element in that cross dimension in the case
1137
+ // when the items are not baseline aligned
1138
+ flexLine.layout.crossDim = yoga::maxOrDefined(
1139
+ flexLine.layout.crossDim,
1140
+ child->dimensionWithMargin(crossAxis, availableInnerWidth));
1141
+ }
1142
+ }
1143
+ }
1144
+ flexLine.layout.mainDim += trailingPaddingAndBorderMain;
1145
+
1146
+ if (isNodeBaselineLayout) {
1147
+ flexLine.layout.crossDim =
1148
+ maxAscentForCurrentLine + maxDescentForCurrentLine;
1149
+ }
1150
+ }
1151
+
1152
+ //
1153
+ // This is the main routine that implements a subset of the flexbox layout
1154
+ // algorithm described in the W3C CSS documentation:
1155
+ // https://www.w3.org/TR/CSS3-flexbox/.
1156
+ //
1157
+ // Limitations of this algorithm, compared to the full standard:
1158
+ // * Display property is always assumed to be 'flex' except for Text nodes,
1159
+ // which are assumed to be 'inline-flex'.
1160
+ // * The 'zIndex' property (or any form of z ordering) is not supported. Nodes
1161
+ // are stacked in document order.
1162
+ // * The 'order' property is not supported. The order of flex items is always
1163
+ // defined by document order.
1164
+ // * The 'visibility' property is always assumed to be 'visible'. Values of
1165
+ // 'collapse' and 'hidden' are not supported.
1166
+ // * There is no support for forced breaks.
1167
+ // * It does not support vertical inline directions (top-to-bottom or
1168
+ // bottom-to-top text).
1169
+ //
1170
+ // Deviations from standard:
1171
+ // * Section 4.5 of the spec indicates that all flex items have a default
1172
+ // minimum main size. For text blocks, for example, this is the width of the
1173
+ // widest word. Calculating the minimum width is expensive, so we forego it
1174
+ // and assume a default minimum main size of 0.
1175
+ // * Min/Max sizes in the main axis are not honored when resolving flexible
1176
+ // lengths.
1177
+ // * The spec indicates that the default value for 'flexDirection' is 'row',
1178
+ // but the algorithm below assumes a default of 'column'.
1179
+ //
1180
+ // Input parameters:
1181
+ // - node: current node to be sized and laid out
1182
+ // - availableWidth & availableHeight: available size to be used for sizing
1183
+ // the node or YGUndefined if the size is not available; interpretation
1184
+ // depends on layout flags
1185
+ // - ownerDirection: the inline (text) direction within the owner
1186
+ // (left-to-right or right-to-left)
1187
+ // - widthSizingMode: indicates the sizing rules for the width (see below
1188
+ // for explanation)
1189
+ // - heightSizingMode: indicates the sizing rules for the height (see below
1190
+ // for explanation)
1191
+ // - performLayout: specifies whether the caller is interested in just the
1192
+ // dimensions of the node or it requires the entire node and its subtree to
1193
+ // be laid out (with final positions)
1194
+ //
1195
+ // Details:
1196
+ // This routine is called recursively to lay out subtrees of flexbox
1197
+ // elements. It uses the information in node.style, which is treated as a
1198
+ // read-only input. It is responsible for setting the layout.direction and
1199
+ // layout.measuredDimensions fields for the input node as well as the
1200
+ // layout.position and layout.lineIndex fields for its child nodes. The
1201
+ // layout.measuredDimensions field includes any border or padding for the
1202
+ // node but does not include margins.
1203
+ //
1204
+ // When calling calculateLayoutImpl and calculateLayoutInternal, if the
1205
+ // caller passes an available size of undefined then it must also pass a
1206
+ // measure mode of SizingMode::MaxContent in that dimension.
1207
+ //
1208
+ static void calculateLayoutImpl(
1209
+ yoga::Node* const node,
1210
+ const float availableWidth,
1211
+ const float availableHeight,
1212
+ const Direction ownerDirection,
1213
+ const SizingMode widthSizingMode,
1214
+ const SizingMode heightSizingMode,
1215
+ const float ownerWidth,
1216
+ const float ownerHeight,
1217
+ const bool performLayout,
1218
+ const LayoutPassReason reason,
1219
+ LayoutData& layoutMarkerData,
1220
+ const uint32_t depth,
1221
+ const uint32_t generationCount) {
1222
+ yoga::assertFatalWithNode(
1223
+ node,
1224
+ yoga::isUndefined(availableWidth)
1225
+ ? widthSizingMode == SizingMode::MaxContent
1226
+ : true,
1227
+ "availableWidth is indefinite so widthSizingMode must be "
1228
+ "SizingMode::MaxContent");
1229
+ yoga::assertFatalWithNode(
1230
+ node,
1231
+ yoga::isUndefined(availableHeight)
1232
+ ? heightSizingMode == SizingMode::MaxContent
1233
+ : true,
1234
+ "availableHeight is indefinite so heightSizingMode must be "
1235
+ "SizingMode::MaxContent");
1236
+
1237
+ (performLayout ? layoutMarkerData.layouts : layoutMarkerData.measures) += 1;
1238
+
1239
+ // Set the resolved resolution in the node's layout.
1240
+ const Direction direction = node->resolveDirection(ownerDirection);
1241
+ node->setLayoutDirection(direction);
1242
+
1243
+ const FlexDirection flexRowDirection =
1244
+ resolveDirection(FlexDirection::Row, direction);
1245
+ const FlexDirection flexColumnDirection =
1246
+ resolveDirection(FlexDirection::Column, direction);
1247
+
1248
+ const auto startEdge =
1249
+ direction == Direction::LTR ? PhysicalEdge::Left : PhysicalEdge::Right;
1250
+ const auto endEdge =
1251
+ direction == Direction::LTR ? PhysicalEdge::Right : PhysicalEdge::Left;
1252
+
1253
+ const float marginRowLeading = node->style().computeInlineStartMargin(
1254
+ flexRowDirection, direction, ownerWidth);
1255
+ node->setLayoutMargin(marginRowLeading, startEdge);
1256
+ const float marginRowTrailing = node->style().computeInlineEndMargin(
1257
+ flexRowDirection, direction, ownerWidth);
1258
+ node->setLayoutMargin(marginRowTrailing, endEdge);
1259
+ const float marginColumnLeading = node->style().computeInlineStartMargin(
1260
+ flexColumnDirection, direction, ownerWidth);
1261
+ node->setLayoutMargin(marginColumnLeading, PhysicalEdge::Top);
1262
+ const float marginColumnTrailing = node->style().computeInlineEndMargin(
1263
+ flexColumnDirection, direction, ownerWidth);
1264
+ node->setLayoutMargin(marginColumnTrailing, PhysicalEdge::Bottom);
1265
+
1266
+ const float marginAxisRow = marginRowLeading + marginRowTrailing;
1267
+ const float marginAxisColumn = marginColumnLeading + marginColumnTrailing;
1268
+
1269
+ node->setLayoutBorder(
1270
+ node->style().computeInlineStartBorder(flexRowDirection, direction),
1271
+ startEdge);
1272
+ node->setLayoutBorder(
1273
+ node->style().computeInlineEndBorder(flexRowDirection, direction),
1274
+ endEdge);
1275
+ node->setLayoutBorder(
1276
+ node->style().computeInlineStartBorder(flexColumnDirection, direction),
1277
+ PhysicalEdge::Top);
1278
+ node->setLayoutBorder(
1279
+ node->style().computeInlineEndBorder(flexColumnDirection, direction),
1280
+ PhysicalEdge::Bottom);
1281
+
1282
+ node->setLayoutPadding(
1283
+ node->style().computeInlineStartPadding(
1284
+ flexRowDirection, direction, ownerWidth),
1285
+ startEdge);
1286
+ node->setLayoutPadding(
1287
+ node->style().computeInlineEndPadding(
1288
+ flexRowDirection, direction, ownerWidth),
1289
+ endEdge);
1290
+ node->setLayoutPadding(
1291
+ node->style().computeInlineStartPadding(
1292
+ flexColumnDirection, direction, ownerWidth),
1293
+ PhysicalEdge::Top);
1294
+ node->setLayoutPadding(
1295
+ node->style().computeInlineEndPadding(
1296
+ flexColumnDirection, direction, ownerWidth),
1297
+ PhysicalEdge::Bottom);
1298
+
1299
+ if (node->hasMeasureFunc()) {
1300
+ measureNodeWithMeasureFunc(
1301
+ node,
1302
+ direction,
1303
+ availableWidth - marginAxisRow,
1304
+ availableHeight - marginAxisColumn,
1305
+ widthSizingMode,
1306
+ heightSizingMode,
1307
+ ownerWidth,
1308
+ ownerHeight,
1309
+ layoutMarkerData,
1310
+ reason);
1311
+
1312
+ // Clean and update all display: contents nodes with a direct path to the
1313
+ // current node as they will not be traversed
1314
+ cleanupContentsNodesRecursively(node);
1315
+ return;
1316
+ }
1317
+
1318
+ const auto childCount = node->getLayoutChildCount();
1319
+ if (childCount == 0) {
1320
+ measureNodeWithoutChildren(
1321
+ node,
1322
+ direction,
1323
+ availableWidth - marginAxisRow,
1324
+ availableHeight - marginAxisColumn,
1325
+ widthSizingMode,
1326
+ heightSizingMode,
1327
+ ownerWidth,
1328
+ ownerHeight);
1329
+
1330
+ // Clean and update all display: contents nodes with a direct path to the
1331
+ // current node as they will not be traversed
1332
+ cleanupContentsNodesRecursively(node);
1333
+ return;
1334
+ }
1335
+
1336
+ // If we're not being asked to perform a full layout we can skip the algorithm
1337
+ // if we already know the size
1338
+ if (!performLayout &&
1339
+ measureNodeWithFixedSize(
1340
+ node,
1341
+ direction,
1342
+ availableWidth - marginAxisRow,
1343
+ availableHeight - marginAxisColumn,
1344
+ widthSizingMode,
1345
+ heightSizingMode,
1346
+ ownerWidth,
1347
+ ownerHeight)) {
1348
+ // Clean and update all display: contents nodes with a direct path to the
1349
+ // current node as they will not be traversed
1350
+ cleanupContentsNodesRecursively(node);
1351
+ return;
1352
+ }
1353
+
1354
+ // At this point we know we're going to perform work. Ensure that each child
1355
+ // has a mutable copy.
1356
+ node->cloneChildrenIfNeeded();
1357
+ // Reset layout flags, as they could have changed.
1358
+ node->setLayoutHadOverflow(false);
1359
+
1360
+ // Clean and update all display: contents nodes with a direct path to the
1361
+ // current node as they will not be traversed
1362
+ cleanupContentsNodesRecursively(node);
1363
+
1364
+ // STEP 1: CALCULATE VALUES FOR REMAINDER OF ALGORITHM
1365
+ const FlexDirection mainAxis =
1366
+ resolveDirection(node->style().flexDirection(), direction);
1367
+ const FlexDirection crossAxis = resolveCrossDirection(mainAxis, direction);
1368
+ const bool isMainAxisRow = isRow(mainAxis);
1369
+ const bool isNodeFlexWrap = node->style().flexWrap() != Wrap::NoWrap;
1370
+
1371
+ const float mainAxisOwnerSize = isMainAxisRow ? ownerWidth : ownerHeight;
1372
+ const float crossAxisOwnerSize = isMainAxisRow ? ownerHeight : ownerWidth;
1373
+
1374
+ const float paddingAndBorderAxisMain =
1375
+ paddingAndBorderForAxis(node, mainAxis, direction, ownerWidth);
1376
+ const float paddingAndBorderAxisCross =
1377
+ paddingAndBorderForAxis(node, crossAxis, direction, ownerWidth);
1378
+ const float leadingPaddingAndBorderCross =
1379
+ node->style().computeFlexStartPaddingAndBorder(
1380
+ crossAxis, direction, ownerWidth);
1381
+
1382
+ SizingMode sizingModeMainDim =
1383
+ isMainAxisRow ? widthSizingMode : heightSizingMode;
1384
+ SizingMode sizingModeCrossDim =
1385
+ isMainAxisRow ? heightSizingMode : widthSizingMode;
1386
+
1387
+ const float paddingAndBorderAxisRow =
1388
+ isMainAxisRow ? paddingAndBorderAxisMain : paddingAndBorderAxisCross;
1389
+ const float paddingAndBorderAxisColumn =
1390
+ isMainAxisRow ? paddingAndBorderAxisCross : paddingAndBorderAxisMain;
1391
+
1392
+ // STEP 2: DETERMINE AVAILABLE SIZE IN MAIN AND CROSS DIRECTIONS
1393
+
1394
+ float availableInnerWidth = calculateAvailableInnerDimension(
1395
+ node,
1396
+ direction,
1397
+ Dimension::Width,
1398
+ availableWidth - marginAxisRow,
1399
+ paddingAndBorderAxisRow,
1400
+ ownerWidth,
1401
+ ownerWidth);
1402
+ float availableInnerHeight = calculateAvailableInnerDimension(
1403
+ node,
1404
+ direction,
1405
+ Dimension::Height,
1406
+ availableHeight - marginAxisColumn,
1407
+ paddingAndBorderAxisColumn,
1408
+ ownerHeight,
1409
+ ownerWidth);
1410
+
1411
+ float availableInnerMainDim =
1412
+ isMainAxisRow ? availableInnerWidth : availableInnerHeight;
1413
+ const float availableInnerCrossDim =
1414
+ isMainAxisRow ? availableInnerHeight : availableInnerWidth;
1415
+
1416
+ // STEP 3: DETERMINE FLEX BASIS FOR EACH ITEM
1417
+
1418
+ // Computed basis + margins + gap
1419
+ float totalMainDim = 0;
1420
+ totalMainDim += computeFlexBasisForChildren(
1421
+ node,
1422
+ availableInnerWidth,
1423
+ availableInnerHeight,
1424
+ widthSizingMode,
1425
+ heightSizingMode,
1426
+ direction,
1427
+ mainAxis,
1428
+ performLayout,
1429
+ layoutMarkerData,
1430
+ depth,
1431
+ generationCount);
1432
+
1433
+ if (childCount > 1) {
1434
+ totalMainDim +=
1435
+ node->style().computeGapForAxis(mainAxis, availableInnerMainDim) *
1436
+ static_cast<float>(childCount - 1);
1437
+ }
1438
+
1439
+ const bool mainAxisOverflows =
1440
+ (sizingModeMainDim != SizingMode::MaxContent) &&
1441
+ totalMainDim > availableInnerMainDim;
1442
+
1443
+ if (isNodeFlexWrap && mainAxisOverflows &&
1444
+ sizingModeMainDim == SizingMode::FitContent) {
1445
+ sizingModeMainDim = SizingMode::StretchFit;
1446
+ }
1447
+ // STEP 4: COLLECT FLEX ITEMS INTO FLEX LINES
1448
+
1449
+ // Iterator representing the beginning of the current line
1450
+ Node::LayoutableChildren::Iterator startOfLineIterator =
1451
+ node->getLayoutChildren().begin();
1452
+
1453
+ // Number of lines.
1454
+ size_t lineCount = 0;
1455
+
1456
+ // Accumulated cross dimensions of all lines so far.
1457
+ float totalLineCrossDim = 0;
1458
+
1459
+ const float crossAxisGap =
1460
+ node->style().computeGapForAxis(crossAxis, availableInnerCrossDim);
1461
+
1462
+ // Max main dimension of all the lines.
1463
+ float maxLineMainDim = 0;
1464
+ for (; startOfLineIterator != node->getLayoutChildren().end(); lineCount++) {
1465
+ auto flexLine = calculateFlexLine(
1466
+ node,
1467
+ ownerDirection,
1468
+ ownerWidth,
1469
+ mainAxisOwnerSize,
1470
+ availableInnerWidth,
1471
+ availableInnerMainDim,
1472
+ startOfLineIterator,
1473
+ lineCount);
1474
+
1475
+ // If we don't need to measure the cross axis, we can skip the entire flex
1476
+ // step.
1477
+ const bool canSkipFlex =
1478
+ !performLayout && sizingModeCrossDim == SizingMode::StretchFit;
1479
+
1480
+ // STEP 5: RESOLVING FLEXIBLE LENGTHS ON MAIN AXIS
1481
+ // Calculate the remaining available space that needs to be allocated. If
1482
+ // the main dimension size isn't known, it is computed based on the line
1483
+ // length, so there's no more space left to distribute.
1484
+
1485
+ bool sizeBasedOnContent = false;
1486
+ // If we don't measure with exact main dimension we want to ensure we don't
1487
+ // violate min and max
1488
+ if (sizingModeMainDim != SizingMode::StretchFit) {
1489
+ const auto& style = node->style();
1490
+ const float minInnerWidth =
1491
+ style
1492
+ .resolvedMinDimension(
1493
+ direction, Dimension::Width, ownerWidth, ownerWidth)
1494
+ .unwrap() -
1495
+ paddingAndBorderAxisRow;
1496
+ const float maxInnerWidth =
1497
+ style
1498
+ .resolvedMaxDimension(
1499
+ direction, Dimension::Width, ownerWidth, ownerWidth)
1500
+ .unwrap() -
1501
+ paddingAndBorderAxisRow;
1502
+ const float minInnerHeight =
1503
+ style
1504
+ .resolvedMinDimension(
1505
+ direction, Dimension::Height, ownerHeight, ownerWidth)
1506
+ .unwrap() -
1507
+ paddingAndBorderAxisColumn;
1508
+ const float maxInnerHeight =
1509
+ style
1510
+ .resolvedMaxDimension(
1511
+ direction, Dimension::Height, ownerHeight, ownerWidth)
1512
+ .unwrap() -
1513
+ paddingAndBorderAxisColumn;
1514
+
1515
+ const float minInnerMainDim =
1516
+ isMainAxisRow ? minInnerWidth : minInnerHeight;
1517
+ const float maxInnerMainDim =
1518
+ isMainAxisRow ? maxInnerWidth : maxInnerHeight;
1519
+
1520
+ if (yoga::isDefined(minInnerMainDim) &&
1521
+ flexLine.sizeConsumed < minInnerMainDim) {
1522
+ availableInnerMainDim = minInnerMainDim;
1523
+ } else if (
1524
+ yoga::isDefined(maxInnerMainDim) &&
1525
+ flexLine.sizeConsumed > maxInnerMainDim) {
1526
+ availableInnerMainDim = maxInnerMainDim;
1527
+ } else {
1528
+ bool useLegacyStretchBehaviour =
1529
+ node->hasErrata(Errata::StretchFlexBasis);
1530
+
1531
+ if (!useLegacyStretchBehaviour &&
1532
+ ((yoga::isDefined(flexLine.layout.totalFlexGrowFactors) &&
1533
+ flexLine.layout.totalFlexGrowFactors == 0) ||
1534
+ (yoga::isDefined(node->resolveFlexGrow()) &&
1535
+ node->resolveFlexGrow() == 0))) {
1536
+ // If we don't have any children to flex or we can't flex the node
1537
+ // itself, space we've used is all space we need. Root node also
1538
+ // should be shrunk to minimum
1539
+ availableInnerMainDim = flexLine.sizeConsumed;
1540
+ }
1541
+
1542
+ sizeBasedOnContent = !useLegacyStretchBehaviour;
1543
+ }
1544
+ }
1545
+
1546
+ if (!sizeBasedOnContent && yoga::isDefined(availableInnerMainDim)) {
1547
+ flexLine.layout.remainingFreeSpace =
1548
+ availableInnerMainDim - flexLine.sizeConsumed;
1549
+ } else if (flexLine.sizeConsumed < 0) {
1550
+ // availableInnerMainDim is indefinite which means the node is being sized
1551
+ // based on its content. sizeConsumed is negative which means
1552
+ // the node will allocate 0 points for its content. Consequently,
1553
+ // remainingFreeSpace is 0 - sizeConsumed.
1554
+ flexLine.layout.remainingFreeSpace = -flexLine.sizeConsumed;
1555
+ }
1556
+
1557
+ if (!canSkipFlex) {
1558
+ resolveFlexibleLength(
1559
+ node,
1560
+ flexLine,
1561
+ mainAxis,
1562
+ crossAxis,
1563
+ direction,
1564
+ ownerWidth,
1565
+ mainAxisOwnerSize,
1566
+ availableInnerMainDim,
1567
+ availableInnerCrossDim,
1568
+ availableInnerWidth,
1569
+ availableInnerHeight,
1570
+ mainAxisOverflows,
1571
+ sizingModeCrossDim,
1572
+ performLayout,
1573
+ layoutMarkerData,
1574
+ depth,
1575
+ generationCount);
1576
+ }
1577
+
1578
+ node->setLayoutHadOverflow(
1579
+ node->getLayout().hadOverflow() ||
1580
+ (flexLine.layout.remainingFreeSpace < 0));
1581
+
1582
+ // STEP 6: MAIN-AXIS JUSTIFICATION & CROSS-AXIS SIZE DETERMINATION
1583
+
1584
+ // At this point, all the children have their dimensions set in the main
1585
+ // axis. Their dimensions are also set in the cross axis with the exception
1586
+ // of items that are aligned "stretch". We need to compute these stretch
1587
+ // values and set the final positions.
1588
+
1589
+ justifyMainAxis(
1590
+ node,
1591
+ flexLine,
1592
+ mainAxis,
1593
+ crossAxis,
1594
+ direction,
1595
+ sizingModeMainDim,
1596
+ sizingModeCrossDim,
1597
+ mainAxisOwnerSize,
1598
+ ownerWidth,
1599
+ availableInnerMainDim,
1600
+ availableInnerCrossDim,
1601
+ availableInnerWidth,
1602
+ performLayout);
1603
+
1604
+ float containerCrossAxis = availableInnerCrossDim;
1605
+ if (sizingModeCrossDim == SizingMode::MaxContent ||
1606
+ sizingModeCrossDim == SizingMode::FitContent) {
1607
+ // Compute the cross axis from the max cross dimension of the children.
1608
+ containerCrossAxis =
1609
+ boundAxis(
1610
+ node,
1611
+ crossAxis,
1612
+ direction,
1613
+ flexLine.layout.crossDim + paddingAndBorderAxisCross,
1614
+ crossAxisOwnerSize,
1615
+ ownerWidth) -
1616
+ paddingAndBorderAxisCross;
1617
+ }
1618
+
1619
+ // If there's no flex wrap, the cross dimension is defined by the container.
1620
+ if (!isNodeFlexWrap && sizingModeCrossDim == SizingMode::StretchFit) {
1621
+ flexLine.layout.crossDim = availableInnerCrossDim;
1622
+ }
1623
+
1624
+ // As-per https://www.w3.org/TR/css-flexbox-1/#cross-sizing, the
1625
+ // cross-size of the line within a single-line container should be bound to
1626
+ // min/max constraints before alignment within the line. In a multi-line
1627
+ // container, affecting alignment between the lines.
1628
+ if (!isNodeFlexWrap) {
1629
+ flexLine.layout.crossDim =
1630
+ boundAxis(
1631
+ node,
1632
+ crossAxis,
1633
+ direction,
1634
+ flexLine.layout.crossDim + paddingAndBorderAxisCross,
1635
+ crossAxisOwnerSize,
1636
+ ownerWidth) -
1637
+ paddingAndBorderAxisCross;
1638
+ }
1639
+
1640
+ // STEP 7: CROSS-AXIS ALIGNMENT
1641
+ // We can skip child alignment if we're just measuring the container.
1642
+ if (performLayout) {
1643
+ for (auto child : flexLine.itemsInFlow) {
1644
+ float leadingCrossDim = leadingPaddingAndBorderCross;
1645
+
1646
+ // For a relative children, we're either using alignItems (owner) or
1647
+ // alignSelf (child) in order to determine the position in the cross
1648
+ // axis
1649
+ const Align alignItem = resolveChildAlignment(node, child);
1650
+
1651
+ // If the child uses align stretch, we need to lay it out one more
1652
+ // time, this time forcing the cross-axis size to be the computed
1653
+ // cross size for the current line.
1654
+ if (alignItem == Align::Stretch &&
1655
+ !child->style().flexStartMarginIsAuto(crossAxis, direction) &&
1656
+ !child->style().flexEndMarginIsAuto(crossAxis, direction)) {
1657
+ // If the child defines a definite size for its cross axis, there's
1658
+ // no need to stretch.
1659
+ if (!child->hasDefiniteLength(
1660
+ dimension(crossAxis), availableInnerCrossDim)) {
1661
+ float childMainSize =
1662
+ child->getLayout().measuredDimension(dimension(mainAxis));
1663
+ const auto& childStyle = child->style();
1664
+ float childCrossSize = childStyle.aspectRatio().isDefined()
1665
+ ? child->style().computeMarginForAxis(
1666
+ crossAxis, availableInnerWidth) +
1667
+ (isMainAxisRow
1668
+ ? childMainSize / childStyle.aspectRatio().unwrap()
1669
+ : childMainSize * childStyle.aspectRatio().unwrap())
1670
+ : flexLine.layout.crossDim;
1671
+
1672
+ childMainSize += child->style().computeMarginForAxis(
1673
+ mainAxis, availableInnerWidth);
1674
+
1675
+ SizingMode childMainSizingMode = SizingMode::StretchFit;
1676
+ SizingMode childCrossSizingMode = SizingMode::StretchFit;
1677
+ constrainMaxSizeForMode(
1678
+ child,
1679
+ direction,
1680
+ mainAxis,
1681
+ availableInnerMainDim,
1682
+ availableInnerWidth,
1683
+ &childMainSizingMode,
1684
+ &childMainSize);
1685
+ constrainMaxSizeForMode(
1686
+ child,
1687
+ direction,
1688
+ crossAxis,
1689
+ availableInnerCrossDim,
1690
+ availableInnerWidth,
1691
+ &childCrossSizingMode,
1692
+ &childCrossSize);
1693
+
1694
+ const float childWidth =
1695
+ isMainAxisRow ? childMainSize : childCrossSize;
1696
+ const float childHeight =
1697
+ !isMainAxisRow ? childMainSize : childCrossSize;
1698
+
1699
+ auto alignContent = node->style().alignContent();
1700
+ auto crossAxisDoesNotGrow =
1701
+ alignContent != Align::Stretch && isNodeFlexWrap;
1702
+ const SizingMode childWidthSizingMode =
1703
+ yoga::isUndefined(childWidth) ||
1704
+ (!isMainAxisRow && crossAxisDoesNotGrow)
1705
+ ? SizingMode::MaxContent
1706
+ : SizingMode::StretchFit;
1707
+ const SizingMode childHeightSizingMode =
1708
+ yoga::isUndefined(childHeight) ||
1709
+ (isMainAxisRow && crossAxisDoesNotGrow)
1710
+ ? SizingMode::MaxContent
1711
+ : SizingMode::StretchFit;
1712
+
1713
+ calculateLayoutInternal(
1714
+ child,
1715
+ childWidth,
1716
+ childHeight,
1717
+ direction,
1718
+ childWidthSizingMode,
1719
+ childHeightSizingMode,
1720
+ availableInnerWidth,
1721
+ availableInnerHeight,
1722
+ true,
1723
+ LayoutPassReason::kStretch,
1724
+ layoutMarkerData,
1725
+ depth,
1726
+ generationCount);
1727
+ }
1728
+ } else {
1729
+ const float remainingCrossDim = containerCrossAxis -
1730
+ child->dimensionWithMargin(crossAxis, availableInnerWidth);
1731
+
1732
+ if (child->style().flexStartMarginIsAuto(crossAxis, direction) &&
1733
+ child->style().flexEndMarginIsAuto(crossAxis, direction)) {
1734
+ leadingCrossDim += yoga::maxOrDefined(0.0f, remainingCrossDim / 2);
1735
+ } else if (child->style().flexEndMarginIsAuto(crossAxis, direction)) {
1736
+ // No-Op
1737
+ } else if (child->style().flexStartMarginIsAuto(
1738
+ crossAxis, direction)) {
1739
+ leadingCrossDim += yoga::maxOrDefined(0.0f, remainingCrossDim);
1740
+ } else if (alignItem == Align::FlexStart) {
1741
+ // No-Op
1742
+ } else if (alignItem == Align::Center) {
1743
+ leadingCrossDim += remainingCrossDim / 2;
1744
+ } else {
1745
+ leadingCrossDim += remainingCrossDim;
1746
+ }
1747
+ }
1748
+ // And we apply the position
1749
+ child->setLayoutPosition(
1750
+ child->getLayout().position(flexStartEdge(crossAxis)) +
1751
+ totalLineCrossDim + leadingCrossDim,
1752
+ flexStartEdge(crossAxis));
1753
+ }
1754
+ }
1755
+
1756
+ const float appliedCrossGap = lineCount != 0 ? crossAxisGap : 0.0f;
1757
+ totalLineCrossDim += flexLine.layout.crossDim + appliedCrossGap;
1758
+ maxLineMainDim =
1759
+ yoga::maxOrDefined(maxLineMainDim, flexLine.layout.mainDim);
1760
+ }
1761
+
1762
+ // STEP 8: MULTI-LINE CONTENT ALIGNMENT
1763
+ // currentLead stores the size of the cross dim
1764
+ if (performLayout && (isNodeFlexWrap || isBaselineLayout(node))) {
1765
+ float leadPerLine = 0;
1766
+ float currentLead = leadingPaddingAndBorderCross;
1767
+ float extraSpacePerLine = 0;
1768
+
1769
+ const float unclampedCrossDim = sizingModeCrossDim == SizingMode::StretchFit
1770
+ ? availableInnerCrossDim + paddingAndBorderAxisCross
1771
+ : node->hasDefiniteLength(dimension(crossAxis), crossAxisOwnerSize)
1772
+ ? node->getResolvedDimension(
1773
+ direction,
1774
+ dimension(crossAxis),
1775
+ crossAxisOwnerSize,
1776
+ ownerWidth)
1777
+ .unwrap()
1778
+ : totalLineCrossDim + paddingAndBorderAxisCross;
1779
+
1780
+ const float innerCrossDim = boundAxis(
1781
+ node,
1782
+ crossAxis,
1783
+ direction,
1784
+ unclampedCrossDim,
1785
+ crossAxisOwnerSize,
1786
+ ownerWidth) -
1787
+ paddingAndBorderAxisCross;
1788
+
1789
+ const float remainingAlignContentDim = innerCrossDim - totalLineCrossDim;
1790
+
1791
+ const auto alignContent = remainingAlignContentDim >= 0
1792
+ ? node->style().alignContent()
1793
+ : fallbackAlignment(node->style().alignContent());
1794
+
1795
+ switch (alignContent) {
1796
+ case Align::FlexEnd:
1797
+ currentLead += remainingAlignContentDim;
1798
+ break;
1799
+ case Align::Center:
1800
+ currentLead += remainingAlignContentDim / 2;
1801
+ break;
1802
+ case Align::Stretch:
1803
+ extraSpacePerLine =
1804
+ remainingAlignContentDim / static_cast<float>(lineCount);
1805
+ break;
1806
+ case Align::SpaceAround:
1807
+ currentLead +=
1808
+ remainingAlignContentDim / (2 * static_cast<float>(lineCount));
1809
+ leadPerLine = remainingAlignContentDim / static_cast<float>(lineCount);
1810
+ break;
1811
+ case Align::SpaceEvenly:
1812
+ currentLead +=
1813
+ remainingAlignContentDim / static_cast<float>(lineCount + 1);
1814
+ leadPerLine =
1815
+ remainingAlignContentDim / static_cast<float>(lineCount + 1);
1816
+ break;
1817
+ case Align::SpaceBetween:
1818
+ if (lineCount > 1) {
1819
+ leadPerLine =
1820
+ remainingAlignContentDim / static_cast<float>(lineCount - 1);
1821
+ }
1822
+ break;
1823
+ case Align::Auto:
1824
+ case Align::FlexStart:
1825
+ case Align::Baseline:
1826
+ break;
1827
+ }
1828
+ Node::LayoutableChildren::Iterator endIterator =
1829
+ node->getLayoutChildren().begin();
1830
+ for (size_t i = 0; i < lineCount; i++) {
1831
+ const Node::LayoutableChildren::Iterator startIterator = endIterator;
1832
+ auto iterator = startIterator;
1833
+
1834
+ // compute the line's height and find the endIndex
1835
+ float lineHeight = 0;
1836
+ float maxAscentForCurrentLine = 0;
1837
+ float maxDescentForCurrentLine = 0;
1838
+ for (; iterator != node->getLayoutChildren().end(); iterator++) {
1839
+ const auto child = *iterator;
1840
+ if (child->style().display() == Display::None) {
1841
+ continue;
1842
+ }
1843
+ if (child->style().positionType() != PositionType::Absolute) {
1844
+ if (child->getLineIndex() != i) {
1845
+ break;
1846
+ }
1847
+ if (child->isLayoutDimensionDefined(crossAxis)) {
1848
+ lineHeight = yoga::maxOrDefined(
1849
+ lineHeight,
1850
+ child->getLayout().measuredDimension(dimension(crossAxis)) +
1851
+ child->style().computeMarginForAxis(
1852
+ crossAxis, availableInnerWidth));
1853
+ }
1854
+ if (resolveChildAlignment(node, child) == Align::Baseline) {
1855
+ const float ascent = calculateBaseline(child) +
1856
+ child->style().computeFlexStartMargin(
1857
+ FlexDirection::Column, direction, availableInnerWidth);
1858
+ const float descent =
1859
+ child->getLayout().measuredDimension(Dimension::Height) +
1860
+ child->style().computeMarginForAxis(
1861
+ FlexDirection::Column, availableInnerWidth) -
1862
+ ascent;
1863
+ maxAscentForCurrentLine =
1864
+ yoga::maxOrDefined(maxAscentForCurrentLine, ascent);
1865
+ maxDescentForCurrentLine =
1866
+ yoga::maxOrDefined(maxDescentForCurrentLine, descent);
1867
+ lineHeight = yoga::maxOrDefined(
1868
+ lineHeight, maxAscentForCurrentLine + maxDescentForCurrentLine);
1869
+ }
1870
+ }
1871
+ }
1872
+ endIterator = iterator;
1873
+ currentLead += i != 0 ? crossAxisGap : 0;
1874
+ lineHeight += extraSpacePerLine;
1875
+
1876
+ for (iterator = startIterator; iterator != endIterator; iterator++) {
1877
+ const auto child = *iterator;
1878
+ if (child->style().display() == Display::None) {
1879
+ continue;
1880
+ }
1881
+ if (child->style().positionType() != PositionType::Absolute) {
1882
+ switch (resolveChildAlignment(node, child)) {
1883
+ case Align::FlexStart: {
1884
+ child->setLayoutPosition(
1885
+ currentLead +
1886
+ child->style().computeFlexStartPosition(
1887
+ crossAxis, direction, availableInnerWidth),
1888
+ flexStartEdge(crossAxis));
1889
+ break;
1890
+ }
1891
+ case Align::FlexEnd: {
1892
+ child->setLayoutPosition(
1893
+ currentLead + lineHeight -
1894
+ child->style().computeFlexEndMargin(
1895
+ crossAxis, direction, availableInnerWidth) -
1896
+ child->getLayout().measuredDimension(
1897
+ dimension(crossAxis)),
1898
+ flexStartEdge(crossAxis));
1899
+ break;
1900
+ }
1901
+ case Align::Center: {
1902
+ float childHeight =
1903
+ child->getLayout().measuredDimension(dimension(crossAxis));
1904
+
1905
+ child->setLayoutPosition(
1906
+ currentLead + (lineHeight - childHeight) / 2,
1907
+ flexStartEdge(crossAxis));
1908
+ break;
1909
+ }
1910
+ case Align::Stretch: {
1911
+ child->setLayoutPosition(
1912
+ currentLead +
1913
+ child->style().computeFlexStartMargin(
1914
+ crossAxis, direction, availableInnerWidth),
1915
+ flexStartEdge(crossAxis));
1916
+
1917
+ // Remeasure child with the line height as it as been only
1918
+ // measured with the owners height yet.
1919
+ if (!child->hasDefiniteLength(
1920
+ dimension(crossAxis), availableInnerCrossDim)) {
1921
+ const float childWidth = isMainAxisRow
1922
+ ? (child->getLayout().measuredDimension(Dimension::Width) +
1923
+ child->style().computeMarginForAxis(
1924
+ mainAxis, availableInnerWidth))
1925
+ : leadPerLine + lineHeight;
1926
+
1927
+ const float childHeight = !isMainAxisRow
1928
+ ? (child->getLayout().measuredDimension(Dimension::Height) +
1929
+ child->style().computeMarginForAxis(
1930
+ crossAxis, availableInnerWidth))
1931
+ : leadPerLine + lineHeight;
1932
+
1933
+ if (!(yoga::inexactEquals(
1934
+ childWidth,
1935
+ child->getLayout().measuredDimension(
1936
+ Dimension::Width)) &&
1937
+ yoga::inexactEquals(
1938
+ childHeight,
1939
+ child->getLayout().measuredDimension(
1940
+ Dimension::Height)))) {
1941
+ calculateLayoutInternal(
1942
+ child,
1943
+ childWidth,
1944
+ childHeight,
1945
+ direction,
1946
+ SizingMode::StretchFit,
1947
+ SizingMode::StretchFit,
1948
+ availableInnerWidth,
1949
+ availableInnerHeight,
1950
+ true,
1951
+ LayoutPassReason::kMultilineStretch,
1952
+ layoutMarkerData,
1953
+ depth,
1954
+ generationCount);
1955
+ }
1956
+ }
1957
+ break;
1958
+ }
1959
+ case Align::Baseline: {
1960
+ child->setLayoutPosition(
1961
+ currentLead + maxAscentForCurrentLine -
1962
+ calculateBaseline(child) +
1963
+ child->style().computeFlexStartPosition(
1964
+ FlexDirection::Column,
1965
+ direction,
1966
+ availableInnerCrossDim),
1967
+ PhysicalEdge::Top);
1968
+
1969
+ break;
1970
+ }
1971
+ case Align::Auto:
1972
+ case Align::SpaceBetween:
1973
+ case Align::SpaceAround:
1974
+ case Align::SpaceEvenly:
1975
+ break;
1976
+ }
1977
+ }
1978
+ }
1979
+
1980
+ currentLead = currentLead + leadPerLine + lineHeight;
1981
+ }
1982
+ }
1983
+
1984
+ // STEP 9: COMPUTING FINAL DIMENSIONS
1985
+
1986
+ node->setLayoutMeasuredDimension(
1987
+ boundAxis(
1988
+ node,
1989
+ FlexDirection::Row,
1990
+ direction,
1991
+ availableWidth - marginAxisRow,
1992
+ ownerWidth,
1993
+ ownerWidth),
1994
+ Dimension::Width);
1995
+
1996
+ node->setLayoutMeasuredDimension(
1997
+ boundAxis(
1998
+ node,
1999
+ FlexDirection::Column,
2000
+ direction,
2001
+ availableHeight - marginAxisColumn,
2002
+ ownerHeight,
2003
+ ownerWidth),
2004
+ Dimension::Height);
2005
+
2006
+ // If the user didn't specify a width or height for the node, set the
2007
+ // dimensions based on the children.
2008
+ if (sizingModeMainDim == SizingMode::MaxContent ||
2009
+ (node->style().overflow() != Overflow::Scroll &&
2010
+ sizingModeMainDim == SizingMode::FitContent)) {
2011
+ // Clamp the size to the min/max size, if specified, and make sure it
2012
+ // doesn't go below the padding and border amount.
2013
+ node->setLayoutMeasuredDimension(
2014
+ boundAxis(
2015
+ node,
2016
+ mainAxis,
2017
+ direction,
2018
+ maxLineMainDim,
2019
+ mainAxisOwnerSize,
2020
+ ownerWidth),
2021
+ dimension(mainAxis));
2022
+
2023
+ } else if (
2024
+ sizingModeMainDim == SizingMode::FitContent &&
2025
+ node->style().overflow() == Overflow::Scroll) {
2026
+ node->setLayoutMeasuredDimension(
2027
+ yoga::maxOrDefined(
2028
+ yoga::minOrDefined(
2029
+ availableInnerMainDim + paddingAndBorderAxisMain,
2030
+ boundAxisWithinMinAndMax(
2031
+ node,
2032
+ direction,
2033
+ mainAxis,
2034
+ FloatOptional{maxLineMainDim},
2035
+ mainAxisOwnerSize,
2036
+ ownerWidth)
2037
+ .unwrap()),
2038
+ paddingAndBorderAxisMain),
2039
+ dimension(mainAxis));
2040
+ }
2041
+
2042
+ if (sizingModeCrossDim == SizingMode::MaxContent ||
2043
+ (node->style().overflow() != Overflow::Scroll &&
2044
+ sizingModeCrossDim == SizingMode::FitContent)) {
2045
+ // Clamp the size to the min/max size, if specified, and make sure it
2046
+ // doesn't go below the padding and border amount.
2047
+ node->setLayoutMeasuredDimension(
2048
+ boundAxis(
2049
+ node,
2050
+ crossAxis,
2051
+ direction,
2052
+ totalLineCrossDim + paddingAndBorderAxisCross,
2053
+ crossAxisOwnerSize,
2054
+ ownerWidth),
2055
+ dimension(crossAxis));
2056
+
2057
+ } else if (
2058
+ sizingModeCrossDim == SizingMode::FitContent &&
2059
+ node->style().overflow() == Overflow::Scroll) {
2060
+ node->setLayoutMeasuredDimension(
2061
+ yoga::maxOrDefined(
2062
+ yoga::minOrDefined(
2063
+ availableInnerCrossDim + paddingAndBorderAxisCross,
2064
+ boundAxisWithinMinAndMax(
2065
+ node,
2066
+ direction,
2067
+ crossAxis,
2068
+ FloatOptional{
2069
+ totalLineCrossDim + paddingAndBorderAxisCross},
2070
+ crossAxisOwnerSize,
2071
+ ownerWidth)
2072
+ .unwrap()),
2073
+ paddingAndBorderAxisCross),
2074
+ dimension(crossAxis));
2075
+ }
2076
+
2077
+ // As we only wrapped in normal direction yet, we need to reverse the
2078
+ // positions on wrap-reverse.
2079
+ if (performLayout && node->style().flexWrap() == Wrap::WrapReverse) {
2080
+ for (auto child : node->getLayoutChildren()) {
2081
+ if (child->style().positionType() != PositionType::Absolute) {
2082
+ child->setLayoutPosition(
2083
+ node->getLayout().measuredDimension(dimension(crossAxis)) -
2084
+ child->getLayout().position(flexStartEdge(crossAxis)) -
2085
+ child->getLayout().measuredDimension(dimension(crossAxis)),
2086
+ flexStartEdge(crossAxis));
2087
+ }
2088
+ }
2089
+ }
2090
+
2091
+ if (performLayout) {
2092
+ // STEP 10: SETTING TRAILING POSITIONS FOR CHILDREN
2093
+ const bool needsMainTrailingPos = needsTrailingPosition(mainAxis);
2094
+ const bool needsCrossTrailingPos = needsTrailingPosition(crossAxis);
2095
+
2096
+ if (needsMainTrailingPos || needsCrossTrailingPos) {
2097
+ for (auto child : node->getLayoutChildren()) {
2098
+ // Absolute children will be handled by their containing block since we
2099
+ // cannot guarantee that their positions are set when their parents are
2100
+ // done with layout.
2101
+ if (child->style().display() == Display::None ||
2102
+ child->style().positionType() == PositionType::Absolute) {
2103
+ continue;
2104
+ }
2105
+ if (needsMainTrailingPos) {
2106
+ setChildTrailingPosition(node, child, mainAxis);
2107
+ }
2108
+
2109
+ if (needsCrossTrailingPos) {
2110
+ setChildTrailingPosition(node, child, crossAxis);
2111
+ }
2112
+ }
2113
+ }
2114
+
2115
+ // STEP 11: SIZING AND POSITIONING ABSOLUTE CHILDREN
2116
+ // Let the containing block layout its absolute descendants.
2117
+ if (node->style().positionType() != PositionType::Static ||
2118
+ node->alwaysFormsContainingBlock() || depth == 1) {
2119
+ layoutAbsoluteDescendants(
2120
+ node,
2121
+ node,
2122
+ isMainAxisRow ? sizingModeMainDim : sizingModeCrossDim,
2123
+ direction,
2124
+ layoutMarkerData,
2125
+ depth,
2126
+ generationCount,
2127
+ 0.0f,
2128
+ 0.0f,
2129
+ availableInnerWidth,
2130
+ availableInnerHeight);
2131
+ }
2132
+ }
2133
+ }
2134
+
2135
+ //
2136
+ // This is a wrapper around the calculateLayoutImpl function. It determines
2137
+ // whether the layout request is redundant and can be skipped.
2138
+ //
2139
+ // Parameters:
2140
+ // Input parameters are the same as calculateLayoutImpl (see above)
2141
+ // Return parameter is true if layout was performed, false if skipped
2142
+ //
2143
+ bool calculateLayoutInternal(
2144
+ yoga::Node* const node,
2145
+ const float availableWidth,
2146
+ const float availableHeight,
2147
+ const Direction ownerDirection,
2148
+ const SizingMode widthSizingMode,
2149
+ const SizingMode heightSizingMode,
2150
+ const float ownerWidth,
2151
+ const float ownerHeight,
2152
+ const bool performLayout,
2153
+ const LayoutPassReason reason,
2154
+ LayoutData& layoutMarkerData,
2155
+ uint32_t depth,
2156
+ const uint32_t generationCount) {
2157
+ LayoutResults* layout = &node->getLayout();
2158
+
2159
+ depth++;
2160
+
2161
+ const bool needToVisitNode =
2162
+ (node->isDirty() && layout->generationCount != generationCount) ||
2163
+ layout->configVersion != node->getConfig()->getVersion() ||
2164
+ layout->lastOwnerDirection != ownerDirection;
2165
+
2166
+ if (needToVisitNode) {
2167
+ // Invalidate the cached results.
2168
+ layout->nextCachedMeasurementsIndex = 0;
2169
+ layout->cachedLayout.availableWidth = -1;
2170
+ layout->cachedLayout.availableHeight = -1;
2171
+ layout->cachedLayout.widthSizingMode = SizingMode::MaxContent;
2172
+ layout->cachedLayout.heightSizingMode = SizingMode::MaxContent;
2173
+ layout->cachedLayout.computedWidth = -1;
2174
+ layout->cachedLayout.computedHeight = -1;
2175
+ }
2176
+
2177
+ CachedMeasurement* cachedResults = nullptr;
2178
+
2179
+ // Determine whether the results are already cached. We maintain a separate
2180
+ // cache for layouts and measurements. A layout operation modifies the
2181
+ // positions and dimensions for nodes in the subtree. The algorithm assumes
2182
+ // that each node gets laid out a maximum of one time per tree layout, but
2183
+ // multiple measurements may be required to resolve all of the flex
2184
+ // dimensions. We handle nodes with measure functions specially here because
2185
+ // they are the most expensive to measure, so it's worth avoiding redundant
2186
+ // measurements if at all possible.
2187
+ if (node->hasMeasureFunc()) {
2188
+ const float marginAxisRow =
2189
+ node->style().computeMarginForAxis(FlexDirection::Row, ownerWidth);
2190
+ const float marginAxisColumn =
2191
+ node->style().computeMarginForAxis(FlexDirection::Column, ownerWidth);
2192
+
2193
+ // First, try to use the layout cache.
2194
+ if (canUseCachedMeasurement(
2195
+ widthSizingMode,
2196
+ availableWidth,
2197
+ heightSizingMode,
2198
+ availableHeight,
2199
+ layout->cachedLayout.widthSizingMode,
2200
+ layout->cachedLayout.availableWidth,
2201
+ layout->cachedLayout.heightSizingMode,
2202
+ layout->cachedLayout.availableHeight,
2203
+ layout->cachedLayout.computedWidth,
2204
+ layout->cachedLayout.computedHeight,
2205
+ marginAxisRow,
2206
+ marginAxisColumn,
2207
+ node->getConfig())) {
2208
+ cachedResults = &layout->cachedLayout;
2209
+ } else {
2210
+ // Try to use the measurement cache.
2211
+ for (size_t i = 0; i < layout->nextCachedMeasurementsIndex; i++) {
2212
+ if (canUseCachedMeasurement(
2213
+ widthSizingMode,
2214
+ availableWidth,
2215
+ heightSizingMode,
2216
+ availableHeight,
2217
+ layout->cachedMeasurements[i].widthSizingMode,
2218
+ layout->cachedMeasurements[i].availableWidth,
2219
+ layout->cachedMeasurements[i].heightSizingMode,
2220
+ layout->cachedMeasurements[i].availableHeight,
2221
+ layout->cachedMeasurements[i].computedWidth,
2222
+ layout->cachedMeasurements[i].computedHeight,
2223
+ marginAxisRow,
2224
+ marginAxisColumn,
2225
+ node->getConfig())) {
2226
+ cachedResults = &layout->cachedMeasurements[i];
2227
+ break;
2228
+ }
2229
+ }
2230
+ }
2231
+ } else if (performLayout) {
2232
+ if (yoga::inexactEquals(
2233
+ layout->cachedLayout.availableWidth, availableWidth) &&
2234
+ yoga::inexactEquals(
2235
+ layout->cachedLayout.availableHeight, availableHeight) &&
2236
+ layout->cachedLayout.widthSizingMode == widthSizingMode &&
2237
+ layout->cachedLayout.heightSizingMode == heightSizingMode) {
2238
+ cachedResults = &layout->cachedLayout;
2239
+ }
2240
+ } else {
2241
+ for (uint32_t i = 0; i < layout->nextCachedMeasurementsIndex; i++) {
2242
+ if (yoga::inexactEquals(
2243
+ layout->cachedMeasurements[i].availableWidth, availableWidth) &&
2244
+ yoga::inexactEquals(
2245
+ layout->cachedMeasurements[i].availableHeight, availableHeight) &&
2246
+ layout->cachedMeasurements[i].widthSizingMode == widthSizingMode &&
2247
+ layout->cachedMeasurements[i].heightSizingMode == heightSizingMode) {
2248
+ cachedResults = &layout->cachedMeasurements[i];
2249
+ break;
2250
+ }
2251
+ }
2252
+ }
2253
+
2254
+ if (!needToVisitNode && cachedResults != nullptr) {
2255
+ layout->setMeasuredDimension(
2256
+ Dimension::Width, cachedResults->computedWidth);
2257
+ layout->setMeasuredDimension(
2258
+ Dimension::Height, cachedResults->computedHeight);
2259
+
2260
+ (performLayout ? layoutMarkerData.cachedLayouts
2261
+ : layoutMarkerData.cachedMeasures) += 1;
2262
+ } else {
2263
+ calculateLayoutImpl(
2264
+ node,
2265
+ availableWidth,
2266
+ availableHeight,
2267
+ ownerDirection,
2268
+ widthSizingMode,
2269
+ heightSizingMode,
2270
+ ownerWidth,
2271
+ ownerHeight,
2272
+ performLayout,
2273
+ reason,
2274
+ layoutMarkerData,
2275
+ depth,
2276
+ generationCount);
2277
+
2278
+ layout->lastOwnerDirection = ownerDirection;
2279
+ layout->configVersion = node->getConfig()->getVersion();
2280
+
2281
+ if (cachedResults == nullptr) {
2282
+ layoutMarkerData.maxMeasureCache = std::max(
2283
+ layoutMarkerData.maxMeasureCache,
2284
+ layout->nextCachedMeasurementsIndex + 1u);
2285
+
2286
+ if (layout->nextCachedMeasurementsIndex ==
2287
+ LayoutResults::MaxCachedMeasurements) {
2288
+ layout->nextCachedMeasurementsIndex = 0;
2289
+ }
2290
+
2291
+ CachedMeasurement* newCacheEntry = nullptr;
2292
+ if (performLayout) {
2293
+ // Use the single layout cache entry.
2294
+ newCacheEntry = &layout->cachedLayout;
2295
+ } else {
2296
+ // Allocate a new measurement cache entry.
2297
+ newCacheEntry =
2298
+ &layout->cachedMeasurements[layout->nextCachedMeasurementsIndex];
2299
+ layout->nextCachedMeasurementsIndex++;
2300
+ }
2301
+
2302
+ newCacheEntry->availableWidth = availableWidth;
2303
+ newCacheEntry->availableHeight = availableHeight;
2304
+ newCacheEntry->widthSizingMode = widthSizingMode;
2305
+ newCacheEntry->heightSizingMode = heightSizingMode;
2306
+ newCacheEntry->computedWidth =
2307
+ layout->measuredDimension(Dimension::Width);
2308
+ newCacheEntry->computedHeight =
2309
+ layout->measuredDimension(Dimension::Height);
2310
+ }
2311
+ }
2312
+
2313
+ if (performLayout) {
2314
+ node->setLayoutDimension(
2315
+ node->getLayout().measuredDimension(Dimension::Width),
2316
+ Dimension::Width);
2317
+ node->setLayoutDimension(
2318
+ node->getLayout().measuredDimension(Dimension::Height),
2319
+ Dimension::Height);
2320
+
2321
+ node->setHasNewLayout(true);
2322
+ node->setDirty(false);
2323
+ }
2324
+
2325
+ layout->generationCount = generationCount;
2326
+
2327
+ LayoutType layoutType;
2328
+ if (performLayout) {
2329
+ layoutType = !needToVisitNode && cachedResults == &layout->cachedLayout
2330
+ ? LayoutType::kCachedLayout
2331
+ : LayoutType::kLayout;
2332
+ } else {
2333
+ layoutType = cachedResults != nullptr ? LayoutType::kCachedMeasure
2334
+ : LayoutType::kMeasure;
2335
+ }
2336
+ Event::publish<Event::NodeLayout>(node, {layoutType});
2337
+
2338
+ return (needToVisitNode || cachedResults == nullptr);
2339
+ }
2340
+
2341
+ void calculateLayout(
2342
+ yoga::Node* const node,
2343
+ const float ownerWidth,
2344
+ const float ownerHeight,
2345
+ const Direction ownerDirection) {
2346
+ Event::publish<Event::LayoutPassStart>(node);
2347
+ LayoutData markerData = {};
2348
+
2349
+ // Increment the generation count. This will force the recursive routine to
2350
+ // visit all dirty nodes at least once. Subsequent visits will be skipped if
2351
+ // the input parameters don't change.
2352
+ gCurrentGenerationCount.fetch_add(1, std::memory_order_relaxed);
2353
+ node->processDimensions();
2354
+ const Direction direction = node->resolveDirection(ownerDirection);
2355
+ float width = YGUndefined;
2356
+ SizingMode widthSizingMode = SizingMode::MaxContent;
2357
+ const auto& style = node->style();
2358
+ if (node->hasDefiniteLength(Dimension::Width, ownerWidth)) {
2359
+ width =
2360
+ (node->getResolvedDimension(
2361
+ direction,
2362
+ dimension(FlexDirection::Row),
2363
+ ownerWidth,
2364
+ ownerWidth)
2365
+ .unwrap() +
2366
+ node->style().computeMarginForAxis(FlexDirection::Row, ownerWidth));
2367
+ widthSizingMode = SizingMode::StretchFit;
2368
+ } else if (style
2369
+ .resolvedMaxDimension(
2370
+ direction, Dimension::Width, ownerWidth, ownerWidth)
2371
+ .isDefined()) {
2372
+ width = style
2373
+ .resolvedMaxDimension(
2374
+ direction, Dimension::Width, ownerWidth, ownerWidth)
2375
+ .unwrap();
2376
+ widthSizingMode = SizingMode::FitContent;
2377
+ } else {
2378
+ width = ownerWidth;
2379
+ widthSizingMode = yoga::isUndefined(width) ? SizingMode::MaxContent
2380
+ : SizingMode::StretchFit;
2381
+ }
2382
+
2383
+ float height = YGUndefined;
2384
+ SizingMode heightSizingMode = SizingMode::MaxContent;
2385
+ if (node->hasDefiniteLength(Dimension::Height, ownerHeight)) {
2386
+ height =
2387
+ (node->getResolvedDimension(
2388
+ direction,
2389
+ dimension(FlexDirection::Column),
2390
+ ownerHeight,
2391
+ ownerWidth)
2392
+ .unwrap() +
2393
+ node->style().computeMarginForAxis(FlexDirection::Column, ownerWidth));
2394
+ heightSizingMode = SizingMode::StretchFit;
2395
+ } else if (style
2396
+ .resolvedMaxDimension(
2397
+ direction, Dimension::Height, ownerHeight, ownerWidth)
2398
+ .isDefined()) {
2399
+ height = style
2400
+ .resolvedMaxDimension(
2401
+ direction, Dimension::Height, ownerHeight, ownerWidth)
2402
+ .unwrap();
2403
+ heightSizingMode = SizingMode::FitContent;
2404
+ } else {
2405
+ height = ownerHeight;
2406
+ heightSizingMode = yoga::isUndefined(height) ? SizingMode::MaxContent
2407
+ : SizingMode::StretchFit;
2408
+ }
2409
+ if (calculateLayoutInternal(
2410
+ node,
2411
+ width,
2412
+ height,
2413
+ ownerDirection,
2414
+ widthSizingMode,
2415
+ heightSizingMode,
2416
+ ownerWidth,
2417
+ ownerHeight,
2418
+ true,
2419
+ LayoutPassReason::kInitial,
2420
+ markerData,
2421
+ 0, // tree root
2422
+ gCurrentGenerationCount.load(std::memory_order_relaxed))) {
2423
+ node->setPosition(node->getLayout().direction(), ownerWidth, ownerHeight);
2424
+ roundLayoutResultsToPixelGrid(node, 0.0f, 0.0f);
2425
+ }
2426
+
2427
+ Event::publish<Event::LayoutPassEnd>(node, {&markerData});
2428
+ }
2429
+
2430
+ } // namespace facebook::yoga