diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AndroidAppsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/AndroidAppsPage.kt index 8904f2c2d4..4d4ac7adc0 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AndroidAppsPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AndroidAppsPage.kt @@ -117,6 +117,7 @@ import com.droiddeck.launcher.IconPack import android.content.pm.ShortcutInfo import android.graphics.Bitmap import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.drop import kotlinx.coroutines.withContext import com.droiddeck.launcher.HomeWidgets @@ -388,21 +389,49 @@ internal fun AndroidAppsPage(s: FrontEndState, a: FrontEndActions, modifier: Mod } val front = LocalFrontFocus.current val steer = rememberCoroutineScope() - // Down goes to the tile straight below. The grid's own search only sees the tiles laid out so far, so above a short or + // The tile a move is heading to, by its id so that a list update cannot shift it, and the number of the latest move. + val aim = remember { arrayOfNulls(1) } + val moves = remember { intArrayOf(0) } + val move = remember { arrayOfNulls(1) } + // Up and Down go to the tile straight above or below. The grid's own search works from where the tiles are drawn at that + // moment, so while a scroll is still catching up with fast presses it skips a row or drifts a column, and above a short or // unseen last row it lands on a tile further left; above a gap in a short last row the row's last tile takes it. + // Presses aim from the tile a move is already heading to, so none is lost before the focus lands. Box(Modifier.weight(1f).fillMaxWidth().onPreviewKeyEvent { event -> val k = event.nativeKeyEvent - if (k.keyCode != KeyEvent.KEYCODE_DPAD_DOWN || front == null) return@onPreviewKeyEvent false - val from = entries.indexOfFirst { tileId(it) == front.last } + val down = k.keyCode == KeyEvent.KEYCODE_DPAD_DOWN + if ((!down && k.keyCode != KeyEvent.KEYCODE_DPAD_UP) || front == null) return@onPreviewKeyEvent false + val aimed = aim[0]?.let { id -> entries.indexOfFirst { tileId(it) == id } } ?: -1 + if (aim[0] != null && aimed < 0) { + // The tile a move was heading to has left the list: that move is dropped and the press aims from the focus. + move[0]?.cancel() + aim[0] = null + } + val from = aimed.takeIf { it >= 0 } ?: entries.indexOfFirst { tileId(it) == front.last } val columns = (grid.layoutInfo.visibleItemsInfo.maxOfOrNull { it.column } ?: 0) + 1 - if (from < 0 || from / columns == entries.lastIndex / columns) return@onPreviewKeyEvent false + if (from < 0) return@onPreviewKeyEvent false + // At the first or last row the press leaves the grid, unless a move has not landed yet: then it waits for it. + val edge = if (down) from / columns == entries.lastIndex / columns else from < columns + if (edge) return@onPreviewKeyEvent aim[0] != null if (k.action == KeyEvent.ACTION_DOWN) { - val id = tileId(entries[minOf(from + columns, entries.lastIndex)]) - val shown = (front.attached[id] ?: 0) > 0 - if (!shown || runCatching { front.items.getValue(id).requestFocus() }.isFailure) steer.launch { - // The row below is not laid out yet: bring it in, then take it. - if (!shown) grid.scrollBy((grid.layoutInfo.visibleItemsInfo.firstOrNull { it.index == from + leadItems }?.size?.height ?: 0).toFloat()) - focusSoon { front.items.getValue(id) } + val to = if (down) minOf(from + columns, entries.lastIndex) else from - columns + val id = tileId(entries[to]) + aim[0] = id + val mine = ++moves[0] + // A newer move replaces the one still on its way, so an older target cannot take focus after it lands. + move[0]?.cancel() + move[0] = steer.launch { + try { + // Show the row before it takes focus. The grid composes rows ahead of the view without placing them, and a + // tile focused there neither scrolls in nor has a place for the next search to start from. + grid.reveal(to + leadItems) + if (moves[0] != mine) return@launch + focusSoon { front.items.getValue(id) } + } finally { + // Also when a touch scroll cancels the reveal, so the edge rows do not keep waiting for a move that ended. + // Only the latest move clears it: a cancelled one finishing late must not clear a newer move's target. + if (moves[0] == mine) aim[0] = null + } } } true @@ -558,6 +587,26 @@ private fun HeaderChoiceButton( } } +/** Scrolls the least that shows item [index] whole inside the padding, a row at a time while it is still outside the laid out items. */ +private suspend fun LazyGridState.reveal(index: Int) { + repeat(4) { + val info = layoutInfo + val shown = info.visibleItemsInfo + val item = shown.firstOrNull { it.index == index } + val by = when { + item != null -> { + val bottom = item.offset.y + item.size.height - (info.viewportEndOffset - info.afterContentPadding) + if (item.offset.y < 0) item.offset.y else maxOf(bottom, 0) + } + index > (shown.lastOrNull() ?: return).index -> shown.last().size.height + index < shown.first().index -> -shown.first().size.height + else -> return + } + if (by == 0) return + scrollBy(by.toFloat()) + } +} + @Composable private fun AlphabetScroller(entries: List, grid: LazyGridState, leadItems: Int, modifier: Modifier) { val colors = MaterialTheme.colorScheme