Skip to content

Commit af6383c

Browse files
committed
library: Optimize Checkbox drawPath
1 parent bdab0b9 commit af6383c

File tree

1 file changed

+19
-31
lines changed
  • miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic

1 file changed

+19
-31
lines changed

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

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fun Checkbox(
106106

107107
val checkAlpha = remember { Animatable(if (isChecked) 1f else 0f) }
108108
val checkStartTrim = remember { Animatable(0.0f) }
109-
val checkEndTrim = remember { Animatable(if (isChecked) 0.845f else 0.1f) }
109+
val checkEndTrim = remember { Animatable(if (isChecked) 0.803f else 0.1f) }
110110

111111
LaunchedEffect(isChecked) {
112112
if (isChecked) {
@@ -121,20 +121,20 @@ fun Checkbox(
121121
}
122122
launch {
123123
checkStartTrim.animateTo(
124-
targetValue = 0.128f,
124+
targetValue = 0.186f,
125125
animationSpec = keyframes {
126126
durationMillis = 100
127-
0.128f at 100
127+
0.186f at 100
128128
}
129129
)
130130
}
131131
launch {
132132
checkEndTrim.animateTo(
133-
targetValue = 0.845f,
133+
targetValue = 0.803f,
134134
animationSpec = keyframes {
135135
durationMillis = 300
136-
0.885f at 200
137-
0.845f at 300
136+
0.845f at 200
137+
0.803f at 300
138138
}
139139
)
140140
}
@@ -190,7 +190,7 @@ fun Checkbox(
190190

191191
Box(
192192
modifier = modifier
193-
.size(24.5.dp)
193+
.size(25.5.dp)
194194
.scale(scale)
195195
.clip(CircleShape)
196196
.background(backgroundColor)
@@ -214,7 +214,7 @@ private fun DrawScope.drawTrimmedCheckmark(
214214
trimStart: Float,
215215
trimEnd: Float
216216
) {
217-
val viewportSize = 25.5f
217+
val viewportSize = 23f
218218
val strokeWidth = size.width * 0.09f
219219

220220
val centerX = size.width / 2
@@ -223,16 +223,16 @@ private fun DrawScope.drawTrimmedCheckmark(
223223
val viewportCenterY = viewportSize / 2
224224

225225
val leftPoint = Offset(
226-
centerX + ((6.8f - viewportCenterX) / viewportSize * size.width),
227-
centerY + ((11.6f - viewportCenterY) / viewportSize * size.height)
226+
centerX + ((5f - viewportCenterX) / viewportSize * size.width),
227+
centerY + ((9.4f - viewportCenterY) / viewportSize * size.height)
228228
)
229229
val middlePoint = Offset(
230-
centerX + ((11.4f - viewportCenterX) / viewportSize * size.width),
231-
centerY + ((16.4f - viewportCenterY) / viewportSize * size.height)
230+
centerX + ((10.3f - viewportCenterX) / viewportSize * size.width),
231+
centerY + ((14.9f - viewportCenterY) / viewportSize * size.height)
232232
)
233233
val rightPoint = Offset(
234-
centerX + ((19f - viewportCenterX) / viewportSize * size.width),
235-
centerY + ((6.7f - viewportCenterY) / viewportSize * size.height)
234+
centerX + ((17.9f - viewportCenterX) / viewportSize * size.width),
235+
centerY + ((5.1f - viewportCenterY) / viewportSize * size.height)
236236
)
237237

238238
val firstSegmentLength = (middlePoint - leftPoint).getDistance()
@@ -258,11 +258,7 @@ private fun DrawScope.drawTrimmedCheckmark(
258258
)
259259

260260
path.moveTo(start.x, start.y)
261-
val controlPoint = Offset(
262-
(start.x + end.x) / 2,
263-
(start.y + end.y) / 2 + (end.y - start.y) * 0.05f
264-
)
265-
path.quadraticTo(controlPoint.x, controlPoint.y, end.x, end.y)
261+
path.lineTo(end.x, end.y)
266262
}
267263

268264
if (endDistance > firstSegmentLength) {
@@ -279,20 +275,11 @@ private fun DrawScope.drawTrimmedCheckmark(
279275
)
280276

281277
if (startDistance < firstSegmentLength) {
282-
val controlPoint = Offset(
283-
(middlePoint.x + start.x) / 2,
284-
(middlePoint.y + start.y) / 2
285-
)
286-
path.quadraticTo(controlPoint.x, controlPoint.y, start.x, start.y)
278+
path.lineTo(end.x, end.y)
287279
} else {
288280
path.moveTo(start.x, start.y)
289281
}
290-
291-
val controlPoint = Offset(
292-
(start.x + end.x) / 2,
293-
(start.y + end.y) / 2 - (end.y - start.y) * 0.05f
294-
)
295-
path.quadraticTo(controlPoint.x, controlPoint.y, end.x, end.y)
282+
path.lineTo(end.x, end.y)
296283
}
297284

298285
drawPath(
@@ -301,7 +288,8 @@ private fun DrawScope.drawTrimmedCheckmark(
301288
style = Stroke(
302289
width = strokeWidth,
303290
cap = StrokeCap.Round,
304-
join = StrokeJoin.Round
291+
join = StrokeJoin.Round,
292+
miter = 10.0f,
305293
)
306294
)
307295
}

0 commit comments

Comments
 (0)