Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit d932b4b

Browse files
authored
Various improvements to the JSON syntax (#246)
- add support for variables, generators, lists - add support for macros - add support for variable overrides - added ScreenExample12
1 parent 55204ae commit d932b4b

File tree

7 files changed

+338
-49
lines changed

7 files changed

+338
-49
lines changed

constraintlayout/compose/src/main/java/androidx/constraintlayout/compose/ConstraintLayout.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ internal fun rememberConstraintLayoutMeasurePolicy(
117117
}
118118
}
119119
}
120+
121+
override fun override(name: String, value: Float) : ConstraintSet {
122+
// nothing here yet
123+
return this
124+
}
120125
}
121126
val layoutSize = measurer.performMeasure(
122127
constraints,
@@ -1231,14 +1236,32 @@ interface ConstraintSet {
12311236
* Applies the [ConstraintSet] to a state.
12321237
*/
12331238
fun applyTo(state: State, measurables: List<Measurable>)
1239+
1240+
fun override(name: String, value: Float) : ConstraintSet
12341241
}
12351242

12361243
fun ConstraintSet(@Language("json5") content : String) = object : ConstraintSet {
1244+
private val overridedVariables = HashMap<String, Float>()
1245+
12371246
override fun applyTo(state: State, measurables: List<Measurable>) {
12381247
measurables.forEach { measurable ->
1239-
state.map((measurable.layoutId ?: createId()), measurable)
1248+
var layoutId = measurable.layoutId ?: measurable.constraintLayoutId ?: createId()
1249+
state.map(layoutId, measurable)
1250+
var tag = measurable.constraintLayoutTag
1251+
if (tag != null && tag is String && layoutId is String) {
1252+
state.setTag(layoutId, tag)
1253+
}
12401254
}
1241-
parseJSON(content, state)
1255+
val layoutVariables = LayoutVariables()
1256+
for (name in overridedVariables.keys) {
1257+
layoutVariables.putOverride(name, overridedVariables[name]!!)
1258+
}
1259+
parseJSON(content, state, layoutVariables)
1260+
}
1261+
1262+
override fun override(name: String, value: Float) : ConstraintSet {
1263+
overridedVariables[name] = value
1264+
return this
12421265
}
12431266
}
12441267

@@ -1254,6 +1277,11 @@ fun ConstraintSet(description: ConstraintSetScope.() -> Unit) = object : Constra
12541277
scope.description()
12551278
scope.applyTo(state)
12561279
}
1280+
1281+
override fun override(name: String, value: Float) : ConstraintSet {
1282+
// nothing yet
1283+
return this
1284+
}
12571285
}
12581286

12591287
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.constraintlayout.compose
18+
19+
import androidx.compose.runtime.Immutable
20+
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.layout.Measurable
22+
import androidx.compose.ui.layout.ParentDataModifier
23+
import androidx.compose.ui.layout.layoutId
24+
import androidx.compose.ui.platform.InspectorInfo
25+
import androidx.compose.ui.platform.InspectorValueInfo
26+
import androidx.compose.ui.platform.debugInspectorInfo
27+
import androidx.compose.ui.unit.Density
28+
29+
fun Modifier.layoutId(layoutId: String, tag: String) = this.then(
30+
ConstraintLayoutTag(
31+
constraintLayoutId = layoutId,
32+
constraintLayoutTag = tag,
33+
inspectorInfo = debugInspectorInfo {
34+
name = "constraintLayoutId"
35+
value = layoutId
36+
}
37+
)
38+
)
39+
40+
@Immutable
41+
private class ConstraintLayoutTag(
42+
override val constraintLayoutTag: String,
43+
override val constraintLayoutId: String,
44+
inspectorInfo: InspectorInfo.() -> Unit
45+
) : ParentDataModifier, ConstraintLayoutTagParentData, InspectorValueInfo(inspectorInfo) {
46+
override fun Density.modifyParentData(parentData: Any?): Any? {
47+
return this@ConstraintLayoutTag
48+
}
49+
50+
override fun hashCode(): Int =
51+
constraintLayoutTag.hashCode()
52+
53+
override fun equals(other: Any?): Boolean {
54+
if (this === other) return true
55+
val otherModifier = other as? ConstraintLayoutTag ?: return false
56+
return constraintLayoutTag == otherModifier.constraintLayoutTag
57+
}
58+
59+
override fun toString(): String =
60+
"ConstraintLayoutTag(id=$constraintLayoutTag)"
61+
}
62+
63+
interface ConstraintLayoutTagParentData {
64+
val constraintLayoutId: String
65+
val constraintLayoutTag: String
66+
}
67+
68+
val Measurable.constraintLayoutTag: Any?
69+
get() = (parentData as? ConstraintLayoutTagParentData)?.constraintLayoutTag
70+
71+
val Measurable.constraintLayoutId: Any?
72+
get() = (parentData as? ConstraintLayoutTagParentData)?.constraintLayoutId

0 commit comments

Comments
 (0)