@@ -22,29 +22,29 @@ import android.os.Looper
22
22
import android.util.Log
23
23
import androidx.annotation.FloatRange
24
24
import androidx.compose.foundation.Canvas
25
- // import androidx.compose.foundation.Image
25
+ import androidx.compose.foundation.Image
26
+ import androidx.compose.foundation.background
26
27
import androidx.compose.foundation.layout.Box
27
28
import androidx.compose.foundation.layout.BoxScope
28
29
import androidx.compose.foundation.layout.LayoutScopeMarker
29
- // import androidx.compose.material.Button
30
- // import androidx.compose.material.Text
30
+ import androidx.compose.foundation.layout.padding
31
+ import androidx.compose.foundation.shape.RoundedCornerShape
32
+ import androidx.compose.foundation.text.BasicText
33
+ import androidx.compose.foundation.text.BasicTextField
31
34
import androidx.compose.runtime.*
32
35
import androidx.compose.runtime.snapshots.SnapshotStateObserver
33
36
import androidx.compose.ui.Modifier
37
+ import androidx.compose.ui.draw.clip
34
38
import androidx.compose.ui.draw.scale
35
39
import androidx.compose.ui.geometry.Offset
36
40
import androidx.compose.ui.graphics.*
37
41
import androidx.compose.ui.layout.*
38
42
import androidx.compose.ui.platform.InspectorValueInfo
39
43
import androidx.compose.ui.platform.debugInspectorInfo
44
+ import androidx.compose.ui.res.painterResource
40
45
import androidx.compose.ui.semantics.semantics
41
- import androidx.compose.ui.unit.Constraints
42
- import androidx.compose.ui.unit.Density
43
- import androidx.compose.ui.unit.Dp
44
- import androidx.compose.ui.unit.IntOffset
45
- import androidx.compose.ui.unit.IntSize
46
- import androidx.compose.ui.unit.LayoutDirection
47
- import androidx.compose.ui.unit.dp
46
+ import androidx.compose.ui.text.TextStyle
47
+ import androidx.compose.ui.unit.*
48
48
import androidx.compose.ui.util.fastForEach
49
49
import androidx.compose.ui.util.fastForEachIndexed
50
50
import androidx.constraintlayout.core.parser.CLKey
@@ -63,7 +63,6 @@ import androidx.constraintlayout.core.widgets.ConstraintWidget.DimensionBehaviou
63
63
import androidx.constraintlayout.core.widgets.analyzer.BasicMeasure
64
64
import androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.Measure.TRY_GIVEN_DIMENSIONS
65
65
import androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.Measure.USE_GIVEN_DIMENSIONS
66
- // import com.google.accompanist.coil.rememberCoilPainter
67
66
import org.intellij.lang.annotations.Language
68
67
import java.lang.StringBuilder
69
68
import java.util.*
@@ -1567,7 +1566,7 @@ open class EditableJSONLayout(@Language("json5") content: String) :
1567
1566
}
1568
1567
}
1569
1568
1570
- data class DesignElement (var id : String , var type : String , var param : String )
1569
+ data class DesignElement (var id : String , var type : String , var params : HashMap < String , String > )
1571
1570
1572
1571
class JSONConstraintSet (@Language(" json5" ) content : String ,
1573
1572
@Language(" json5" ) overrideVariables : String? = null )
@@ -2183,27 +2182,53 @@ internal open class Measurer : BasicMeasure.Measurer, DesignInfoProvider {
2183
2182
fun createDesignElements () {
2184
2183
for (element in designElements) {
2185
2184
var id = element.id
2186
- when (element.type) {
2187
- /* // commenting for now until we provide hooks
2188
- "button" -> {
2189
- Button(
2190
- modifier = Modifier.layoutId(id),
2191
- onClick = {},
2192
- ) {
2193
- Text(text = element.param)
2185
+ var function = DesignElements .map[element.type]
2186
+ if (function != null ) {
2187
+ function(id, element.params)
2188
+ } else {
2189
+ when (element.type) {
2190
+ " button" -> {
2191
+ val text = element.params[" text" ] ? : " text"
2192
+ var style = TextStyle .Default
2193
+ val fontSizeString = element.params[" size" ]
2194
+ if (fontSizeString != null ) {
2195
+ val fontSize = fontSizeString.toFloat().sp
2196
+ style = TextStyle (fontSize = fontSize)
2197
+ }
2198
+ BasicText (modifier = Modifier
2199
+ .layoutId(id)
2200
+ .clip(RoundedCornerShape (20 ))
2201
+ .background(Color .LightGray )
2202
+ .padding(8 .dp),
2203
+ text = text, style = style)
2204
+ }
2205
+ " text" -> {
2206
+ val text = element.params[" text" ] ? : " text"
2207
+ var style = TextStyle .Default
2208
+ val fontSizeString = element.params[" size" ]
2209
+ if (fontSizeString != null ) {
2210
+ val fontSize = fontSizeString.toFloat().sp
2211
+ style = TextStyle (fontSize = fontSize)
2212
+ }
2213
+ BasicText (modifier = Modifier .layoutId(id),
2214
+ text = text, style = style)
2215
+ }
2216
+ " textfield" -> {
2217
+ val text = element.params[" text" ] ? : " text"
2218
+ BasicTextField (
2219
+ modifier = Modifier .layoutId(id),
2220
+ value = text,
2221
+ onValueChange = {}
2222
+ )
2223
+ }
2224
+ " image" -> {
2225
+ Image (
2226
+ modifier = Modifier .layoutId(id),
2227
+ painter = painterResource(id = android.R .drawable.ic_menu_gallery),
2228
+ contentDescription = " Placeholder Image"
2229
+ )
2194
2230
}
2195
2231
}
2196
- "text" -> {
2197
- Text(modifier = Modifier.layoutId(id),
2198
- text=element.param)
2199
- }
2200
- "image" -> {
2201
- Image(modifier = Modifier.layoutId(id),
2202
- painter = rememberCoilPainter(element.param),
2203
- contentDescription = ""
2204
- )
2205
- }
2206
- */
2207
2232
}
2208
2233
}
2209
2234
}
@@ -2215,6 +2240,13 @@ internal open class Measurer : BasicMeasure.Measurer, DesignInfoProvider {
2215
2240
}
2216
2241
}
2217
2242
2243
+ object DesignElements {
2244
+ var map = HashMap <String , @Composable (String , HashMap <String , String >) - > Unit > ()
2245
+ fun define (name : String , function : @Composable (String , HashMap <String , String >) -> Unit ) {
2246
+ map[name] = function
2247
+ }
2248
+ }
2249
+
2218
2250
internal fun buildMapping (state : State , measurables : List <Measurable >) {
2219
2251
measurables.fastForEach { measurable ->
2220
2252
val parentData = measurable.parentData as ? ConstraintLayoutParentData
0 commit comments