Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iosApp/iosApp/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0.4</string>
<key>CFBundleVersion</key>
<string>531</string>
<string>532</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>CADisableMinimumFrameDurationOnPhone</key>
Expand Down
65 changes: 20 additions & 45 deletions miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/Switch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ fun Switch(
animationSpec = tween(durationMillis = 200)
)

val toggleableModifier = if (onCheckedChange != null) {
Modifier.toggleable(
value = checked,
onValueChange = {
onCheckedChange(it)
hapticFeedback.performHapticFeedback(
if (checked) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff
)
},
enabled = enabled,
role = Role.Switch,
interactionSource = null,
indication = null
)
} else {
Modifier
}


Box(
modifier = modifier
.wrapContentSize(Alignment.Center)
Expand All @@ -118,51 +137,7 @@ fun Switch(
interactionSource = interactionSource,
enabled = enabled
)
.pointerInput(checked) {
if (!enabled) return@pointerInput
val touchSlop = 16f
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)
val initialOffset = down.position
var validHorizontalDrag = false
do {
val event = awaitPointerEvent()
val currentOffset = event.changes[0].position
val dx = (currentOffset.x - initialOffset.x).absoluteValue
val dy = (currentOffset.y - initialOffset.y).absoluteValue
if (dy > touchSlop) {
validHorizontalDrag = false
break
} else if (dx > touchSlop) {
validHorizontalDrag = true
}
} while (event.changes.all { it.pressed })

if (validHorizontalDrag && !isPressed && !isDragged) {
if (onCheckedChange == null) return@awaitEachGesture
onCheckedChange.invoke(!checked)
hapticFeedback.performHapticFeedback(
if (checked) HapticFeedbackType.ToggleOff
else HapticFeedbackType.ToggleOn
)
}
}
}
.toggleable(
value = checked,
onValueChange = {
if (onCheckedChange == null) return@toggleable
onCheckedChange.invoke(!checked)
hapticFeedback.performHapticFeedback(
if (checked) HapticFeedbackType.ToggleOn
else HapticFeedbackType.ToggleOff
)
},
enabled = enabled,
role = Role.Switch,
indication = null,
interactionSource = null
)
.then(toggleableModifier)
) {
Box(
modifier = Modifier
Expand Down
Loading