11package com.getcode.ui.theme
22
3- import androidx.compose.animation.core.animateFloatAsState
4- import androidx.compose.foundation.Canvas
5- import androidx.compose.foundation.gestures.detectTapGestures
6- import androidx.compose.foundation.layout.size
7- import androidx.compose.material.ContentAlpha
8- import androidx.compose.material.SwitchColors
9- import androidx.compose.material.SwitchDefaults
3+ import androidx.compose.material3.Switch
4+ import androidx.compose.material3.SwitchColors
5+ import androidx.compose.material3.SwitchDefaults
106import androidx.compose.runtime.Composable
11- import androidx.compose.runtime.getValue
127import androidx.compose.ui.Modifier
13- import androidx.compose.ui.geometry.CornerRadius
14- import androidx.compose.ui.geometry.Offset
158import androidx.compose.ui.graphics.Color
16- import androidx.compose.ui.input.pointer.pointerInput
17- import androidx.compose.ui.platform.LocalDensity
18- import androidx.compose.ui.unit.dp
199import com.getcode.theme.CodeTheme
2010import com.getcode.theme.SystemGreen
2111import com.getcode.theme.White
22- import com.getcode.ui.core.addIf
2312
2413object CodeToggleSwitchDefaults {
2514
15+ private const val DISABLED_ALPHA = 0.38f
16+
2617 val colors: SwitchColors
2718 @Composable get() = SwitchDefaults .colors(
28- disabledCheckedThumbColor = Color (0xFFB2B9B5 ),
29- disabledUncheckedThumbColor = Color (0xFFB2B9B5 ),
19+ disabledCheckedThumbColor = Color (0x80B2B9B5 ),
20+ disabledUncheckedThumbColor = Color (0x80B2B9B5 ),
3021 disabledCheckedTrackColor = Color (0xFF2A8A4B ),
31- disabledUncheckedTrackColor = CodeTheme .colors.toggleUncheckedTrackColor.copy(ContentAlpha .disabled ),
22+ disabledUncheckedTrackColor = CodeTheme .colors.toggleUncheckedTrackColor.copy(DISABLED_ALPHA ),
3223 checkedThumbColor = White ,
3324 uncheckedThumbColor = White ,
3425 checkedTrackColor = SystemGreen ,
35- checkedTrackAlpha = 1f ,
36- uncheckedTrackAlpha = 1f ,
37- uncheckedTrackColor = CodeTheme .colors.toggleUncheckedTrackColor
26+ uncheckedTrackColor = CodeTheme .colors.toggleUncheckedTrackColor,
27+ uncheckedBorderColor = Color .White .copy(0.15f ),
28+ checkedBorderColor = SystemGreen ,
29+ disabledUncheckedBorderColor = CodeTheme .colors.toggleUncheckedTrackColor.copy(DISABLED_ALPHA ),
30+ disabledCheckedBorderColor = Color (0xFF2A8A4B ),
3831 )
3932}
4033
@@ -45,46 +38,11 @@ fun CodeToggleSwitch(
4538 enabled : Boolean = true,
4639 onCheckedChange : ((Boolean ) -> Unit )? = null,
4740) {
48- val width = 51 .dp
49- val height = 31 .dp
50- val gapBetweenThumbAndTrackEdge = 2 .dp
51-
52- val thumbRadius = (height / 2 ) - gapBetweenThumbAndTrackEdge
53- val animatePosition = animateFloatAsState(
54- targetValue = if (checked) {
55- with (LocalDensity .current) { (width - thumbRadius - gapBetweenThumbAndTrackEdge).toPx() }
56- } else {
57- with (LocalDensity .current) { (thumbRadius + gapBetweenThumbAndTrackEdge).toPx() }
58- }, label = " thumb position"
41+ Switch (
42+ checked = checked,
43+ modifier = modifier,
44+ enabled = enabled,
45+ onCheckedChange = onCheckedChange,
46+ colors = CodeToggleSwitchDefaults .colors
5947 )
60-
61- val colors = CodeToggleSwitchDefaults .colors
62- val trackColor by colors.trackColor(enabled = enabled, checked = checked)
63- val thumbColor by colors.thumbColor(enabled = enabled, checked = checked)
64- Canvas (
65- modifier = modifier
66- .size(width = width, height = height)
67- .addIf(onCheckedChange != null ) {
68- Modifier .pointerInput(Unit ) {
69- detectTapGestures(
70- onTap = {
71- onCheckedChange?.invoke(! checked)
72- }
73- )
74- }
75- }
76- ) {
77- drawRoundRect(
78- color = trackColor,
79- cornerRadius = CornerRadius (x = 45 .dp.toPx(), y = 45 .dp.toPx())
80- )
81- drawCircle(
82- color = thumbColor,
83- radius = thumbRadius.toPx(),
84- center = Offset (
85- x = animatePosition.value,
86- y = size.height / 2
87- )
88- )
89- }
9048}
0 commit comments