Merge pull request #114 from esensar/simple-bounce-on-page-change

Add a simple bounce on page changes
This commit is contained in:
Tibor Kaputa 2023-09-04 21:31:39 +02:00 committed by GitHub
commit 8ee21b43a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import android.util.AttributeSet
import android.util.Size
import android.util.SizeF
import android.view.View
import android.view.animation.OvershootInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.RelativeLayout
import androidx.core.graphics.drawable.toBitmap
@ -83,7 +84,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
getHandler = { handler },
getNextPageBound = { right - sideMargins.right - cellWidth / 2 },
getPrevPageBound = { left + sideMargins.left + cellWidth / 2 },
pageChangeStarted = { closeFolder() }
pageChangeStarted = {
widgetViews.forEach { it.resetTouches() }
closeFolder()
}
)
private var currentlyOpenFolder: HomeScreenFolder? = null
@ -1205,6 +1209,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
}
fun isClickingGridItem(x: Int, y: Int): HomeScreenGridItem? {
if (pager.isAnimatingPageChange() || pager.isSwiped()) {
return null
}
currentlyOpenFolder?.also { folder ->
folder.getItems().forEach { gridItem ->
val rect = getClickableRect(gridItem)
@ -1661,9 +1669,9 @@ private class AnimatedGridPager(
fun isSwiped() = abs(pageChangeSwipedPercentage) > 0f
fun isAnimatingPageChange() = pageChangeAnimLeftPercentage > 0f
fun isAnimatingPageChange() = pageChangeAnimLeftPercentage != 0f
fun shouldDisplayPageChangeIndicator() = isSwiped() || isAnimatingPageChange() || pageChangeIndicatorsAlpha > 0f
fun shouldDisplayPageChangeIndicator() = isSwiped() || isAnimatingPageChange() || pageChangeIndicatorsAlpha != 0f
fun getPageChangeIndicatorsAlpha() = if (pageChangeIndicatorsAlpha != 0f) {
(pageChangeIndicatorsAlpha * 255.0f).toInt()
@ -1707,7 +1715,7 @@ private class AnimatedGridPager(
currentPage
}
val rangeEnd = rangeEndPage.toFloat()
val lerpAmount = if (pageChangeAnimLeftPercentage > 0f) {
val lerpAmount = if (pageChangeAnimLeftPercentage != 0f) {
1 - pageChangeAnimLeftPercentage
} else {
abs(pageChangeSwipedPercentage)
@ -1874,11 +1882,13 @@ private class AnimatedGridPager(
val startingAt = 1 - abs(pageChangeSwipedPercentage)
pageChangeSwipedPercentage = 0f
getHandler().removeCallbacks(startFadingIndicators)
pageChangeStarted()
if (redraw) {
redrawGrid()
}
ValueAnimator.ofFloat(startingAt, 0f)
.apply {
interpolator = OvershootInterpolator(1f)
addUpdateListener {
pageChangeAnimLeftPercentage = it.animatedValue as Float
redrawGrid()

View File

@ -68,4 +68,8 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y)
}
}
fun resetTouches() {
longPressHandler.removeCallbacksAndMessages(null)
}
}