Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eba8f9d8 authored by amehfooz's avatar amehfooz Committed by Ahmed Mehfooz
Browse files

Add hover support for PinPadButton

This CL adds a hover state for the PinPadButton
by using the default behavior in combinedClickable
along with a CircleShape clip.

The existing isPressed animation is kept by using
a null indication when isPressed is true.

Bug: 283802717
Tested: Made sure a hover highlight is displayed using
a mouse with the Pixel Tablet. Also, ensured the existing
UI and animations are unchanged.
Flag: com.android.systemui.scene_container

Change-Id: Ife685063d7b27126e528af81e7ed70211a39e243
parent d9deb875
Loading
Loading
Loading
Loading
+29 −40
Original line number Diff line number Diff line
@@ -24,30 +24,31 @@ import androidx.compose.animation.core.AnimationVector1D
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@@ -63,9 +64,7 @@ import com.android.systemui.common.ui.compose.Icon
import com.android.systemui.res.R
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.DurationUnit
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/** Renders the PIN button pad. */
@@ -234,7 +233,9 @@ private fun PinPadButton(
    onLongPressed: (() -> Unit)? = null,
    content: @Composable (contentColor: () -> Color) -> Unit,
) {
    var isPressed: Boolean by remember { mutableStateOf(false) }
    val interactionSource = remember { MutableInteractionSource() }
    val isPressed by interactionSource.collectIsPressedAsState()
    val indication = LocalIndication.current.takeUnless { isPressed }

    val view = LocalView.current
    LaunchedEffect(isPressed) {
@@ -281,8 +282,6 @@ private fun PinPadButton(
            animationSpec = colorAnimationSpec
        )

    val scope = rememberCoroutineScope()

    Box(
        contentAlignment = Alignment.Center,
        modifier =
@@ -297,24 +296,14 @@ private fun PinPadButton(
                    cornerRadius = CornerRadius(cornerRadius.toPx()),
                )
            }
            .clip(CircleShape)
            .thenIf(isEnabled) {
                    Modifier.pointerInput(Unit) {
                        detectTapGestures(
                            onPress = {
                                scope.launch {
                                    isPressed = true
                                    val minDuration = async {
                                        delay(pinButtonPressedDuration + pinButtonHoldTime)
                                    }
                                    tryAwaitRelease()
                                    minDuration.await()
                                    isPressed = false
                                }
                            },
                            onTap = { onClicked() },
                            onLongPress = onLongPressed?.let { { onLongPressed() } },
                Modifier.combinedClickable(
                    interactionSource = interactionSource,
                    indication = indication,
                    onClick = onClicked,
                    onLongClick = onLongPressed
                )
                    }
            },
    ) {
        content(contentColor::value)