Skip to content

Commit aed49cc

Browse files
committed
library: Commonize Path.Companion.smoothRoundedRectangle
1 parent 0c27ce5 commit aed49cc

File tree

8 files changed

+50
-282
lines changed

8 files changed

+50
-282
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ android-targetSdk = "35"
66
androidGradlePlugin = "8.8.0"
77
androidx-activity-compose = "1.10.0"
88
androidx-window = "1.3.0"
9+
androidx-graphics-shapes = "1.0.0-alpha03"
910
compose-plugin = "1.8.0-alpha03"
1011
dokka = "2.0.0"
1112
kotlin = "2.1.10"
1213

1314
[libraries]
1415
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" }
15-
androidx-graphics-shapes = { group = "androidx.graphics", name = "graphics-shapes", version = "1.0.1" }
16-
androidx-graphics-shapes-web = { module = "org.jetbrains.androidx.graphics:graphics-shapes", version = "1.0.0-alpha03" }
16+
androidx-graphics-shapes = { module = "org.jetbrains.androidx.graphics:graphics-shapes", version.ref = "androidx-graphics-shapes" }
1717
androidx-window = { group = "androidx.window", name = "window", version.ref = "androidx-window" }
1818
compose-window-size = { module = "org.jetbrains.compose.material3:material3-window-size-class", version.ref = "compose-plugin" }
1919

miuix/build.gradle.kts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,17 @@ kotlin {
3939
}
4040

4141
sourceSets {
42-
val desktopMain by getting
43-
4442
androidMain.dependencies {
4543
implementation(libs.androidx.activity.compose)
46-
implementation(libs.androidx.graphics.shapes)
4744
implementation(libs.androidx.window)
4845
}
4946
commonMain.dependencies {
5047
implementation(compose.runtime)
5148
implementation(compose.foundation)
5249
implementation(compose.ui)
5350
implementation(compose.components.resources)
54-
implementation(libs.compose.window.size)
55-
}
56-
nativeMain.dependencies {
5751
implementation(libs.androidx.graphics.shapes)
58-
}
59-
desktopMain.dependencies {
60-
implementation(libs.androidx.graphics.shapes)
61-
}
62-
jsMain.dependencies {
63-
implementation(libs.androidx.graphics.shapes.web)
64-
}
65-
wasmJsMain.dependencies {
66-
implementation(libs.androidx.graphics.shapes.web)
52+
implementation(libs.compose.window.size)
6753
}
6854
}
6955
}

miuix/src/androidMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.android.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.kt

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,35 @@ import androidx.compose.ui.graphics.Outline
77
import androidx.compose.ui.graphics.Path
88
import androidx.compose.ui.unit.Dp
99
import androidx.compose.ui.unit.LayoutDirection
10+
import androidx.graphics.shapes.CornerRounding
11+
import androidx.graphics.shapes.Cubic
12+
import androidx.graphics.shapes.RoundedPolygon
1013

11-
expect fun Path.Companion.smoothRoundedRectangle(
14+
fun Path.Companion.smoothRoundedRectangle(
1215
smoothing: Float,
1316
size: Size,
1417
topLeft: Float,
1518
topRight: Float,
1619
bottomLeft: Float,
17-
bottomRight: Float,
18-
): Path
20+
bottomRight: Float
21+
): Path {
22+
if (size.width <= 0f || size.height <= 0f) return Path()
23+
24+
return RoundedPolygon(
25+
vertices = floatArrayOf(
26+
0f, 0f,
27+
size.width, 0f,
28+
size.width, size.height,
29+
0f, size.height
30+
),
31+
perVertexRounding = listOf(
32+
CornerRounding(radius = topLeft, smoothing = smoothing),
33+
CornerRounding(radius = topRight, smoothing = smoothing),
34+
CornerRounding(radius = bottomRight, smoothing = smoothing),
35+
CornerRounding(radius = bottomLeft, smoothing = smoothing),
36+
)
37+
).toComposePath()
38+
}
1939

2040
fun SmoothRoundedCornerShape(
2141
corner: Dp,
@@ -92,5 +112,29 @@ class SmoothRoundedCornerShape(
92112
}
93113
}
94114

115+
fun RoundedPolygon.toComposePath(path: Path = Path()): Path {
116+
pathFromCubicList(path, cubics)
117+
return path
118+
}
119+
120+
private fun pathFromCubicList(
121+
path: Path,
122+
cubicList: List<Cubic>
123+
) {
124+
var first = true
125+
path.rewind()
126+
for (element in cubicList) {
127+
if (first) {
128+
path.moveTo(element.anchor0X, element.anchor0Y)
129+
first = false
130+
}
131+
path.cubicTo(
132+
element.control0X, element.control0Y, element.control1X, element.control1Y,
133+
element.anchor1X, element.anchor1Y
134+
)
135+
}
136+
path.close()
137+
}
138+
95139
const val DefaultSmoothing = 0.6f
96140

miuix/src/desktopMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.desktop.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

miuix/src/jsMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.js.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

miuix/src/nativeMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.native.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

miuix/src/wasmJsMain/kotlin/top/yukonga/miuix/kmp/utils/SmoothRoundedCornerShape.wasmJs.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)