Skip to content

Commit 1ff5b95

Browse files
committed
library: Replace smooth rounded corners
* from https://github.com/Kyant0/Capsule
1 parent 49417e1 commit 1ff5b95

File tree

18 files changed

+771
-290
lines changed

18 files changed

+771
-290
lines changed

docs/guide/utils.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,32 +203,76 @@ The `PressFeedbackType` enum defines different types of visual feedback that can
203203
| Sink | Applies a sink effect, where the component scales down slightly when pressed |
204204
| Tilt | Applies a tilt effect, where the component tilts slightly based on the touch position |
205205

206-
## Smooth Rounded Corners (SmoothRoundedCornerShape)
206+
## Smooth Rounded Corners (G2RoundedCornerShape)
207207

208-
`SmoothRoundedCornerShape` provides smoother rounded corners compared to the standard `RoundedCornerShape`.
208+
`G2RoundedCornerShape` provides visually smoother corners than the standard `RoundedCornerShape` by blending part of the circular arc with Bézier transitions. It supports: a single uniform corner size, per-corner sizes (Dp / px / percent), preset or custom smoothness via `CornerSmoothness`, and a ready-made `CapsuleShape()` helper.
209+
210+
> Source: https://github.com/Kyant0/Capsule (Apache-2.0 License).
211+
212+
### API Overview
213+
214+
Key entry points (overloads):
215+
216+
```kotlin
217+
G2RoundedCornerShape(size: Dp, cornerSmoothness: CornerSmoothness = CornerSmoothness.Default)
218+
G2RoundedCornerShape(
219+
topStart: Dp = 0.dp,
220+
topEnd: Dp = 0.dp,
221+
bottomEnd: Dp = 0.dp,
222+
bottomStart: Dp = 0.dp,
223+
cornerSmoothness: CornerSmoothness = CornerSmoothness.Default
224+
)
225+
G2RoundedCornerShape(percent: Int, cornerSmoothness: CornerSmoothness = CornerSmoothness.Default)
226+
CapsuleShape(cornerSmoothness: CornerSmoothness = CornerSmoothness.Default)
227+
```
228+
229+
`CornerSmoothness` parameters:
230+
* `circleFraction`: 0f..1f portion of a quarter circle preserved (1f = normal rounded corner, no smoothing blend)
231+
* `extendedFraction`: how much the control points extend horizontally/vertically to create a softer capsule-like shape
232+
233+
Presets:
234+
* `CornerSmoothness.Default` – balanced smoothness (softened corners)
235+
* `CornerSmoothness.None` – equivalent to a regular rounded corner (no extra smoothing)
209236

210237
### Basic Usage
211238

212239
```kotlin
213-
Surface(
214-
shape = SmoothRoundedCornerShape(16.dp)
215-
) {
216-
// Content
240+
Surface(shape = G2RoundedCornerShape(16.dp)) {
241+
/* 内容 */
217242
}
218243
```
219244

220-
### Custom Smoothness and Different Angles
245+
### Per-Corner Sizes
221246

222247
```kotlin
223248
Surface(
224-
shape = SmoothRoundedCornerShape(
225-
smoothing = 0.8f, // Smoothness, higher values are smoother
249+
shape = G2RoundedCornerShape(
226250
topStart = 16.dp,
227251
topEnd = 16.dp,
228252
bottomStart = 8.dp,
229-
bottomEnd = 8.dp
253+
bottomEnd = 8.dp,
254+
cornerSmoothness = CornerSmoothness.Default
230255
)
231-
) {
256+
) { /* Content */ }
257+
```
258+
259+
### Capsule Shape
260+
261+
```kotlin
262+
Surface(shape = CapsuleShape()) { /* Content */ }
263+
```
264+
265+
### Custom Smoothness
266+
267+
You can craft your own smoothness (smaller `circleFraction` & higher `extendedFraction` => softer / more elongated transition):
268+
269+
```kotlin
270+
val ExtraSmooth = CornerSmoothness(
271+
circleFraction = 0.55f, // retain 55% of the arc; lower -> more smoothing
272+
extendedFraction = 0.90f // push Bézier handles further for a pill-like feel
273+
)
274+
275+
Surface(shape = G2RoundedCornerShape(24.dp, cornerSmoothness = ExtraSmooth)) {
232276
// Content
233277
}
234278
```

