11package com.w2sv.wifiwidget.ui.designsystem
22
3+ import androidx.annotation.FloatRange
34import androidx.compose.foundation.background
45import androidx.compose.foundation.layout.Box
56import androidx.compose.foundation.layout.BoxScope
@@ -18,7 +19,9 @@ import androidx.compose.ui.res.stringResource
1819import androidx.compose.ui.tooling.preview.Preview
1920import androidx.compose.ui.unit.Dp
2021import androidx.compose.ui.unit.dp
22+ import androidx.constraintlayout.compose.ConstrainScope
2123import androidx.constraintlayout.compose.ConstraintLayout
24+ import androidx.constraintlayout.compose.ConstraintLayoutBaseScope
2225import androidx.constraintlayout.compose.Dimension
2326import com.w2sv.core.common.R
2427import com.w2sv.wifiwidget.ui.theme.AppTheme
@@ -37,64 +40,67 @@ data class Margins(val start: Dp = 0.dp, val top: Dp = 0.dp, val end: Dp = 0.dp,
3740 * End-anchoring applied to the below slot.
3841 */
3942enum class BelowEndAnchoring {
40- LabelEnd ,
43+ CentralEnd ,
4144 ParentEnd ;
4245
4346 companion object {
44- val Default get() = LabelEnd
47+ val Default get() = CentralEnd
4548 }
4649}
4750
4851@Composable
4952fun TLayout (
50- label : BoxScopeComposable ,
53+ central : BoxScopeComposable ,
5154 modifier : Modifier = Modifier ,
5255 leading : BoxScopeComposable ? = null,
5356 trailing : BoxScopeComposable ? = null,
5457 below : BoxScopeComposable ? = null,
55- labelMargins : Margins = Margins (start = 16.dp) ,
56- belowMargins : Margins = Margins (top = 2.dp) ,
58+ centralMargins : Margins = Margins .empty ,
59+ belowMargins : Margins = Margins .empty ,
5760 belowEndAnchoring : BelowEndAnchoring = BelowEndAnchoring .Default
5861) {
5962 ConstraintLayout (modifier = modifier) {
60- val (leadingRef, labelRef , belowRef, trailingRef) = createRefs()
63+ val (leadingRef, centralRef , belowRef, trailingRef) = createRefs()
6164
6265 val hasLeading = leading != null
6366 val hasTrailing = trailing != null
64- val hasBelow = below != null
67+
68+ val topRowTop = createTopBarrier(leadingRef, centralRef, trailingRef)
69+ val topRowBottom = createBottomBarrier(leadingRef, centralRef, trailingRef)
6570
6671 leading?.let {
6772 Box (
6873 modifier = Modifier .constrainAs(leadingRef) {
6974 start.linkTo(parent.start)
70- centerVerticallyTo(labelRef )
75+ linkTo(top = topRowTop, bottom = topRowBottom )
7176 },
7277 content = it
7378 )
7479 }
7580
7681 Box (
77- modifier = Modifier .constrainAs(labelRef) {
78- linkTo(
79- top = parent.top,
80- bottom = if (hasBelow) belowRef.top else parent.bottom
81- )
82+ modifier = Modifier .constrainAs(centralRef) {
8283 linkTo(
84+ top = topRowTop,
85+ bottom = topRowBottom,
8386 start = if (hasLeading) leadingRef.end else parent.start,
8487 end = if (hasTrailing) trailingRef.start else parent.end,
85- startMargin = labelMargins.start
88+ margins = centralMargins
8689 )
8790 width = Dimension .fillToConstraints
8891 },
89- content = label
92+ content = central
9093 )
9194
9295 trailing?.let {
9396 Box (
9497 modifier = Modifier .constrainAs(trailingRef) {
95- start.linkTo(labelRef.end)
96- end.linkTo(parent.end)
97- centerVerticallyTo(labelRef)
98+ linkTo(
99+ top = topRowTop,
100+ bottom = topRowBottom,
101+ start = centralRef.end,
102+ end = parent.end
103+ )
98104 },
99105 content = it
100106 )
@@ -103,13 +109,15 @@ fun TLayout(
103109 below?.let {
104110 Box (
105111 modifier = Modifier .constrainAs(belowRef) {
106- top.linkTo(labelRef.bottom, margin = belowMargins.top)
107112 linkTo(
108- start = labelRef .start,
113+ start = centralRef .start,
109114 end = when (belowEndAnchoring) {
110- BelowEndAnchoring .LabelEnd -> labelRef .end
115+ BelowEndAnchoring .CentralEnd -> centralRef .end
111116 BelowEndAnchoring .ParentEnd -> parent.end
112- }
117+ },
118+ top = centralRef.bottom,
119+ bottom = parent.bottom,
120+ margins = belowMargins
113121 )
114122 width = Dimension .fillToConstraints
115123 },
@@ -119,14 +127,44 @@ fun TLayout(
119127 }
120128}
121129
130+ private fun ConstrainScope.linkTo (
131+ start : ConstraintLayoutBaseScope .VerticalAnchor ,
132+ top : ConstraintLayoutBaseScope .HorizontalAnchor ,
133+ end : ConstraintLayoutBaseScope .VerticalAnchor ,
134+ bottom : ConstraintLayoutBaseScope .HorizontalAnchor ,
135+ margins : Margins ,
136+ goneMargins : Margins = Margins .empty,
137+ @FloatRange(from = 0.0 , to = 1.0 ) horizontalBias : Float = 0.5f,
138+ @FloatRange(from = 0.0 , to = 1.0 ) verticalBias : Float = 0.5f
139+ ) {
140+ linkTo(
141+ start = start,
142+ end = end,
143+ startMargin = margins.start,
144+ endMargin = margins.end,
145+ startGoneMargin = goneMargins.start,
146+ endGoneMargin = goneMargins.end,
147+ bias = horizontalBias
148+ )
149+ linkTo(
150+ top = top,
151+ bottom = bottom,
152+ topMargin = margins.top,
153+ bottomMargin = margins.bottom,
154+ topGoneMargin = goneMargins.top,
155+ bottomGoneMargin = goneMargins.bottom,
156+ bias = verticalBias
157+ )
158+ }
159+
122160@Preview
123161@Composable
124162private fun Complete () {
125163 AppTheme {
126164 Surface {
127165 TLayout (
128166 modifier = Modifier .fillMaxWidth(),
129- label = {
167+ central = {
130168 Text (
131169 text = stringResource(R .string.dynamic_colors),
132170 color = MaterialTheme .colorScheme.onSurface
@@ -157,7 +195,7 @@ private fun BelowEndAnchoredToParent() {
157195 Surface {
158196 TLayout (
159197 modifier = Modifier .fillMaxWidth(),
160- label = {
198+ central = {
161199 Text (
162200 text = stringResource(R .string.dynamic_colors),
163201 color = MaterialTheme .colorScheme.onSurface
@@ -192,7 +230,7 @@ private fun WithoutTrailing() {
192230 Surface {
193231 TLayout (
194232 modifier = Modifier .fillMaxWidth(),
195- label = {
233+ central = {
196234 Text (
197235 text = stringResource(R .string.dynamic_colors),
198236 color = MaterialTheme .colorScheme.onSurface
@@ -217,7 +255,7 @@ private fun WithoutLeading() {
217255 Surface {
218256 TLayout (
219257 modifier = Modifier .fillMaxWidth(),
220- label = {
258+ central = {
221259 Text (
222260 text = stringResource(R .string.dynamic_colors),
223261 color = MaterialTheme .colorScheme.onSurface
@@ -247,7 +285,7 @@ private fun WithoutLeadingAndBelow() {
247285 Surface {
248286 TLayout (
249287 modifier = Modifier .fillMaxWidth(),
250- label = {
288+ central = {
251289 Text (
252290 text = stringResource(R .string.dynamic_colors),
253291 color = MaterialTheme .colorScheme.onSurface
0 commit comments