Skip to content

Commit 32355cd

Browse files
committed
library: Optimize interface dragging
1 parent 23fa25b commit 32355cd

File tree

2 files changed

+10
-46
lines changed

2 files changed

+10
-46
lines changed

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import androidx.compose.animation.core.animateDpAsState
55
import androidx.compose.animation.core.animateFloatAsState
66
import androidx.compose.animation.core.tween
77
import androidx.compose.foundation.Canvas
8-
import androidx.compose.foundation.Indication
98
import androidx.compose.foundation.background
10-
import androidx.compose.foundation.gestures.detectDragGestures
119
import androidx.compose.foundation.gestures.detectTapGestures
1210
import androidx.compose.foundation.interaction.MutableInteractionSource
1311
import androidx.compose.foundation.layout.requiredSize
@@ -43,7 +41,6 @@ import top.yukonga.miuix.kmp.utils.squircleshape.SquircleShape
4341
* @param modifier The modifier to be applied to the [Checkbox].
4442
* @param enabled Whether the [Checkbox] is enabled.
4543
* @param interactionSource The interaction source to be applied to the [Checkbox].
46-
* @param indication The indication to be applied to the [Checkbox].
4744
*/
4845
@Composable
4946
fun Checkbox(
@@ -52,7 +49,6 @@ fun Checkbox(
5249
modifier: Modifier = Modifier,
5350
enabled: Boolean = true,
5451
interactionSource: MutableInteractionSource? = null,
55-
indication: Indication? = null,
5652
) {
5753
@Suppress("NAME_SHADOWING")
5854
val interactionSource = interactionSource ?: remember { MutableInteractionSource() }
@@ -79,15 +75,14 @@ fun Checkbox(
7975
enabled = enabled,
8076
role = Role.Checkbox,
8177
interactionSource = interactionSource,
82-
indication = indication
78+
indication = null
8379
)
8480
} else {
8581
Modifier
8682
}
8783
}
8884

89-
Surface(
90-
shape = SquircleShape(100f),
85+
Box(
9186
modifier = modifier
9287
.wrapContentSize(Alignment.Center)
9388
.size(22.dp)
@@ -106,19 +101,6 @@ fun Checkbox(
106101
}
107102
)
108103
}
109-
.pointerInput(Unit) {
110-
detectDragGestures(
111-
onDragEnd = {
112-
isPressed = false
113-
},
114-
onDragCancel = {
115-
isPressed = false
116-
},
117-
onDrag = { change, _ ->
118-
change.consume()
119-
}
120-
)
121-
}
122104
.then(toggleableModifier)
123105
) {
124106
Canvas(

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

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import androidx.compose.animation.core.spring
77
import androidx.compose.animation.core.tween
88
import androidx.compose.foundation.Indication
99
import androidx.compose.foundation.background
10-
import androidx.compose.foundation.gestures.detectDragGestures
10+
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
1111
import androidx.compose.foundation.gestures.detectTapGestures
12+
import androidx.compose.foundation.gestures.detectVerticalDragGestures
1213
import androidx.compose.foundation.interaction.MutableInteractionSource
1314
import androidx.compose.foundation.layout.padding
1415
import androidx.compose.foundation.layout.requiredSize
@@ -42,7 +43,6 @@ import kotlin.math.absoluteValue
4243
* @param modifier The modifier to be applied to the [Switch].
4344
* @param enabled Whether the [Switch] is enabled.
4445
* @param interactionSource The interaction source to be applied to the [Switch].
45-
* @param indication The indication to be applied to the [Switch].
4646
*/
4747
@Composable
4848
fun Switch(
@@ -51,7 +51,6 @@ fun Switch(
5151
modifier: Modifier = Modifier,
5252
enabled: Boolean = true,
5353
interactionSource: MutableInteractionSource? = null,
54-
indication: Indication? = null
5554
) {
5655
@Suppress("NAME_SHADOWING")
5756
val interactionSource = interactionSource ?: remember { MutableInteractionSource() }
@@ -102,7 +101,7 @@ fun Switch(
102101
enabled = enabled,
103102
role = Role.Switch,
104103
interactionSource = interactionSource,
105-
indication = indication
104+
indication = null
106105
)
107106
} else {
108107
Modifier
@@ -118,21 +117,7 @@ fun Switch(
118117
.clip(SquircleShape(100.dp))
119118
.background(if (enabled) backgroundColor else disabledBackgroundColor)
120119
.pointerInput(Unit) {
121-
detectTapGestures(
122-
onPress = {
123-
isPressed = true
124-
},
125-
onTap = {
126-
isPressed = false
127-
if (enabled) {
128-
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
129-
onCheckedChange?.invoke(!isChecked)
130-
}
131-
}
132-
)
133-
}
134-
.pointerInput(Unit) {
135-
detectDragGestures(
120+
detectHorizontalDragGestures(
136121
onDragStart = {
137122
isPressed = true
138123
hasVibrated = false
@@ -149,14 +134,11 @@ fun Switch(
149134
isPressed = false
150135
dragOffset = 0f
151136
},
152-
onDrag = { change, dragAmount ->
153-
if (!enabled) return@detectDragGestures
154-
val newOffset = dragOffset + dragAmount.x / 2
137+
onHorizontalDrag = { change, dragAmount ->
138+
if (!enabled) return@detectHorizontalDragGestures
139+
val newOffset = dragOffset + dragAmount / 2
155140
dragOffset =
156-
if (isChecked) newOffset.coerceIn(-24f, 0f) else newOffset.coerceIn(
157-
0f,
158-
24f
159-
)
141+
if (isChecked) newOffset.coerceIn(-24f, 0f) else newOffset.coerceIn(0f, 24f)
160142
if (isChecked) {
161143
if (dragOffset in -23f..-1f) {
162144
hasVibrated = false

0 commit comments

Comments
 (0)