From d033e431514af8adac2fee2dd60d26a53fbf8d37 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 8 Oct 2026 12:55:04 +0000 Subject: [PATCH] app: jump on long scrollbar drags Dragging the scrollbar thumb of a lazy list or grid scrolled by the dragged distance, and a large scrollBy composes and measures every item in between. On the Magisk log, fast thumb drags froze the UI for up to several seconds per frame. When the target is more than a viewport away, jump to the matching item index with scrollToItem() instead; shorter moves still scroll by pixels so slow drags stay smooth. Assisted-by: Claude Opus 5.5 --- .../magisk/ui/component/Scrollbar.kt | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Scrollbar.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Scrollbar.kt index bced05baf..5b08cb1bc 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Scrollbar.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Scrollbar.kt @@ -12,6 +12,9 @@ import androidx.compose.foundation.gestures.awaitFirstDown import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.calculateEndPadding import androidx.compose.foundation.layout.calculateStartPadding +import androidx.compose.foundation.lazy.LazyListState +import androidx.compose.foundation.lazy.grid.LazyGridState +import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridState import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -39,6 +42,7 @@ import androidx.compose.ui.unit.dp import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlin.math.abs import kotlin.math.roundToInt interface ScrollbarStateAdapter { @@ -128,11 +132,29 @@ fun rememberScrollbarAdapter(state: ScrollableState): ScrollbarStateAdapter { } else 0f } - override suspend fun drag(block: suspend (onScroll: suspend (Float) -> Unit) -> Unit) { - state.scroll(MutatePriority.PreventUserInput) { - block { fraction -> - info?.let { scrollBy(fraction.coerceIn(0f, 1f) * range - it.scrollOffset) } + private val itemCount: Int + get() = when (state) { + is LazyListState -> state.layoutInfo.totalItemsCount + is LazyGridState -> state.layoutInfo.totalItemsCount + is LazyStaggeredGridState -> state.layoutInfo.totalItemsCount + else -> 0 + } + + override suspend fun scrollTo(fraction: Float) { + val info = info ?: return + val target = fraction.coerceIn(0f, 1f) * range + val delta = target - info.scrollOffset + val count = itemCount + if (count > 0 && abs(delta) > info.viewportSize) { + // Jump by index, as scrolling by a large delta composes every item in between + val index = (target / info.contentSize * count).toInt().coerceIn(0, count - 1) + when (state) { + is LazyListState -> state.scrollToItem(index) + is LazyGridState -> state.scrollToItem(index) + is LazyStaggeredGridState -> state.scrollToItem(index) } + } else { + state.scroll(MutatePriority.PreventUserInput) { scrollBy(delta) } } } }