-
Notifications
You must be signed in to change notification settings - Fork 175
/
ConstraintLayoutBaseScope.kt
919 lines (849 loc) · 33.5 KB
/
ConstraintLayoutBaseScope.kt
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
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.constraintlayout.compose
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.core.widgets.ConstraintWidget
/**
* Common scope for [ConstraintLayoutScope] and [ConstraintSetScope], the content being shared
* between the inline DSL API and the ConstraintSet-based API.
*/
abstract class ConstraintLayoutBaseScope {
protected val tasks = mutableListOf<(State) -> Unit>()
fun applyTo(state: State): Unit = tasks.forEach { it(state) }
open fun reset() {
tasks.clear()
helperId = HelpersStartId
helpersHashCode = 0
}
@PublishedApi
internal var helpersHashCode: Int = 0
private fun updateHelpersHashCode(value: Int) {
helpersHashCode = (helpersHashCode * 1009 + value) % 1000000007
}
private val HelpersStartId = 1000
private var helperId = HelpersStartId
private fun createHelperId() = helperId++
/**
* Represents a vertical anchor (e.g. start/end of a layout, guideline) that layouts
* can link to in their `Modifier.constrainAs` or `constrain` blocks.
*
* @param reference The [LayoutReference] that this anchor belongs to.
*/
@Stable
data class VerticalAnchor internal constructor(
internal val id: Any,
internal val index: Int,
val reference: LayoutReference
)
/**
* Represents a horizontal anchor (e.g. top/bottom of a layout, guideline) that layouts
* can link to in their `Modifier.constrainAs` or `constrain` blocks.
*
* @param reference The [LayoutReference] that this anchor belongs to.
*/
@Stable
data class HorizontalAnchor internal constructor(
internal val id: Any,
internal val index: Int,
val reference: LayoutReference
)
/**
* Represents a horizontal anchor corresponding to the [FirstBaseline] of a layout that other
* layouts can link to in their `Modifier.constrainAs` or `constrain` blocks.
*
* @param reference The [LayoutReference] that this anchor belongs to.
*/
// TODO(popam): investigate if this can be just a HorizontalAnchor
@Stable
data class BaselineAnchor internal constructor(
internal val id: Any,
val reference: LayoutReference
)
/**
* Specifies additional constraints associated to the horizontal chain identified with [ref].
*/
fun constrain(
ref: HorizontalChainReference,
constrainBlock: HorizontalChainScope.() -> Unit
): HorizontalChainScope = HorizontalChainScope(ref.id).apply {
constrainBlock()
this@ConstraintLayoutBaseScope.tasks.addAll(this.tasks)
}
/**
* Specifies additional constraints associated to the vertical chain identified with [ref].
*/
fun constrain(
ref: VerticalChainReference,
constrainBlock: VerticalChainScope.() -> Unit
): VerticalChainScope = VerticalChainScope(ref.id).apply {
constrainBlock()
this@ConstraintLayoutBaseScope.tasks.addAll(this.tasks)
}
/**
* Specifies the constraints associated to the layout identified with [ref].
*/
fun constrain(
ref: ConstrainedLayoutReference,
constrainBlock: ConstrainScope.() -> Unit
): ConstrainScope = ConstrainScope(ref.id).apply {
constrainBlock()
this@ConstraintLayoutBaseScope.tasks.addAll(this.tasks)
}
/**
* Creates a guideline at a specific offset from the start of the [ConstraintLayout].
*/
fun createGuidelineFromStart(offset: Dp): VerticalAnchor {
val id = createHelperId()
tasks.add { state ->
state.verticalGuideline(id).apply {
if (state.layoutDirection == LayoutDirection.Ltr) start(offset) else end(offset)
}
}
updateHelpersHashCode(1)
updateHelpersHashCode(offset.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a specific offset from the left of the [ConstraintLayout].
*/
fun createGuidelineFromAbsoluteLeft(offset: Dp): VerticalAnchor {
val id = createHelperId()
tasks.add { state -> state.verticalGuideline(id).apply { start(offset) } }
updateHelpersHashCode(2)
updateHelpersHashCode(offset.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a specific offset from the start of the [ConstraintLayout].
* A [fraction] of 0f will correspond to the start of the [ConstraintLayout], while 1f will
* correspond to the end.
*/
fun createGuidelineFromStart(fraction: Float): VerticalAnchor {
val id = createHelperId()
tasks.add { state ->
state.verticalGuideline(id).apply {
if (state.layoutDirection == LayoutDirection.Ltr) {
percent(fraction)
} else {
percent(1f - fraction)
}
}
}
updateHelpersHashCode(3)
updateHelpersHashCode(fraction.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a width fraction from the left of the [ConstraintLayout].
* A [fraction] of 0f will correspond to the left of the [ConstraintLayout], while 1f will
* correspond to the right.
*/
// TODO(popam, b/157781990): this is not really percenide
fun createGuidelineFromAbsoluteLeft(fraction: Float): VerticalAnchor {
val id = createHelperId()
tasks.add { state -> state.verticalGuideline(id).apply { percent(fraction) } }
updateHelpersHashCode(4)
updateHelpersHashCode(fraction.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a specific offset from the end of the [ConstraintLayout].
*/
fun createGuidelineFromEnd(offset: Dp): VerticalAnchor {
val id = createHelperId()
tasks.add { state ->
state.verticalGuideline(id).apply {
if (state.layoutDirection == LayoutDirection.Ltr) end(offset) else start(offset)
}
}
updateHelpersHashCode(5)
updateHelpersHashCode(offset.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a specific offset from the right of the [ConstraintLayout].
*/
fun createGuidelineFromAbsoluteRight(offset: Dp): VerticalAnchor {
val id = createHelperId()
tasks.add { state -> state.verticalGuideline(id).apply { end(offset) } }
updateHelpersHashCode(6)
updateHelpersHashCode(offset.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a width fraction from the end of the [ConstraintLayout].
* A [fraction] of 0f will correspond to the end of the [ConstraintLayout], while 1f will
* correspond to the start.
*/
fun createGuidelineFromEnd(fraction: Float): VerticalAnchor {
return createGuidelineFromStart(1f - fraction)
}
/**
* Creates a guideline at a width fraction from the right of the [ConstraintLayout].
* A [fraction] of 0f will correspond to the right of the [ConstraintLayout], while 1f will
* correspond to the left.
*/
fun createGuidelineFromAbsoluteRight(fraction: Float): VerticalAnchor {
return createGuidelineFromAbsoluteLeft(1f - fraction)
}
/**
* Creates a guideline at a specific offset from the top of the [ConstraintLayout].
*/
fun createGuidelineFromTop(offset: Dp): HorizontalAnchor {
val id = createHelperId()
tasks.add { state -> state.horizontalGuideline(id).apply { start(offset) } }
updateHelpersHashCode(7)
updateHelpersHashCode(offset.hashCode())
return HorizontalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a height percenide from the top of the [ConstraintLayout].
* A [fraction] of 0f will correspond to the top of the [ConstraintLayout], while 1f will
* correspond to the bottom.
*/
fun createGuidelineFromTop(fraction: Float): HorizontalAnchor {
val id = createHelperId()
tasks.add { state -> state.horizontalGuideline(id).apply { percent(fraction) } }
updateHelpersHashCode(8)
updateHelpersHashCode(fraction.hashCode())
return HorizontalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a specific offset from the bottom of the [ConstraintLayout].
*/
fun createGuidelineFromBottom(offset: Dp): HorizontalAnchor {
val id = createHelperId()
tasks.add { state -> state.horizontalGuideline(id).apply { end(offset) } }
updateHelpersHashCode(9)
updateHelpersHashCode(offset.hashCode())
return HorizontalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates a guideline at a height percenide from the bottom of the [ConstraintLayout].
* A [fraction] of 0f will correspond to the bottom of the [ConstraintLayout], while 1f will
* correspond to the top.
*/
fun createGuidelineFromBottom(fraction: Float): HorizontalAnchor {
return createGuidelineFromTop(1f - fraction)
}
/**
* Creates and returns a start barrier, containing the specified elements.
*/
fun createStartBarrier(
vararg elements: LayoutReference,
margin: Dp = 0.dp
): VerticalAnchor {
val id = createHelperId()
tasks.add { state ->
val direction = if (state.layoutDirection == LayoutDirection.Ltr) {
SolverDirection.LEFT
} else {
SolverDirection.RIGHT
}
state.barrier(id, direction).apply {
add(*(elements.map { it.id }.toTypedArray()))
}.margin(state.convertDimension(margin))
}
updateHelpersHashCode(10)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(margin.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates and returns a left barrier, containing the specified elements.
*/
fun createAbsoluteLeftBarrier(
vararg elements: LayoutReference,
margin: Dp = 0.dp
): VerticalAnchor {
val id = createHelperId()
tasks.add { state ->
state.barrier(id, SolverDirection.LEFT).apply {
add(*(elements.map { it.id }.toTypedArray()))
}.margin(state.convertDimension(margin))
}
updateHelpersHashCode(11)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(margin.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates and returns a top barrier, containing the specified elements.
*/
fun createTopBarrier(
vararg elements: LayoutReference,
margin: Dp = 0.dp
): HorizontalAnchor {
val id = createHelperId()
tasks.add { state ->
state.barrier(id, SolverDirection.TOP).apply {
add(*(elements.map { it.id }.toTypedArray()))
}.margin(state.convertDimension(margin))
}
updateHelpersHashCode(12)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(margin.hashCode())
return HorizontalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates and returns an end barrier, containing the specified elements.
*/
fun createEndBarrier(
vararg elements: LayoutReference,
margin: Dp = 0.dp
): VerticalAnchor {
val id = createHelperId()
tasks.add { state ->
val direction = if (state.layoutDirection == LayoutDirection.Ltr) {
SolverDirection.RIGHT
} else {
SolverDirection.LEFT
}
state.barrier(id, direction).apply {
add(*(elements.map { it.id }.toTypedArray()))
}.margin(state.convertDimension(margin))
}
updateHelpersHashCode(13)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(margin.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates and returns a right barrier, containing the specified elements.
*/
fun createAbsoluteRightBarrier(
vararg elements: LayoutReference,
margin: Dp = 0.dp
): VerticalAnchor {
val id = createHelperId()
tasks.add { state ->
state.barrier(id, SolverDirection.RIGHT).apply {
add(*(elements.map { it.id }.toTypedArray()))
}.margin(state.convertDimension(margin))
}
updateHelpersHashCode(14)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(margin.hashCode())
return VerticalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* Creates and returns a bottom barrier, containing the specified elements.
*/
fun createBottomBarrier(
vararg elements: LayoutReference,
margin: Dp = 0.dp
): HorizontalAnchor {
val id = createHelperId()
tasks.add { state ->
state.barrier(id, SolverDirection.BOTTOM).apply {
add(*(elements.map { it.id }.toTypedArray()))
}.margin(state.convertDimension(margin))
}
updateHelpersHashCode(15)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(margin.hashCode())
return HorizontalAnchor(id, 0, LayoutReferenceImpl(id))
}
/**
* This creates a flow helper
* Flow helpers allows a long sequence of Composable widgets to wrap onto
* multiple rows or columns.
* [flowVertically] if set to true aranges the Composables from top to bottom.
* Normally they are arranged from left to right.
* [verticalGap] defines the gap between views in the y axis
* [horizontalGap] defines the gap between views in the x axis
* [maxElement] defines the maximum element on a row before it if the
* [padding] sets padding around the content
* [wrapMode] sets the way reach maxElements is handled
* Flow.WRAP_NONE (default) -- no wrap behavior,
* Flow.WRAP_CHAIN - create additional chains
* [verticalAlign] set the way elements are aligned vertically. Center is default
* [horizontalAlign] set the way elements are aligned horizontally. Center is default
* [horizontalFlowBias] set the way elements are aligned vertically Center is default
* [verticalFlowBias] sets the top bottom bias of the vertical chain
* [verticalStyle] sets the style of a vertical chain (Spread,Packed, or SpreadInside)
* [horizontalStyle] set the style of the horizontal chain (Spread, Packed, or SpreadInside)
*/
fun createFlow(
vararg elements: LayoutReference?,
flowVertically: Boolean = false,
verticalGap: Dp = 0.dp,
horizontalGap: Dp = 0.dp,
maxElement: Int = 0,
padding: Dp = 0.dp,
wrapMode: Wrap = Wrap.None,
verticalAlign: VerticalAlign = VerticalAlign.Center,
horizontalAlign: HorizontalAlign = HorizontalAlign.Center,
horizontalFlowBias: Float = 0.0f,
verticalFlowBias: Float = 0.0f,
verticalStyle: FlowStyle = FlowStyle.Packed,
horizontalStyle: FlowStyle = FlowStyle.Packed,
): ConstrainedLayoutReference {
val id = createHelperId()
tasks.add { state ->
state.getFlow(id, flowVertically).apply {
add(*(elements.map { it?.id }.toTypedArray()))
horizontalChainStyle = horizontalStyle.style
setVerticalChainStyle(verticalStyle.style)
verticalBias(verticalFlowBias)
horizontalBias(horizontalFlowBias)
setHorizontalAlign(horizontalAlign.mode)
setVerticalAlign(verticalAlign.mode)
setWrapMode(wrapMode.mode)
paddingLeft = state.convertDimension(padding)
paddingTop = state.convertDimension(padding)
paddingRight = state.convertDimension(padding)
paddingBottom = state.convertDimension(padding)
maxElementsWrap = maxElement
setHorizontalGap(state.convertDimension(horizontalGap))
setVerticalGap(state.convertDimension(verticalGap))
}
}
updateHelpersHashCode(16)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
return ConstrainedLayoutReference(id)
}
/**
* This creates a flow helper
* Flow helpers allows a long sequence of Composable widgets to wrap onto
* multiple rows or columns.
* [flowVertically] if set to true aranges the Composables from top to bottom.
* Normally they are arranged from left to right.
* [verticalGap] defines the gap between views in the y axis
* [horizontalGap] defines the gap between views in the x axis
* [maxElement] defines the maximum element on a row before it if the
* [paddingHorizontal] sets paddingLeft and paddingRight of the content
* [paddingVertical] sets paddingTop and paddingBottom of the content
* [wrapMode] sets the way reach maxElements is handled
* Flow.WRAP_NONE (default) -- no wrap behavior,
* Flow.WRAP_CHAIN - create additional chains
* [verticalAlign] set the way elements are aligned vertically. Center is default
* [horizontalAlign] set the way elements are aligned horizontally. Center is default
* [horizontalFlowBias] set the way elements are aligned vertically Center is default
* [verticalFlowBias] sets the top bottom bias of the vertical chain
* [verticalStyle] sets the style of a vertical chain (Spread,Packed, or SpreadInside)
* [horizontalStyle] set the style of the horizontal chain (Spread, Packed, or SpreadInside)
*/
fun createFlow(
vararg elements: LayoutReference?,
flowVertically: Boolean = false,
verticalGap: Dp = 0.dp,
horizontalGap: Dp = 0.dp,
maxElement: Int = 0,
paddingHorizontal: Dp = 0.dp,
paddingVertical: Dp = 0.dp,
wrapMode: Wrap = Wrap.None,
verticalAlign: VerticalAlign = VerticalAlign.Center,
horizontalAlign: HorizontalAlign = HorizontalAlign.Center,
horizontalFlowBias: Float = 0.0f,
verticalFlowBias: Float = 0.0f,
verticalStyle: FlowStyle = FlowStyle.Packed,
horizontalStyle: FlowStyle = FlowStyle.Packed,
): ConstrainedLayoutReference {
val id = createHelperId()
tasks.add { state ->
state.getFlow(id, flowVertically).apply {
add(*(elements.map { it?.id }.toTypedArray()))
horizontalChainStyle = horizontalStyle.style
setVerticalChainStyle(verticalStyle.style)
verticalBias(verticalFlowBias)
horizontalBias(horizontalFlowBias)
setHorizontalAlign(horizontalAlign.mode)
setVerticalAlign(verticalAlign.mode)
setWrapMode(wrapMode.mode)
paddingLeft = state.convertDimension(paddingHorizontal)
paddingTop = state.convertDimension(paddingVertical)
paddingRight = state.convertDimension(paddingHorizontal)
paddingBottom = state.convertDimension(paddingVertical)
maxElementsWrap = maxElement
setHorizontalGap(state.convertDimension(horizontalGap))
setVerticalGap(state.convertDimension(verticalGap))
}
}
updateHelpersHashCode(16)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
return ConstrainedLayoutReference(id)
}
/**
* This creates a flow helper
* Flow helpers allows a long sequence of Composable widgets to wrap onto
* multiple rows or columns.
* [flowVertically] if set to true aranges the Composables from top to bottom.
* Normally they are arranged from left to right.
* [verticalGap] defines the gap between views in the y axis
* [horizontalGap] defines the gap between views in the x axis
* [maxElement] defines the maximum element on a row before it if the
* [paddingLeft] sets paddingLeft of the content
* [paddingTop] sets paddingTop of the content
* [paddingRight] sets paddingRight of the content
* [paddingBottom] sets paddingBottom of the content
* [wrapMode] sets the way reach maxElements is handled
* Flow.WRAP_NONE (default) -- no wrap behavior,
* Flow.WRAP_CHAIN - create additional chains
* [verticalAlign] set the way elements are aligned vertically. Center is default
* [horizontalAlign] set the way elements are aligned horizontally. Center is default
* [horizontalFlowBias] set the way elements are aligned vertically Center is default
* [verticalFlowBias] sets the top bottom bias of the vertical chain
* [verticalStyle] sets the style of a vertical chain (Spread,Packed, or SpreadInside)
* [horizontalStyle] set the style of the horizontal chain (Spread, Packed, or SpreadInside)
*/
fun createFlow(
vararg elements: LayoutReference?,
flowVertically: Boolean = false,
verticalGap: Dp = 0.dp,
horizontalGap: Dp = 0.dp,
maxElement: Int = 0,
paddingLeft: Dp = 0.dp,
paddingTop: Dp = 0.dp,
paddingRight: Dp = 0.dp,
paddingBottom: Dp = 0.dp,
wrapMode: Wrap = Wrap.None,
verticalAlign: VerticalAlign = VerticalAlign.Center,
horizontalAlign: HorizontalAlign = HorizontalAlign.Center,
horizontalFlowBias: Float = 0.0f,
verticalFlowBias: Float = 0.0f,
verticalStyle: FlowStyle = FlowStyle.Packed,
horizontalStyle: FlowStyle = FlowStyle.Packed,
): ConstrainedLayoutReference {
val id = createHelperId()
tasks.add { state ->
state.getFlow(id, flowVertically).apply {
add(*(elements.map { it?.id }.toTypedArray()))
horizontalChainStyle = horizontalStyle.style
setVerticalChainStyle(verticalStyle.style)
verticalBias(verticalFlowBias)
horizontalBias(horizontalFlowBias)
setHorizontalAlign(horizontalAlign.mode)
setVerticalAlign(verticalAlign.mode)
setWrapMode(wrapMode.mode)
setPaddingLeft(state.convertDimension(paddingLeft))
setPaddingTop(state.convertDimension(paddingTop))
setPaddingRight(state.convertDimension(paddingRight))
setPaddingBottom(state.convertDimension(paddingBottom))
maxElementsWrap = maxElement
setHorizontalGap(state.convertDimension(horizontalGap))
setVerticalGap(state.convertDimension(verticalGap))
}
}
updateHelpersHashCode(16)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
return ConstrainedLayoutReference(id)
}
/**
* Creates a horizontal chain including the referenced layouts.
*
* Use [constrain] with the resulting [HorizontalChainReference] to modify the start/left and
* end/right constraints of this chain.
*/
// TODO(popam, b/157783937): this API should be improved
fun createHorizontalChain(
vararg elements: LayoutReference,
chainStyle: ChainStyle = ChainStyle.Spread
): HorizontalChainReference {
val id = createHelperId()
tasks.add { state ->
val helper = state.helper(
id,
androidx.constraintlayout.core.state.State.Helper.HORIZONTAL_CHAIN
) as androidx.constraintlayout.core.state.helpers.HorizontalChainReference
helper.add(*(elements.map { it.id }.toTypedArray()))
helper.style(chainStyle.style)
helper.apply()
if (chainStyle.bias != null) {
state.constraints(elements[0].id).horizontalBias(chainStyle.bias)
}
}
updateHelpersHashCode(16)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(chainStyle.hashCode())
return HorizontalChainReference(id)
}
/**
* Creates a vertical chain including the referenced layouts.
*
* Use [constrain] with the resulting [VerticalChainReference] to modify the top and
* bottom constraints of this chain.
*/
// TODO(popam, b/157783937): this API should be improved
fun createVerticalChain(
vararg elements: LayoutReference,
chainStyle: ChainStyle = ChainStyle.Spread
): VerticalChainReference {
val id = createHelperId()
tasks.add { state ->
val helper = state.helper(
id,
androidx.constraintlayout.core.state.State.Helper.VERTICAL_CHAIN
) as androidx.constraintlayout.core.state.helpers.VerticalChainReference
helper.add(*(elements.map { it.id }.toTypedArray()))
helper.style(chainStyle.style)
helper.apply()
if (chainStyle.bias != null) {
state.constraints(elements[0].id).verticalBias(chainStyle.bias)
}
}
updateHelpersHashCode(17)
elements.forEach { updateHelpersHashCode(it.hashCode()) }
updateHelpersHashCode(chainStyle.hashCode())
return VerticalChainReference(id)
}
}
/**
* Represents a [ConstraintLayout] item that requires a unique identifier. Typically a layout or a
* helper such as barriers, guidelines or chains.
*/
@Stable
abstract class LayoutReference internal constructor(internal open val id: Any) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as LayoutReference
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
}
/**
* Basic implementation of [LayoutReference], used as fallback for items that don't fit other
* implementations of [LayoutReference], such as [ConstrainedLayoutReference].
*/
@Stable
internal class LayoutReferenceImpl internal constructor(id: Any) : LayoutReference(id)
/**
* Represents a layout within a [ConstraintLayout].
*
* This is a [LayoutReference] that may be constrained to other elements.
*/
@Stable
class ConstrainedLayoutReference(override val id: Any) : LayoutReference(id) {
/**
* The start anchor of this layout. Represents left in LTR layout direction, or right in RTL.
*/
@Stable
val start = ConstraintLayoutBaseScope.VerticalAnchor(id, -2, this)
/**
* The left anchor of this layout.
*/
@Stable
val absoluteLeft = ConstraintLayoutBaseScope.VerticalAnchor(id, 0, this)
/**
* The top anchor of this layout.
*/
@Stable
val top = ConstraintLayoutBaseScope.HorizontalAnchor(id, 0, this)
/**
* The end anchor of this layout. Represents right in LTR layout direction, or left in RTL.
*/
@Stable
val end = ConstraintLayoutBaseScope.VerticalAnchor(id, -1, this)
/**
* The right anchor of this layout.
*/
@Stable
val absoluteRight = ConstraintLayoutBaseScope.VerticalAnchor(id, 1, this)
/**
* The bottom anchor of this layout.
*/
@Stable
val bottom = ConstraintLayoutBaseScope.HorizontalAnchor(id, 1, this)
/**
* The baseline anchor of this layout.
*/
@Stable
val baseline = ConstraintLayoutBaseScope.BaselineAnchor(id, this)
}
/**
* Represents a horizontal chain within a [ConstraintLayout].
*
* The anchors correspond to the first and last elements in the chain.
*/
@Stable
class HorizontalChainReference internal constructor(id: Any) : LayoutReference(id) {
/**
* The start anchor of the first element in the chain.
*
* Represents left in LTR layout direction, or right in RTL.
*/
@Stable
val start = ConstraintLayoutBaseScope.VerticalAnchor(id, -2, this)
/**
* The left anchor of the first element in the chain.
*/
@Stable
val absoluteLeft = ConstraintLayoutBaseScope.VerticalAnchor(id, 0, this)
/**
* The end anchor of the last element in the chain.
*
* Represents right in LTR layout direction, or left in RTL.
*/
@Stable
val end = ConstraintLayoutBaseScope.VerticalAnchor(id, -1, this)
/**
* The right anchor of the last element in the chain.
*/
@Stable
val absoluteRight = ConstraintLayoutBaseScope.VerticalAnchor(id, 1, this)
}
/**
* Represents a vertical chain within a [ConstraintLayout].
*
* The anchors correspond to the first and last elements in the chain.
*/
@Stable
class VerticalChainReference internal constructor(id: Any) : LayoutReference(id) {
/**
* The top anchor of the first element in the chain.
*/
@Stable
val top = ConstraintLayoutBaseScope.HorizontalAnchor(id, 0, this)
/**
* The bottom anchor of the last element in the chain.
*/
@Stable
val bottom = ConstraintLayoutBaseScope.HorizontalAnchor(id, 1, this)
}
/**
* The style of a horizontal or vertical chain.
*/
@Immutable
class ChainStyle internal constructor(
internal val style: SolverChain,
internal val bias: Float? = null
) {
companion object {
/**
* A chain style that evenly distributes the contained layouts.
*/
@Stable
val Spread = ChainStyle(SolverChain.SPREAD)
/**
* A chain style where the first and last layouts are affixed to the constraints
* on each end of the chain and the rest are evenly distributed.
*/
@Stable
val SpreadInside = ChainStyle(SolverChain.SPREAD_INSIDE)
/**
* A chain style where the contained layouts are packed together and placed to the
* center of the available space.
*/
@Stable
val Packed = Packed(0.5f)
/**
* A chain style where the contained layouts are packed together and placed in
* the available space according to a given [bias].
*/
@Stable
fun Packed(bias: Float) = ChainStyle(SolverChain.PACKED, bias)
}
}
/**
* The overall visibility of a widget in a [ConstraintLayout].
*/
@Immutable
class Visibility internal constructor(
internal val solverValue: Int
) {
companion object {
/**
* Indicates that the widget will be painted in the [ConstraintLayout]. All render-time
* transforms will apply normally.
*/
@Stable
val Visible = Visibility(ConstraintWidget.VISIBLE)
/**
* The widget will not be painted in the [ConstraintLayout] but its dimensions and constraints
* will still apply.
*
* Equivalent to forcing the alpha to 0.0.
*/
@Stable
val Invisible = Visibility(ConstraintWidget.INVISIBLE)
/**
* Like [Invisible], but the dimensions of the widget will collapse to (0,0), the
* constraints will still apply.
*/
@Stable
val Gone = Visibility(ConstraintWidget.GONE)
}
}
/**
* Wrap defines the type of chain
*/
@Immutable
class Wrap internal constructor(
internal val mode: Int
) {
companion object {
val None =
Wrap(androidx.constraintlayout.core.widgets.Flow.WRAP_NONE)
val Chain =
Wrap(androidx.constraintlayout.core.widgets.Flow.WRAP_CHAIN)
val Aligned =
Wrap(androidx.constraintlayout.core.widgets.Flow.WRAP_ALIGNED)
}
}
/**
* Defines how objects align vertically within the chain
*/
@Immutable
class VerticalAlign internal constructor(
internal val mode: Int
) {
companion object {
val Top = VerticalAlign(androidx.constraintlayout.core.widgets.Flow.VERTICAL_ALIGN_TOP)
val Bottom =
VerticalAlign(androidx.constraintlayout.core.widgets.Flow.VERTICAL_ALIGN_BOTTOM)
val Center =
VerticalAlign(androidx.constraintlayout.core.widgets.Flow.VERTICAL_ALIGN_CENTER)
val Baseline =
VerticalAlign(androidx.constraintlayout.core.widgets.Flow.VERTICAL_ALIGN_BASELINE)
}
}
/**
* Defines how objects align horizontally in the chain
*/
@Immutable
class HorizontalAlign internal constructor(
internal val mode: Int
) {
companion object {
val Start =
HorizontalAlign(androidx.constraintlayout.core.widgets.Flow.HORIZONTAL_ALIGN_START)
val End = HorizontalAlign(androidx.constraintlayout.core.widgets.Flow.HORIZONTAL_ALIGN_END)
val Center =
HorizontalAlign(androidx.constraintlayout.core.widgets.Flow.HORIZONTAL_ALIGN_CENTER)
}
}
/**
* Defines how widgets are spaced in a chain
*/
@Immutable
class FlowStyle internal constructor(
internal val style: Int
) {
companion object {
val Spread = FlowStyle(0)
val SpreadInside = FlowStyle(1)
val Packed = FlowStyle(2)
}
}