/
FlexLayout.java
1728 lines (1623 loc) · 50.3 KB
/
FlexLayout.java
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
package com.github.mmin18.widget;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import com.github.mmin18.flexlayout.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
/**
* A powerful Android layout view that use java expression in layout params to describe relative positions.
* <br>
* https://github.com/mmin18/FlexLayout
* <br><br>
* layout_left, layout_right, layout_top, layout_bottom, layout_centerX, layout_centerY, layout_width, layout_height<br>
* wrap_content, match_parent, 100%, 50%+100dp<br>
* this.width*1.6, prev.right+10dp, next.left-10dp<br>
* parent.height/2 (only parent.width and parent.height are supported)<br>
* other_view.width*0.5-30px (other_view is defined as res id)<br>
* screen.width, screen.height (screen width/height is relative to current orientation)<br>
* max(view1.right,view2.right), min(), round(), ceil(), floor(), abs(), mod(), pow()<br>
* prev.visible, prev.gone, prev.tag<br>
* a==b, a!=b, a<=b, a<b, a>=b, a>b<br>
* a&&b, a||b<br>
* a?b:c<br>
*/
public class FlexLayout extends ViewGroup {
public FlexLayout(Context context) {
this(context, null);
}
public FlexLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlexLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (isInEditMode()) {
DEBUG = Boolean.TRUE;
if (EDIT_MODE_ID_MAP == null) {
EDIT_MODE_ID_MAP = new HashMap<String, Integer>();
}
}
}
boolean isRtl() {
// Arabic RTL support. Simply flip everything from right to left
// Enabled if you set android:supportsRtl="true" in AndroidManifest.xml
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return getLayoutDirection() == LAYOUT_DIRECTION_RTL;
} else {
return false;
}
}
int myWidthMeasureSpec;
int myHeightMeasureSpec;
int myWidth;
int myHeight;
public static class LayoutParams extends ViewGroup.LayoutParams {
static int UNSPECIFIED = -5;
RPN left, right, top, bottom;
RPN centerX, centerY;
RPN width2, height2;
float mLeft, mRight, mTop, mBottom;
float mCenterX, mCenterY;
float mWidth, mHeight;
int mMeasuredWidth, mMeasuredHeight;
int editModeId; // only available in edit mode
String positionDescription; // only available in debug mode
static final int[] ViewGroup_Layout = new int[]{android.R.attr.layout_width, android.R.attr.layout_height};
public LayoutParams(Context c, AttributeSet attrs) {
super(0, 0);
if (EDIT_MODE_ID_MAP != null) {
String idStr = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "id");
if (idStr != null) {
if (idStr.startsWith("@+id/")) {
idStr = idStr.substring("@+id/".length());
} else if (idStr.startsWith("@id/")) {
idStr = idStr.substring("@id/".length());
} else if (idStr.startsWith("@android:id/")) {
idStr = "android:" + idStr.substring("@android:id/".length());
} else {
throw new IllegalArgumentException("unidentified id " + idStr);
}
editModeId = getEditModeId(idStr);
}
}
if (isDebug(c)) {
positionDescription = attrs.getPositionDescription();
}
TypedArray a = c.obtainStyledAttributes(attrs, ViewGroup_Layout);
width = a.getLayoutDimension(0, UNSPECIFIED);
height = a.getLayoutDimension(1, UNSPECIFIED);
a.recycle();
a = c.obtainStyledAttributes(attrs, R.styleable.FlexLayout_Layout);
this.left = RPN.parse(c, a.getString(R.styleable.FlexLayout_Layout_layout_left), "layout_left");
this.top = RPN.parse(c, a.getString(R.styleable.FlexLayout_Layout_layout_top), "layout_top");
this.right = RPN.parse(c, a.getString(R.styleable.FlexLayout_Layout_layout_right), "layout_right");
this.bottom = RPN.parse(c, a.getString(R.styleable.FlexLayout_Layout_layout_bottom), "layout_bottom");
this.centerX = RPN.parse(c, a.getString(R.styleable.FlexLayout_Layout_layout_centerX), "layout_centerX");
this.centerY = RPN.parse(c, a.getString(R.styleable.FlexLayout_Layout_layout_centerY), "layout_centerY");
String str = a.getString(R.styleable.FlexLayout_Layout_layout_width);
if ("match_parent".equals(str) || "fill_parent".equals(str)) {
width = MATCH_PARENT;
} else if ("wrap_content".equals(str)) {
width = WRAP_CONTENT;
} else {
this.width2 = RPN.parse(c, str, "layout_width");
}
str = a.getString(R.styleable.FlexLayout_Layout_layout_height);
if ("match_parent".equals(str) || "fill_parent".equals(str)) {
height = MATCH_PARENT;
} else if ("wrap_content".equals(str)) {
height = WRAP_CONTENT;
} else {
this.height2 = RPN.parse(c, str, "layout_height");
}
a.recycle();
int co = 0;
if (this.left != null)
co++;
if (this.right != null)
co++;
if (this.centerX != null)
co++;
if (this.width2 != null || this.width != LayoutParams.UNSPECIFIED)
co++;
if (co < 1) {
throw new IllegalArgumentException("no LayoutParams in layout_left|layout_right|layout_centerX|layout_width");
} else if (co > 2) {
// I should just throw a IllegalArgumentException if there is both left, right and width defined
// But some people insist to do it, so I just set width UNSPECIFIED to solve this problem
// The rule is:
// If you defined left and right, centerX and width will be ignored
// Or if you defined centerX and width, left and right will be ignored
// Otherwise the logic is too wired I will just throw a exception
if (this.left != null && this.right != null) {
this.width2 = null;
this.width = LayoutParams.UNSPECIFIED;
} else if (this.centerX != null && (this.width2 != null || this.width != LayoutParams.UNSPECIFIED)) {
this.left = null;
this.right = null;
} else {
throw new IllegalArgumentException("too many restriction on LayoutParams");
}
}
co = 0;
if (this.top != null)
co++;
if (this.bottom != null)
co++;
if (this.centerY != null)
co++;
if (this.height2 != null || this.height != LayoutParams.UNSPECIFIED)
co++;
if (co < 1) {
throw new IllegalArgumentException("no LayoutParams in layout_top|layout_bottom|layout_centerY|layout_height");
} else if (co > 2) {
// Same story on the Y asix
if (this.top != null && this.bottom != null) {
this.height2 = null;
this.height = LayoutParams.UNSPECIFIED;
} else if (this.centerY != null && (this.height2 != null || this.height != LayoutParams.UNSPECIFIED)) {
this.top = null;
this.bottom = null;
} else {
throw new IllegalArgumentException("too many restriction on LayoutParams");
}
}
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
void reset() {
mLeft = Float.NaN;
mRight = Float.NaN;
mTop = Float.NaN;
mBottom = Float.NaN;
mCenterX = Float.NaN;
mCenterY = Float.NaN;
mWidth = Float.NaN;
mHeight = Float.NaN;
mMeasuredWidth = -1;
mMeasuredHeight = -1;
}
float getLeft() {
if (mLeft == mLeft) {
return mLeft;
}
if (mWidth == mWidth) {
if (mRight == mRight) {
return mRight - mWidth;
}
if (mCenterX == mCenterX) {
return mCenterX - mWidth / 2;
}
}
if (mCenterX == mCenterX && mRight == mRight) {
return 2 * mCenterX - mRight;
}
return Float.NaN;
}
float getRight() {
if (mRight == mRight) {
return mRight;
}
if (mWidth == mWidth) {
if (mLeft == mLeft) {
return mLeft + mWidth;
}
if (mCenterX == mCenterX) {
return mCenterX + mWidth / 2;
}
}
if (mCenterX == mCenterX && mLeft == mLeft) {
return 2 * mCenterX - mLeft;
}
return Float.NaN;
}
float getTop() {
if (mTop == mTop) {
return mTop;
}
if (mHeight == mHeight) {
if (mBottom == mBottom) {
return mBottom - mHeight;
}
if (mCenterY == mCenterY) {
return mCenterY - mHeight / 2;
}
}
if (mCenterY == mCenterY && mBottom == mBottom) {
return 2 * mCenterY - mBottom;
}
return Float.NaN;
}
float getBottom() {
if (mBottom == mBottom) {
return mBottom;
}
if (mHeight == mHeight) {
if (mTop == mTop) {
return mTop + mHeight;
}
if (mCenterY == mCenterY) {
return mCenterY + mHeight / 2;
}
}
if (mCenterY == mCenterY && mTop == mTop) {
return 2 * mCenterY - mTop;
}
return Float.NaN;
}
float getCenterX() {
if (mCenterX == mCenterX) {
return mCenterX;
}
if (mWidth == mWidth) {
if (mLeft == mLeft) {
return mLeft + mWidth / 2;
}
if (mRight == mRight) {
return mRight - mWidth / 2;
}
}
if (mLeft == mLeft && mRight == mRight) {
return (mLeft + mRight) / 2;
}
return Float.NaN;
}
float getCenterY() {
if (mCenterY == mCenterY) {
return mCenterY;
}
if (mHeight == mHeight) {
if (mTop == mTop) {
return mTop + mHeight / 2;
}
if (mBottom == mBottom) {
return mBottom - mHeight / 2;
}
}
if (mTop == mTop && mBottom == mBottom) {
return (mTop + mBottom) / 2;
}
return Float.NaN;
}
float getWidth() {
if (mWidth == mWidth) {
return mWidth;
}
if (mLeft == mLeft) {
if (mRight == mRight) {
return mRight - mLeft;
}
if (mCenterX == mCenterX) {
return (mCenterX - mLeft) * 2;
}
}
if (mRight == mRight && mCenterX == mCenterX) {
return (mRight - mCenterX) * 2;
}
return Float.NaN;
}
float getHeight() {
if (mHeight == mHeight) {
return mHeight;
}
if (mTop == mTop) {
if (mBottom == mBottom) {
return mBottom - mTop;
}
if (mCenterY == mCenterY) {
return (mCenterY - mTop) * 2;
}
}
if (mBottom == mBottom && mCenterY == mCenterY) {
return (mBottom - mCenterY) * 2;
}
return Float.NaN;
}
boolean isValidH() {
int c = 0;
if (mLeft == mLeft)
c++;
if (mRight == mRight)
c++;
if (mWidth == mWidth)
c++;
if (mCenterX == mCenterX)
c++;
return c >= 2;
}
boolean isValidV() {
int c = 0;
if (mTop == mTop)
c++;
if (mBottom == mBottom)
c++;
if (mHeight == mHeight)
c++;
if (mCenterY == mCenterY)
c++;
return c >= 2;
}
boolean isValid() {
return isValidH() && isValidV();
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new FlexLayout.LayoutParams(getContext(), attrs);
}
/**
* Returns a set of layout parameters with a width of
* {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT},
* a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and no spanning.
*/
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new FlexLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
// Override to allow type-checking of LayoutParams.
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof FlexLayout.LayoutParams;
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new FlexLayout.LayoutParams(p);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int paddingLeft = getPaddingLeft();
final int paddingRight = getPaddingRight();
final int paddingTop = getPaddingTop();
final int paddingBottom = getPaddingBottom();
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
myWidthMeasureSpec = widthMeasureSpec;
myHeightMeasureSpec = heightMeasureSpec;
int maxWidth, maxHeight;
if (widthMode == MeasureSpec.EXACTLY) {
myWidth = maxWidth = (widthSize - paddingLeft - paddingRight);
} else if (widthMode == MeasureSpec.AT_MOST) {
myWidth = -1;
maxWidth = (widthSize - paddingLeft - paddingRight);
} else {
myWidth = -1;
maxWidth = -1;
}
if (heightMode == MeasureSpec.EXACTLY) {
myHeight = maxHeight = (heightSize - paddingTop - paddingBottom);
} else if (heightMode == MeasureSpec.AT_MOST) {
myHeight = -1;
maxHeight = (heightSize - paddingTop - paddingBottom);
} else {
myHeight = -1;
maxHeight = -1;
}
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
FlexLayout.LayoutParams lp = (FlexLayout.LayoutParams) child.getLayoutParams();
lp.reset();
if (child.getVisibility() == View.GONE) {
lp.mWidth = 0;
lp.mHeight = 0;
}
if (lp.left == null) {
int c = 0;
if (lp.right != null)
c++;
if (lp.centerX != null)
c++;
if (lp.width2 != null || lp.width != LayoutParams.UNSPECIFIED)
c++;
if (c < 2)
lp.mLeft = 0;
}
if (lp.top == null) {
int c = 0;
if (lp.bottom != null)
c++;
if (lp.centerY != null)
c++;
if (lp.height2 != null || lp.height != LayoutParams.UNSPECIFIED)
c++;
if (c < 2)
lp.mTop = 0;
}
}
boolean ready = count == 0;
for (int j = 0; j < count * 4; j++) {
int vCount = 0, invIndex = -1;
int calcCount = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
FlexLayout.LayoutParams lp = (FlexLayout.LayoutParams) child.getLayoutParams();
if (lp.left != null && lp.mLeft != lp.mLeft) {
float v = lp.left.eval(this, i, 0, lp.positionDescription);
if (v == v) {
lp.mLeft = v;
calcCount++;
}
}
if (lp.right != null && lp.mRight != lp.mRight) {
float v = lp.right.eval(this, i, 0, lp.positionDescription);
if (v == v) {
lp.mRight = v;
calcCount++;
}
}
if (lp.top != null && lp.mTop != lp.mTop) {
float v = lp.top.eval(this, i, 1, lp.positionDescription);
if (v == v) {
lp.mTop = v;
calcCount++;
}
}
if (lp.bottom != null && lp.mBottom != lp.mBottom) {
float v = lp.bottom.eval(this, i, 1, lp.positionDescription);
if (v == v) {
lp.mBottom = v;
calcCount++;
}
}
if (lp.centerX != null && lp.mCenterX != lp.mCenterX) {
float v = lp.centerX.eval(this, i, 0, lp.positionDescription);
if (v == v) {
lp.mCenterX = v;
calcCount++;
}
}
if (lp.centerY != null && lp.mCenterY != lp.mCenterY) {
float v = lp.centerY.eval(this, i, 1, lp.positionDescription);
if (v == v) {
lp.mCenterY = v;
calcCount++;
}
}
if (lp.mWidth != lp.mWidth) {
if (lp.width2 != null) {
float v = lp.width2.eval(this, i, 0, lp.positionDescription);
if (v == v) {
lp.mWidth = v;
calcCount++;
}
} else if (lp.width != LayoutParams.UNSPECIFIED) {
if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT && myWidth != -1) {
lp.mWidth = myWidth;
calcCount++;
} else if (lp.width >= 0) {
lp.mWidth = lp.width;
calcCount++;
} else {
if (lp.mMeasuredWidth == -1 && measureChild(this, child, lp, lp.width, lp.height)) {
calcCount++;
}
if (lp.mMeasuredWidth != -1 && lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
lp.mWidth = lp.mMeasuredWidth;
calcCount++;
}
}
}
}
if (lp.mHeight != lp.mHeight) {
if (lp.height2 != null) {
float v = lp.height2.eval(this, i, 1, lp.positionDescription);
if (v == v) {
lp.mHeight = v;
calcCount++;
}
} else if (lp.height != LayoutParams.UNSPECIFIED) {
if (lp.height == ViewGroup.LayoutParams.MATCH_PARENT && myHeight != -1) {
lp.mHeight = myHeight;
calcCount++;
} else if (lp.height >= 0) {
lp.mHeight = lp.height;
calcCount++;
} else {
if (lp.mMeasuredHeight == -1 && measureChild(this, child, lp, lp.width, lp.height)) {
calcCount++;
}
if (lp.mMeasuredHeight != -1 && lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
lp.mHeight = lp.mMeasuredHeight;
calcCount++;
}
}
}
}
if (lp.isValid()) {
vCount++;
} else if (invIndex == -1) {
invIndex = i;
}
}
if (vCount == count && myWidth != -1 && myHeight != -1) {
ready = true;
break;
}
if (calcCount == 0) {
if (myWidth == -1 || myHeight == -1) {
int maxw = 0;
int maxh = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
FlexLayout.LayoutParams lp = (FlexLayout.LayoutParams) child.getLayoutParams();
float r = lp.getRight();
if (r == r) {
maxw = Math.max(maxw, Math.round(r));
} else if (lp.mMeasuredWidth != -1) {
float l = lp.getLeft();
if (l == l) {
maxw = Math.max(maxw, Math.round(l + lp.mMeasuredWidth));
} else {
maxw = Math.max(maxw, lp.mMeasuredWidth);
}
}
float b = lp.getBottom();
if (b == b) {
maxh = Math.max(maxh, Math.round(b));
} else if (lp.mMeasuredHeight != -1) {
float t = lp.getTop();
if (t == t) {
maxh = Math.max(maxh, Math.round(t + lp.mMeasuredHeight));
} else {
maxh = Math.max(maxh, lp.mMeasuredHeight);
}
}
lp.mMeasuredWidth = -1;
lp.mMeasuredHeight = -1;
}
if (myWidth == -1) {
myWidth = maxWidth == -1 ? maxw : Math.min(maxw, maxWidth);
}
if (myHeight == -1) {
myHeight = maxHeight == -1 ? maxh : Math.min(maxh, maxHeight);
}
} else {
throw new IllegalStateException("incomplete layout, circular dependency? (index=" + invIndex + ")");
}
}
}
if (!ready) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
FlexLayout.LayoutParams lp = (FlexLayout.LayoutParams) child.getLayoutParams();
if (!lp.isValid()) {
if (sb.length() > 0) {
sb.append(',');
}
sb.append(i);
}
}
throw new IllegalStateException("incomplete layout, circular dependency? (index=" + sb + ")");
}
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
FlexLayout.LayoutParams lp = (FlexLayout.LayoutParams) child.getLayoutParams();
int mwspec, mhspec;
if (lp.width2 != null && lp.mWidth == lp.mWidth) {
mwspec = MeasureSpec.makeMeasureSpec(Math.round(lp.mWidth), MeasureSpec.EXACTLY);
} else if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
mwspec = MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.AT_MOST);
} else if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
mwspec = MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY);
} else {
mwspec = MeasureSpec.makeMeasureSpec(Math.round(lp.getWidth()), MeasureSpec.EXACTLY);
}
if (lp.height2 != null && lp.mHeight == lp.mHeight) {
mhspec = MeasureSpec.makeMeasureSpec(Math.round(lp.mHeight), MeasureSpec.EXACTLY);
} else if (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
mhspec = MeasureSpec.makeMeasureSpec(myHeight, MeasureSpec.AT_MOST);
} else if (lp.height == ViewGroup.LayoutParams.MATCH_PARENT) {
mhspec = MeasureSpec.makeMeasureSpec(myHeight, MeasureSpec.EXACTLY);
} else {
mhspec = MeasureSpec.makeMeasureSpec(Math.round(lp.getHeight()), MeasureSpec.EXACTLY);
}
child.measure(mwspec, mhspec);
}
setMeasuredDimension(myWidth + paddingLeft + paddingRight, myHeight + paddingTop + paddingBottom);
}
static boolean measureChild(FlexLayout fl, View child, LayoutParams lp, int lpWidth, int lpHeight) {
int dimenW, dimenH;
if (lpWidth == LayoutParams.UNSPECIFIED) {
float w = lp.getWidth();
if (w == w) {
dimenW = Math.round(w);
} else if (onlyRefSelf(lp.width2) && onlyRefSelf(lp.left) && onlyRefSelf(lp.right) && onlyRefSelf(lp.centerX)) {
// in case layout_width="this.height"
dimenW = ViewGroup.LayoutParams.WRAP_CONTENT;
} else {
return false;
}
} else {
dimenW = lpWidth;
}
if (lpHeight == LayoutParams.UNSPECIFIED) {
float h = lp.getHeight();
if (h == h) {
dimenH = Math.round(h);
} else if (onlyRefSelf(lp.height2) && onlyRefSelf(lp.top) && onlyRefSelf(lp.bottom) && onlyRefSelf(lp.centerY)) {
// in case layout_height="this.width"
dimenH = ViewGroup.LayoutParams.WRAP_CONTENT;
} else {
return false;
}
} else {
dimenH = lpHeight;
}
int specW;
if (fl.myWidth == -1) {
specW = getChildMeasureSpec(fl.myWidthMeasureSpec, fl.getPaddingLeft() + fl.getPaddingRight(), dimenW);
} else {
specW = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(fl.myWidth, MeasureSpec.EXACTLY), 0, dimenW);
}
int specH;
if (fl.myHeight == -1) {
specH = getChildMeasureSpec(fl.myHeightMeasureSpec, fl.getPaddingTop() + fl.getPaddingBottom(), dimenH);
} else {
specH = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(fl.myHeight, MeasureSpec.EXACTLY), 0, dimenH);
}
child.measure(specW, specH);
lp.mMeasuredWidth = child.getMeasuredWidth();
lp.mMeasuredHeight = child.getMeasuredHeight();
return true;
}
static boolean onlyRefSelf(RPN exp) {
if (exp != null) {
for (Object obj : exp.list) {
if (obj instanceof Ref) {
Ref ref = (Ref) obj;
if (ref.target != Ref.TARGET_THIS) {
return false;
}
}
}
}
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();
// The layout has actually already been performed and the positions
// cached. Apply the cached values to the children.
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
FlexLayout.LayoutParams lp = (FlexLayout.LayoutParams) child.getLayoutParams();
if (isRtl()) {
// Arabic RTL support. Simply flip everything from right to left
int w = r - l;
child.layout(w - paddingLeft - Math.round(lp.getRight()), paddingTop + Math.round(lp.getTop()),
w - paddingLeft - Math.round(lp.getLeft()), paddingTop + Math.round(lp.getBottom()));
} else {
child.layout(paddingLeft + Math.round(lp.getLeft()), paddingTop + Math.round(lp.getTop()),
paddingLeft + Math.round(lp.getRight()), paddingTop + Math.round(lp.getBottom()));
}
}
}
}
//
// Parser and Eval
// Shunting-yard algorithm & RPN
//
static abstract class Operator {
public static final int ASSOC_LEFT = 1;
public static final int ASSOC_RIGHT = 2;
public static final int FLAG_FUNCTION = 1;
public final String op;
public final int prec;
public final int assoc;
public final int argc;
public final int flag;
public Operator(String op, int prec, int assoc, int argc, int flag) {
this.op = op;
this.prec = prec;
this.assoc = assoc;
this.argc = argc;
this.flag = flag;
}
public abstract float eval(FlexLayout fl, int index, int xy, float a, float b);
@Override
public String toString() {
return op;
}
}
// Precedence
// () sp dp dip px pt mm in
// !
// * / %
// + -
// < <= > >=
// == !=
// &&
// ||
// ?:
static final Operator MUL = new Operator("*", 8, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return a * b;
}
};
static final Operator DIV = new Operator("/", 8, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return a / b;
}
};
static final Operator PERC = new Operator("%", 8, Operator.ASSOC_RIGHT, 1, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (xy == 0) {
if (fl.myWidth == -1) {
return Float.NaN;
} else {
return fl.myWidth * a * 0.01f;
}
} else {
if (fl.myHeight == -1) {
return Float.NaN;
} else {
return fl.myHeight * a * 0.01f;
}
}
}
};
static final Operator ADD = new Operator("+", 7, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return a + b;
}
};
static final Operator SUB = new Operator("-", 7, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return a - b;
}
};
static final Operator NOT = new Operator("!", 9, Operator.ASSOC_RIGHT, 1, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a) {
return a == 0 ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator CP_LT = new Operator("<", 6, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a < b ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator CP_LT_EQ = new Operator("<=", 6, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a <= b ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator CP_GT = new Operator(">", 6, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a > b ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator CP_GT_EQ = new Operator(">=", 6, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a >= b ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator CP_EQ = new Operator("==", 5, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a == b ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator CP_NOT_EQ = new Operator("!=", 5, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a != b ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator LOG_AND = new Operator("&&", 4, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a != 0 && b != 0 ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator LOG_OR = new Operator("||", 3, Operator.ASSOC_LEFT, 2, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
if (a == a && b == b) {
return a != 0 || b != 0 ? 1 : 0;
} else {
return Float.NaN;
}
}
};
static final Operator BL = new Operator("(", 0, 0, 0, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return Float.NaN;
}
};
static final Operator BR = new Operator(")", 0, 0, 0, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return Float.NaN;
}
};
static final Operator COMMA = new Operator(",", 0, Operator.ASSOC_LEFT, 0, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return Float.NaN;
}
};
static final Operator U_SP = new Operator("sp", 10, Operator.ASSOC_RIGHT, 1, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, a, fl.getResources().getDisplayMetrics());
}
};
static final Operator U_DP = new Operator("dp", 10, Operator.ASSOC_RIGHT, 1, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, a, fl.getResources().getDisplayMetrics());
}
};
static final Operator U_DIP = new Operator("dip", 10, Operator.ASSOC_RIGHT, 1, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, a, fl.getResources().getDisplayMetrics());
}
};
static final Operator U_PX = new Operator("px", 10, Operator.ASSOC_RIGHT, 1, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, a, fl.getResources().getDisplayMetrics());
}
};
static final Operator U_PT = new Operator("pt", 10, Operator.ASSOC_RIGHT, 1, 0) {
@Override
public float eval(FlexLayout fl, int index, int xy, float a, float b) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, a, fl.getResources().getDisplayMetrics());
}
};