From b2cc4668ac2b6af5024c5063e00a86554654e895 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 4 Feb 2023 17:42:52 -0500 Subject: [PATCH] Adding bounce click. Not working yet. #298 --- .../ui/components/common/AnimationHelpers.kt | 50 +++++++++++++++++++ .../jerboa/ui/components/common/AppBars.kt | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/jerboa/ui/components/common/AnimationHelpers.kt diff --git a/app/src/main/java/com/jerboa/ui/components/common/AnimationHelpers.kt b/app/src/main/java/com/jerboa/ui/components/common/AnimationHelpers.kt new file mode 100644 index 000000000..8a2903e4e --- /dev/null +++ b/app/src/main/java/com/jerboa/ui/components/common/AnimationHelpers.kt @@ -0,0 +1,50 @@ +package com.jerboa.ui.components.common + +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.foundation.clickable +import androidx.compose.foundation.gestures.awaitFirstDown +import androidx.compose.foundation.gestures.waitForUpOrCancellation +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.composed +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.input.pointer.pointerInput + +enum class ButtonState { Pressed, Idle } + +fun Modifier.bounceClick() = composed { + var buttonState + by remember { mutableStateOf(ButtonState.Idle) } + val scale by animateFloatAsState( + if (buttonState + == ButtonState.Pressed + ) { + 0.70f + } else { + 1f + } + ) + this.graphicsLayer { + scaleX = scale + scaleY = scale + } + .clickable( + interactionSource = remember { MutableInteractionSource() }, + indication = null, + onClick = { } + ).pointerInput(buttonState) { + awaitPointerEventScope { + buttonState = if (buttonState == ButtonState.Pressed) { + waitForUpOrCancellation() + ButtonState.Idle + } else { + awaitFirstDown(false) + ButtonState.Pressed + } + } + } +} diff --git a/app/src/main/java/com/jerboa/ui/components/common/AppBars.kt b/app/src/main/java/com/jerboa/ui/components/common/AppBars.kt index 308cf49e6..02788d830 100644 --- a/app/src/main/java/com/jerboa/ui/components/common/AppBars.kt +++ b/app/src/main/java/com/jerboa/ui/components/common/AppBars.kt @@ -288,7 +288,7 @@ fun ActionBarButton( val barMod = if (noClick) { Modifier } else { - Modifier.clickable(onClick = { + Modifier.bounceClick().clickable(onClick = { if (account !== null) { onClick() } else {