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

Commit 8f5994d

Browse files
authored
Design elements (#351)
* Add support for user-customizable design elements * Add box
1 parent d569887 commit 8f5994d

File tree

2 files changed

+44
-19
lines changed
  • constraintlayout/compose/src/main/java/androidx/constraintlayout/compose
  • projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout

2 files changed

+44
-19
lines changed

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

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,32 @@ internal open class Measurer : BasicMeasure.Measurer, DesignInfoProvider {
21782178

21792179
private var designElements = arrayListOf<DesignElement>()
21802180

2181+
2182+
private fun getColor(str: String?, defaultColor: Color = Color.Black) : Color {
2183+
if (str != null && str.startsWith('#')) {
2184+
var str2 = str.substring(1)
2185+
if(str2.length == 6) {
2186+
str2 = "FF$str2"
2187+
}
2188+
try {
2189+
return Color(java.lang.Long.parseLong(str2, 16).toInt())
2190+
} catch (e: Exception) {
2191+
return defaultColor
2192+
}
2193+
}
2194+
return defaultColor
2195+
}
2196+
2197+
private fun getTextStyle(params: HashMap<String, String>) : TextStyle {
2198+
val fontSizeString = params["size"]
2199+
var fontSize = TextUnit.Unspecified
2200+
if (fontSizeString != null) {
2201+
fontSize = fontSizeString.toFloat().sp
2202+
}
2203+
var textColor = getColor(params["color"])
2204+
return TextStyle(fontSize = fontSize, color = textColor)
2205+
}
2206+
21812207
@Composable
21822208
fun createDesignElements() {
21832209
for (element in designElements) {
@@ -2189,29 +2215,28 @@ internal open class Measurer : BasicMeasure.Measurer, DesignInfoProvider {
21892215
when (element.type) {
21902216
"button" -> {
21912217
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-
}
2218+
val colorBackground = getColor(element.params["backgroundColor"], Color.LightGray)
21982219
BasicText(modifier = Modifier
21992220
.layoutId(id)
22002221
.clip(RoundedCornerShape(20))
2201-
.background(Color.LightGray)
2222+
.background(colorBackground)
22022223
.padding(8.dp),
2203-
text = text, style = style)
2224+
text = text, style = getTextStyle(element.params))
2225+
}
2226+
"box" -> {
2227+
val text = element.params["text"] ?: ""
2228+
val colorBackground = getColor(element.params["backgroundColor"], Color.LightGray)
2229+
Box(modifier = Modifier.layoutId(id).background(colorBackground)) {
2230+
BasicText(
2231+
modifier = Modifier.padding(8.dp),
2232+
text = text, style = getTextStyle(element.params)
2233+
)
2234+
}
22042235
}
22052236
"text" -> {
22062237
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-
}
22132238
BasicText(modifier = Modifier.layoutId(id),
2214-
text = text, style = style)
2239+
text = text, style = getTextStyle(element.params))
22152240
}
22162241
"textfield" -> {
22172242
val text = element.params["text"] ?: "text"

projects/ComposeConstraintLayout/app/src/main/java/com/example/constraintlayout/MainActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import com.google.accompanist.coil.rememberCoilPainter
1818

1919
class MainActivity : AppCompatActivity() {
2020
private var mFrameLayout: FrameLayout? = null
21-
private var composeNum = 39
21+
private var composeNum = 38
2222
private var MAX = 39
2323

2424
var map = HashMap<Int, String>();
@@ -40,13 +40,13 @@ class MainActivity : AppCompatActivity() {
4040
}
4141

4242
private fun defineDesignElements() {
43-
DesignElements.define("wow") {
43+
DesignElements.define("text-material") {
4444
id, params ->
4545
val text = params["text"] ?: "text"
4646
Text(modifier = Modifier.layoutId(id),
4747
text= text)
4848
}
49-
DesignElements.define("button") {
49+
DesignElements.define("button-material") {
5050
id, params ->
5151
val text = params["text"] ?: "text"
5252
Button(modifier = Modifier.layoutId(id),
@@ -55,7 +55,7 @@ class MainActivity : AppCompatActivity() {
5555
Text(text = text)
5656
}
5757
}
58-
DesignElements.define("image") {
58+
DesignElements.define("image-coil") {
5959
id, params ->
6060
val url = params["url"] ?: "url"
6161
val description = params["description"] ?: "Image Description"

0 commit comments

Comments
 (0)