Fix regular scrollbar (#8139)

At some point the scrollbar stops responding to
scroll changes. Not too confident with this fix
but it works.

(cherry picked from commit 1cf1b34e7f32d71e3508f81e8348ff522d389109)
This commit is contained in:
Ivan Iskandar 2022-10-04 10:03:33 +07:00 committed by Jobobby04
parent 17111a4d02
commit 6ddc153af4

View File

@ -48,12 +48,11 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.composed import androidx.compose.ui.composed
import androidx.compose.ui.draw.CacheDrawScope import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.draw.DrawResult
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource import androidx.compose.ui.input.nestedscroll.NestedScrollSource
@ -117,13 +116,11 @@ private fun Modifier.drawScrollbar(
orientation, reverseDirection, atEnd, showScrollbar, orientation, reverseDirection, atEnd, showScrollbar,
thickness, color, alpha, thumbSize, startOffset, positionOffset, thickness, color, alpha, thumbSize, startOffset, positionOffset,
) )
onDrawWithContent { drawContent()
drawContent() drawScrollbar()
drawScrollbar()
}
} }
private fun CacheDrawScope.onDrawScrollbar( private fun ContentDrawScope.onDrawScrollbar(
orientation: Orientation, orientation: Orientation,
reverseDirection: Boolean, reverseDirection: Boolean,
atEnd: Boolean, atEnd: Boolean,
@ -167,13 +164,13 @@ private fun CacheDrawScope.onDrawScrollbar(
private fun Modifier.drawScrollbar( private fun Modifier.drawScrollbar(
orientation: Orientation, orientation: Orientation,
reverseScrolling: Boolean, reverseScrolling: Boolean,
onBuildDrawCache: CacheDrawScope.( onDraw: ContentDrawScope.(
reverseDirection: Boolean, reverseDirection: Boolean,
atEnd: Boolean, atEnd: Boolean,
thickness: Float, thickness: Float,
color: Color, color: Color,
alpha: () -> Float, alpha: () -> Float,
) -> DrawResult, ) -> Unit,
): Modifier = composed { ): Modifier = composed {
val scrolled = remember { val scrolled = remember {
MutableSharedFlow<Unit>( MutableSharedFlow<Unit>(
@ -216,8 +213,8 @@ private fun Modifier.drawScrollbar(
val color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.364f) val color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.364f)
Modifier Modifier
.nestedScroll(nestedScrollConnection) .nestedScroll(nestedScrollConnection)
.drawWithCache { .drawWithContent {
onBuildDrawCache(reverseDirection, atEnd, thickness, color, alpha::value) onDraw(reverseDirection, atEnd, thickness, color, alpha::value)
} }
} }