-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back rotary input support for home screen (#1887)
- Loading branch information
Showing
2 changed files
with
135 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
wear/src/main/java/io/homeassistant/companion/android/util/RotaryEventDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.homeassistant.companion.android.util | ||
|
||
import android.view.MotionEvent | ||
import android.view.ViewConfiguration | ||
import androidx.compose.foundation.gestures.ScrollableState | ||
import androidx.compose.foundation.gestures.scrollBy | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.core.view.InputDeviceCompat | ||
import androidx.core.view.MotionEventCompat | ||
import androidx.core.view.ViewConfigurationCompat | ||
import kotlinx.coroutines.launch | ||
|
||
val LocalRotaryEventDispatcher = staticCompositionLocalOf<RotaryEventDispatcher> { | ||
noLocalProvidedFor("LocalRotaryEventDispatcher") | ||
} | ||
|
||
class RotaryEventDispatcher( | ||
var scrollState: ScrollableState? = null | ||
) { | ||
suspend fun onRotate(delta: Float): Float? = | ||
scrollState?.scrollBy(delta) | ||
} | ||
|
||
@Composable | ||
fun RotaryEventHandlerSetup(rotaryEventDispatcher: RotaryEventDispatcher) { | ||
val view = LocalView.current | ||
val context = LocalContext.current | ||
val scope = rememberCoroutineScope() | ||
|
||
view.requestFocus() | ||
view.setOnGenericMotionListener { _, event -> | ||
if (event?.action != MotionEvent.ACTION_SCROLL || !event.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)) { | ||
return@setOnGenericMotionListener false | ||
} | ||
|
||
val delta = -event.getAxisValue(MotionEventCompat.AXIS_SCROLL) * ViewConfigurationCompat.getScaledVerticalScrollFactor( | ||
ViewConfiguration.get(context), context | ||
) | ||
|
||
scope.launch { | ||
rotaryEventDispatcher.onRotate(delta) | ||
} | ||
true | ||
} | ||
} | ||
|
||
@Composable | ||
fun RotaryEventState(scrollState: ScrollableState?) { | ||
val dispater = LocalRotaryEventDispatcher.current | ||
SideEffect { | ||
dispater.scrollState = scrollState | ||
} | ||
} | ||
|
||
private fun noLocalProvidedFor(noOp: String): Nothing { | ||
error("composition local $noOp not present") | ||
} |