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

Commit 0d6f6fd

Browse files
committed
Add surface component
1 parent 11f0ff2 commit 0d6f6fd

File tree

11 files changed

+100
-0
lines changed

11 files changed

+100
-0
lines changed

componentbox/src/androidMain/kotlin/com/dropbox/componentbox/material/inflate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ actual fun Component.Inflate(context: Context?) {
2828
is Component.Switch -> TODO()
2929
is Component.Text -> Inflate(context)
3030
is Component.Vector -> TODO()
31+
is Component.Surface -> Inflate(context)
3132
}
3233
}
3334

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.dropbox.componentbox.material.surface
2+
3+
import androidx.compose.foundation.isSystemInDarkTheme
4+
import androidx.compose.material.MaterialTheme
5+
import androidx.compose.material.Surface
6+
import androidx.compose.runtime.Composable
7+
import androidx.compose.ui.graphics.Color
8+
import androidx.compose.ui.unit.dp
9+
import com.dropbox.componentbox.foundation.Component
10+
import com.dropbox.componentbox.foundation.Context
11+
import com.dropbox.componentbox.material.Inflate
12+
import com.dropbox.componentbox.util.build
13+
import com.dropbox.componentbox.util.translate
14+
15+
@Composable
16+
fun Component.Surface.Inflate(context: Context?) {
17+
val isDark = isSystemInDarkTheme() || !MaterialTheme.colors.isLight
18+
19+
val color = if (this.color?.dark != null && this.color?.light != null) {
20+
if (isDark) Color(this.color!!.dark) else Color(this.color!!.light)
21+
} else {
22+
MaterialTheme.colors.surface
23+
}
24+
25+
val contentColor = if (this.contentColor?.dark != null && this.contentColor?.light != null) {
26+
if (isDark) Color(this.contentColor!!.dark) else Color(this.contentColor!!.light)
27+
} else {
28+
MaterialTheme.colors.onSurface
29+
}
30+
31+
val shape = this.shape.translate()
32+
val elevation = this.elevation?.dp ?: 0.dp
33+
34+
Surface(
35+
modifier = modifier.build(),
36+
shape = shape,
37+
color = color,
38+
contentColor = contentColor,
39+
elevation = elevation
40+
) {
41+
components?.forEach { component ->
42+
component.Inflate(context)
43+
}
44+
}
45+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.dropbox.componentbox.util
2+
3+
import androidx.compose.foundation.shape.CircleShape
4+
import androidx.compose.foundation.shape.RoundedCornerShape
5+
import androidx.compose.ui.graphics.RectangleShape
6+
import androidx.compose.ui.unit.Dp
7+
import androidx.compose.ui.unit.dp
8+
import com.dropbox.componentbox.foundation.Shape
9+
10+
fun Shape?.translate(size: Dp = 0.dp) = when (this) {
11+
Shape.RectangleShape -> RectangleShape
12+
Shape.RoundedCornerShape -> RoundedCornerShape(size)
13+
Shape.CircleShape -> CircleShape
14+
null -> RoundedCornerShape(size)
15+
}

componentbox/src/androidMain/kotlin/com/dropbox/componentbox/util/translate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.dropbox.componentbox.util
22

33
import androidx.compose.material.MaterialTheme
44
import androidx.compose.runtime.Composable
5+
import androidx.compose.ui.graphics.Color
56
import androidx.compose.ui.text.TextStyle
67

78
@Composable
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.dropbox.componentbox.foundation
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class BorderStroke(
7+
val width: Int,
8+
val color: Color
9+
)

componentbox/src/commonMain/kotlin/com/dropbox/componentbox/foundation/Component.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,17 @@ sealed class Component {
8585
var contentScale: ContentScale? = null,
8686
var color: String? = null
8787
) : Component()
88+
89+
@Serializable
90+
data class Surface(
91+
val id: String,
92+
var modifier: Modifier? = null,
93+
var shape: Shape? = null,
94+
var color: Color? = null,
95+
var contentColor: Color? = null,
96+
var borderStroke: BorderStroke? = null,
97+
var elevation: Int? = null,
98+
var components: MutableList<Component>? = null,
99+
) : Component()
88100
}
89101

componentbox/src/commonMain/kotlin/com/dropbox/componentbox/foundation/ComponentType.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ enum class ComponentType {
1515
Table,
1616
Text,
1717
Vector,
18+
Surface
1819
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.dropbox.componentbox.foundation
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
enum class Shape {
7+
RectangleShape,
8+
RoundedCornerShape,
9+
CircleShape
10+
}

componentbox/src/commonMain/kotlin/com/dropbox/componentbox/foundation/color.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.dropbox.componentbox.foundation
22

3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
36
data class Color(
47
val title: String,
58
val light: Long,

desktop/src/jvmMain/kotlin/com/dropbox/componentbox/store/reducers/component/helpers/buildComponent.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ internal fun ComponentType.build(id: String): Component {
1717
ComponentType.Table -> Component.Column(id, isLazy = true, isTable = true, modifier = Modifier())
1818
ComponentType.Text -> Component.Text(id, modifier = Modifier())
1919
ComponentType.Vector -> Component.Vector(id, modifier = Modifier())
20+
ComponentType.Surface -> Component.Surface(id, modifier = Modifier())
2021
}
2122
}

0 commit comments

Comments
 (0)