@@ -27,58 +27,50 @@ import kotlin.js.toJsNumber
2727
2828@OptIn(ExperimentalWasmJsInterop ::class )
2929internal class WebHapticFeedback : HapticFeedback {
30- // Check if API is supported before doing anything
31- private val isVibrationSupported = isVibrationSupported()
3230
33- // Declare these hardcoded patterns to avoid js-interop on every call
34- private val ConfirmVibrationPattern : JsArray <JsNumber > = vibrationPatternOf(18 , 32 , 36 )
35- private val RejectVibrationPattern : JsArray <JsNumber > = vibrationPatternOf(18 , 28 , 18 , 28 , 18 )
36- private val SinglePulseVibrationPattern : JsArray <JsNumber > = vibrationPatternOf(12 )
37- private val SoftTickVibrationPattern : JsArray <JsNumber > = vibrationPatternOf(6 )
31+ // on Android these values are configured
32+ // see config_longPressVibePattern in https://android.googlesource.com/platform/frameworks/base/+/refs/heads/main/core/res/res/values/config.xml
33+ // We don't have a high-level browser API right now. So we hardcode the patterns here.
34+ // TODO: to eventually avoid the hardcoded values, follow the new browser API proposal https://github.com/WICG/web-haptics
35+ private companion object {
36+ val ConfirmVibrationPattern = vibrationPatternOf(18 , 32 , 36 )
37+ val RejectVibrationPattern = vibrationPatternOf(18 , 28 , 18 , 28 , 18 )
38+ val SinglePulseVibrationPattern = vibrationPatternOf(12 )
39+ val SoftTickVibrationPattern = vibrationPatternOf(6 )
40+ val LongPressVibrationPattern = vibrationPatternOf(0 , 30 )
41+ val VirtualKeyVibrationPattern = vibrationPatternOf(0 , 20 )
42+ }
3843
3944 override fun performHapticFeedback (hapticFeedbackType : HapticFeedbackType ) {
40- if (! isVibrationSupported) return
4145 val pattern = vibrationPatternFor(hapticFeedbackType) ? : return
4246 vibrate(pattern)
4347 }
4448
45- // We don't have a high-level browser API right now. So we hardcode the patterns here.
46- // TODO: to eventually avoid the hardcoded values, follow the new browser API proposal https://github.com/WICG/web-haptics
47- // and rely on it once it's implemented
49+
4850 @OptIn(ExperimentalWasmJsInterop ::class )
49- internal fun vibrationPatternFor (hapticFeedbackType : HapticFeedbackType ): JsArray <JsNumber >? {
51+ private fun vibrationPatternFor (hapticFeedbackType : HapticFeedbackType ): JsArray <JsNumber >? {
5052 return when (hapticFeedbackType) {
5153 HapticFeedbackType .Confirm -> ConfirmVibrationPattern
54+ HapticFeedbackType .ContextClick -> SinglePulseVibrationPattern
55+ HapticFeedbackType .GestureEnd -> SinglePulseVibrationPattern
56+ HapticFeedbackType .GestureThresholdActivate -> SinglePulseVibrationPattern
57+ HapticFeedbackType .KeyboardTap -> SoftTickVibrationPattern
58+ HapticFeedbackType .LongPress -> LongPressVibrationPattern
5259 HapticFeedbackType .Reject -> RejectVibrationPattern
53- HapticFeedbackType .ContextClick ,
54- HapticFeedbackType .GestureEnd ,
55- HapticFeedbackType .GestureThresholdActivate ,
56- HapticFeedbackType .LongPress ,
57- HapticFeedbackType .ToggleOff ,
58- HapticFeedbackType .ToggleOn ,
59- HapticFeedbackType .VirtualKey -> SinglePulseVibrationPattern
60- HapticFeedbackType .KeyboardTap ,
61- HapticFeedbackType .SegmentFrequentTick ,
60+ HapticFeedbackType .SegmentFrequentTick -> SoftTickVibrationPattern
6261 HapticFeedbackType .SegmentTick -> SoftTickVibrationPattern
63- HapticFeedbackType .TextHandleMove -> null
62+ HapticFeedbackType .TextHandleMove -> ConfirmVibrationPattern
63+ HapticFeedbackType .ToggleOff -> SinglePulseVibrationPattern
64+ HapticFeedbackType .ToggleOn -> SinglePulseVibrationPattern
65+ HapticFeedbackType .VirtualKey -> VirtualKeyVibrationPattern
6466 else -> null
6567 }
6668 }
6769}
6870
6971@OptIn(ExperimentalWasmJsInterop ::class )
7072private fun vibrationPatternOf (vararg durations : Int ): JsArray <JsNumber > =
71- durations.map { it.toDouble().toJsNumber() }.toJsArray()
72-
73- @OptIn(ExperimentalWasmJsInterop ::class )
74- private fun isVibrationSupported (): Boolean = js(
75- // language=javascript
76- """
77- typeof window !== 'undefined' &&
78- window.navigator != null &&
79- typeof window.navigator.vibrate === 'function'
80- """
81- )
73+ durations.map { it.toJsNumber() }.toJsArray()
8274
8375// language=javascript
8476@OptIn(ExperimentalWasmJsInterop ::class )
0 commit comments