Skip to content

Commit 046b7ff

Browse files
committed
Implement roundedCornerOuterBorder, implement Percentage on both platforms, and update the demo
1 parent 0ec0764 commit 046b7ff

File tree

7 files changed

+53
-23
lines changed

7 files changed

+53
-23
lines changed

compose-multiplatform-common/src/commonMain/kotlin/com/huanshankeji/compose/ui/ModifierOrAttrsScope.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ expect class ModifierOrAttrsScope<out TElement : Element> {
4040
* as different orders of CSS properties do in the HTML `style` attribute.
4141
*/
4242
expect class StyleScope {
43-
fun margin(value: LengthOrPercentage)
43+
fun margin(value: Length)
4444
fun height(value: HeightOrWidth)
4545
fun width(value: HeightOrWidth)
4646

@@ -49,16 +49,11 @@ expect class StyleScope {
4949
/**
5050
* Currently inconsistent, adds inner border on desktop and Android and outer padding on web.
5151
*/
52-
fun border(width: Length, color: Color)
52+
fun platformBorder(width: Length, color: Color)
5353

5454
fun outerBorder(width: Length, color: Color)
5555

56-
// TODO
57-
/*
58-
class CornerSize
59-
60-
fun roundedCornerBorder(width: Length, color: Color, cornerRadius: CornerSize)
61-
*/
56+
fun roundedCornerOuterBorder(width: Length, color: Color, cornerRadius: LengthOrPercentage)
6257
}
6358

6459
fun StyleScope.height(value: LengthOrPercentage) =

compose-multiplatform-common/src/commonMain/kotlin/com/huanshankeji/compose/ui/unit/Sizes.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package com.huanshankeji.compose.ui.unit
33
// Percentage is only supported on JS.
44
expect sealed interface LengthOrPercentage
55
expect sealed interface Length : LengthOrPercentage
6+
expect class Percentage : LengthOrPercentage
7+
8+
expect val Int.percent: Percentage
69

710
expect class DpOrPx : Length
811

compose-multiplatform-common/src/jsMain/kotlin/com/huanshankeji/compose/ui/ModifierOrAttrsScope.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ actual class ModifierOrAttrsScope<out TElement : Element>(val attrsScope: AttrsS
2727
}
2828

2929
actual class StyleScope(val styleScope: org.jetbrains.compose.web.css.StyleScope) {
30-
actual fun margin(value: LengthOrPercentage) =
30+
actual fun margin(value: Length) =
31+
styleScope.margin(value.platformValue)
32+
33+
fun margin(value: LengthOrPercentage) =
3134
styleScope.margin(value.platformValue)
3235

3336
actual fun height(value: HeightOrWidth) =
@@ -52,9 +55,14 @@ actual class StyleScope(val styleScope: org.jetbrains.compose.web.css.StyleScope
5255
actual fun backgroundColor(color: Color) =
5356
styleScope.backgroundColor(color.platformValue)
5457

55-
actual fun border(width: Length, color: Color) =
58+
actual fun platformBorder(width: Length, color: Color) =
5659
styleScope.border(width.platformValue, LineStyle.Solid, color.platformValue)
5760

5861
actual fun outerBorder(width: Length, color: Color) =
59-
border(width, color)
62+
platformBorder(width, color)
63+
64+
actual fun roundedCornerOuterBorder(width: Length, color: Color, cornerRadius: LengthOrPercentage) {
65+
outerBorder(width, color)
66+
styleScope.borderRadius(cornerRadius.platformValue)
67+
}
6068
}

compose-multiplatform-common/src/jsMain/kotlin/com/huanshankeji/compose/ui/unit/Sizes.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.huanshankeji.compose.ui.unit
22

33
import org.jetbrains.compose.web.css.*
4+
import org.jetbrains.compose.web.css.percent as platformPercent
45

56
actual sealed interface LengthOrPercentage {
67
val platformValue: CSSLengthOrPercentageValue
@@ -14,9 +15,9 @@ actual sealed interface Length : LengthOrPercentage {
1415
class Impl(override val platformValue: CSSLengthValue) : Length
1516
}
1617

17-
// JS only
18-
class Percentage(override val platformValue: CSSPercentageValue) : LengthOrPercentage
18+
actual class Percentage(override val platformValue: CSSPercentageValue) : LengthOrPercentage
1919

20+
actual val Int.percent get() = Percentage(this.platformPercent)
2021

2122
actual class DpOrPx(override val platformValue: CSSpxValue) : Length
2223

compose-multiplatform-common/src/jvmMain/kotlin/com/huanshankeji/compose/ui/ModifierOrAttrsScopeJvm.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package com.huanshankeji.compose.ui
33
import androidx.compose.foundation.background
44
import androidx.compose.foundation.border
55
import androidx.compose.foundation.layout.*
6+
import androidx.compose.foundation.shape.RoundedCornerShape
67
import androidx.compose.ui.Modifier
78
import com.huanshankeji.compose.ui.color.Color
8-
import com.huanshankeji.compose.ui.unit.HeightOrWidth
9+
import com.huanshankeji.compose.ui.unit.*
910
import com.huanshankeji.compose.ui.unit.HeightOrWidth.*
10-
import com.huanshankeji.compose.ui.unit.Length
11-
import com.huanshankeji.compose.ui.unit.LengthOrPercentage
12-
import com.huanshankeji.compose.ui.unit.asDpOrPx
1311

1412
fun <TElement : Element> ModifierOrAttrs<TElement>.toModifier(): Modifier =
1513
this?.let {
@@ -40,7 +38,7 @@ actual class StyleScope(val modifierOrAttrsScope: ModifierOrAttrsScope<*>) {
4038
fun modify(block: Modifier.() -> Modifier) =
4139
modifierOrAttrsScope.modify(block)
4240

43-
actual fun margin(value: LengthOrPercentage) = modify {
41+
actual fun margin(value: Length) = modify {
4442
padding(value.asDpOrPx().platformValue)
4543
}
4644

@@ -66,12 +64,26 @@ actual class StyleScope(val modifierOrAttrsScope: ModifierOrAttrsScope<*>) {
6664
background(color.platformValue)
6765
}
6866

69-
actual fun border(width: Length, color: Color) = modify {
67+
actual fun platformBorder(width: Length, color: Color) = modify {
7068
border(width.asDpOrPx().platformValue, color.platformValue)
7169
}
7270

7371
actual fun outerBorder(width: Length, color: Color) {
74-
border(width, color)
72+
platformBorder(width, color)
73+
margin(width)
74+
}
75+
76+
actual fun roundedCornerOuterBorder(width: Length, color: Color, cornerRadius: LengthOrPercentage) {
77+
modify {
78+
border(
79+
width.asDpOrPx().platformValue, color.platformValue, when (cornerRadius) {
80+
is DpOrPx -> RoundedCornerShape(cornerRadius.platformValue)
81+
is Percentage -> RoundedCornerShape(cornerRadius.value)
82+
// TODO
83+
else -> throw AssertionError()
84+
}
85+
)
86+
}
7587
margin(width)
7688
}
7789
}

compose-multiplatform-common/src/jvmMain/kotlin/com/huanshankeji/compose/ui/unit/Sizes.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@ import androidx.compose.ui.unit.dp
55

66
actual sealed interface LengthOrPercentage
77
actual sealed interface Length : LengthOrPercentage
8+
actual typealias Percentage = PercentageImpl
9+
10+
class PercentageImpl(val value: Int) : LengthOrPercentage {
11+
init {
12+
require(value in 0..100)
13+
}
14+
}
15+
16+
actual val Int.percent get() = Percentage(this)
817

918
actual class DpOrPx(val platformValue: Dp) : Length
1019

11-
fun Length.asDpOrPx() : DpOrPx =
20+
fun Length.asDpOrPx(): DpOrPx =
1221
when (this) {
1322
is DpOrPx -> this
1423
// TODO: this else branch is not needed but marked as an error by the IDE plugin
1524
else -> throw AssertionError()
1625
}
17-
fun LengthOrPercentage.asDpOrPx() : DpOrPx =
26+
27+
fun LengthOrPercentage.asDpOrPx(): DpOrPx =
1828
when (this) {
1929
is DpOrPx -> this
2030
// TODO

demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo/App.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ fun App() {
8282

8383
Row({
8484
style {
85+
// The order of function calls can't be changed!
86+
roundedCornerOuterBorder(4.dpOrPx, Colors.blue, 16.dpOrPx)
8587
backgroundColor(rgbColor(0U, 0x80U, 0x00U))
86-
outerBorder(4.dpOrPx, Colors.blue)
8788
}
8889
}) {
8990
@Composable

0 commit comments

Comments
 (0)