docs/zh_CN/guide/utils.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,32 +203,76 @@ Box(
203203
| Sink | 应用下沉效果,组件在按下时轻微缩小 |
204204
| Tilt | 应用倾斜效果,组件根据触摸位置轻微倾斜 |
205205

206-
## 平滑圆角 (SmoothRoundedCornerShape)
206+
## 平滑圆角 (G2RoundedCornerShape)
207207

208-
`SmoothRoundedCornerShape` 提供了比标准 `RoundedCornerShape` 更加平滑的圆角效果。
208+
`G2RoundedCornerShape` 通过在标准圆角圆弧与 Bézier 过渡之间混合,实现比 `RoundedCornerShape` 更柔和、视觉更自然的圆角。它支持:统一圆角值、分别设置四个角(支持 Dp / px / 百分比)、通过 `CornerSmoothness` 预设或自定义平滑度,以及快捷的 `CapsuleShape()` 胶囊形状。
209+
210+
> 来源: https://github.com/Kyant0/Capsule (Apache-2.0 License)。
211+
212+
### 核心 API
213+
214+
常用构造函数(重载):
215+
216+
```kotlin
217+
G2RoundedCornerShape(size: Dp, cornerSmoothness: CornerSmoothness = CornerSmoothness.Default)
218+
G2RoundedCornerShape(
219+
topStart: Dp = 0.dp,
220+
topEnd: Dp = 0.dp,
221+
bottomEnd: Dp = 0.dp,
222+
bottomStart: Dp = 0.dp,
223+
cornerSmoothness: CornerSmoothness = CornerSmoothness.Default
224+
)
225+
G2RoundedCornerShape(percent: Int, cornerSmoothness: CornerSmoothness = CornerSmoothness.Default)
226+
CapsuleShape(cornerSmoothness: CornerSmoothness = CornerSmoothness.Default)
227+
```
228+
229+
`CornerSmoothness` 参数说明:
230+
* `circleFraction`: 0f..1f,表示保留四分之一圆弧的比例(1f = 传统圆角,不做平滑混合)
231+
* `extendedFraction`: 控制 Bézier 控制点向外延伸的程度,越大越接近椭圆/胶囊视觉
232+
233+
预设:
234+
* `CornerSmoothness.Default` – 默认柔滑(推荐常规使用)
235+
* `CornerSmoothness.None` – 与普通圆角等效(无额外平滑)
209236

210237
### 基本使用
211238

212239
```kotlin
213-
Surface(
214-
shape = SmoothRoundedCornerShape(16.dp)
215-
) {
216-
// 内容
240+
Surface(shape = G2RoundedCornerShape(16.dp)) {
241+
/* 内容 */
217242
}
218243
```
219244

220-
### 自定义平滑程度和不同角度
245+
### 分别指定四个角
221246

222247
```kotlin
223248
Surface(
224-
shape = SmoothRoundedCornerShape(
225-
smoothing = 0.8f, // 平滑度,值越高越平滑
249+
shape = G2RoundedCornerShape(
226250
topStart = 16.dp,
227251
topEnd = 16.dp,
228252
bottomStart = 8.dp,
229-
bottomEnd = 8.dp
253+
bottomEnd = 8.dp,
254+
cornerSmoothness = CornerSmoothness.Default
230255
)
231-
) {
256+
) { /* 内容 */ }
257+
```
258+
259+
### 胶囊形状
260+
261+
```kotlin
262+
Surface(shape = CapsuleShape()) { /* 内容 */ }
263+
```
264+
265+
### 自定义平滑度
266+
267+
(降低 `circleFraction` + 提高 `extendedFraction` => 更软、更延展)
268+
269+
```kotlin
270+
val ExtraSmooth = CornerSmoothness(
271+
circleFraction = 0.55f, // 保留 55% 圆弧,越低越“圆润”
272+
extendedFraction = 0.90f // 控制点更外扩,接近胶囊
273+
)
274+
275+
Surface(shape = G2RoundedCornerShape(24.dp, cornerSmoothness = ExtraSmooth)) {
232276
// 内容
233277
}
234278
```

iosApp/iosApp/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundleShortVersionString</key>
1818
<string>1.0.4</string>
1919
<key>CFBundleVersion</key>
20-
<string>511</string>
20+
<string>512</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/Button.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import androidx.compose.foundation.layout.defaultMinSize
1111
import androidx.compose.foundation.layout.padding
1212
import androidx.compose.runtime.Composable
1313
import androidx.compose.runtime.Immutable
14-
import androidx.compose.runtime.getValue
1514
import androidx.compose.runtime.remember
16-
import androidx.compose.runtime.rememberUpdatedState
1715
import androidx.compose.ui.Alignment
1816
import androidx.compose.ui.Modifier
1917
import androidx.compose.ui.graphics.Color
@@ -23,7 +21,7 @@ import androidx.compose.ui.semantics.semantics
2321
import androidx.compose.ui.unit.Dp
2422
import androidx.compose.ui.unit.dp
2523
import top.yukonga.miuix.kmp.theme.MiuixTheme
26-
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
24+
import top.yukonga.miuix.kmp.utils.G2RoundedCornerShape
2725

2826
/**
2927
* A [Button] component with Miuix style.
@@ -50,7 +48,7 @@ fun Button(
5048
insideMargin: PaddingValues = ButtonDefaults.InsideMargin,
5149
content: @Composable RowScope.() -> Unit
5250
) {
53-
val shape = remember(cornerRadius) { SmoothRoundedCornerShape(cornerRadius) }
51+
val shape = remember(cornerRadius) { G2RoundedCornerShape(cornerRadius) }
5452
val color = if (enabled) colors.color else colors.disabledColor
5553
Surface(
5654
onClick = onClick,
@@ -95,7 +93,7 @@ fun TextButton(
9593
minHeight: Dp = ButtonDefaults.MinHeight,
9694
insideMargin: PaddingValues = ButtonDefaults.InsideMargin
9795
) {
98-
val shape = remember(cornerRadius) { SmoothRoundedCornerShape(cornerRadius) }
96+
val shape = remember(cornerRadius) { G2RoundedCornerShape(cornerRadius) }
9997
val color = if (enabled) colors.color else colors.disabledColor
10098
val textColor = if (enabled) colors.textColor else colors.disabledTextColor
10199
Surface(

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/Card.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ import androidx.compose.foundation.layout.Column
1818
import androidx.compose.foundation.layout.ColumnScope
1919
import androidx.compose.foundation.layout.PaddingValues
2020
import androidx.compose.foundation.layout.padding
21-
import androidx.compose.foundation.shape.RoundedCornerShape
2221
import androidx.compose.runtime.Composable
2322
import androidx.compose.runtime.CompositionLocalProvider
2423
import androidx.compose.runtime.Immutable
25-
import androidx.compose.runtime.getValue
2624
import androidx.compose.runtime.remember
27-
import androidx.compose.runtime.rememberUpdatedState
2825
import androidx.compose.ui.Modifier
2926
import androidx.compose.ui.draw.clip
3027
import androidx.compose.ui.graphics.Color
@@ -36,8 +33,9 @@ import androidx.compose.ui.unit.Dp
3633
import androidx.compose.ui.unit.dp
3734
import top.yukonga.miuix.kmp.theme.LocalContentColor
3835
import top.yukonga.miuix.kmp.theme.MiuixTheme
36+
import top.yukonga.miuix.kmp.utils.CornerSmoothness
37+
import top.yukonga.miuix.kmp.utils.G2RoundedCornerShape
3938
import top.yukonga.miuix.kmp.utils.PressFeedbackType
40-
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
4139
import top.yukonga.miuix.kmp.utils.pressSink
4240
import top.yukonga.miuix.kmp.utils.pressTilt
4341

@@ -165,8 +163,8 @@ private fun BasicCard(
165163
cornerRadius: Dp = CardDefaults.CornerRadius,
166164
content: @Composable () -> Unit,
167165
) {
168-
val shape = remember(cornerRadius) { SmoothRoundedCornerShape(cornerRadius) }
169-
val clipShape = remember(cornerRadius) { RoundedCornerShape(cornerRadius) }
166+
val shape = remember(cornerRadius) { G2RoundedCornerShape(cornerRadius) }
167+
val clipShape = remember(cornerRadius) { G2RoundedCornerShape(cornerRadius, CornerSmoothness.None) }
170168

171169
CompositionLocalProvider(
172170
LocalContentColor provides colors.contentColor,
@@ -176,8 +174,8 @@ private fun BasicCard(
176174
.semantics(mergeDescendants = false) {
177175
isTraversalGroup = true
178176
}
179-
.background(color = colors.color, shape = shape)
180-
.clip(clipShape), // For touch feedback, there is a problem when using SmoothRoundedCornerShape.
177+
.clip(clipShape) // For touch feedback, there is a problem when using G2RoundedCornerShape.
178+
.background(color = colors.color, shape = shape),
181179
propagateMinConstraints = true,
182180
) {
183181
content()

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/ColorPalette.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import androidx.compose.ui.unit.Dp
3636
import androidx.compose.ui.unit.IntSize
3737
import androidx.compose.ui.unit.dp
3838
import androidx.compose.ui.util.lerp
39-
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
39+
import top.yukonga.miuix.kmp.utils.CapsuleShape
40+
import top.yukonga.miuix.kmp.utils.G2RoundedCornerShape
4041
import kotlin.math.abs
4142
import kotlin.math.max
4243
import kotlin.math.min
@@ -118,7 +119,7 @@ fun ColorPalette(
118119
modifier = Modifier
119120
.fillMaxWidth()
120121
.height(26.dp)
121-
.clip(SmoothRoundedCornerShape(50.dp))
122+
.clip(CapsuleShape())
122123
.background(lastEmittedColor ?: initialColor)
123124
)
124125
}
@@ -175,7 +176,7 @@ private fun PaletteCanvas(
175176
val totalColumns = hueColumns + if (includeGrayColumn) 1 else 0
176177
val rowSV = remember(rows) { buildRowSV(rows) }
177178
val grayV = remember(rows) { buildGrayV(rows) }
178-
val shape = SmoothRoundedCornerShape(cornerRadius)
179+
val shape = G2RoundedCornerShape(cornerRadius)
179180

180181
var sizePx by remember { mutableStateOf(IntSize.Zero) }
181182

0 commit comments

Comments
 (0)