-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding bounce click. Not working yet. #298
- Loading branch information
1 parent
57a2567
commit b2cc466
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/jerboa/ui/components/common/AnimationHelpers.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,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 | ||
} | ||
} | ||
} | ||
} |
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