diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt index 473bc0e1d9..fe0866e8a7 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt @@ -3,6 +3,8 @@ package com.droiddeck.launcher.ui import android.view.KeyEvent import androidx.activity.compose.BackHandler import androidx.compose.animation.core.Spring +import androidx.compose.animation.core.tween +import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.animateFloatAsState import androidx.compose.foundation.LocalIndication import androidx.compose.foundation.background @@ -30,6 +32,7 @@ import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.GridItemSpan import androidx.compose.foundation.lazy.grid.LazyGridScope +import androidx.compose.foundation.gestures.animateScrollBy import androidx.compose.foundation.lazy.grid.LazyGridState import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.items @@ -426,10 +429,11 @@ private fun LibraryGrid( if (key.action == KeyEvent.ACTION_DOWN) { val target = "card:${visible[next].key}" val targetIndex = headerItems + next + if (split == 0) 0 else if (next < split) 1 else 2 + val currentIndex = headerItems + index + if (split == 0) 0 else if (index < split) 1 else 2 val shown = grid.layoutInfo.visibleItemsInfo.any { it.index == targetIndex } if (!shown || (focus.attached[target] ?: 0) == 0 || runCatching { focus.items.getValue(target).requestFocus() }.isFailure) scope.launch { - grid.scrollToItem(targetIndex) + launch { grid.revealRow(currentIndex, targetIndex, direction, with(density) { 10.dp.toPx() }) } focusWithinFrames({ focus.last == target }) { focus.items[target] ?: FocusRequester.Default } } } @@ -487,6 +491,20 @@ private fun LibraryGrid( } } +internal suspend fun LazyGridState.revealRow(currentIndex: Int, targetIndex: Int, direction: Int, gapPx: Float) { + val current = layoutInfo.visibleItemsInfo.firstOrNull { it.index == currentIndex } + if (current != null) { + val row = current.size.height + gapPx + val delta = if (direction > 0) { + current.offset.y + 2 * row - gapPx - (layoutInfo.viewportSize.height - layoutInfo.afterContentPadding) + } else { + current.offset.y - row - layoutInfo.beforeContentPadding + } + if (direction > 0 && delta > 0f || direction < 0 && delta < 0f) animateScrollBy(delta, tween(durationMillis = 160, easing = FastOutSlowInEasing)) + } + if (layoutInfo.visibleItemsInfo.none { it.index == targetIndex }) scrollToItem(targetIndex) +} + private fun LazyGridScope.libraryGrid(entries: List, empty: Int, tile: @Composable (LibraryEntry) -> Unit) { if (entries.isEmpty()) item(key = "empty", span = { GridItemSpan(maxLineSpan) }) { Box(Modifier.padding(vertical = 32.dp).fillMaxWidth(), contentAlignment = Alignment.Center) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt index 57b54897b1..60a0c1790f 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt @@ -885,7 +885,7 @@ private fun Storefront( val visibleTarget = grid.layoutInfo.visibleItemsInfo.any { it.index == targetIndex } if (!visibleTarget || (focus.attached[target] ?: 0) == 0 || runCatching { focus.items.getValue(target).requestFocus() }.isFailure) scope.launch { - grid.scrollToItem(targetIndex) + launch { grid.revealRow(headerItems + index, targetIndex, direction, with(density) { 10.dp.toPx() }) } focusWithinFrames({ focus.last == target }) { focus.items[target] ?: androidx.compose.ui.focus.FocusRequester.Default } } }