Skip to content

Commit e944337

Browse files
authored
library: Simplified Switch gestures (#105)
1 parent 4e33d11 commit e944337

File tree

2 files changed

+21
-46
lines changed

2 files changed

+21
-46
lines changed

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>531</string>
20+
<string>532</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

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

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ fun Switch(
105105
animationSpec = tween(durationMillis = 200)
106106
)
107107

108+
val toggleableModifier = if (onCheckedChange != null) {
109+
Modifier.toggleable(
110+
value = checked,
111+
onValueChange = {
112+
onCheckedChange(it)
113+
hapticFeedback.performHapticFeedback(
114+
if (checked) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff
115+
)
116+
},
117+
enabled = enabled,
118+
role = Role.Switch,
119+
interactionSource = null,
120+
indication = null
121+
)
122+
} else {
123+
Modifier
124+
}
125+
126+
108127
Box(
109128
modifier = modifier
110129
.wrapContentSize(Alignment.Center)
@@ -118,51 +137,7 @@ fun Switch(
118137
interactionSource = interactionSource,
119138
enabled = enabled
120139
)
121-
.pointerInput(checked) {
122-
if (!enabled) return@pointerInput
123-
val touchSlop = 16f
124-
awaitEachGesture {
125-
val down = awaitFirstDown(requireUnconsumed = false)
126-
val initialOffset = down.position
127-
var validHorizontalDrag = false
128-
do {
129-
val event = awaitPointerEvent()
130-
val currentOffset = event.changes[0].position
131-
val dx = (currentOffset.x - initialOffset.x).absoluteValue
132-
val dy = (currentOffset.y - initialOffset.y).absoluteValue
133-
if (dy > touchSlop) {
134-
validHorizontalDrag = false
135-
break
136-
} else if (dx > touchSlop) {
137-
validHorizontalDrag = true
138-
}
139-
} while (event.changes.all { it.pressed })
140-
141-
if (validHorizontalDrag && !isPressed && !isDragged) {
142-
if (onCheckedChange == null) return@awaitEachGesture
143-
onCheckedChange.invoke(!checked)
144-
hapticFeedback.performHapticFeedback(
145-
if (checked) HapticFeedbackType.ToggleOff
146-
else HapticFeedbackType.ToggleOn
147-
)
148-
}
149-
}
150-
}
151-
.toggleable(
152-
value = checked,
153-
onValueChange = {
154-
if (onCheckedChange == null) return@toggleable
155-
onCheckedChange.invoke(!checked)
156-
hapticFeedback.performHapticFeedback(
157-
if (checked) HapticFeedbackType.ToggleOn
158-
else HapticFeedbackType.ToggleOff
159-
)
160-
},
161-
enabled = enabled,
162-
role = Role.Switch,
163-
indication = null,
164-
interactionSource = null
165-
)
140+
.then(toggleableModifier)
166141
) {
167142
Box(
168143
modifier = Modifier

0 commit comments

Comments
 (0)