@@ -32,13 +32,15 @@ internal class WebHapticFeedback : HapticFeedback {
3232 // see config_longPressVibePattern in https://android.googlesource.com/platform/frameworks/base/+/refs/heads/main/core/res/res/values/config.xml
3333 // We don't have a high-level browser API right now. So we hardcode the patterns here.
3434 // 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 )
35+ companion object {
36+ private val ConfirmVibrationPattern = vibrationPatternOf(18 , 32 , 36 )
37+ private val RejectVibrationPattern = vibrationPatternOf(18 , 28 , 18 , 28 , 18 )
38+ private val SinglePulseVibrationPattern = vibrationPatternOf(12 )
39+ private val SoftTickVibrationPattern = vibrationPatternOf(6 )
40+ private val LongPressVibrationPattern = vibrationPatternOf(0 , 30 )
41+ private val VirtualKeyVibrationPattern = vibrationPatternOf(0 , 20 )
42+
43+ fun webHapticFeedbackOrDefault (): HapticFeedback = if (isVibrationSupported()) WebHapticFeedback () else DefaultHapticFeedback ()
4244 }
4345
4446 override fun performHapticFeedback (hapticFeedbackType : HapticFeedbackType ) {
@@ -77,4 +79,13 @@ private fun vibrationPatternOf(vararg durations: Int): JsArray<JsNumber> =
7779private fun vibrate (pattern : JsArray <JsNumber >) {
7880 // Assuming the API support has been checked in advance, we can safely call it
7981 js(" window.navigator.vibrate(pattern)" )
80- }
82+ }
83+
84+ private fun isVibrationSupported (): Boolean = js(
85+ // language=javascript
86+ """
87+ typeof window !== 'undefined' &&
88+ window.navigator != null &&
89+ typeof window.navigator.vibrate === 'function'
90+ """
91+ )
0 commit comments