Skip to content

Commit 3e2e9ef

Browse files
committed
Introduce webHapticFeedbackOrDefault helper function
1 parent 022c7c3 commit 3e2e9ef

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

compose/ui/ui/src/webMain/kotlin/androidx/compose/ui/platform/DefaultHapticFeedback.web.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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> =
7779
private 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+
)

compose/ui/ui/src/webMain/kotlin/androidx/compose/ui/window/ComposeWindowInternal.web.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ internal class ComposeWindow(
207207
@VisibleForTesting
208208
internal val archComponentsOwner = DefaultArchitectureComponentsOwner()
209209

210-
private val hapticFeedback = if (isVibrationSupported()) WebHapticFeedback() else DefaultHapticFeedback()
210+
private val hapticFeedback = WebHapticFeedback.webHapticFeedbackOrDefault()
211211

212212
private val navigationEventInput = BackNavigationEventInput()
213213

@@ -879,11 +879,3 @@ private external interface ShadowRootExt {
879879
val activeElement: Element?
880880
}
881881

882-
private fun isVibrationSupported(): Boolean = js(
883-
//language=javascript
884-
"""
885-
typeof window !== 'undefined' &&
886-
window.navigator != null &&
887-
typeof window.navigator.vibrate === 'function'
888-
"""
889-
)

0 commit comments

Comments
 (0)