-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNeslib.Yoga.pas
4690 lines (4106 loc) · 168 KB
/
Neslib.Yoga.pas
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
unit Neslib.Yoga;
{ Delphi version of YogaLayout.
Has the following differences compared to the C version:
* There is no configuration object. It always uses this configuration:
- No experimental features
- Use Web Defaults (since non web defaults is considered legacy), unless
YOGA_NO_WEB_DEFAULTS is defined.
* The following node properties are not supported:
- Border (this is for informational purposes only anyway)
- Context (you should subclass TYogaNode instead)
* Use the name Parent instead of Owner (which is more accurate for Delphi)
* No DirtiedFunc callback that gets called when the Dirty property of a node
is set.
* No support for cloning. This adds complexity because you are supposed to
override TYogaNode for your own purposes.
Instead of a configurable PointScaleFactor you can set the define
YOGA_NO_PIXEL_ALIGN. When set, calculated positions and dimensions may have
fractions. Otherwise, positions and dimensions are aligned to integer
positions on the pixel grid.
Ideas for improvements:
----------------------
Reduce memory by fitting TYogaValue and TFloatOptional into a single 32-bit
value. For UI positions and dimensions, we don't need the precision of 23
mantissa bits. The Single format is as follows:
Sign bit - 8 exponent bits - 23 mantissa bits
Three possible approaches:
1. Use the lowest two mantissa bits to store TYogaUnit (Undefined, Point,
Percent, Auto). For values between -2000 and 2000 this means that the
value is precise up to at least 3-4 decimal digits after the point (the
smaller the value, the more precise after the point. For example, a value
of 1.0 is precise up to 6-7 digits).
2. Use the lowest mantissa bit to signal between Point and Percent values.
The values Undefined and Auto use Inf and NaN values. This increases
precision and may also be easier in some cases.
We could use -Inf ($FF800000) for Auto and any negative NaN ($FF800001-
$FFFFFFFF) for Undefined. It is easy then to check for Auto or Undefined
using a comparison of >= $FF800000.
3. As 2, but Undefined is value 10e20 like in the C++ version.
You could perform calculations with these values directly. They will introduce
a bit of error, but the error may grow propagate depending on the calculation.
For accurate calculations without error (other than the error inherent to the
floating-point format), you should clear the lowest 1 or 2 bits before using
the value in calculations.
You can test out the individual floating-point bits on this website:
https://www.h-schmidt.net/FloatConverter/IEEE754.html }
{$SCOPEDENUMS ON}
{ Enable this define to disable alignment of calculated positions and dimensions
to an integer pixel grid. }
{.$DEFINE YOGA_NO_PIXEL_ALIGN}
{ Enable this define to use the legacy non-web defaults for certain node
properties. }
{.DEFINE YOGA_NO_WEB_DEFAULTS}
interface
uses
System.Types;
const
YOGA_UNDEFINED = 10e20;
type
TYogaAlign = (Auto, FlexStart, Center, FlexEnd, Stretch, Baseline,
SpaceBetween, SpaceAround);
TYogaDirection = (Inherit, LTR, RTL);
TYogaDisplay = (Flex, None);
TYogaEdge = (Left, Top, Right, Bottom, Start, Stop, Horizontal, Vertical, All);
TYogaFlexDirection = (Column, ColumnReverse, Row, RowReverse);
TYogaJustify = (FlexStart, Center, FlexEnd, SpaceBetween, SpaceAround,
SpaceEvenly);
TYogaMeasureMode = (Undefined, Exactly, AtMost);
TYogaNodeType = (Default, Text);
TYogaOverflow = (Visible, Hidden, Scroll);
TYogaPositionType = (Relative, Absolute);
TYogaUnit = (Undefined, Point, Percent, Auto);
TYogaWrap = (No, Wrap, WrapReverse);
type
TYogaValue = record
{$REGION 'Internal Declarations'}
private
FValue: Single;
FUnits: TYogaUnit;
{$ENDREGION 'Internal Declarations'}
public
procedure SetPoint(const AValue: Single);
procedure SetPercent(const AValue: Single);
procedure SetUndefined;
procedure SetAuto;
class operator Implicit(const AValue: Single): TYogaValue; static;
class operator Equal(const ALeft, ARight: TYogaValue): Boolean; static;
class operator NotEqual(const ALeft, ARight: TYogaValue): Boolean; static;
class function Point(const AValue: Single): TYogaValue; static;
class function Percent(const AValue: Single): TYogaValue; static;
class function Undefined: TYogaValue; static;
class function Auto: TYogaValue; static;
property Value: Single read FValue;
property Units: TYogaUnit read FUnits;
end;
type
TYogaNode = class;
TYogaMeasureEvent = function(const ANode: TYogaNode; const AWidth: Single;
const AWidthMode: TYogaMeasureMode; const AHeight: Single;
const AHeightMode: TYogaMeasureMode): TSizeF of object;
TYogaBaselineEvent = function(const ANode: TYogaNode; const AWidth,
AHeight: Single): Single of object;
{ NOTE: Nodes own their children }
TYogaNode = class
{$REGION 'Internal Declarations'}
private const
DEFAULT_FLEX_GROW = 0.0;
{$IFDEF YOGA_NO_WEB_DEFAULTS}
DEFAULT_FLEX_SHRINK = 0.0;
{$ELSE}
DEFAULT_FLEX_SHRINK = 1.0;
{$ENDIF}
MAX_CACHED_RESULT_COUNT = 16;
private class var
GCurrentGenerationCount: Cardinal;
GDepth: Cardinal;
private type
TEnumerator = record
private
[unsafe] FNode: TYogaNode;
FIndex: Integer;
function GetCurrent: TYogaNode;
public
class function Create(const ANode: TYogaNode): TEnumerator; static;
function MoveNext: Boolean;
property Current: TYogaNode read GetCurrent;
end;
private type
TFloatOptional = record
private
FValue: Single;
FIsUndefined: Boolean;
function GetValue: Single;
procedure SetValue(const AValue: Single);
public
constructor Create(const AValue: Single);
procedure Init; overload; inline;
procedure Init(const AValue: Single); overload;
function Unwrap: Single;
class function Max(const AA, AB: TFloatOptional): TFloatOptional; static;
class operator Implicit(const AValue: Single): TFloatOptional; inline; static;
class operator Add(const ALeft, ARight: TFloatOptional): TFloatOptional; static;
class operator GreaterThan(const ALeft, ARight: TFloatOptional): Boolean; static;
class operator LessThan(const ALeft, ARight: TFloatOptional): Boolean; static;
class operator GreaterThanOrEqual(const ALeft, ARight: TFloatOptional): Boolean; inline; static;
class operator LessThanOrEqual(const ALeft, ARight: TFloatOptional): Boolean; inline; static;
class operator Equal(const ALeft, ARight: TFloatOptional): Boolean; static;
class operator Equal(const ALeft: TFloatOptional; const ARight: Single): Boolean; static;
class operator NotEqual(const ALeft, ARight: TFloatOptional): Boolean; inline; static;
class operator NotEqual(const ALeft: TFloatOptional; const ARight: Single): Boolean; inline; static;
property Value: Single read GetValue write SetValue;
property IsUndefined: Boolean read FIsUndefined;
end;
private type
TEdgeValues = array [TYogaEdge] of TYogaValue;
TDimension = (Width, Height);
TDimensionValues = array [TDimension] of TYogaValue;
private const
DEFAULT_EDGE_VALUES: TEdgeValues = (
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined));
DEFAULT_DIMENSIONS_AUTO: TDimensionValues = (
(FValue: 0; FUnits: TYogaUnit.Auto),
(FValue: 0; FUnits: TYogaUnit.Auto));
DEFAULT_DIMENSIONS_UNDEFINED: TDimensionValues = (
(FValue: 0; FUnits: TYogaUnit.Undefined),
(FValue: 0; FUnits: TYogaUnit.Undefined));
DIM: array [TYogaFlexDirection] of TDimension = (
TDimension.Height, TDimension.Height, TDimension.Width, TDimension.Width);
POS: array [TYogaFlexDirection] of TYogaEdge = (
TYogaEdge.Top, TYogaEdge.Bottom, TYogaEdge.Left, TYogaEdge.Right);
LEADING: array [TYogaFlexDirection] of TYogaEdge = (
TYogaEdge.Top, TYogaEdge.Bottom, TYogaEdge.Left, TYogaEdge.Right);
TRAILING: array [TYogaFlexDirection] of TYogaEdge = (
TYogaEdge.Bottom, TYogaEdge.Top, TYogaEdge.Right, TYogaEdge.Left);
FLOAT_OPT_ZERO: TFloatOptional = (FValue: 0; FIsUndefined: False);
private type
TStyle = record
public
Direction: TYogaDirection;
FlexDirection: TYogaFlexDirection;
JustifyContent: TYogaJustify;
AlignContent: TYogaAlign;
AlignItems: TYogaAlign;
AlignSelf: TYogaAlign;
PositionType: TYogaPositionType;
FlexWrap: TYogaWrap;
Overflow: TYogaOverflow;
Display: TYogaDisplay;
Flex: TFloatOptional;
FlexGrow: TFloatOptional;
FlexShrink: TFloatOptional;
FlexBasis: TYogaValue;
Margin: TEdgeValues;
Position: TEdgeValues;
Padding: TEdgeValues;
Dimensions: TDimensionValues;
MinDimensions: TDimensionValues;
MaxDimensions: TDimensionValues;
AspectRatio: TFloatOptional;
public
procedure Init; inline;
class operator Equal(const ALeft, ARight: TStyle): Boolean; static;
class operator NotEqual(const ALeft, ARight: TStyle): Boolean; inline; static;
end;
private type
TPositionFloats = array [TYogaEdge.Left..TYogaEdge.Bottom] of Single;
TDimensionFloats = array [TDimension] of Single;
TMarginFloats = array [TYogaEdge.Left..TYogaEdge.Stop] of Single;
private type
TCachedMeasurement = record
public
AvailableWidth: Single;
AvailableHeight: Single;
ComputedWidth: Single;
ComputedHeight: Single;
WidthMeasureMode: TYogaMeasureMode;
HeightMeasureMode: TYogaMeasureMode;
public
procedure Init; inline;
end;
PCachedMeasurement = ^TCachedMeasurement;
private type
TLayout = record
public
Position: TPositionFloats;
Dimensions: TDimensionFloats;
Margin: TMarginFloats;
Padding: TMarginFloats;
ComputedFlexBasisGeneration: Cardinal;
ComputedFlexBasis: TFloatOptional;
GenerationCount: Cardinal;
NextCachedMeasurementsIndex: Integer;
CachedMeasurements: array [0..MAX_CACHED_RESULT_COUNT - 1] of TCachedMeasurement;
MeasuredDimensions: TDimensionFloats;
CachedLayout: TCachedMeasurement;
Direction: TYogaDirection;
LastParentDirection: TYogaDirection;
HadOverflow: Boolean;
public
procedure Init;
end;
private type
{ This struct is an helper model to hold the data for step 4 of flexbox
algo, which is collecting the flex items in a line.
- ItemsOnLine: Number of items which can fit in a line considering the
available Inner dimension, the flex items computed flexbasis and their
margin. It may be different than the difference between start and end
indicates because we skip over absolute-positioned items.
- SizeConsumedOnCurrentLine: It is accumulation of the dimensions and
margin of all the children on the current line. This will be used in
order to either set the dimensions of the node if none already exist or
to compute the remaining space left for the flexible children.
- TotalFlexGrowFactors: total flex grow factors of flex items which are to
be layed in the current line
- TotalFlexShrinkFactors: total flex shrink factors of flex items which
are to be layed in the current line
- EndOfLineIndex: Its the end index of the last flex item which was
examined and it may or may not be part of the current line(as it may be
absolutely positioned or inculding it may have caused to overshoot
AvailableInnerDim)
- RelativeChildren: Maintain a vector of the child nodes that can shrink
and/or grow. }
TCollectFlexItemsRowValues = record
public
ItemsOnLine: Integer;
SizeConsumedOnCurrentLine: Single;
TotalFlexGrowFactors: Single;
TotalFlexShrinkScaledFactors: Single;
EndOfLineIndex: Integer;
RelativeChildren: TArray<TYogaNode>;
RelativeChildCount: Integer;
RemainingFreeSpace: Single;
{ The size of the MainDim for the row after considering size, padding,
margin and border of flex items. This is used to calculate MaxLineDim
after going through all the rows to decide on the main axis size of
owner. }
MainDim: Single;
{ The size of the CrossDim for the row after considering size, padding,
margin and border of flex items. Used for calculating containers
CrossSize. }
CrossDim: Single;
public
procedure AddRelativeChild(const AChild: TYogaNode);
end;
private
[unsafe] FParent: TYogaNode; // Reference
FChildren: TArray<TYogaNode>; // References
FChildCount: Integer;
FStyle: TStyle;
FLayout: TLayout;
FLineIndex: Integer;
FResolvedDimensions: TDimensionValues;
FNodeType: TYogaNodeType;
FHasNewLayout: Boolean;
FIsDirty: Boolean;
FOnMeasure: TYogaMeasureEvent;
FOnBaseline: TYogaBaselineEvent;
procedure SetAlignContent(const AValue: TYogaAlign);
procedure SetAlignItems(const AValue: TYogaAlign);
procedure SetAlignSelf(const AValue: TYogaAlign);
function GetAspectRatio: Single;
procedure SetAspectRatio(const AValue: Single);
function GetChild(const AIndex: Integer): TYogaNode;
procedure SetDirection(const AValue: TYogaDirection);
procedure SetDisplay(const AValue: TYogaDisplay);
function GetFlex: Single;
procedure SetFlex(const AValue: Single);
function GetFlexBasis: TYogaValue;
procedure SetFlexBasis(const AValue: TYogaValue);
procedure SetFlexDirection(const AValue: TYogaFlexDirection);
function GetFlexGrow: Single;
procedure SetFlexGrow(const AValue: Single);
function GetFlexShrink: Single;
procedure SetFlexShrink(const AValue: Single);
procedure SetFlexWrap(const AValue: TYogaWrap);
function GetHeight: TYogaValue;
procedure SetHeight(const AValue: TYogaValue);
function GetIsMeasureDefined: Boolean;
function GetIsBaselineDefined: Boolean;
procedure SetJustifyContent(const AValue: TYogaJustify);
function GetLayoutMargin(const AIndex: TYogaEdge): Single;
function GetLayoutPadding(const AIndex: TYogaEdge): Single;
function GetMargin(const AIndex: TYogaEdge): TYogaValue;
procedure SetMargin(const AIndex: TYogaEdge; const AValue: TYogaValue);
function GetMaxWidth: TYogaValue;
procedure SetMaxWidth(const AValue: TYogaValue);
function GetMaxHeight: TYogaValue;
procedure SetMaxHeight(const AValue: TYogaValue);
function GetMinWidth: TYogaValue;
procedure SetMinWidth(const AValue: TYogaValue);
function GetMinHeight: TYogaValue;
procedure SetMinHeight(const AValue: TYogaValue);
procedure SetOnMeasure(const AValue: TYogaMeasureEvent);
procedure SetOverflow(const AValue: TYogaOverflow);
function GetPadding(const AIndex: TYogaEdge): TYogaValue;
procedure SetPadding(const AIndex: TYogaEdge; const AValue: TYogaValue);
function GetPos(const AIndex: TYogaEdge): TYogaValue;
procedure SetPos(const AIndex: TYogaEdge; const AValue: TYogaValue);
procedure SetPositionType(const AValue: TYogaPositionType);
function GetWidth: TYogaValue;
procedure SetWidth(const AValue: TYogaValue);
private
procedure SetDirty(const AIsDirty: Boolean);
procedure MarkDirtyAndPropogate;
procedure ResolveDimension;
function ResolveDirection(const AParentDirection: TYogaDirection): TYogaDirection;
function ResolveFlexGrow: Single;
function ResolveFlexShrink: Single;
procedure ResolveFlexibleLength(
var ACollectedFlexItemsValues: TCollectFlexItemsRowValues; const AMainAxis,
ACrossAxis: TYogaFlexDirection; const AMainAxisParentSize,
AAvailableInnerMainDim, AAvailableInnerCrossDim, AAvailableInnerWidth,
AAvailableInnerHeight: Single; const AFlexBasisOverflows: Boolean;
const AMeasureModeCrossDim: TYogaMeasureMode;
const APerformLayout: Boolean);
class procedure DistributeFreeSpaceFirstPass(
var ACollectedFlexItemsValues: TCollectFlexItemsRowValues;
const AMainAxis: TYogaFlexDirection; const AMainAxisParentSize,
AAvailableInnerMainDim, AAvailableInnerWidth: Single); static;
function DistributeFreeSpaceSecondPass(
var ACollectedFlexItemsValues: TCollectFlexItemsRowValues;
const AMainAxis, ACrossAxis: TYogaFlexDirection;
const AMainAxisParentSize, AAvailableInnerMainDim, AAvailableInnerCrossDim,
AAvailableInnerWidth, AAvailableInnerHeight: Single;
const AFlexBasisOverflows: Boolean;
const AMeasureModeCrossDim: TYogaMeasureMode;
const APerformLayout: Boolean): Single;
function ResolveFlexBasisPtr: TYogaValue;
function IsFlexible: Boolean;
function IsBaselineLayout: Boolean;
function Baseline: Single;
function IsStyleDimDefined(const AAxis: TYogaFlexDirection;
const AParentSize: Single): Boolean; inline;
function IsLayoutDimDefined(const AAxis: TYogaFlexDirection): Boolean; inline;
function IsLeadingPositionDefined(const AAxis: TYogaFlexDirection): Boolean;
function GetLeadingPosition(const AAxis: TYogaFlexDirection;
const AAxisSize: Single): TFloatOptional;
function IsTrailingPositionDefined(const AAxis: TYogaFlexDirection): Boolean;
function GetTrailingPosition(const AAxis: TYogaFlexDirection;
const AAxisSize: Single): TFloatOptional;
function MarginLeadingValue(const AAxis: TYogaFlexDirection): TYogaValue;
function MarginTrailingValue(const AAxis: TYogaFlexDirection): TYogaValue;
function GetMarginForAxis(const AAxis: TYogaFlexDirection;
const AWidthSize: Single): TFloatOptional;
function PaddingForAxis(const AAxis: TYogaFlexDirection;
const AWidthSize: Single): Single; inline;
function GetLeadingMargin(const AAxis: TYogaFlexDirection;
const AWidthSize: Single): TFloatOptional;
function GetTrailingMargin(const AAxis: TYogaFlexDirection;
const AWidthSize: Single): TFloatOptional;
function GetLeadingPadding(const AAxis: TYogaFlexDirection;
const AWidthSize: Single): TFloatOptional;
function GetTrailingPadding(const AAxis: TYogaFlexDirection;
const AWidthSize: Single): TFloatOptional;
function BoundAxis(const AAxis: TYogaFlexDirection; const AValue, AAxisSize,
AWidthSize: Single): Single; inline;
function BoundAxisWithinMinAndMax(const AAxis: TYogaFlexDirection;
const AValue, AAxisSize: Single): TFloatOptional; inline;
function LayoutInternal(const AAvailableWidth, AAvailableHeight: Single;
const AParentDirection: TYogaDirection; const AWidthMeasureMode,
AHeightMeasureMode: TYogaMeasureMode; const AParentWidth,
AParentHeight: Single; const APerformLayout: Boolean): Boolean;
procedure LayoutImpl(const AAvailableWidth, AAvailableHeight: Single;
const AParentDirection: TYogaDirection; const AWidthMeasureMode,
AHeightMeasureMode: TYogaMeasureMode; const AParentWidth,
AParentHeight: Single; const APerformLayout: Boolean);
procedure AbsoluteLayoutChild(const AChild: TYogaNode; const AWidth: Single;
const AWidthMode: TYogaMeasureMode; const AHeight: Single;
const ADirection: TYogaDirection);
procedure DoMeasureSetMeasuredDimensions(const AAvailableWidth,
AAvailableHeight: Single; const AWidthMeasureMode,
AHeightMeasureMode: TYogaMeasureMode; const AParentWidth,
AParentHeight: Single);
procedure EmptyContainerSetMeasuredDimensions(const AAvailableWidth,
AAvailableHeight: Single; const AWidthMeasureMode,
AHeightMeasureMode: TYogaMeasureMode; const AParentWidth,
AParentHeight: Single);
function FixedSizeSetMeasuredDimensions(const AAvailableWidth,
AAvailableHeight: Single; const AWidthMeasureMode,
AHeightMeasureMode: TYogaMeasureMode; const AParentWidth,
AParentHeight: Single): Boolean;
function CalculateAvailableInnerDim(const AAxis: TYogaFlexDirection;
const AAvailableDim, AParentDim: Single): Single;
procedure ComputeFlexBasisForChildren(const AAvailableInnerWidth,
AAvailableInnerHeight: Single; const AWidthMeasureMode,
AHeightMeasureMode: TYogaMeasureMode; const ADirection: TYogaDirection;
const AMainAxis: TYogaFlexDirection; const APerformLayout: Boolean;
var ATotalOuterFlexBasis: Single);
procedure ComputeFlexBasisForChild(const AChild: TYogaNode;
const AWidth: Single; const AWidthMode: TYogaMeasureMode; const AHeight,
AParentWidth, AParentHeight: Single; const AHeightMode: TYogaMeasureMode;
const ADirection: TYogaDirection);
procedure CalculateCollectFlexItemsRowValues(
const AParentDirection: TYogaDirection; const AMainAxisParentSize,
AAvailableInnerWidth, AAvailableInnerMainDim: Single;
const AStartOfLineIndex, ALineCount: Integer;
out AValues: TCollectFlexItemsRowValues);
procedure JustifyMainAxis(var ACollectedFlexItemValues: TCollectFlexItemsRowValues;
const AStartOfLineIndex: Integer; const AMainAxis, ACrossAxis: TYogaFlexDirection;
const AMeasureModeMainDim, AMeasureModeCrossDim: TYogaMeasureMode;
const AMainAxisParentSize, AParentWidth, AAvailableInnerMainDim,
AAvailableInnerCrossDim, AAvailableInnerWidth: Single;
const APerformLayout: Boolean);
function AlignItem(const AChild: TYogaNode): TYogaAlign; inline;
function DimWithMargin(const AAxis: TYogaFlexDirection;
const AWidthSize: Single): Single; inline;
procedure ConstrainMaxSizeForMode(const AAxis: TYogaFlexDirection;
const AParentAxisSize, AParentWidth: Single; var AMode: TYogaMeasureMode;
var ASize: Single);
procedure SetPosition(const ADirection: TYogaDirection; const AMainSize,
ACrossSize, AParentWidth: Single);
procedure SetChildTrailingPosition(const AChild: TYogaNode;
const AAxis: TYogaFlexDirection);
procedure ZeroOutLayoutRecursively;
function RelativePosition(const AAxis: TYogaFlexDirection;
const AAxisSize: Single): TFloatOptional;
{$IFNDEF YOGA_NO_PIXEL_ALIGN}
procedure RoundToPixelGrid(const AAbsoluteLeft, AAbsoluteTop: Single);
{$ENDIF}
private
class function ComputedEdgeValue(const AEdges: TEdgeValues;
const AEdge: TYogaEdge; const ADefault: TYogaValue): TYogaValue; static;
class function CanUseCachedMeasurement(const AWidthMode: TYogaMeasureMode;
const AWidth: Single; const AHeightMode: TYogaMeasureMode;
const AHeight: Single; const ALastWidthMode: TYogaMeasureMode;
const ALastWidth: Single; const ALastHeightMode: TYogaMeasureMode;
const ALastHeight, ALastComputedWidth, ALastComputedHeight, AMarginRow,
AMarginColumn: Single): Boolean; static;
class function EdgeValuesEqual(const ALeft, ARight: TEdgeValues): Boolean; static;
class function DimensionValuesEqual(const ALeft, ARight: TDimensionValues): Boolean; static;
private
class function GetStyleValue(const AValue: TYogaValue): TYogaValue; inline; static;
procedure SetStyleValue(var AValue: TYogaValue; const ANewValue: Single);
procedure SetStyleValuePercent(var AValue: TYogaValue; const ANewValue: Single);
procedure SetStyleValueAuto(var AValue: TYogaValue);
class function GetStyleEdgeValue(const AValues: TEdgeValues;
const AEdge: TYogaEdge): TYogaValue; static;
procedure SetStyleEdgeValue(var AValues: TEdgeValues;
const AEdge: TYogaEdge; const AValue: Single);
procedure SetStyleEdgeValuePercent(var AValues: TEdgeValues;
const AEdge: TYogaEdge; const AValue: Single);
procedure SetStyleEdgeValueAuto(var AValues: TEdgeValues;
const AEdge: TYogaEdge);
function GetLayoutValue(const AValues: TMarginFloats;
const AEdge: TYogaEdge): Single; inline;
{$ENDREGION 'Internal Declarations'}
public
constructor Create; overload;
{ This frees the children }
destructor Destroy; override;
procedure MarkDirty;
procedure CopyStyle(const ASourceNode: TYogaNode);
procedure Add(const AChild: TYogaNode);
procedure Insert(const AIndex: Integer; const AChild: TYogaNode);
{ This frees the child }
procedure Delete(const AIndex: Integer);
procedure Remove(const AChild: TYogaNode);
{ This frees the children }
procedure Clear;
function IndexOf(const AChild: TYogaNode): Integer;
{ Does a recursive calculation }
procedure CalculateLayout; overload;
procedure CalculateLayout(const AAvailableWidth, AAvailableHeight: Single); overload;
procedure CalculateLayout(const ADirection: TYogaDirection); overload;
procedure CalculateLayout(const AAvailableWidth, AAvailableHeight: Single;
const ADirection: TYogaDirection); overload;
function GetEnumerator: TEnumerator;
property NodeType: TYogaNodeType read FNodeType write FNodeType;
property IsDirty: Boolean read FIsDirty;
property HasNewLayout: Boolean read FHasNewLayout write FHasNewLayout;
property Parent: TYogaNode read FParent;
property IsMeasureDefined: Boolean read GetIsMeasureDefined;
property IsBaselineDefined: Boolean read GetIsBaselineDefined;
{ Style related (input) }
{ @Container }
property Direction: TYogaDirection read FStyle.Direction write SetDirection;
{ @Container }
property FlexDirection: TYogaFlexDirection read FStyle.FlexDirection write SetFlexDirection;
{ @Container }
property JustifyContent: TYogaJustify read FStyle.JustifyContent write SetJustifyContent;
{ @Container }
property Display: TYogaDisplay read FStyle.Display write SetDisplay;
{ @Container }
property AlignItems: TYogaAlign read FStyle.AlignItems write SetAlignItems;
{ @Item }
property AlignSelf: TYogaAlign read FStyle.AlignSelf write SetAlignSelf;
{ @Container }
property AlignContent: TYogaAlign read FStyle.AlignContent write SetAlignContent;
{ @Item (non-root) }
property PositionType: TYogaPositionType read FStyle.PositionType write SetPositionType;
{ @Container }
property FlexWrap: TYogaWrap read FStyle.FlexWrap write SetFlexWrap;
{ @Item (non-root) }
property Flex: Single read GetFlex write SetFlex;
property FlexGrow: Single read GetFlexGrow write SetFlexGrow;
property FlexShrink: Single read GetFlexShrink write SetFlexShrink;
property FlexBasis: TYogaValue read GetFlexBasis write SetFlexBasis;
{ @Item }
property Width: TYogaValue read GetWidth write SetWidth;
property Height: TYogaValue read GetHeight write SetHeight;
property MaxWidth: TYogaValue read GetMaxWidth write SetMaxWidth;
property MaxHeight: TYogaValue read GetMaxHeight write SetMaxHeight;
property MinWidth: TYogaValue read GetMinWidth write SetMinWidth;
property MinHeight: TYogaValue read GetMinHeight write SetMinHeight;
{ @Item }
property AspectRatio: Single read GetAspectRatio write SetAspectRatio;
{ @Container? }
property Overflow: TYogaOverflow read FStyle.Overflow write SetOverflow;
{ @Item (non-root) }
property Left: TYogaValue index TYogaEdge.Left read GetPos write SetPos;
property Top: TYogaValue index TYogaEdge.Top read GetPos write SetPos;
property Right: TYogaValue index TYogaEdge.Right read GetPos write SetPos;
property Bottom: TYogaValue index TYogaEdge.Bottom read GetPos write SetPos;
property Start: TYogaValue index TYogaEdge.Start read GetPos write SetPos;
property Stop: TYogaValue index TYogaEdge.Stop read GetPos write SetPos;
property Positions[const AIndex: TYogaEdge]: TYogaValue read GetPos write SetPos;
{ @Item (non-root) }
property Margin: TYogaValue index TYogaEdge.All read GetMargin write SetMargin;
property MarginLeft: TYogaValue index TYogaEdge.Left read GetMargin write SetMargin;
property MarginTop: TYogaValue index TYogaEdge.Top read GetMargin write SetMargin;
property MarginRight: TYogaValue index TYogaEdge.Right read GetMargin write SetMargin;
property MarginBottom: TYogaValue index TYogaEdge.Bottom read GetMargin write SetMargin;
property MarginStart: TYogaValue index TYogaEdge.Start read GetMargin write SetMargin;
property MarginStop: TYogaValue index TYogaEdge.Stop read GetMargin write SetMargin;
property MarginHorizontal: TYogaValue index TYogaEdge.Horizontal read GetMargin write SetMargin;
property MarginVertical: TYogaValue index TYogaEdge.Vertical read GetMargin write SetMargin;
property Margins[const AIndex: TYogaEdge]: TYogaValue read GetMargin write SetMargin;
{ @Item }
property Padding: TYogaValue index TYogaEdge.All read GetPadding write SetPadding;
property PaddingLeft: TYogaValue index TYogaEdge.Left read GetPadding write SetPadding;
property PaddingTop: TYogaValue index TYogaEdge.Top read GetPadding write SetPadding;
property PaddingRight: TYogaValue index TYogaEdge.Right read GetPadding write SetPadding;
property PaddingBottom: TYogaValue index TYogaEdge.Bottom read GetPadding write SetPadding;
property PaddingStart: TYogaValue index TYogaEdge.Start read GetPadding write SetPadding;
property PaddingStop: TYogaValue index TYogaEdge.Stop read GetPadding write SetPadding;
property PaddingHorizontal: TYogaValue index TYogaEdge.Horizontal read GetPadding write SetPadding;
property PaddingVertical: TYogaValue index TYogaEdge.Vertical read GetPadding write SetPadding;
property Paddings[const AIndex: TYogaEdge]: TYogaValue read GetPadding write SetPadding;
{ Layout related (output) }
{ These are relative to the parent }
property LayoutLeft: Single read FLayout.Position[TYogaEdge.Left];
property LayoutTop: Single read FLayout.Position[TYogaEdge.Top];
property LayoutRight: Single read FLayout.Position[TYogaEdge.Right];
property LayoutBottom: Single read FLayout.Position[TYogaEdge.Bottom];
property LayoutWidth: Single read FLayout.Dimensions[TDimension.Width];
property LayoutHeight: Single read FLayout.Dimensions[TDimension.Height];
property LayoutMarginLeft: Single index TYogaEdge.Left read GetLayoutMargin;
property LayoutMarginTop: Single index TYogaEdge.Top read GetLayoutMargin;
property LayoutMarginRight: Single index TYogaEdge.Right read GetLayoutMargin;
property LayoutMarginBottom: Single index TYogaEdge.Bottom read GetLayoutMargin;
property LayoutMarginStart: Single index TYogaEdge.Start read GetLayoutMargin;
property LayoutMarginStop: Single index TYogaEdge.Stop read GetLayoutMargin;
property LayoutMargins[const AIndex: TYogaEdge]: Single read GetLayoutMargin;
property LayoutPaddingLeft: Single index TYogaEdge.Left read GetLayoutPadding;
property LayoutPaddingTop: Single index TYogaEdge.Top read GetLayoutPadding;
property LayoutPaddingRight: Single index TYogaEdge.Right read GetLayoutPadding;
property LayoutPaddingBottom: Single index TYogaEdge.Bottom read GetLayoutPadding;
property LayoutPaddingStart: Single index TYogaEdge.Start read GetLayoutPadding;
property LayoutPaddingStop: Single index TYogaEdge.Stop read GetLayoutPadding;
property LayoutPaddings[const AIndex: TYogaEdge]: Single read GetLayoutPadding;
property LayoutDirection: TYogaDirection read FLayout.Direction;
property LayoutHadOverflow: Boolean read FLayout.HadOverflow;
property ChildCount: Integer read FChildCount;
property Children[const AIndex: Integer]: TYogaNode read GetChild; default;
property OnMeasure: TYogaMeasureEvent read FOnMeasure write SetOnMeasure;
property OnBaseline: TYogaBaselineEvent read FOnBaseline write FOnBaseline;
end;
function YogaIsUndefined(const AValue: Single): Boolean; inline;
function YogaMax(const AA, AB: Single): Single;
implementation
uses
System.Math,
System.SysUtils;
const
YOGA_VALUE_ZERO: TYogaValue = (FValue: 0; FUnits: TYogaUnit.Point);
YOGA_VALUE_UNDEFINED: TYogaValue = (FValue: YOGA_UNDEFINED; FUnits: TYogaUnit.Undefined);
YOGA_VALUE_AUTO: TYogaValue = (FValue: YOGA_UNDEFINED; FUnits: TYogaUnit.Auto);
function YogaIsUndefined(const AValue: Single): Boolean; inline;
begin
Result := (AValue >= 10e8) or (AValue <= -10e8);
end;
function YogaEqual(const ALeft, ARight: Single): Boolean;
var
UL, UR: Boolean;
begin
UL := YogaIsUndefined(ALeft);
UR := YogaIsUndefined(ARight);
if (not UL) and (not UL) then
Result := (Abs(ALeft - ARight) < 0.0001)
else
Result := (UL = UR);
end;
function YogaSanitize(const AValue: Single): Single;
begin
if (YogaIsUndefined(AValue)) then
Result := 0.0
else
Result := AValue;
end;
function YogaMax(const AA, AB: Single): Single;
var
UA: Boolean;
begin
UA := YogaIsUndefined(AA);
if (not UA) and (not YogaIsUndefined(AB)) then
Result := Max(AA, AB)
else if (UA) then
Result := AB
else
Result := AA;
end;
function YogaMin(const AA, AB: Single): Single;
var
UA: Boolean;
begin
UA := YogaIsUndefined(AA);
if (not UA) and (not YogaIsUndefined(AB)) then
Result := Min(AA, AB)
else if (UA) then
Result := AB
else
Result := AA;
end;
{$IFNDEF YOGA_NO_PIXEL_ALIGN}
function YogaRoundValueToPixelGrid(const AValue: Single; const AForceCeil,
AForceFloor: Boolean): Single;
var
Fractial: Single;
begin
Result := AValue;
Fractial := Frac(Result);
if (YogaEqual(Fractial, 0.0)) then
{ First we check if the value is already rounded }
Result := Result - Fractial
else if (YogaEqual(Fractial, 1.0)) then
Result := Result - Fractial + 1.0
else if (AForceCeil) then
{ Next we check if we need to use forced rounding }
Result := Result - Fractial + 1.0
else if (AForceFloor) then
Result := Result - Fractial
else
begin
{ Finally we just round the value }
Result := Result - Fractial;
if (not YogaIsUndefined(Fractial)) and ((Fractial > 0.5) or YogaEqual(Fractial, 0.5)) then
Result := Result + 1.0;
end;
if (YogaIsUndefined(Result)) then
Result := YOGA_UNDEFINED;
end;
{$ENDIF}
type
TYogaValueHelper = record helper for TYogaValue
public
function Resolve(const AParentSize: Single): TYogaNode.TFloatOptional; inline;
function ResolveMargin(const AParentSize: Single): TYogaNode.TFloatOptional; inline;
end;
type
TYogaFlexDirectionHelper = record helper for TYogaFlexDirection
public
function IsRow: Boolean; inline;
function IsColumn: Boolean; inline;
function Resolve(const ADirection: TYogaDirection): TYogaFlexDirection; inline;
function Cross(const ADirection: TYogaDirection): TYogaFlexDirection;
end;
type
TYogaMeasureModeHelper = record helper for TYogaMeasureMode
public
function SizeIsExactAndMatchesOldMeasuresSize(const ASize,
ALastComputedSize: Single): Boolean; inline;
function OldSizeIsUnspecifiedAndStillFits(const ASize: Single;
const ALastSizeMode: TYogaMeasureMode;
const ALastComputedSize: Single): Boolean; inline;
function NewMeasureSizeIsStricterAndStillValid(const ASize: Single;
const ALastSizeMode: TYogaMeasureMode;
const ALastSize, ALastComputedSize: Single): Boolean; inline;
end;
{ TYogaValue }
class function TYogaValue.Auto: TYogaValue;
begin
Result.FValue := 0;
Result.FUnits := TYogaUnit.Auto;
end;
class operator TYogaValue.Equal(const ALeft, ARight: TYogaValue): Boolean;
begin
if (ALeft.FUnits <> ARight.FUnits) then
Exit(False);
if (ALeft.FUnits = TYogaUnit.Undefined)
or ((YogaIsUndefined(ALeft.FValue)) and (YogaIsUndefined(ARight.FValue)))
then
Exit(True);
Result := (Abs(ALeft.FValue - ARight.FValue) < 0.0001);
end;
class operator TYogaValue.Implicit(const AValue: Single): TYogaValue;
begin
Result.FValue := AValue;
if YogaIsUndefined(AValue) then
Result.FUnits := TYogaUnit.Undefined
else
Result.FUnits := TYogaUnit.Point;
end;
class operator TYogaValue.NotEqual(const ALeft, ARight: TYogaValue): Boolean;
begin
Result := not (ALeft = ARight);
end;
class function TYogaValue.Percent(const AValue: Single): TYogaValue;
begin
Result.FValue := AValue;
if YogaIsUndefined(AValue) then
Result.FUnits := TYogaUnit.Undefined
else
Result.FUnits := TYogaUnit.Percent;
end;
class function TYogaValue.Point(const AValue: Single): TYogaValue;
begin
Result.FValue := AValue;
if YogaIsUndefined(AValue) then
Result.FUnits := TYogaUnit.Undefined
else
Result.FUnits := TYogaUnit.Point;
end;
procedure TYogaValue.SetAuto;
begin
FValue := 0;
FUnits := TYogaUnit.Auto;
end;
procedure TYogaValue.SetPercent(const AValue: Single);
begin
FValue := AValue;
if YogaIsUndefined(AValue) then
FUnits := TYogaUnit.Undefined
else
FUnits := TYogaUnit.Percent;
end;
procedure TYogaValue.SetPoint(const AValue: Single);
begin
FValue := AValue;
if YogaIsUndefined(AValue) then
FUnits := TYogaUnit.Undefined
else
FUnits := TYogaUnit.Point;
end;
procedure TYogaValue.SetUndefined;
begin
FValue := YOGA_UNDEFINED;
FUnits := TYogaUnit.Undefined;
end;
class function TYogaValue.Undefined: TYogaValue;
begin
Result.FValue := YOGA_UNDEFINED;
Result.FUnits := TYogaUnit.Undefined;
end;
{ TYogaValueHelper }
function TYogaValueHelper.Resolve(
const AParentSize: Single): TYogaNode.TFloatOptional;
begin
case FUnits of
TYogaUnit.Point:
Result.Init(FValue);
TYogaUnit.Percent:
Result.Init(FValue * AParentSize * 0.01);
else
Result.Init;
end;
end;
function TYogaValueHelper.ResolveMargin(
const AParentSize: Single): TYogaNode.TFloatOptional;
begin
if (FUnits = TYogaUnit.Auto) then
Result.Init(0.0)
else
Result := Resolve(AParentSize);
end;
{ TYogaFlexDirectionHelper }
function TYogaFlexDirectionHelper.Cross(
const ADirection: TYogaDirection): TYogaFlexDirection;
begin
if (IsColumn) then
Result := TYogaFlexDirection.Row.Resolve(ADirection)
else
Result := TYogaFlexDirection.Column.Resolve(ADirection);
end;
function TYogaFlexDirectionHelper.IsColumn: Boolean;
begin
Result := (Self in [TYogaFlexDirection.Column, TYogaFlexDirection.ColumnReverse]);
end;
function TYogaFlexDirectionHelper.IsRow: Boolean;
begin
Result := (Self in [TYogaFlexDirection.Row, TYogaFlexDirection.RowReverse]);
end;
function TYogaFlexDirectionHelper.Resolve(
const ADirection: TYogaDirection): TYogaFlexDirection;
begin
if (ADirection = TYogaDirection.RTL) then
begin
if (Self = TYogaFlexDirection.Row) then
Exit(TYogaFlexDirection.RowReverse)
else if (Self = TYogaFlexDirection.RowReverse) then
Exit(TYogaFlexDirection.Row);
end;
Result := Self;
end;
{ TYogaMeasureModeHelper }
function TYogaMeasureModeHelper.NewMeasureSizeIsStricterAndStillValid(
const ASize: Single; const ALastSizeMode: TYogaMeasureMode; const ALastSize,
ALastComputedSize: Single): Boolean;
begin
Result := (ALastSizeMode = TYogaMeasureMode.AtMost)
and (Self = TYogaMeasureMode.AtMost)
and (not YogaIsUndefined(ALastSize))
and (not YogaIsUndefined(ASize))
and (not YogaIsUndefined(ALastComputedSize))
and (ALastSize > ASize)
and ((ALastComputedSize <= ASize) or YogaEqual(ASize, ALastComputedSize));
end;
function TYogaMeasureModeHelper.OldSizeIsUnspecifiedAndStillFits(
const ASize: Single; const ALastSizeMode: TYogaMeasureMode;
const ALastComputedSize: Single): Boolean;
begin
Result := (Self = TYogaMeasureMode.AtMost)
and (ALastSizeMode = TYogaMeasureMode.Undefined)
and ((ASize >= ALastComputedSize) or YogaEqual(ASize, ALastComputedSize));
end;
function TYogaMeasureModeHelper.SizeIsExactAndMatchesOldMeasuresSize(
const ASize, ALastComputedSize: Single): Boolean;
begin
Result := (Self = TYogaMeasureMode.Exactly) and YogaEqual(ASize, ALastComputedSize);
end;
{ TYogaNode }
constructor TYogaNode.Create;
begin
inherited Create;
FStyle.Init;
FLayout.Init;
FResolvedDimensions[TDimension.Width].SetUndefined;
FResolvedDimensions[TDimension.Height].SetUndefined;
FHasNewLayout := True;
end;