This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckbox.kt
535 lines (506 loc) · 20.9 KB
/
Checkbox.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.material3
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.snap
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.selection.triStateToggleable
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.tokens.CheckboxTokens
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathMeasure
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.lerp
import kotlin.math.floor
import kotlin.math.max
/**
* Material Design checkbox.
*
* Checkboxes allow users to select one or more items from a set. Checkboxes can turn an option on
* or off.
*
* ![Checkbox image](https://developer.android.com/images/reference/androidx/compose/material3/checkbox.png)
*
* @sample androidx.compose.material3.samples.CheckboxSample
*
* @see [TriStateCheckbox] if you require support for an indeterminate state.
*
* @param checked whether this checkbox is checked or unchecked
* @param onCheckedChange called when this checkbox is clicked. If `null`, then this checkbox will
* not be interactable, unless something else handles its input events and updates its state.
* @param modifier the [Modifier] to be applied to this checkbox
* @param enabled controls the enabled state of this checkbox. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this checkbox. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this checkbox in different states.
* @param colors [CheckboxColors] that will be used to resolve the colors used for this checkbox in
* different states. See [CheckboxDefaults.colors].
*/
@Composable
fun Checkbox(
checked: Boolean,
onCheckedChange: ((Boolean) -> Unit)?,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
colors: CheckboxColors = CheckboxDefaults.colors()
) {
TriStateCheckbox(
state = ToggleableState(checked),
onClick = if (onCheckedChange != null) {
{ onCheckedChange(!checked) }
} else null,
interactionSource = interactionSource,
enabled = enabled,
colors = colors,
modifier = modifier
)
}
/**
* Material Design checkbox parent.
*
* Checkboxes can have a parent-child relationship with other checkboxes. When the parent checkbox
* is checked, all child checkboxes are checked. If a parent checkbox is unchecked, all child
* checkboxes are unchecked. If some, but not all, child checkboxes are checked, the parent checkbox
* becomes an indeterminate checkbox.
*
* ![Checkbox image](https://developer.android.com/images/reference/androidx/compose/material3/indeterminate-checkbox.png)
*
* @sample androidx.compose.material3.samples.TriStateCheckboxSample
*
* @see [Checkbox] if you want a simple component that represents Boolean state
*
* @param state whether this checkbox is checked, unchecked, or in an indeterminate state
* @param onClick called when this checkbox is clicked. If `null`, then this checkbox will not be
* interactable, unless something else handles its input events and updates its [state].
* @param modifier the [Modifier] to be applied to this checkbox
* @param enabled controls the enabled state of this checkbox. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this checkbox. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this checkbox in different states.
* @param colors [CheckboxColors] that will be used to resolve the colors used for this checkbox in
* different states. See [CheckboxDefaults.colors].
*/
@Composable
fun TriStateCheckbox(
state: ToggleableState,
onClick: (() -> Unit)?,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
colors: CheckboxColors = CheckboxDefaults.colors()
) {
val toggleableModifier =
if (onClick != null) {
Modifier.triStateToggleable(
state = state,
onClick = onClick,
enabled = enabled,
role = Role.Checkbox,
interactionSource = interactionSource,
indication = rememberRipple(
bounded = false,
radius = CheckboxTokens.StateLayerSize / 2
)
)
} else {
Modifier
}
CheckboxImpl(
enabled = enabled,
value = state,
modifier = modifier
.then(
if (onClick != null) {
Modifier.minimumTouchTargetSize()
} else {
Modifier
}
)
.then(toggleableModifier)
.padding(CheckboxDefaultPadding),
colors = colors
)
}
/**
* Represents the colors used by the three different sections (checkmark, box, and border) of a
* [Checkbox] or [TriStateCheckbox] in different states.
*
* See [CheckboxDefaults.colors] for the default implementation that follows Material
* specifications.
*/
@Stable
interface CheckboxColors {
/**
* Represents the color used for the checkmark inside the checkbox, depending on [state].
*
* @param state the [ToggleableState] of the checkbox
*/
@Composable
fun checkmarkColor(state: ToggleableState): State<Color>
/**
* Represents the color used for the box (background) of the checkbox, depending on [enabled]
* and [state].
*
* @param enabled whether the checkbox is enabled or not
* @param state the [ToggleableState] of the checkbox
*/
@Composable
fun boxColor(enabled: Boolean, state: ToggleableState): State<Color>
/**
* Represents the color used for the border of the checkbox, depending on [enabled] and [state].
*
* @param enabled whether the checkbox is enabled or not
* @param state the [ToggleableState] of the checkbox
*/
@Composable
fun borderColor(enabled: Boolean, state: ToggleableState): State<Color>
}
/**
* Defaults used in [Checkbox] and [TriStateCheckbox].
*/
object CheckboxDefaults {
/**
* Creates a [CheckboxColors] that will animate between the provided colors according to the
* Material specification.
*
* @param checkedColor the color that will be used for the border and box when checked
* @param uncheckedColor color that will be used for the border when unchecked
* @param checkmarkColor color that will be used for the checkmark when checked
* @param disabledCheckedColor color that will be used for the box and border when disabled and
* checked
* @param disabledUncheckedColor color that will be used for the box and border when disabled
* and not checked
* @param disabledIndeterminateColor color that will be used for the box and
* border in a [TriStateCheckbox] when disabled AND in an [ToggleableState.Indeterminate] state.
*/
@Composable
fun colors(
checkedColor: Color =
MaterialTheme.colorScheme.fromToken(CheckboxTokens.SelectedContainerColor),
uncheckedColor: Color =
MaterialTheme.colorScheme.fromToken(CheckboxTokens.UnselectedOutlineColor),
checkmarkColor: Color =
MaterialTheme.colorScheme.fromToken(CheckboxTokens.SelectedIconColor),
disabledCheckedColor: Color =
MaterialTheme.colorScheme
.fromToken(CheckboxTokens.SelectedDisabledContainerColor)
.copy(alpha = CheckboxTokens.SelectedDisabledContainerOpacity),
disabledUncheckedColor: Color =
MaterialTheme.colorScheme
.fromToken(CheckboxTokens.UnselectedDisabledOutlineColor)
.copy(alpha = CheckboxTokens.UnselectedDisabledContainerOpacity),
disabledIndeterminateColor: Color = disabledCheckedColor
): CheckboxColors {
return remember(
checkedColor,
uncheckedColor,
checkmarkColor,
disabledCheckedColor,
disabledUncheckedColor,
disabledIndeterminateColor,
) {
DefaultCheckboxColors(
checkedBorderColor = checkedColor,
checkedBoxColor = checkedColor,
checkedCheckmarkColor = checkmarkColor,
uncheckedCheckmarkColor = checkmarkColor.copy(alpha = 0f),
uncheckedBoxColor = checkedColor.copy(alpha = 0f),
disabledCheckedBoxColor = disabledCheckedColor,
disabledUncheckedBoxColor = disabledUncheckedColor.copy(alpha = 0f),
disabledIndeterminateBoxColor = disabledIndeterminateColor,
uncheckedBorderColor = uncheckedColor,
disabledBorderColor = disabledCheckedColor,
disabledIndeterminateBorderColor = disabledIndeterminateColor,
)
}
}
}
@Composable
private fun CheckboxImpl(
enabled: Boolean,
value: ToggleableState,
modifier: Modifier,
colors: CheckboxColors
) {
val transition = updateTransition(value)
val checkDrawFraction = transition.animateFloat(
transitionSpec = {
when {
initialState == ToggleableState.Off -> tween(CheckAnimationDuration)
targetState == ToggleableState.Off -> snap(BoxOutDuration)
else -> spring()
}
}
) {
when (it) {
ToggleableState.On -> 1f
ToggleableState.Off -> 0f
ToggleableState.Indeterminate -> 1f
}
}
val checkCenterGravitationShiftFraction = transition.animateFloat(
transitionSpec = {
when {
initialState == ToggleableState.Off -> snap()
targetState == ToggleableState.Off -> snap(BoxOutDuration)
else -> tween(durationMillis = CheckAnimationDuration)
}
}
) {
when (it) {
ToggleableState.On -> 0f
ToggleableState.Off -> 0f
ToggleableState.Indeterminate -> 1f
}
}
val checkCache = remember { CheckDrawingCache() }
val checkColor = colors.checkmarkColor(value)
val boxColor = colors.boxColor(enabled, value)
val borderColor = colors.borderColor(enabled, value)
Canvas(modifier.wrapContentSize(Alignment.Center).requiredSize(CheckboxSize)) {
val strokeWidthPx = floor(StrokeWidth.toPx())
drawBox(
boxColor = boxColor.value,
borderColor = borderColor.value,
radius = RadiusSize.toPx(),
strokeWidth = strokeWidthPx
)
drawCheck(
checkColor = checkColor.value,
checkFraction = checkDrawFraction.value,
crossCenterGravitation = checkCenterGravitationShiftFraction.value,
strokeWidthPx = strokeWidthPx,
drawingCache = checkCache
)
}
}
private fun DrawScope.drawBox(
boxColor: Color,
borderColor: Color,
radius: Float,
strokeWidth: Float
) {
val halfStrokeWidth = strokeWidth / 2.0f
val stroke = Stroke(strokeWidth)
val checkboxSize = size.width
if (boxColor == borderColor) {
drawRoundRect(
boxColor,
size = Size(checkboxSize, checkboxSize),
cornerRadius = CornerRadius(radius),
style = Fill
)
} else {
drawRoundRect(
boxColor,
topLeft = Offset(strokeWidth, strokeWidth),
size = Size(checkboxSize - strokeWidth * 2, checkboxSize - strokeWidth * 2),
cornerRadius = CornerRadius(max(0f, radius - strokeWidth)),
style = Fill
)
drawRoundRect(
borderColor,
topLeft = Offset(halfStrokeWidth, halfStrokeWidth),
size = Size(checkboxSize - strokeWidth, checkboxSize - strokeWidth),
cornerRadius = CornerRadius(radius - halfStrokeWidth),
style = stroke
)
}
}
private fun DrawScope.drawCheck(
checkColor: Color,
checkFraction: Float,
crossCenterGravitation: Float,
strokeWidthPx: Float,
drawingCache: CheckDrawingCache
) {
val stroke = Stroke(width = strokeWidthPx, cap = StrokeCap.Square)
val width = size.width
val checkCrossX = 0.4f
val checkCrossY = 0.7f
val leftX = 0.2f
val leftY = 0.5f
val rightX = 0.8f
val rightY = 0.3f
val gravitatedCrossX = lerp(checkCrossX, 0.5f, crossCenterGravitation)
val gravitatedCrossY = lerp(checkCrossY, 0.5f, crossCenterGravitation)
// gravitate only Y for end to achieve center line
val gravitatedLeftY = lerp(leftY, 0.5f, crossCenterGravitation)
val gravitatedRightY = lerp(rightY, 0.5f, crossCenterGravitation)
with(drawingCache) {
checkPath.reset()
checkPath.moveTo(width * leftX, width * gravitatedLeftY)
checkPath.lineTo(width * gravitatedCrossX, width * gravitatedCrossY)
checkPath.lineTo(width * rightX, width * gravitatedRightY)
// TODO: replace with proper declarative non-android alternative when ready (b/158188351)
pathMeasure.setPath(checkPath, false)
pathToDraw.reset()
pathMeasure.getSegment(
0f, pathMeasure.length * checkFraction, pathToDraw, true
)
}
drawPath(drawingCache.pathToDraw, checkColor, style = stroke)
}
@Immutable
private class CheckDrawingCache(
val checkPath: Path = Path(),
val pathMeasure: PathMeasure = PathMeasure(),
val pathToDraw: Path = Path()
)
/**
* Default [CheckboxColors] implementation.
*/
@Immutable
private class DefaultCheckboxColors(
private val checkedCheckmarkColor: Color,
private val uncheckedCheckmarkColor: Color,
private val checkedBoxColor: Color,
private val uncheckedBoxColor: Color,
private val disabledCheckedBoxColor: Color,
private val disabledUncheckedBoxColor: Color,
private val disabledIndeterminateBoxColor: Color,
private val checkedBorderColor: Color,
private val uncheckedBorderColor: Color,
private val disabledBorderColor: Color,
private val disabledIndeterminateBorderColor: Color
) : CheckboxColors {
@Composable
override fun checkmarkColor(state: ToggleableState): State<Color> {
val target = if (state == ToggleableState.Off) {
uncheckedCheckmarkColor
} else {
checkedCheckmarkColor
}
val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
return animateColorAsState(target, tween(durationMillis = duration))
}
@Composable
override fun boxColor(enabled: Boolean, state: ToggleableState): State<Color> {
val target = if (enabled) {
when (state) {
ToggleableState.On, ToggleableState.Indeterminate -> checkedBoxColor
ToggleableState.Off -> uncheckedBoxColor
}
} else {
when (state) {
ToggleableState.On -> disabledCheckedBoxColor
ToggleableState.Indeterminate -> disabledIndeterminateBoxColor
ToggleableState.Off -> disabledUncheckedBoxColor
}
}
// If not enabled 'snap' to the disabled state, as there should be no animations between
// enabled / disabled.
return if (enabled) {
val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
animateColorAsState(target, tween(durationMillis = duration))
} else {
rememberUpdatedState(target)
}
}
@Composable
override fun borderColor(enabled: Boolean, state: ToggleableState): State<Color> {
val target = if (enabled) {
when (state) {
ToggleableState.On, ToggleableState.Indeterminate -> checkedBorderColor
ToggleableState.Off -> uncheckedBorderColor
}
} else {
when (state) {
ToggleableState.Indeterminate -> disabledIndeterminateBorderColor
ToggleableState.On, ToggleableState.Off -> disabledBorderColor
}
}
// If not enabled 'snap' to the disabled state, as there should be no animations between
// enabled / disabled.
return if (enabled) {
val duration = if (state == ToggleableState.Off) BoxOutDuration else BoxInDuration
animateColorAsState(target, tween(durationMillis = duration))
} else {
rememberUpdatedState(target)
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as DefaultCheckboxColors
if (checkedCheckmarkColor != other.checkedCheckmarkColor) return false
if (uncheckedCheckmarkColor != other.uncheckedCheckmarkColor) return false
if (checkedBoxColor != other.checkedBoxColor) return false
if (uncheckedBoxColor != other.uncheckedBoxColor) return false
if (disabledCheckedBoxColor != other.disabledCheckedBoxColor) return false
if (disabledUncheckedBoxColor != other.disabledUncheckedBoxColor) return false
if (disabledIndeterminateBoxColor != other.disabledIndeterminateBoxColor) return false
if (checkedBorderColor != other.checkedBorderColor) return false
if (uncheckedBorderColor != other.uncheckedBorderColor) return false
if (disabledBorderColor != other.disabledBorderColor) return false
if (disabledIndeterminateBorderColor != other.disabledIndeterminateBorderColor) return false
return true
}
override fun hashCode(): Int {
var result = checkedCheckmarkColor.hashCode()
result = 31 * result + uncheckedCheckmarkColor.hashCode()
result = 31 * result + checkedBoxColor.hashCode()
result = 31 * result + uncheckedBoxColor.hashCode()
result = 31 * result + disabledCheckedBoxColor.hashCode()
result = 31 * result + disabledUncheckedBoxColor.hashCode()
result = 31 * result + disabledIndeterminateBoxColor.hashCode()
result = 31 * result + checkedBorderColor.hashCode()
result = 31 * result + uncheckedBorderColor.hashCode()
result = 31 * result + disabledBorderColor.hashCode()
result = 31 * result + disabledIndeterminateBorderColor.hashCode()
return result
}
}
private const val BoxInDuration = 50
private const val BoxOutDuration = 100
private const val CheckAnimationDuration = 100
// TODO(b/188529841): Update the padding and size when the Checkbox spec is finalized.
private val CheckboxDefaultPadding = 2.dp
private val CheckboxSize = 20.dp
private val StrokeWidth = 2.dp
private val RadiusSize = 2.dp